com.sun.jini.jeri.internal.http
Class HttpClientConnection

java.lang.Object
  extended by com.sun.jini.jeri.internal.http.HttpClientConnection
All Implemented Interfaces:
TimedConnection
Direct Known Subclasses:
HttpEndpoint.Connection, HttpsEndpoint.HttpClient

public class HttpClientConnection
extends Object
implements TimedConnection

Class representing a client-side HTTP connection used to send HTTP requests.

Author:
Sun Microsystems, Inc.

Nested Class Summary
private  class HttpClientConnection.OutboundRequestImpl
          HTTP-based implementation of OutboundRequest abstraction.
 
Field Summary
private  String[] acks
           
private static int BUSY
           
private static String clientString
           
private static int CLOSED
           
private static int DIRECT
           
private static int HTTP_MAJOR
           
private static int HTTP_MINOR
           
private static int IDLE
           
private  InputStream in
           
private  HttpClientManager manager
           
private  int mode
           
private  OutputStream out
           
private  boolean persist
           
private static int PROXIED
           
private  ServerInfo proxyInfo
           
private  Socket sock
           
private  int state
           
private  Object stateLock
           
private  ServerInfo targetInfo
           
private static int TUNNELED
           
 
Constructor Summary
HttpClientConnection(String host, int port, HttpClientSocketFactory factory, HttpClientManager manager)
          Creates HttpClientConnection which sends requests directly to given host/port through a socket obtained from the given socket factory.
HttpClientConnection(String targetHost, int targetPort, String proxyHost, int proxyPort, boolean tunnel, boolean persist, HttpClientSocketFactory factory, HttpClientManager manager)
          Creates HttpClientConnection which sends requests to given target host/port through the indicated HTTP proxy over a socket provided by the specified socket factory.
 
Method Summary
private  void analyzePostResponse(StartLine inLine, Header inHeader)
          Analyzes POST response message start line and header, updating cached target/proxy server information if necessary.
private  void analyzeProxyResponse(StartLine inLine, Header inHeader)
          Analyzes CONNECT or OPTIONS response message start line and header sent by proxy, updating proxy server information if necessary.
private  void connect(HttpClientSocketFactory factory)
          Opens underlying connection.
private  Header createBaseHeader()
          Creates base header containing fields common to all HTTP messages sent by this connection.
private  Header createConnectHeader()
          Creates header for use in CONNECT messages sent to proxies.
private  Header createOptionsHeader()
          Creates header for use in OPTIONS messages sent to proxies.
private  Header createPostHeader(StartLine sline)
          Creates header for outbound HTTP POST message with given start line.
private  StartLine createPostLine()
          Creates start line for outbound HTTP POST message.
private  void disconnect()
          Closes and releases reference to underlying socket.
private  void fetchServerInfo()
          Fetches latest server/proxy HTTP information from cache.
private  void flushServerInfo()
          Flushes current copy of server/proxy HTTP information to cache.
 Socket getSocket()
          Returns socket over which requests are sent.
protected  void idle()
          Upcall indicating that connection has become idle.
private  void markBusy()
          Marks connection busy.
private  void markIdle()
          Marks connection idle.
 OutboundRequest newRequest()
          Initiates new request to connection target.
 boolean ping()
          Pings target.
private  boolean ping(boolean setup)
          Pings target.
private  boolean requestProxyConnect()
          Sends CONNECT request to proxy.
private  boolean requestProxyOptions()
          Sends OPTIONS request to proxy.
private  void setupConnection(HttpClientSocketFactory factory)
          Establishes connection using sockets from the given socket factory.
 boolean shutdown(boolean force)
          Attempts to shut down connection, returning true if connection is closed.
private  boolean supportsChunking()
          Returns true if requests sent over this connection should chunk output.
private  boolean supportsPersist(StartLine sline, Header header)
          Returns true if the given response line and header indicate that the connection can be persisted, and use of persistent connections has not been disabled.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_MAJOR

private static final int HTTP_MAJOR
See Also:
Constant Field Values

HTTP_MINOR

private static final int HTTP_MINOR
See Also:
Constant Field Values

clientString

private static final String clientString

DIRECT

private static final int DIRECT
See Also:
Constant Field Values

PROXIED

private static final int PROXIED
See Also:
Constant Field Values

TUNNELED

private static final int TUNNELED
See Also:
Constant Field Values

IDLE

private static final int IDLE
See Also:
Constant Field Values

BUSY

private static final int BUSY
See Also:
Constant Field Values

CLOSED

private static final int CLOSED
See Also:
Constant Field Values

mode

private final int mode

stateLock

private final Object stateLock

state

private int state

manager

private final HttpClientManager manager

targetInfo

private ServerInfo targetInfo

proxyInfo

private ServerInfo proxyInfo

persist

private final boolean persist

acks

private String[] acks

sock

private Socket sock

