org.apache.hc.core5.http.impl.io

## Class DefaultBHttpClientConnection

- java.lang.Object
- - org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnection

- All Implemented Interfaces:
    Closeable, AutoCloseable, HttpConnection, BHttpConnection, HttpClientConnection, SocketModalCloseable, ModalCloseable
  Direct Known Subclasses:
    LoggingBHttpClientConnection

      public class DefaultBHttpClientConnection
      extends Object
      implements HttpClientConnection

  Default implementation of `HttpClientConnection`.
  Since:
    4.3

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultBHttpClientConnection(Http1Config http1Config)`
    `DefaultBHttpClientConnection(Http1Config http1Config, CharsetDecoder charDecoder, CharsetEncoder charEncoder)`
    `DefaultBHttpClientConnection(Http1Config http1Config, CharsetDecoder charDecoder, CharsetEncoder charEncoder, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy, HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory, HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory)` Creates new instance of DefaultBHttpClientConnection.
    `DefaultBHttpClientConnection(Http1Config http1Config, CharsetDecoder charDecoder, CharsetEncoder charEncoder, ContentLengthStrategy incomingContentStrategy, ContentLengthStrategy outgoingContentStrategy, ResponseOutOfOrderStrategy responseOutOfOrderStrategy, HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory, HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory)` Creates new instance of DefaultBHttpClientConnection.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected boolean` | `awaitInput(Timeout timeout)`
    `void` | `bind(Socket socket)` Binds this connection to the given `Socket`.
    `protected void` | `bind(SocketHolder socketHolder)`
    `void` | `close()` Closes this connection gracefully.
    `void` | `close(CloseMode closeMode)` Closes this process or endpoint and releases any system resources associated with it.
    `protected InputStream` | `createContentInputStream(long len, SessionInputBuffer buffer, InputStream inputStream)`
    `protected OutputStream` | `createContentOutputStream(long len, SessionOutputBuffer buffer, OutputStream outputStream, Supplier<List<? extends Header>> trailers)`
    `protected SocketHolder` | `ensureOpen()`
    `void` | `flush()` Writes out all pending buffered data over the open connection.
    `EndpointDetails` | `getEndpointDetails()` Returns this connection's endpoint details.
    `SocketAddress` | `getLocalAddress()` Returns this connection's local address or `null` if it is not bound yet.
    `ProtocolVersion` | `getProtocolVersion()` Returns this connection's protocol version or `null` if unknown.
    `SocketAddress` | `getRemoteAddress()` Returns this connection's remote address or `null` if it is not connected yet or unconnected.
    `protected SocketHolder` | `getSocketHolder()`
    `Timeout` | `getSocketTimeout()` Returns the socket timeout value.
    `SSLSession` | `getSSLSession()` Returns this connection's SSL session or `null` if TLS has not been activated.
    `protected void` | `incrementRequestCount()`
    `protected void` | `incrementResponseCount()`
    `boolean` | `isConsistent()` Checks whether this connection is in a consistent state.
    `boolean` | `isDataAvailable(Timeout timeout)` Checks if input data is available from the connection.
    `boolean` | `isOpen()` Checks if this connection is open.
    `boolean` | `isStale()` Checks whether this connection has gone down.
    `protected void` | `onRequestSubmitted(ClassicHttpRequest request)`
    `protected void` | `onResponseReceived(ClassicHttpResponse response)`
    `void` | `receiveResponseEntity(ClassicHttpResponse response)` Receives the next response entity available from this connection and attaches it to an existing HttpResponse object.
    `ClassicHttpResponse` | `receiveResponseHeader()` Receives the request line and headers of the next response available from this connection.
    `void` | `sendRequestEntity(ClassicHttpRequest request)` Sends the request entity over the connection.
    `void` | `sendRequestHeader(ClassicHttpRequest request)` Sends the request line and all headers over the connection.
    `void` | `setSocketTimeout(Timeout timeout)` Sets the socket timeout value.
    `void` | `terminateRequest(ClassicHttpRequest request)` Terminates request prematurely potentially leaving the connection in a inconsistent state.
    `String` | `toString()`

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

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

    - ### Methods inherited from interface org.apache.hc.core5.http.io.BHttpConnection

      `flush, isDataAvailable, isStale`

    - ### Methods inherited from interface org.apache.hc.core5.http.HttpConnection

      `close, getEndpointDetails, getLocalAddress, getProtocolVersion, getRemoteAddress, getSSLSession, isOpen`

    - ### Methods inherited from interface org.apache.hc.core5.http.SocketModalCloseable

      `getSocketTimeout, setSocketTimeout`

    - ### Methods inherited from interface org.apache.hc.core5.io.ModalCloseable

      `close`

