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

      @NotThreadSafe
      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
    Modifier and Type | Field and Description
    --- | ---
    `protected HttpConnectionMetricsImpl` | `connMetrics`
    `protected ContentDecoder` | `contentDecoder`
    `protected ContentEncoder` | `contentEncoder`
    `protected HttpContext` | `context`
    `protected boolean` | `hasBufferedInput`
    `protected boolean` | `hasBufferedOutput`
    `protected SessionInputBufferImpl` | `inbuf`
    `protected ContentLengthStrategy` | `incomingContentStrategy`
    `protected HttpTransportMetricsImpl` | `inTransportMetrics`
    `protected SessionOutputBufferImpl` | `outbuf`
    `protected ContentLengthStrategy` | `outgoingContentStrategy`
    `protected HttpTransportMetricsImpl` | `outTransportMetrics`
    `protected SocketAddress` | `remote`
    `protected HttpRequest` | `request`
    `protected 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 and Description
    --- | ---
    `` | `NHttpConnectionBase(IOSession session, ByteBufferAllocator allocator, HttpParams params)` Deprecated. (4.3) use `NHttpConnectionBase(IOSession, int, int, ByteBufferAllocator, CharsetDecoder, CharsetEncoder, ContentLengthStrategy, ContentLengthStrategy)`
    `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.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `assertNotClosed()` Assets if the connection is still open.
    `protected void` | `bind(IOSession session)` Binds the connection to a different `IOSession`.
    `void` | `close()` Closes this connection gracefully.
    `protected HttpConnectionMetricsImpl` | `createConnectionMetrics(HttpTransportMetrics inTransportMetric, HttpTransportMetrics outTransportMetric)` Deprecated. (4.3) use decorator to add additional metrics.
    `protected ContentDecoder` | `createContentDecoder(long len, ReadableByteChannel channel, SessionInputBuffer buffer, HttpTransportMetricsImpl metrics)` Factory method for `ContentDecoder` instances.
    `protected ContentEncoder` | `createContentEncoder(long len, WritableByteChannel channel, SessionOutputBuffer buffer, HttpTransportMetricsImpl metrics)` Factory method for `ContentEncoder` instances.
    `protected ContentLengthStrategy` | `createIncomingContentStrategy()` Deprecated. (4.3) use constructor.
    `protected ContentLengthStrategy` | `createOutgoingContentStrategy()` Deprecated. (4.3) use constructor.
    `protected HttpTransportMetricsImpl` | `createTransportMetrics()` Deprecated. (4.3) no longer used.
    `HttpContext` | `getContext()` Returns an HTTP execution context associated with this connection.
    `HttpRequest` | `getHttpRequest()` Returns the current HTTP request if one is being received / transmitted.
    `HttpResponse` | `getHttpResponse()` Returns the current HTTP response if one is being received / transmitted.
    `InetAddress` | `getLocalAddress()`
    `int` | `getLocalPort()`
    `HttpConnectionMetrics` | `getMetrics()` Returns a collection of connection metrics.
    `InetAddress` | `getRemoteAddress()`
    `int` | `getRemotePort()`
    `Socket` | `getSocket()` Return the underlying socket
    `int` | `getSocketTimeout()` Returns the socket timeout value.
    `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()` Checks if this connection is open.
    `boolean` | `isStale()` Checks whether this connection has gone down.
    `protected HttpEntity` | `prepareDecoder(HttpMessage message)` Initializes a specific `ContentDecoder` implementation based on the properties of the given `HttpMessage` and generates an instance of `HttpEntity` matching the properties of the content decoder.
    `protected void` | `prepareEncoder(HttpMessage message)` Initializes a specific `ContentEncoder` implementation based on the properties of the given `HttpMessage`.
    `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)` Sets the socket timeout value.
    `void` | `shutdown()` Force-closes this connection.
    `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.
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### incomingContentStrategy

          protected final ContentLengthStrategy incomingContentStrategy

    - #### outgoingContentStrategy

          protected final ContentLengthStrategy outgoingContentStrategy

    - #### inbuf

          protected final SessionInputBufferImpl inbuf

    - #### outbuf

          protected final SessionOutputBufferImpl outbuf

    - #### inTransportMetrics

          protected final HttpTransportMetricsImpl inTransportMetrics

    - #### outTransportMetrics

          protected final HttpTransportMetricsImpl outTransportMetrics

    - #### connMetrics

          protected final HttpConnectionMetricsImpl connMetrics

    - #### context

          protected HttpContext context

    - #### session

          protected IOSession session

    - #### remote

          protected 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 HttpRequest request

    - #### response

          protected volatile HttpResponse response

    - #### status

          protected volatile int status

  - ### Constructor Detail

    - #### NHttpConnectionBase

          @Deprecated
          public NHttpConnectionBase(IOSession session,
                                        ByteBufferAllocator allocator,
                                        HttpParams params)

      Deprecated. (4.3) use `NHttpConnectionBase(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.

    - #### 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. If `null` `HeapByteBufferAllocator.INSTANCE` will be used.
        `chardecoder` - decoder to be used for decoding HTTP protocol elements. If `null` simple type cast will be used for byte to char conversion.
        `charencoder` - encoder to be used for encoding HTTP protocol elements. If `null` simple type cast will be used for char to byte conversion.
        `incomingContentStrategy` - incoming content length strategy. If `null` `LaxContentLengthStrategy.INSTANCE` will be used.
        `outgoingContentStrategy` - outgoing content length strategy. If `null` `StrictContentLengthStrategy.INSTANCE` will be used.
      Since:
        4.3

  - ### Method Detail

    - #### bind

          protected void bind(IOSession session)

      Binds the connection to a different `IOSession`. This may be necessary when the underlying I/O session gets upgraded with SSL/TLS encryption.
      Since:
        4.2

    - #### createIncomingContentStrategy

          @Deprecated
          protected ContentLengthStrategy createIncomingContentStrategy()

      Deprecated. (4.3) use constructor.
      Since:
        4.2

    - #### createOutgoingContentStrategy

          @Deprecated
          protected ContentLengthStrategy createOutgoingContentStrategy()

      Deprecated. (4.3) use constructor.
      Since:
        4.2

    - #### createTransportMetrics

          @Deprecated
          protected HttpTransportMetricsImpl 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: `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 interface `NHttpConnection`
      Returns:
        connection status.

    - #### getContext

          public HttpContext getContext()

      Description copied from interface: `NHttpConnection`
      Returns an HTTP execution context associated with this connection.
      Specified by:
        `getContext` in interface `NHttpConnection`
      Returns:
        HTTP context

    - #### getHttpRequest

          public HttpRequest getHttpRequest()

      Description copied from interface: `NHttpConnection`
      Returns the current HTTP request if one is being received / transmitted. Otherwise returns `null`.
      Specified by:
        `getHttpRequest` in interface `NHttpConnection`
      Returns:
        HTTP request, if available, `null` otherwise.

    - #### getHttpResponse

          public HttpResponse getHttpResponse()

      Description copied from interface: `NHttpConnection`
      Returns the current HTTP response if one is being received / transmitted. Otherwise returns `null`.
      Specified by:
        `getHttpResponse` in interface `NHttpConnection`
      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 interface `IOControl`

    - #### 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 interface `IOControl`

    - #### 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 interface `IOControl`

    - #### 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 interface `IOControl`

    - #### prepareDecoder

          protected HttpEntity prepareDecoder(HttpMessage message)
                                       throws HttpException

      Initializes a specific `ContentDecoder` implementation based on the properties of the given `HttpMessage` and generates an instance of `HttpEntity` matching 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 for `ContentDecoder` instances.
      Parameters:
        `len` - content length, if known, `ContentLengthStrategy.CHUNKED` or `ContentLengthStrategy.IDENTITY`, if unknown.
        `channel` - the session channel.
        `buffer` - the session buffer.
        `metrics` - transport metrics.
      Returns:
        content decoder.
      Since:
        4.1

    - #### prepareEncoder

          protected void prepareEncoder(HttpMessage message)
                                 throws HttpException

      Initializes a specific `ContentEncoder` implementation based on the properties of the given `HttpMessage`.
      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 for `ContentEncoder` instances.
      Parameters:
        `len` - content length, if known, `ContentLengthStrategy.CHUNKED` or `ContentLengthStrategy.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 interface `SessionBufferStatus`
      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 interface `SessionBufferStatus`
      Returns:
        `true` if the session output buffer contains data, `false` otherwise.

    - #### assertNotClosed

          protected void assertNotClosed()
                                  throws ConnectionClosedException

      Assets if the connection is still open.
      Throws:
        `ConnectionClosedException` - in case the connection has already been closed.

    - #### close

          public void close()
                     throws IOException

      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. Use `shutdown` instead.
      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Specified by:
        `close` in interface `HttpConnection`
      Throws:
        `IOException`

    - #### isOpen

          public boolean isOpen()

      Description copied from interface: `HttpConnection`
      Checks if this connection is open.
      Specified by:
        `isOpen` in interface `HttpConnection`
      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 interface `HttpConnection`
      Returns:
        `true` if attempts to use this connection are likely to succeed, or `false` if they are likely to fail and this connection should be closed

    - #### getLocalAddress

          public InetAddress getLocalAddress()

      Specified by:
        `getLocalAddress` in interface `HttpInetConnection`

    - #### getLocalPort

          public int getLocalPort()

      Specified by:
        `getLocalPort` in interface `HttpInetConnection`

    - #### getRemoteAddress

          public InetAddress getRemoteAddress()

      Specified by:
        `getRemoteAddress` in interface `HttpInetConnection`

    - #### getRemotePort

          public int getRemotePort()

      Specified by:
        `getRemotePort` in interface `HttpInetConnection`

    - #### setSocketTimeout

          public void setSocketTimeout(int timeout)

      Description copied from interface: `HttpConnection`
      Sets the socket timeout value.
      Specified by:
        `setSocketTimeout` in interface `HttpConnection`
      Parameters:
        `timeout` - timeout value in milliseconds

    - #### getSocketTimeout

          public int getSocketTimeout()

      Description copied from interface: `HttpConnection`
      Returns the socket timeout value.
      Specified by:
        `getSocketTimeout` in interface `HttpConnection`
      Returns:
        positive value in milliseconds if a timeout is set, `0` if timeout is disabled or `-1` if timeout is undefined.

    - #### shutdown

          public void shutdown()
                        throws IOException

      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 interface `HttpConnection`
      Specified by:
        `shutdown` in interface `IOControl`
      Throws:
        `IOException`

    - #### getMetrics

          public HttpConnectionMetrics getMetrics()

      Description copied from interface: `HttpConnection`
      Returns a collection of connection metrics.
      Specified by:
        `getMetrics` in interface `HttpConnection`
      Returns:
        HttpConnectionMetrics

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### getSocket

          public Socket getSocket()

      Description copied from interface: `SocketAccessor`
      Return the underlying socket
      Specified by:
        `getSocket` in interface `SocketAccessor`
      Returns:
        - the underlying Socket, may be `null`.

Copyright © 2005–2013 The Apache Software Foundation. All rights reserved.