out

private OutputStream out

in

private InputStream in
Constructor Detail

HttpClientConnection

public HttpClientConnection(String host,
                            int port,
                            HttpClientSocketFactory factory,
                            HttpClientManager manager)
                     throws IOException
Creates HttpClientConnection which sends requests directly to given host/port through a socket obtained from the given socket factory. The createSocket method of the given socket factory may be called more than once in cases where connection establishment involves multiple HTTP message exchanges.

Throws:
IOException

HttpClientConnection

public HttpClientConnection(String targetHost,
                            int targetPort,
                            String proxyHost,
                            int proxyPort,
                            boolean tunnel,
                            boolean persist,
                            HttpClientSocketFactory factory,
                            HttpClientManager manager)
                     throws IOException
Creates HttpClientConnection which sends requests to given target host/port through the indicated HTTP proxy over a socket provided by the specified socket factory. If tunnel is true, requests are tunneled through the proxy over an additional layered socket (also provided by the socket factory). If persist and tunnel are both false, the connection can only be used for a single request. If persist is true and tunnel is false, a persistent connection is maintained if possible. The createSocket and createTunnelSocket methods of the given socket factory may be called more than once in cases where connection establishment involves multiple HTTP message exchanges.

Throws:
IOException
Method Detail

ping

public boolean ping()
             throws IOException
Pings target. Returns true if ping succeeded, false if it fails "cleanly" (i.e., if a valid HTTP response indicating request failure is received).

Throws:
IOException

newRequest

public OutboundRequest newRequest()
                           throws IOException
Initiates new request to connection target. Throws an IOException if the connection is currently busy.

Throws:
IOException

idle

protected void idle()
Upcall indicating that connection has become idle. Subclasses may override this method to perform an appropriate action, such as scheduling an idle timeout.


getSocket

public Socket getSocket()
Returns socket over which requests are sent.


shutdown

public boolean shutdown(boolean force)
Attempts to shut down connection, returning true if connection is closed. If force is true, connection is always shut down; if force is false, connection is only shut down if idle.

Specified by:
shutdown in interface TimedConnection

fetchServerInfo

private void fetchServerInfo()
Fetches latest server/proxy HTTP information from cache.


flushServerInfo

private void flushServerInfo()
Flushes current copy of server/proxy HTTP information to cache.


markBusy

private void markBusy()
               throws IOException
Marks connection busy. Throws IOException if connection closed.

Throws:
IOException

markIdle

private void markIdle()
Marks connection idle. Does nothing if connection closed.


setupConnection

private void setupConnection(HttpClientSocketFactory factory)
                      throws IOException
Establishes connection using sockets from the given socket factory. Throws IOException if connection setup fails.

Throws:
IOException

connect

private void connect(HttpClientSocketFactory factory)
              throws IOException
Opens underlying connection. If tunneling through an HTTP proxy, attempts CONNECT request.

Throws:
IOException

disconnect

private void disconnect()
Closes and releases reference to underlying socket.


ping

private boolean ping(boolean setup)
              throws IOException
Pings target. Returns true if succeeded, false if failed "cleanly".

Throws:
IOException

requestProxyOptions

private boolean requestProxyOptions()
                             throws IOException
Sends OPTIONS request to proxy. Returns true if OPTIONS succeeded, false otherwise.

Throws:
IOException

requestProxyConnect

private boolean requestProxyConnect()
                             throws IOException
Sends CONNECT request to proxy. Returns true if CONNECT succeeded, false otherwise.

Throws:
IOException

createPostLine

private StartLine createPostLine()
Creates start line for outbound HTTP POST message.


createBaseHeader

private Header createBaseHeader()
Creates base header containing fields common to all HTTP messages sent by this connection.


createConnectHeader

private Header createConnectHeader()
Creates header for use in CONNECT messages sent to proxies.


createOptionsHeader

private Header createOptionsHeader()
Creates header for use in OPTIONS messages sent to proxies.


createPostHeader

private Header createPostHeader(StartLine sline)
Creates header for outbound HTTP POST message with given start line.


analyzePostResponse

private void analyzePostResponse(StartLine inLine,
                                 Header inHeader)
Analyzes POST response message start line and header, updating cached target/proxy server information if necessary.


analyzeProxyResponse

private void analyzeProxyResponse(StartLine inLine,
                                  Header inHeader)
Analyzes CONNECT or OPTIONS response message start line and header sent by proxy, updating proxy server information if necessary.


supportsChunking

private boolean supportsChunking()
Returns true if requests sent over this connection should chunk output.


supportsPersist

private boolean supportsPersist(StartLine sline,
                                Header header)
Returns true if the given response line and header indicate that the connection can be persisted, and use of persistent connections has not been disabled.



Copyright 2007-2010, multiple authors.
Licensed under the Apache License, Version 2.0, see the NOTICE file for attributions.