- - ### Constructor Detail

    - #### DefaultBHttpClientConnection

          public DefaultBHttpClientConnection(Http1Config http1Config,
                                      CharsetDecoder charDecoder,
                                      CharsetEncoder charEncoder,
                                      ContentLengthStrategy incomingContentStrategy,
                                      ContentLengthStrategy outgoingContentStrategy,
                                      ResponseOutOfOrderStrategy responseOutOfOrderStrategy,
                                      HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
                                      HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory)

      Creates new instance of DefaultBHttpClientConnection.
      Parameters:
        `http1Config` - Message http1Config. If `null` `Http1Config.DEFAULT` 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` `DefaultContentLengthStrategy.INSTANCE` will be used.
        `outgoingContentStrategy` - outgoing content length strategy. If `null` `DefaultContentLengthStrategy.INSTANCE` will be used.
        `responseOutOfOrderStrategy` - response out of order strategy. If `null` `NoResponseOutOfOrderStrategy.INSTANCE` will be used.
        `requestWriterFactory` - request writer factory. If `null` `DefaultHttpRequestWriterFactory.INSTANCE` will be used.
        `responseParserFactory` - response parser factory. If `null` `DefaultHttpResponseParserFactory.INSTANCE` will be used.

    - #### DefaultBHttpClientConnection

          public DefaultBHttpClientConnection(Http1Config http1Config,
                                      CharsetDecoder charDecoder,
                                      CharsetEncoder charEncoder,
                                      ContentLengthStrategy incomingContentStrategy,
                                      ContentLengthStrategy outgoingContentStrategy,
                                      HttpMessageWriterFactory<ClassicHttpRequest> requestWriterFactory,
                                      HttpMessageParserFactory<ClassicHttpResponse> responseParserFactory)

      Creates new instance of DefaultBHttpClientConnection.
      Parameters:
        `http1Config` - Message http1Config. If `null` `Http1Config.DEFAULT` 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` `DefaultContentLengthStrategy.INSTANCE` will be used.
        `outgoingContentStrategy` - outgoing content length strategy. If `null` `DefaultContentLengthStrategy.INSTANCE` will be used.
        `requestWriterFactory` - request writer factory. If `null` `DefaultHttpRequestWriterFactory.INSTANCE` will be used.
        `responseParserFactory` - response parser factory. If `null` `DefaultHttpResponseParserFactory.INSTANCE` will be used.

    - #### DefaultBHttpClientConnection

          public DefaultBHttpClientConnection(Http1Config http1Config,
                                      CharsetDecoder charDecoder,
                                      CharsetEncoder charEncoder)

    - #### DefaultBHttpClientConnection

          public DefaultBHttpClientConnection(Http1Config http1Config)

  - ### Method Detail

    - #### onResponseReceived

          protected void onResponseReceived(ClassicHttpResponse response)

    - #### onRequestSubmitted

          protected void onRequestSubmitted(ClassicHttpRequest request)

    - #### bind

          public void bind(Socket socket)
                    throws IOException

      Binds this connection to the given `Socket`. This socket will be used by the connection to send and receive data.

      After this method's execution the connection status will be reported as open and the `isOpen()` will return `true`.

      Parameters:
        `socket` - the socket.
      Throws:
        `IOException` - in case of an I/O error.

    - #### sendRequestHeader

          public void sendRequestHeader(ClassicHttpRequest request)
                                 throws HttpException,
                                        IOException

      Description copied from interface: `HttpClientConnection`
      Sends the request line and all headers over the connection.
      Specified by:
        `sendRequestHeader` in interface `HttpClientConnection`
      Parameters:
        `request` - the request whose headers to send.
      Throws:
        `HttpException` - in case of HTTP protocol violation
        `IOException` - in case of an I/O error

    - #### sendRequestEntity

          public void sendRequestEntity(ClassicHttpRequest request)
                                 throws HttpException,
                                        IOException

      Description copied from interface: `HttpClientConnection`
      Sends the request entity over the connection.
      Specified by:
        `sendRequestEntity` in interface `HttpClientConnection`
      Parameters:
        `request` - the request whose entity to send.
      Throws:
        `HttpException` - in case of HTTP protocol violation
        `IOException` - in case of an I/O error

    - #### isConsistent

          public boolean isConsistent()

      Description copied from interface: `HttpClientConnection`
      Checks whether this connection is in a consistent state.
      Specified by:
        `isConsistent` in interface `HttpClientConnection`
      Returns:
        `true` if the connection is known to be in a inconsistent state and cannot be re-used.
      See Also:
        `HttpClientConnection.terminateRequest(ClassicHttpRequest)`

    - #### terminateRequest

          public void terminateRequest(ClassicHttpRequest request)
                                throws HttpException,
                                       IOException

      Description copied from interface: `HttpClientConnection`
      Terminates request prematurely potentially leaving the connection in a inconsistent state.
      Specified by:
        `terminateRequest` in interface `HttpClientConnection`
      Parameters:
        `request` - the request to be terminated prematurely.
      Throws:
        `HttpException`
        `IOException`
      See Also:
        `HttpClientConnection.isConsistent()`

    - #### receiveResponseHeader

          public ClassicHttpResponse receiveResponseHeader()
                                                    throws HttpException,
                                                           IOException

      Description copied from interface: `HttpClientConnection`
      Receives the request line and headers of the next response available from this connection. The caller should examine the HttpResponse object to find out if it should try to receive a response entity as well.
      Specified by:
        `receiveResponseHeader` in interface `HttpClientConnection`
      Returns:
        a new HttpResponse object with status line and headers initialized.
      Throws:
        `HttpException` - in case of HTTP protocol violation
        `IOException` - in case of an I/O error

    - #### receiveResponseEntity

          public void receiveResponseEntity(ClassicHttpResponse response)
                                     throws HttpException,
                                            IOException

      Description copied from interface: `HttpClientConnection`
      Receives the next response entity available from this connection and attaches it to an existing HttpResponse object.
      Specified by:
        `receiveResponseEntity` in interface `HttpClientConnection`
      Parameters:
        `response` - the response to attach the entity to
      Throws:
        `HttpException` - in case of HTTP protocol violation
        `IOException` - in case of an I/O error

    - #### ensureOpen

          protected SocketHolder ensureOpen()
                                     throws IOException

      Throws:
        `IOException`

    - #### bind

          protected void bind(SocketHolder socketHolder)
                       throws IOException

      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.

    - #### getProtocolVersion

          public ProtocolVersion getProtocolVersion()

      Description copied from interface: `HttpConnection`
      Returns this connection's protocol version or `null` if unknown.
      Specified by:
        `getProtocolVersion` in interface `HttpConnection`
      Returns:
        this connection's protocol version or `null` if unknown.
      Since:
        5.0

    - #### getSocketHolder

          protected SocketHolder getSocketHolder()

    - #### createContentOutputStream

          protected OutputStream createContentOutputStream(long len,
                                               SessionOutputBuffer buffer,
                                               OutputStream outputStream,
                                               Supplier<List<? extends Header>> trailers)

    - #### createContentInputStream

          protected InputStream createContentInputStream(long len,
                                             SessionInputBuffer buffer,
                                             InputStream inputStream)

    - #### getRemoteAddress

          public SocketAddress getRemoteAddress()

      Description copied from interface: `HttpConnection`
      Returns this connection's remote address or `null` if it is not connected yet or unconnected.
      Specified by:
        `getRemoteAddress` in interface `HttpConnection`
      Returns:
        this connection's remote address or `null` if it is not connected yet or unconnected.

    - #### getLocalAddress

          public SocketAddress getLocalAddress()

      Description copied from interface: `HttpConnection`
      Returns this connection's local address or `null` if it is not bound yet.
      Specified by:
        `getLocalAddress` in interface `HttpConnection`
      Returns:
        this connection's local address or `null` if it is not bound yet.

    - #### setSocketTimeout

          public void setSocketTimeout(Timeout timeout)

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

    - #### getSocketTimeout

          public Timeout getSocketTimeout()

      Description copied from interface: `SocketModalCloseable`
      Returns the socket timeout value.
      Specified by:
        `getSocketTimeout` in interface `SocketModalCloseable`
      Returns:
        timeout value.

    - #### close

          public void close(CloseMode closeMode)

      Description copied from interface: `ModalCloseable`
      Closes this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.
      Specified by:
        `close` in interface `ModalCloseable`
      Parameters:
        `closeMode` - How to close the receiver.

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

    - #### awaitInput

          protected boolean awaitInput(Timeout timeout)
                                throws IOException

      Throws:
        `IOException`

    - #### isDataAvailable

          public boolean isDataAvailable(Timeout timeout)
                                  throws IOException

      Description copied from interface: `BHttpConnection`
      Checks if input data is available from the connection. May wait for the specified time until some data becomes available. Note that some implementations may completely ignore the timeout parameter.
      Specified by:
        `isDataAvailable` in interface `BHttpConnection`
      Parameters:
        `timeout` - the maximum time to wait for data
      Returns:
        true if data is available; false if there was no data available even after waiting for `timeout`.
      Throws:
        `IOException` - if an error happens on the connection

    - #### isStale

          public boolean isStale()
                          throws IOException

      Description copied from interface: `BHttpConnection`
      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 `BHttpConnection`
      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
      Throws:
        `IOException`

    - #### flush

          public void flush()
                     throws IOException

      Description copied from interface: `BHttpConnection`
      Writes out all pending buffered data over the open connection.
      Specified by:
        `flush` in interface `BHttpConnection`
      Throws:
        `IOException` - in case of an I/O error

    - #### incrementRequestCount

          protected void incrementRequestCount()

    - #### incrementResponseCount

          protected void incrementResponseCount()

    - #### getSSLSession

          public SSLSession getSSLSession()

      Description copied from interface: `HttpConnection`
      Returns this connection's SSL session or `null` if TLS has not been activated.
      Specified by:
        `getSSLSession` in interface `HttpConnection`
      Returns:
        this connection's SSL session or `null` if TLS has not been activated.

    - #### getEndpointDetails

          public EndpointDetails getEndpointDetails()

      Description copied from interface: `HttpConnection`
      Returns this connection's endpoint details.
      Specified by:
        `getEndpointDetails` in interface `HttpConnection`
      Returns:
        this connection's endpoint details.

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

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