Package org.apache.http.impl.nio
Class NHttpConnectionBase
java.lang.Object
org.apache.http.impl.nio.NHttpConnectionBase
- All Implemented Interfaces:
Closeable,AutoCloseable,HttpConnection,HttpInetConnection,IOControl,NHttpConnection,SessionBufferStatus,SocketAccessor
- Direct Known Subclasses:
DefaultNHttpClientConnection,DefaultNHttpServerConnection
public class NHttpConnectionBase
extends Object
implements NHttpConnection, 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 HttpConnectionMetricsImplprivate final MessageConstraintsprotected ContentDecoderprotected ContentEncoderprotected HttpContextprivate final intprotected booleanprotected booleanprotected final SessionInputBufferImplprotected final ContentLengthStrategyprotected final HttpTransportMetricsImplprotected final SessionOutputBufferImplprotected final ContentLengthStrategyprotected final HttpTransportMetricsImplprotected SocketAddressprotected HttpRequestprotected 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, MessageConstraints constraints, ContentLengthStrategy incomingContentStrategy, 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, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy) Creates new instance NHttpConnectionBase given the underlying I/O session.NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, HttpParams params) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAssets if the connection is still open.protected voidBinds the connection to a differentIOSession.voidclose()Closes this connection gracefully.protected HttpConnectionMetricsImplcreateConnectionMetrics(HttpTransportMetrics inTransportMetric, HttpTransportMetrics outTransportMetric) Deprecated.(4.3) use decorator to add additional metrics.protected ContentDecodercreateContentDecoder(long len, ReadableByteChannel channel, SessionInputBuffer buffer, HttpTransportMetricsImpl metrics) Factory method forContentDecoderinstances.protected ContentEncodercreateContentEncoder(long len, WritableByteChannel channel, SessionOutputBuffer buffer, HttpTransportMetricsImpl metrics) Factory method forContentEncoderinstances.protected ContentLengthStrategyDeprecated.(4.3) use constructor.protected ContentLengthStrategyDeprecated.(4.3) use constructor.protected HttpTransportMetricsImplDeprecated.(4.3) no longer used.Returns an HTTP execution context associated with this connection.Returns the current HTTP request if one is being received / transmitted.Returns the current HTTP response if one is being received / transmitted.intReturns a collection of connection metrics.intReturn the underlying socketintReturns the socket timeout value.intReturns status of the connection:booleanDetermines if the session input buffer contains data.booleanDetermines if the session output buffer contains data.booleanisOpen()Checks if this connection is open.booleanisStale()Checks whether this connection has gone down.protected HttpEntityprepareDecoder(HttpMessage message) Initializes a specificContentDecoderimplementation based on the properties of the givenHttpMessageand generates an instance ofHttpEntitymatching the properties of the content decoder.protected voidprepareEncoder(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.private voidsetSession(IOSession session) voidsetSocketTimeout(int timeout) Sets the socket timeout value.voidshutdown()Force-closes this connection.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
-
outgoingContentStrategy
-
inbuf
-
outbuf
-
fragmentSizeHint
private final int fragmentSizeHint -
constraints
-
inTransportMetrics
-
outTransportMetrics
-
connMetrics
-
context
-
session
-
remote
-
contentDecoder
-
hasBufferedInput
protected volatile boolean hasBufferedInput -
contentEncoder
-
hasBufferedOutput
protected volatile boolean hasBufferedOutput -
request
-
response
-
status
protected volatile int status
-
-
Constructor Details
-
NHttpConnectionBase
@Deprecated public NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, 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, MessageConstraints constraints, ContentLengthStrategy incomingContentStrategy, 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, ContentLengthStrategy incomingContentStrategy, 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
-
setSession
-
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 HttpConnectionMetricsImpl createConnectionMetrics(HttpTransportMetrics inTransportMetric, 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
Description copied from interface:NHttpConnectionReturns an HTTP execution context associated with this connection.- Specified by:
getContextin interfaceNHttpConnection- Returns:
- HTTP context
-
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
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
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:
HttpException- in case of an HTTP protocol violation.
-
createContentDecoder
protected ContentDecoder createContentDecoder(long len, ReadableByteChannel channel, SessionInputBuffer buffer, 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
Initializes a specificContentEncoderimplementation based on the properties of the givenHttpMessage.- Parameters:
message- the HTTP message.- Throws:
HttpException- in case of an HTTP protocol violation.
-
createContentEncoder
protected ContentEncoder createContentEncoder(long len, WritableByteChannel channel, SessionOutputBuffer buffer, 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
Assets if the connection is still open.- Throws:
ConnectionClosedException- in case the connection has already been closed.
-
close
Description copied from interface:HttpConnectionCloses this connection gracefully. This method will attempt to flush the internal output buffer prior to closing the underlying socket. This method MUST NOT be called from a different thread to force shutdown of the connection. Useshutdowninstead.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceHttpConnection- Throws:
IOException
-
isOpen
public boolean isOpen()Description copied from interface:HttpConnectionChecks if this connection is open.- Specified by:
isOpenin interfaceHttpConnection- Returns:
- true if it is open, false if it is closed.
-
isStale
public boolean isStale()Description copied from interface:HttpConnectionChecks whether this connection has gone down. Network connections may get closed during some time of inactivity for several reasons. The next time a read is attempted on such a connection it will throw an IOException. This method tries to alleviate this inconvenience by trying to find out if a connection is still usable. Implementations may do that by attempting a read with a very small timeout. Thus this method may block for a small amount of time before returning a result. It is therefore an expensive operation.- Specified by:
isStalein interfaceHttpConnection- Returns:
trueif attempts to use this connection are likely to succeed, orfalseif they are likely to fail and this connection should be closed
-
getLocalAddress
- Specified by:
getLocalAddressin interfaceHttpInetConnection
-
getLocalPort
public int getLocalPort()- Specified by:
getLocalPortin interfaceHttpInetConnection
-
getRemoteAddress
- Specified by:
getRemoteAddressin interfaceHttpInetConnection
-
getRemotePort
public int getRemotePort()- Specified by:
getRemotePortin interfaceHttpInetConnection
-
setSocketTimeout
public void setSocketTimeout(int timeout) Description copied from interface:HttpConnectionSets the socket timeout value.- Specified by:
setSocketTimeoutin interfaceHttpConnection- Parameters:
timeout- timeout value in milliseconds
-
getSocketTimeout
public int getSocketTimeout()Description copied from interface:HttpConnectionReturns the socket timeout value.- Specified by:
getSocketTimeoutin interfaceHttpConnection- Returns:
- positive value in milliseconds if a timeout is set,
0if timeout is disabled or-1if timeout is undefined.
-
shutdown
Description copied from interface:HttpConnectionForce-closes this connection. This is the only method of a connection which may be called from a different thread to terminate the connection. This method will not attempt to flush the transmitter's internal buffer prior to closing the underlying socket.- Specified by:
shutdownin interfaceHttpConnection- Specified by:
shutdownin interfaceIOControl- Throws:
IOException- in an error occurs
-
getMetrics
Description copied from interface:HttpConnectionReturns a collection of connection metrics.- Specified by:
getMetricsin interfaceHttpConnection- Returns:
- HttpConnectionMetrics
-
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)