Package org.apache.http.impl.nio
Class NHttpConnectionBase
- java.lang.Object
-
- org.apache.http.impl.nio.NHttpConnectionBase
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,org.apache.http.HttpConnection
,org.apache.http.HttpInetConnection
,IOControl
,NHttpConnection
,SessionBufferStatus
,SocketAccessor
- Direct Known Subclasses:
DefaultNHttpClientConnection
,DefaultNHttpServerConnection
public class NHttpConnectionBase extends java.lang.Object implements NHttpConnection, org.apache.http.HttpInetConnection, SessionBufferStatus, SocketAccessor
This class serves as a base for allNHttpConnection
implementations and provides functionality common to both client and server HTTP connections.- Since:
- 4.0
-
-
Field Summary
Fields Modifier and Type Field Description protected org.apache.http.impl.HttpConnectionMetricsImpl
connMetrics
protected ContentDecoder
contentDecoder
protected ContentEncoder
contentEncoder
protected org.apache.http.protocol.HttpContext
context
protected boolean
hasBufferedInput
protected boolean
hasBufferedOutput
protected SessionInputBufferImpl
inbuf
protected org.apache.http.entity.ContentLengthStrategy
incomingContentStrategy
protected org.apache.http.impl.io.HttpTransportMetricsImpl
inTransportMetrics
protected SessionOutputBufferImpl
outbuf
protected org.apache.http.entity.ContentLengthStrategy
outgoingContentStrategy
protected org.apache.http.impl.io.HttpTransportMetricsImpl
outTransportMetrics
protected java.net.SocketAddress
remote
protected org.apache.http.HttpRequest
request
protected org.apache.http.HttpResponse
response
protected IOSession
session
protected int
status
-
Fields inherited from interface org.apache.http.nio.NHttpConnection
ACTIVE, CLOSED, CLOSING
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
NHttpConnectionBase(IOSession session, int buffersize, int fragmentSizeHint, ByteBufferAllocator allocator, java.nio.charset.CharsetDecoder chardecoder, java.nio.charset.CharsetEncoder charencoder, org.apache.http.config.MessageConstraints constraints, org.apache.http.entity.ContentLengthStrategy incomingContentStrategy, org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy)
Creates new instance NHttpConnectionBase given the underlying I/O session.protected
NHttpConnectionBase(IOSession session, int buffersize, int fragmentSizeHint, ByteBufferAllocator allocator, java.nio.charset.CharsetDecoder chardecoder, java.nio.charset.CharsetEncoder charencoder, org.apache.http.entity.ContentLengthStrategy incomingContentStrategy, org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy)
Creates new instance NHttpConnectionBase given the underlying I/O session.NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, org.apache.http.params.HttpParams params)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
assertNotClosed()
Assets if the connection is still open.protected void
bind(IOSession session)
Binds the connection to a differentIOSession
.void
close()
protected org.apache.http.impl.HttpConnectionMetricsImpl
createConnectionMetrics(org.apache.http.io.HttpTransportMetrics inTransportMetric, org.apache.http.io.HttpTransportMetrics outTransportMetric)
Deprecated.(4.3) use decorator to add additional metrics.protected ContentDecoder
createContentDecoder(long len, java.nio.channels.ReadableByteChannel channel, SessionInputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics)
Factory method forContentDecoder
instances.protected ContentEncoder
createContentEncoder(long len, java.nio.channels.WritableByteChannel channel, SessionOutputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics)
Factory method forContentEncoder
instances.protected org.apache.http.entity.ContentLengthStrategy
createIncomingContentStrategy()
Deprecated.(4.3) use constructor.protected org.apache.http.entity.ContentLengthStrategy
createOutgoingContentStrategy()
Deprecated.(4.3) use constructor.protected org.apache.http.impl.io.HttpTransportMetricsImpl
createTransportMetrics()
Deprecated.(4.3) no longer used.org.apache.http.protocol.HttpContext
getContext()
Returns an HTTP execution context associated with this connection.org.apache.http.HttpRequest
getHttpRequest()
Returns the current HTTP request if one is being received / transmitted.org.apache.http.HttpResponse
getHttpResponse()
Returns the current HTTP response if one is being received / transmitted.java.net.InetAddress
getLocalAddress()
int
getLocalPort()
org.apache.http.HttpConnectionMetrics
getMetrics()
java.net.InetAddress
getRemoteAddress()
int
getRemotePort()
java.net.Socket
getSocket()
Return the underlying socketint
getSocketTimeout()
int
getStatus()
Returns status of the connection:boolean
hasBufferedInput()
Determines if the session input buffer contains data.boolean
hasBufferedOutput()
Determines if the session output buffer contains data.boolean
isOpen()
boolean
isStale()
protected org.apache.http.HttpEntity
prepareDecoder(org.apache.http.HttpMessage message)
Initializes a specificContentDecoder
implementation based on the properties of the givenHttpMessage
and generates an instance ofHttpEntity
matching the properties of the content decoder.protected void
prepareEncoder(org.apache.http.HttpMessage message)
Initializes a specificContentEncoder
implementation based on the properties of the givenHttpMessage
.void
requestInput()
Requests event notifications to be triggered when the underlying channel is ready for input operations.void
requestOutput()
Requests event notifications to be triggered when the underlying channel is ready for output operations.void
setSocketTimeout(int timeout)
void
shutdown()
Shuts down the underlying channel.void
suspendInput()
Suspends event notifications about the underlying channel being ready for input operations.void
suspendOutput()
Suspends event notifications about the underlying channel being ready for output operations.java.lang.String
toString()
-
-
-
Field Detail
-
incomingContentStrategy
protected final org.apache.http.entity.ContentLengthStrategy incomingContentStrategy
-
outgoingContentStrategy
protected final org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy
-
inbuf
protected final SessionInputBufferImpl inbuf
-
outbuf
protected final SessionOutputBufferImpl outbuf
-
inTransportMetrics
protected final org.apache.http.impl.io.HttpTransportMetricsImpl inTransportMetrics
-
outTransportMetrics
protected final org.apache.http.impl.io.HttpTransportMetricsImpl outTransportMetrics
-
connMetrics
protected final org.apache.http.impl.HttpConnectionMetricsImpl connMetrics
-
context
protected org.apache.http.protocol.HttpContext context
-
session
protected IOSession session
-
remote
protected java.net.SocketAddress remote
-
contentDecoder
protected volatile ContentDecoder contentDecoder
-
hasBufferedInput
protected volatile boolean hasBufferedInput
-
contentEncoder
protected volatile ContentEncoder contentEncoder
-
hasBufferedOutput
protected volatile boolean hasBufferedOutput
-
request
protected volatile org.apache.http.HttpRequest request
-
response
protected volatile org.apache.http.HttpResponse response
-
status
protected volatile int status
-
-
Constructor Detail
-
NHttpConnectionBase
@Deprecated public NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, org.apache.http.params.HttpParams params)
Deprecated.Creates a new instance of this class given the underlying I/O session.- Parameters:
session
- the underlying I/O session.allocator
- byte buffer allocator.params
- HTTP parameters.
-
NHttpConnectionBase
protected NHttpConnectionBase(IOSession session, int buffersize, int fragmentSizeHint, ByteBufferAllocator allocator, java.nio.charset.CharsetDecoder chardecoder, java.nio.charset.CharsetEncoder charencoder, org.apache.http.config.MessageConstraints constraints, org.apache.http.entity.ContentLengthStrategy incomingContentStrategy, org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy)
Creates new instance NHttpConnectionBase given the underlying I/O session.- Parameters:
session
- the underlying I/O session.buffersize
- buffer size. Must be a positive number.fragmentSizeHint
- fragment size hint.allocator
- memory allocator. Ifnull
HeapByteBufferAllocator.INSTANCE
will be used.chardecoder
- decoder to be used for decoding HTTP protocol elements. Ifnull
simple type cast will be used for byte to char conversion.charencoder
- encoder to be used for encoding HTTP protocol elements. Ifnull
simple type cast will be used for char to byte conversion.constraints
- Message constraints. Ifnull
MessageConstraints.DEFAULT
will be used.incomingContentStrategy
- incoming content length strategy. Ifnull
LaxContentLengthStrategy.INSTANCE
will be used.outgoingContentStrategy
- outgoing content length strategy. Ifnull
StrictContentLengthStrategy.INSTANCE
will be used.- Since:
- 4.4
-
NHttpConnectionBase
protected NHttpConnectionBase(IOSession session, int buffersize, int fragmentSizeHint, ByteBufferAllocator allocator, java.nio.charset.CharsetDecoder chardecoder, java.nio.charset.CharsetEncoder charencoder, org.apache.http.entity.ContentLengthStrategy incomingContentStrategy, org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy)
Creates new instance NHttpConnectionBase given the underlying I/O session.- Parameters:
session
- the underlying I/O session.buffersize
- buffer size. Must be a positive number.fragmentSizeHint
- fragment size hint.allocator
- memory allocator. Ifnull
HeapByteBufferAllocator.INSTANCE
will be used.chardecoder
- decoder to be used for decoding HTTP protocol elements. Ifnull
simple type cast will be used for byte to char conversion.charencoder
- encoder to be used for encoding HTTP protocol elements. Ifnull
simple type cast will be used for char to byte conversion.incomingContentStrategy
- incoming content length strategy. Ifnull
LaxContentLengthStrategy.INSTANCE
will be used.outgoingContentStrategy
- outgoing content length strategy. Ifnull
StrictContentLengthStrategy.INSTANCE
will be used.- Since:
- 4.3
-
-
Method Detail
-
bind
protected void bind(IOSession session)
Binds the connection to a differentIOSession
. This may be necessary when the underlying I/O session gets upgraded with SSL/TLS encryption.- Since:
- 4.2
-
createIncomingContentStrategy
@Deprecated protected org.apache.http.entity.ContentLengthStrategy createIncomingContentStrategy()
Deprecated.(4.3) use constructor.- Since:
- 4.2
-
createOutgoingContentStrategy
@Deprecated protected org.apache.http.entity.ContentLengthStrategy createOutgoingContentStrategy()
Deprecated.(4.3) use constructor.- Since:
- 4.2
-
createTransportMetrics
@Deprecated protected org.apache.http.impl.io.HttpTransportMetricsImpl createTransportMetrics()
Deprecated.(4.3) no longer used.- Since:
- 4.1
-
createConnectionMetrics
@Deprecated protected org.apache.http.impl.HttpConnectionMetricsImpl createConnectionMetrics(org.apache.http.io.HttpTransportMetrics inTransportMetric, org.apache.http.io.HttpTransportMetrics outTransportMetric)
Deprecated.(4.3) use decorator to add additional metrics.- Since:
- 4.1
-
getStatus
public int getStatus()
Description copied from interface:NHttpConnection
Returns status of the connection:NHttpConnection.ACTIVE
: connection is active.NHttpConnection.CLOSING
: connection is being closed.NHttpConnection.CLOSED
: connection has been closed.- Specified by:
getStatus
in interfaceNHttpConnection
- Returns:
- connection status.
-
getContext
public org.apache.http.protocol.HttpContext getContext()
Description copied from interface:NHttpConnection
Returns an HTTP execution context associated with this connection.- Specified by:
getContext
in interfaceNHttpConnection
- Returns:
- HTTP context
-
getHttpRequest
public org.apache.http.HttpRequest getHttpRequest()
Description copied from interface:NHttpConnection
Returns the current HTTP request if one is being received / transmitted. Otherwise returnsnull
.- Specified by:
getHttpRequest
in interfaceNHttpConnection
- Returns:
- HTTP request, if available,
null
otherwise.
-
getHttpResponse
public org.apache.http.HttpResponse getHttpResponse()
Description copied from interface:NHttpConnection
Returns the current HTTP response if one is being received / transmitted. Otherwise returnsnull
.- Specified by:
getHttpResponse
in interfaceNHttpConnection
- Returns:
- HTTP response, if available,
null
otherwise.
-
requestInput
public void requestInput()
Description copied from interface:IOControl
Requests event notifications to be triggered when the underlying channel is ready for input operations.- Specified by:
requestInput
in interfaceIOControl
-
requestOutput
public void requestOutput()
Description copied from interface:IOControl
Requests event notifications to be triggered when the underlying channel is ready for output operations.- Specified by:
requestOutput
in interfaceIOControl
-
suspendInput
public void suspendInput()
Description copied from interface:IOControl
Suspends event notifications about the underlying channel being ready for input operations.- Specified by:
suspendInput
in interfaceIOControl
-
suspendOutput
public void suspendOutput()
Description copied from interface:IOControl
Suspends event notifications about the underlying channel being ready for output operations.- Specified by:
suspendOutput
in interfaceIOControl
-
prepareDecoder
protected org.apache.http.HttpEntity prepareDecoder(org.apache.http.HttpMessage message) throws org.apache.http.HttpException
Initializes a specificContentDecoder
implementation based on the properties of the givenHttpMessage
and generates an instance ofHttpEntity
matching the properties of the content decoder.- Parameters:
message
- the HTTP message.- Returns:
- HTTP entity.
- Throws:
org.apache.http.HttpException
- in case of an HTTP protocol violation.
-
createContentDecoder
protected ContentDecoder createContentDecoder(long len, java.nio.channels.ReadableByteChannel channel, SessionInputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics)
Factory method forContentDecoder
instances.- Parameters:
len
- content length, if known,ContentLengthStrategy.CHUNKED
orContentLengthStrategy.IDENTITY
, if unknown.channel
- the session channel.buffer
- the session buffer.metrics
- transport metrics.- Returns:
- content decoder.
- Since:
- 4.1
-
prepareEncoder
protected void prepareEncoder(org.apache.http.HttpMessage message) throws org.apache.http.HttpException
Initializes a specificContentEncoder
implementation based on the properties of the givenHttpMessage
.- Parameters:
message
- the HTTP message.- Throws:
org.apache.http.HttpException
- in case of an HTTP protocol violation.
-
createContentEncoder
protected ContentEncoder createContentEncoder(long len, java.nio.channels.WritableByteChannel channel, SessionOutputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics)
Factory method forContentEncoder
instances.- Parameters:
len
- content length, if known,ContentLengthStrategy.CHUNKED
orContentLengthStrategy.IDENTITY
, if unknown.channel
- the session channel.buffer
- the session buffer.metrics
- transport metrics.- Returns:
- content encoder.
- Since:
- 4.1
-
hasBufferedInput
public boolean hasBufferedInput()
Description copied from interface:SessionBufferStatus
Determines if the session input buffer contains data.- Specified by:
hasBufferedInput
in interfaceSessionBufferStatus
- Returns:
true
if the session input buffer contains data,false
otherwise.
-
hasBufferedOutput
public boolean hasBufferedOutput()
Description copied from interface:SessionBufferStatus
Determines if the session output buffer contains data.- Specified by:
hasBufferedOutput
in interfaceSessionBufferStatus
- Returns:
true
if the session output buffer contains data,false
otherwise.
-
assertNotClosed
protected void assertNotClosed() throws org.apache.http.ConnectionClosedException
Assets if the connection is still open.- Throws:
org.apache.http.ConnectionClosedException
- in case the connection has already been closed.
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfaceorg.apache.http.HttpConnection
- Throws:
java.io.IOException
-
isOpen
public boolean isOpen()
- Specified by:
isOpen
in interfaceorg.apache.http.HttpConnection
-
isStale
public boolean isStale()
- Specified by:
isStale
in interfaceorg.apache.http.HttpConnection
-
getLocalAddress
public java.net.InetAddress getLocalAddress()
- Specified by:
getLocalAddress
in interfaceorg.apache.http.HttpInetConnection
-
getLocalPort
public int getLocalPort()
- Specified by:
getLocalPort
in interfaceorg.apache.http.HttpInetConnection
-
getRemoteAddress
public java.net.InetAddress getRemoteAddress()
- Specified by:
getRemoteAddress
in interfaceorg.apache.http.HttpInetConnection
-
getRemotePort
public int getRemotePort()
- Specified by:
getRemotePort
in interfaceorg.apache.http.HttpInetConnection
-
setSocketTimeout
public void setSocketTimeout(int timeout)
- Specified by:
setSocketTimeout
in interfaceorg.apache.http.HttpConnection
-
getSocketTimeout
public int getSocketTimeout()
- Specified by:
getSocketTimeout
in interfaceorg.apache.http.HttpConnection
-
shutdown
public void shutdown() throws java.io.IOException
Description copied from interface:IOControl
Shuts down the underlying channel.
-
getMetrics
public org.apache.http.HttpConnectionMetrics getMetrics()
- Specified by:
getMetrics
in interfaceorg.apache.http.HttpConnection
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getSocket
public java.net.Socket getSocket()
Description copied from interface:SocketAccessor
Return the underlying socket- Specified by:
getSocket
in interfaceSocketAccessor
- Returns:
- - the underlying Socket, may be
null
.
-
-