Class AbstractClientConnAdapter

java.lang.Object
org.apache.http.impl.conn.AbstractClientConnAdapter
All Implemented Interfaces:
ConnectionReleaseTrigger, ManagedClientConnection, HttpClientConnection, HttpConnection, HttpInetConnection
Direct Known Subclasses:
AbstractPooledConnAdapter

public abstract class AbstractClientConnAdapter
extends Object
implements ManagedClientConnection
Abstract adapter from operated to managed client connections. Read and write methods are delegated to the wrapped connection. Operations affecting the connection state have to be implemented by derived classes. Operations for querying the connection state are delegated to the wrapped connection if there is one, or return a default value if there is none.
This adapter tracks the checkpoints for reusable communication states, as indicated by markReusable and queried by isMarkedReusable. All send and receive operations will automatically clear the mark.
Connection release calls are delegated to the connection manager, if there is one. abortConnection will clear the reusability mark first. The connection manager is expected to tolerate multiple calls to the release method.
Since:
4.0
Version:
$Revision: 672969 $ $Date: 2008-06-30 18:09:50 -0700 (Mon, 30 Jun 2008) $
Author:
Roland Weber
  • Constructor Details

    • AbstractClientConnAdapter

      protected AbstractClientConnAdapter​(ClientConnectionManager mgr, OperatedClientConnection conn)
      Creates a new connection adapter. The adapter is initially not marked as reusable.
      Parameters:
      mgr - the connection manager, or null
      conn - the connection to wrap, or null
  • Method Details

    • detach

      protected void detach()
      Detaches this adapter from the wrapped connection. This adapter becomes useless.
    • getWrappedConnection

      protected OperatedClientConnection getWrappedConnection()
    • getManager

      protected ClientConnectionManager getManager()
    • assertNotAborted

      protected final void assertNotAborted() throws InterruptedIOException
      Asserts that the connection has not been aborted.
      Throws:
      InterruptedIOException - if the connection has been aborted
    • assertValid

      protected final void assertValid​(OperatedClientConnection wrappedConn)
      Asserts that there is a wrapped connection to delegate to.
      Throws:
      IllegalStateException - if there is no wrapped connection or connection has been aborted
    • 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
    • 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.
    • getMetrics

      public HttpConnectionMetrics getMetrics()
      Description copied from interface: HttpConnection
      Returns a collection of connection metrcis
      Specified by:
      getMetrics in interface HttpConnection
      Returns:
      HttpConnectionMetrics
    • flush

      public void flush() throws IOException
      Description copied from interface: HttpClientConnection
      Writes out all pending buffered data over the open connection.
      Specified by:
      flush in interface HttpClientConnection
      Throws:
      IOException
    • isResponseAvailable

      public boolean isResponseAvailable​(int timeout) throws IOException
      Description copied from interface: HttpClientConnection
      Checks if response 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:
      isResponseAvailable in interface HttpClientConnection
      Parameters:
      timeout - the maximum time in milliseconds to wait for data
      Returns:
      true if data is available; false if there was no data available even after waiting for timeout milliseconds.
      Throws:
      IOException - if an error happens on the connection
    • receiveResponseEntity

      public void receiveResponseEntity​(HttpResponse 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
      IOException
    • receiveResponseHeader

      public HttpResponse 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
      IOException
    • sendRequestEntity

      public void sendRequestEntity​(HttpEntityEnclosingRequest 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
      IOException
    • sendRequestHeader

      public void sendRequestHeader​(HttpRequest 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
      IOException
    • 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
    • isSecure

      public boolean isSecure()
      Description copied from interface: ManagedClientConnection
      Indicates whether this connection is secure. The return value is well-defined only while the connection is open. It may change even while the connection is open.
      Specified by:
      isSecure in interface ManagedClientConnection
      Returns:
      true if this connection is secure, false otherwise
    • getSSLSession

      public SSLSession getSSLSession()
      Description copied from interface: ManagedClientConnection
      Obtains the SSL session of the underlying connection, if any. If this connection is open, and the underlying socket is an SSLSocket, the SSL session of that socket is obtained. This is a potentially blocking operation.
      Note: Whether the underlying socket is an SSL socket can not necessarily be determined via ManagedClientConnection.isSecure(). Plain sockets may be considered secure, for example if they are connected to a known host in the same network segment. On the other hand, SSL sockets may be considered insecure, for example depending on the chosen cipher suite.
      Specified by:
      getSSLSession in interface ManagedClientConnection
      Returns:
      the underlying SSL session if available, null otherwise
    • markReusable

      public void markReusable()
      Description copied from interface: ManagedClientConnection
      Marks this connection as being in a reusable communication state. The checkpoints for reuseable communication states (in the absence of pipelining) are before sending a request and after receiving the response in it's entirety. The connection will automatically clear the checkpoint when used for communication. A call to this method indicates that the next checkpoint has been reached.
      A reusable communication state is necessary but not sufficient for the connection to be reused. A route mismatch, the connection being closed, or other circumstances might prevent reuse.
      Specified by:
      markReusable in interface ManagedClientConnection
    • unmarkReusable

      public void unmarkReusable()
      Description copied from interface: ManagedClientConnection
      Marks this connection as not being in a reusable state. This can be used immediately before releasing this connection to prevent it's reuse. Reasons for preventing reuse include error conditions and the evaluation of a reuse strategy.
      Note: It is not necessary to call here before writing to or reading from this connection. Communication attempts will automatically unmark the state as non-reusable. It can then be switched back using markReusable.
      Specified by:
      unmarkReusable in interface ManagedClientConnection
    • isMarkedReusable

      public boolean isMarkedReusable()
      Description copied from interface: ManagedClientConnection
      Indicates whether this connection is in a reusable communication state. See markReusable and unmarkReusable for details.
      Specified by:
      isMarkedReusable in interface ManagedClientConnection
      Returns:
      true if this connection is marked as being in a reusable communication state, false otherwise
    • setIdleDuration

      public void setIdleDuration​(long duration, TimeUnit unit)
      Description copied from interface: ManagedClientConnection
      Sets the duration that this connection can remain idle before it is reused. The connection should not be used again if this time elapses. The idle duration must be reset after each request sent over this connection. The elapsed time starts counting when the connection is released, which is typically after the headers (and any response body, if present) is fully consumed.
      Specified by:
      setIdleDuration in interface ManagedClientConnection
    • releaseConnection

      public void releaseConnection()
      Description copied from interface: ConnectionReleaseTrigger
      Releases the connection with the option of keep-alive. This is a "graceful" release and may cause IO operations for consuming the remainder of a response entity. Use abortConnection for a hard release. The connection may be reused as specified by the duration.
      Specified by:
      releaseConnection in interface ConnectionReleaseTrigger
    • abortConnection

      public void abortConnection()
      Description copied from interface: ConnectionReleaseTrigger
      Releases the connection without the option of keep-alive. This is a "hard" release that implies a shutdown of the connection. Use releaseConnection for a graceful release.
      Specified by:
      abortConnection in interface ConnectionReleaseTrigger