Package org.apache.http.impl.nio
Class NHttpConnectionBase
java.lang.Object
org.apache.http.impl.nio.NHttpConnectionBase
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.http.HttpConnection,org.apache.http.HttpInetConnection,IOControl,NHttpConnection,SessionBufferStatus,SocketAccessor
- Direct Known Subclasses:
DefaultNHttpClientConnection,DefaultNHttpServerConnection
public class NHttpConnectionBase
extends Object
implements NHttpConnection, org.apache.http.HttpInetConnection, SessionBufferStatus, SocketAccessor
This class serves as a base for all
NHttpConnection implementations and provides
functionality common to both client and server HTTP connections.- Since:
- 4.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.apache.http.impl.HttpConnectionMetricsImplprotected ContentDecoderprotected ContentEncoderprotected org.apache.http.protocol.HttpContextprotected booleanprotected booleanprotected final SessionInputBufferImplprotected final org.apache.http.entity.ContentLengthStrategyprotected final org.apache.http.impl.io.HttpTransportMetricsImplprotected final SessionOutputBufferImplprotected final org.apache.http.entity.ContentLengthStrategyprotected final org.apache.http.impl.io.HttpTransportMetricsImplprotected SocketAddressprotected org.apache.http.HttpRequestprotected org.apache.http.HttpResponseprotected IOSessionprotected intFields inherited from interface org.apache.http.nio.NHttpConnection
ACTIVE, CLOSED, CLOSING -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNHttpConnectionBase(IOSession session, int bufferSize, int fragmentSizeHint, ByteBufferAllocator allocator, CharsetDecoder charDecoder, 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.protectedNHttpConnectionBase(IOSession session, int bufferSize, int fragmentSizeHint, ByteBufferAllocator allocator, CharsetDecoder charDecoder, 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) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAssets if the connection is still open.protected voidBinds the connection to a differentIOSession.voidclose()protected org.apache.http.impl.HttpConnectionMetricsImplcreateConnectionMetrics(org.apache.http.io.HttpTransportMetrics inTransportMetric, org.apache.http.io.HttpTransportMetrics outTransportMetric) Deprecated.(4.3) use decorator to add additional metrics.protected ContentDecodercreateContentDecoder(long len, ReadableByteChannel channel, SessionInputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics) Factory method forContentDecoderinstances.protected ContentEncodercreateContentEncoder(long len, WritableByteChannel channel, SessionOutputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics) Factory method forContentEncoderinstances.protected org.apache.http.entity.ContentLengthStrategyDeprecated.(4.3) use constructor.protected org.apache.http.entity.ContentLengthStrategyDeprecated.(4.3) use constructor.protected org.apache.http.impl.io.HttpTransportMetricsImplDeprecated.(4.3) no longer used.org.apache.http.protocol.HttpContextReturns an HTTP execution context associated with this connection.org.apache.http.HttpRequestReturns the current HTTP request if one is being received / transmitted.org.apache.http.HttpResponseReturns the current HTTP response if one is being received / transmitted.intorg.apache.http.HttpConnectionMetricsintReturn the underlying socketintintReturns status of the connection:booleanDetermines if the session input buffer contains data.booleanDetermines if the session output buffer contains data.booleanisOpen()booleanisStale()protected org.apache.http.HttpEntityprepareDecoder(org.apache.http.HttpMessage message) Initializes a specificContentDecoderimplementation based on the properties of the givenHttpMessageand generates an instance ofHttpEntitymatching the properties of the content decoder.protected voidprepareEncoder(org.apache.http.HttpMessage message) Initializes a specificContentEncoderimplementation based on the properties of the givenHttpMessage.voidRequests event notifications to be triggered when the underlying channel is ready for input operations.voidRequests event notifications to be triggered when the underlying channel is ready for output operations.voidsetSocketTimeout(int timeout) voidshutdown()Shuts down the underlying channel.voidSuspends event notifications about the underlying channel being ready for input operations.voidSuspends event notifications about the underlying channel being ready for output operations.toString()
-
Field Details
-
incomingContentStrategy
protected final org.apache.http.entity.ContentLengthStrategy incomingContentStrategy -
outgoingContentStrategy
protected final org.apache.http.entity.ContentLengthStrategy outgoingContentStrategy -
inbuf
-
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
-
remote
-
contentDecoder
-
hasBufferedInput
protected volatile boolean hasBufferedInput -
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 Details
-
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, CharsetDecoder charDecoder, 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. IfnullHeapByteBufferAllocator.INSTANCEwill be used.charDecoder- decoder to be used for decoding HTTP protocol elements. Ifnullsimple type cast will be used for byte to char conversion.charEncoder- encoder to be used for encoding HTTP protocol elements. Ifnullsimple type cast will be used for char to byte conversion.constraints- Message constraints. IfnullMessageConstraints.DEFAULTwill be used.incomingContentStrategy- incoming content length strategy. IfnullLaxContentLengthStrategy.INSTANCEwill be used.outgoingContentStrategy- outgoing content length strategy. IfnullStrictContentLengthStrategy.INSTANCEwill be used.- Since:
- 4.4
-
NHttpConnectionBase
protected NHttpConnectionBase(IOSession session, int bufferSize, int fragmentSizeHint, ByteBufferAllocator allocator, CharsetDecoder charDecoder, 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. IfnullHeapByteBufferAllocator.INSTANCEwill be used.charDecoder- decoder to be used for decoding HTTP protocol elements. Ifnullsimple type cast will be used for byte to char conversion.charEncoder- encoder to be used for encoding HTTP protocol elements. Ifnullsimple type cast will be used for char to byte conversion.incomingContentStrategy- incoming content length strategy. IfnullLaxContentLengthStrategy.INSTANCEwill be used.outgoingContentStrategy- outgoing content length strategy. IfnullStrictContentLengthStrategy.INSTANCEwill be used.- Since:
- 4.3
-
-
Method Details
-
bind
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.(4.3) use constructor.- Since:
- 4.2
-
createOutgoingContentStrategy
Deprecated.(4.3) use constructor.- Since:
- 4.2
-
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:NHttpConnectionReturns status of the connection:NHttpConnection.ACTIVE: connection is active.NHttpConnection.CLOSING: connection is being closed.NHttpConnection.CLOSED: connection has been closed.- Specified by:
getStatusin interfaceNHttpConnection- Returns:
- connection status.
-
getContext
public org.apache.http.protocol.HttpContext getContext()Description copied from interface:NHttpConnectionReturns an HTTP execution context associated with this connection.- Specified by:
getContextin interfaceNHttpConnection- Returns:
- HTTP context
-
getHttpRequest
public org.apache.http.HttpRequest getHttpRequest()Description copied from interface:NHttpConnectionReturns the current HTTP request if one is being received / transmitted. Otherwise returnsnull.- Specified by:
getHttpRequestin interfaceNHttpConnection- Returns:
- HTTP request, if available,
nullotherwise.
-
getHttpResponse
public org.apache.http.HttpResponse getHttpResponse()Description copied from interface:NHttpConnectionReturns the current HTTP response if one is being received / transmitted. Otherwise returnsnull.- Specified by:
getHttpResponsein interfaceNHttpConnection- Returns:
- HTTP response, if available,
nullotherwise.
-
requestInput
public void requestInput()Description copied from interface:IOControlRequests event notifications to be triggered when the underlying channel is ready for input operations.- Specified by:
requestInputin interfaceIOControl
-
requestOutput
public void requestOutput()Description copied from interface:IOControlRequests event notifications to be triggered when the underlying channel is ready for output operations.- Specified by:
requestOutputin interfaceIOControl
-
suspendInput
public void suspendInput()Description copied from interface:IOControlSuspends event notifications about the underlying channel being ready for input operations.- Specified by:
suspendInputin interfaceIOControl
-
suspendOutput
public void suspendOutput()Description copied from interface:IOControlSuspends event notifications about the underlying channel being ready for output operations.- Specified by:
suspendOutputin interfaceIOControl
-
prepareDecoder
protected org.apache.http.HttpEntity prepareDecoder(org.apache.http.HttpMessage message) throws org.apache.http.HttpException Initializes a specificContentDecoderimplementation based on the properties of the givenHttpMessageand generates an instance ofHttpEntitymatching 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, ReadableByteChannel channel, SessionInputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics) Factory method forContentDecoderinstances.- Parameters:
len- content length, if known,ContentLengthStrategy.CHUNKEDorContentLengthStrategy.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 specificContentEncoderimplementation 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, WritableByteChannel channel, SessionOutputBuffer buffer, org.apache.http.impl.io.HttpTransportMetricsImpl metrics) Factory method forContentEncoderinstances.- Parameters:
len- content length, if known,ContentLengthStrategy.CHUNKEDorContentLengthStrategy.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:SessionBufferStatusDetermines if the session input buffer contains data.- Specified by:
hasBufferedInputin interfaceSessionBufferStatus- Returns:
trueif the session input buffer contains data,falseotherwise.
-
hasBufferedOutput
public boolean hasBufferedOutput()Description copied from interface:SessionBufferStatusDetermines if the session output buffer contains data.- Specified by:
hasBufferedOutputin interfaceSessionBufferStatus- Returns:
trueif the session output buffer contains data,falseotherwise.
-
assertNotClosed
protected void assertNotClosed() throws org.apache.http.ConnectionClosedExceptionAssets if the connection is still open.- Throws:
org.apache.http.ConnectionClosedException- in case the connection has already been closed.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceorg.apache.http.HttpConnection- Throws:
IOException
-
isOpen
public boolean isOpen()- Specified by:
isOpenin interfaceorg.apache.http.HttpConnection
-
isStale
public boolean isStale()- Specified by:
isStalein interfaceorg.apache.http.HttpConnection
-
getLocalAddress
- Specified by:
getLocalAddressin interfaceorg.apache.http.HttpInetConnection
-
getLocalPort
public int getLocalPort()- Specified by:
getLocalPortin interfaceorg.apache.http.HttpInetConnection
-
getRemoteAddress
- Specified by:
getRemoteAddressin interfaceorg.apache.http.HttpInetConnection
-
getRemotePort
public int getRemotePort()- Specified by:
getRemotePortin interfaceorg.apache.http.HttpInetConnection
-
setSocketTimeout
public void setSocketTimeout(int timeout) - Specified by:
setSocketTimeoutin interfaceorg.apache.http.HttpConnection
-
getSocketTimeout
public int getSocketTimeout()- Specified by:
getSocketTimeoutin interfaceorg.apache.http.HttpConnection
-
shutdown
Description copied from interface:IOControlShuts down the underlying channel.- Specified by:
shutdownin interfaceorg.apache.http.HttpConnection- Specified by:
shutdownin interfaceIOControl- Throws:
IOException- in an error occurs
-
getMetrics
public org.apache.http.HttpConnectionMetrics getMetrics()- Specified by:
getMetricsin interfaceorg.apache.http.HttpConnection
-
toString
-
getSocket
Description copied from interface:SocketAccessorReturn the underlying socket- Specified by:
getSocketin interfaceSocketAccessor- Returns:
- - the underlying Socket, may be
null.
-
NHttpConnectionBase(IOSession, int, int, ByteBufferAllocator, CharsetDecoder, CharsetEncoder, ContentLengthStrategy, ContentLengthStrategy)