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
Fields inherited from interface org.apache.http.nio.NHttpConnection
ACTIVE, CLOSED, CLOSING
-
Constructor Summary
ConstructorsConstructorDescriptionNHttpConnectionBase
(IOSession session, ByteBufferAllocator allocator, HttpParams params) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this connection gracefully.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.int
Returns a collection of connection metrics.int
Return the underlying socketint
Returns the socket timeout value.int
Returns status of the connection:boolean
Determines if the session input buffer contains data.boolean
Determines if the session output buffer contains data.boolean
isOpen()
Checks if this connection is open.boolean
isStale()
Checks whether this connection has gone down.void
Requests event notifications to be triggered when the underlying channel is ready for input operations.void
Requests event notifications to be triggered when the underlying channel is ready for output operations.void
setSocketTimeout
(int timeout) Sets the socket timeout value.void
shutdown()
Force-closes this connection.void
Suspends event notifications about the underlying channel being ready for input operations.void
Suspends event notifications about the underlying channel being ready for output operations.toString()
-
Constructor Details
-
NHttpConnectionBase
@Deprecated public NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, HttpParams params) Deprecated.(4.3) useNHttpConnectionBase(IOSession, int, int, ByteBufferAllocator, CharsetDecoder, CharsetEncoder, ContentLengthStrategy, ContentLengthStrategy)
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.
-
-
Method Details
-
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
Description copied from interface:NHttpConnection
Returns an HTTP execution context associated with this connection.- Specified by:
getContext
in interfaceNHttpConnection
- Returns:
- HTTP context
-
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
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
-
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.
-
close
Description copied from interface:HttpConnection
Closes 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. Useshutdown
instead.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceHttpConnection
- Throws:
IOException
-
isOpen
public boolean isOpen()Description copied from interface:HttpConnection
Checks if this connection is open.- Specified by:
isOpen
in interfaceHttpConnection
- Returns:
- true if it is open, false if it is closed.
-
isStale
public boolean isStale()Description copied from interface:HttpConnection
Checks 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:
isStale
in interfaceHttpConnection
- Returns:
true
if attempts to use this connection are likely to succeed, orfalse
if they are likely to fail and this connection should be closed
-
getLocalAddress
- Specified by:
getLocalAddress
in interfaceHttpInetConnection
-
getLocalPort
public int getLocalPort()- Specified by:
getLocalPort
in interfaceHttpInetConnection
-
getRemoteAddress
- Specified by:
getRemoteAddress
in interfaceHttpInetConnection
-
getRemotePort
public int getRemotePort()- Specified by:
getRemotePort
in interfaceHttpInetConnection
-
setSocketTimeout
public void setSocketTimeout(int timeout) Description copied from interface:HttpConnection
Sets the socket timeout value.- Specified by:
setSocketTimeout
in interfaceHttpConnection
- Parameters:
timeout
- timeout value in milliseconds
-
getSocketTimeout
public int getSocketTimeout()Description copied from interface:HttpConnection
Returns the socket timeout value.- Specified by:
getSocketTimeout
in interfaceHttpConnection
- Returns:
- positive value in milliseconds if a timeout is set,
0
if timeout is disabled or-1
if timeout is undefined.
-
shutdown
Description copied from interface:HttpConnection
Force-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:
shutdown
in interfaceHttpConnection
- Specified by:
shutdown
in interfaceIOControl
- Throws:
IOException
- in an error occurs
-
getMetrics
Description copied from interface:HttpConnection
Returns a collection of connection metrics.- Specified by:
getMetrics
in interfaceHttpConnection
- Returns:
- HttpConnectionMetrics
-
toString
-
getSocket
Description copied from interface:SocketAccessor
Return the underlying socket- Specified by:
getSocket
in interfaceSocketAccessor
- Returns:
- - the underlying Socket, may be
null
.
-
NHttpConnectionBase(IOSession, int, int, ByteBufferAllocator, CharsetDecoder, CharsetEncoder, ContentLengthStrategy, ContentLengthStrategy)