org.w3c.www.protocol.http
Class HttpManager

java.lang.Object
  extended by org.w3c.www.protocol.http.HttpManager
All Implemented Interfaces:
PropertyMonitoring
Direct Known Subclasses:
DAVManager

public class HttpManager
extends java.lang.Object
implements PropertyMonitoring

The client side HTTP request manager. This class is the user interface (along with the other public classes of this package) for the W3C client side library implementing HTTP. A typical request is launched though the following sequence:

 HttpManager     manager = HttpManager.getManager() ;
 Request request = manager.createRequest() ;
 request.setMethod(HTTP.GET) ;
 request.setURL(new URL("http://www.w3.org/pub/WWW/"));
 Reply    reply = manager.runRequest(request) ;
 // Get the reply input stream that contains the actual data:
 InputStream in = reply.getInputStream() ;
 ...
 


Field Summary
protected  java.util.Hashtable _tmp_servers
           
static java.lang.String ACCEPT_ENCODING_P
          Header properties - Set the accept encodings.
static java.lang.String ACCEPT_LANGUAGE_P
          Header properties - Set the accept language.
static java.lang.String ACCEPT_P
          Header properties - Set the accept header.
protected  int conn_count
           
protected  int conn_max
           
static java.lang.String CONN_MAX_P
          The maximum number of simultaneous connectionlrus.
protected  int conn_timeout
           
static java.lang.String CONN_TIMEOUT_P
          The connection timeout of the client socket.
protected  LRUList connectionsLru
          The LRU list of connections.
static java.lang.String DEFAULT_ACCEPT
          The default value for the Accept header.
static java.lang.String DEFAULT_USER_AGENT
          The default value for the User-Agent header.
static java.lang.String FILTERS_PROP_P
          The name of the property containing the ProprequestFilter to launch.
protected  boolean keepbody
           
static java.lang.String KEEPBODY_P
          Header properties - should we reuse a connection for POST?
protected  boolean lenient
           
static java.lang.String LENIENT_P
          Header properties - are we parsing answers in a lenient way?
static java.lang.String MAX_STALE_P
          Header properties - The allowed drift for getting cached resources.
static java.lang.String MIN_FRESH_P
          Header properties - The minium freshness required on cached resources.
static java.lang.String ONLY_IF_CACHED_P
          Header properties - Set the only if cached flag on requests.
static java.lang.String PROXY_HOST_P
          Header properties - What is the proxy host name.
static java.lang.String PROXY_PORT_P
          Header properties - What is the proxy port number.
static java.lang.String PROXY_SET_P
          Header properties - Should we use a proxy ?
static java.lang.String SERVER_CLASS_P
          The name of the property indicating the class of HttpServer to use.
protected  java.lang.Class serverclass
          The class to instantiate to create new HttpServer instances.
protected  java.util.Hashtable servers
          The server this manager knows about, indexed by FQDN of target servers.
protected  Request template
          The template request (the request we will clone to create new requests)
protected  int timeout
           
static java.lang.String TIMEOUT_P
          The SO_TIMEOUT of the client socket.
static java.lang.String USER_AGENT_P
          Header properties - Set the user agent.
 
Constructor Summary
protected HttpManager()
          Create a new HttpManager.
 
Method Summary
protected  boolean closeAnyConnection()
          Close some connections, but pickling the least recently used ones.
 Request createRequest()
          Create a new default outgoing request.
protected  void decrConnCount(HttpServer server)
          Decrement the number of established connections.
protected  void deleteConnection(org.w3c.www.protocol.http.HttpConnection conn)
          The given connection has been deleted.
protected  org.w3c.www.protocol.http.HttpConnection getConnection(HttpServer server)
          Try reusing one of the idle connection of that server, if any.
 RequestFilter getGlobalFilter(java.lang.Class cls)
          Find back an instance of a global filter.
 java.lang.String getGlobalHeader(java.lang.String name)
          Global settings - Get a global request header default value.
static HttpManager getManager()
           
protected static HttpManager getManager(java.lang.Class managerclass, java.util.Properties p)
           
static HttpManager getManager(java.util.Properties p)
          Get an instance of the HTTP manager.
 ObservableProperties getProperties()
          Get this manager properties.
 MimeParserFactory getReplyFactory()
           
 java.lang.String getServerKey(Request request)
          Get the String key for the server instance handling that request.
protected  void incrConnCount(HttpServer server)
          A new client connection has been established.
 boolean isLenient()
          Is this manager parsing headers in a lenient way?
protected  HttpServer lookupServer(java.lang.String host, int port)
          Get the appropriate server object for handling request to given target.
static void main(java.lang.String[] args)
           
protected  boolean negotiateConnection(HttpServer server)
          One of our server handler wants to open a connection.
protected  void notifyConnection(org.w3c.www.protocol.http.HttpConnection conn)
          The given connection has just been created.
 void notifyIdle(org.w3c.www.protocol.http.HttpConnection conn)
          The given connection can be reused, but is now idle.
 void notifyUse(org.w3c.www.protocol.http.HttpConnection conn)
          The given connection is about to be used.
 boolean propertyChanged(java.lang.String name)
          PropertyMonitoring implementation - Update properties on the fly !
 Reply runRequest(Request request)
          Run the given request, in synchronous mode.
 void setAllowUserInteraction(boolean onoff)
          Allow the manager to interact with the user if needed.
 void setConnTimeout(int conn_timeout)
          Global settings - Set the connection timeout for the socket
 void setFilter(RequestFilter filter)
          Add a global filter.
 void setFilter(java.net.URL[] incs, java.net.URL[] exs, RequestFilter filter)
          Add a new request filter.
 void setGlobalHeader(java.lang.String name, java.lang.String value)
          Global settings - Define a global request header.
 void setLenient(boolean lenient)
          Global settings - set the HTTP parsing lenient or not.
 void setMaxConnections(int max_conn)
          Global settings - Set the max number of allowed connections.
 void setProxy(java.net.URL proxy)
          Global settings - Set an optional proxy to use.
 void setRequestTimeout(int ms)
          Global settings - Set the request timeout.
 void setTimeout(int timeout)
          Global settings - Set the timeout on the socket
 void sync()
          Dump all in-memory cached state to persistent storage.
protected  boolean tooManyConnections()
           
 java.lang.String toString()
          DEBUGGING !
protected  boolean updateProxy()
          Update the proxy configuration to match current properties setting.
 boolean usingProxy()
          Does this manager uses a proxy to fulfill requests ?
protected  void waitForConnection(HttpServer server)
          Wait for a connection to come up.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SERVER_CLASS_P

public static final java.lang.String SERVER_CLASS_P
The name of the property indicating the class of HttpServer to use.

See Also:
Constant Field Values

FILTERS_PROP_P

public static final java.lang.String FILTERS_PROP_P
The name of the property containing the ProprequestFilter to launch.

See Also:
Constant Field Values

CONN_MAX_P

public static final java.lang.String CONN_MAX_P
The maximum number of simultaneous connectionlrus.

See Also:
Constant Field Values

TIMEOUT_P

public static final java.lang.String TIMEOUT_P
The SO_TIMEOUT of the client socket.

See Also:
Constant Field Values

CONN_TIMEOUT_P

public static final java.lang.String CONN_TIMEOUT_P
The connection timeout of the client socket.

See Also:
Constant Field Values

MAX_STALE_P

public static final java.lang.String MAX_STALE_P
Header properties - The allowed drift for getting cached resources.

See Also:
Constant Field Values

MIN_FRESH_P

public static final java.lang.String MIN_FRESH_P
Header properties - The minium freshness required on cached resources.

See Also:
Constant Field Values

ONLY_IF_CACHED_P

public static final java.lang.String ONLY_IF_CACHED_P
Header properties - Set the only if cached flag on requests.

See Also:
Constant Field Values

USER_AGENT_P

public static final java.lang.String USER_AGENT_P
Header properties - Set the user agent.

See Also:
Constant Field Values

ACCEPT_P

public static final java.lang.String ACCEPT_P
Header properties - Set the accept header.

See Also:
Constant Field Values

ACCEPT_LANGUAGE_P

public static final java.lang.String ACCEPT_LANGUAGE_P
Header properties - Set the accept language.

See Also:
Constant Field Values

ACCEPT_ENCODING_P

public static final java.lang.String ACCEPT_ENCODING_P
Header properties - Set the accept encodings.

See Also:
Constant Field Values

LENIENT_P

public static final java.lang.String LENIENT_P
Header properties - are we parsing answers in a lenient way?

See Also:
Constant Field Values

KEEPBODY_P

public static final java.lang.String KEEPBODY_P
Header properties - should we reuse a connection for POST?

See Also:
Constant Field Values

PROXY_SET_P

public static final java.lang.String PROXY_SET_P
Header properties - Should we use a proxy ?

See Also:
Constant Field Values

PROXY_HOST_P

public static final java.lang.String PROXY_HOST_P
Header properties - What is the proxy host name.

See Also:
Constant Field Values

PROXY_PORT_P

public static final java.lang.String PROXY_PORT_P
Header properties - What is the proxy port number.

See Also:
Constant Field Values

DEFAULT_ACCEPT

public static final java.lang.String DEFAULT_ACCEPT
The default value for the Accept header.

See Also:
Constant Field Values

DEFAULT_USER_AGENT

public static final java.lang.String DEFAULT_USER_AGENT
The default value for the User-Agent header.

See Also:
Constant Field Values

serverclass

protected java.lang.Class serverclass
The class to instantiate to create new HttpServer instances.


servers

protected java.util.Hashtable servers
The server this manager knows about, indexed by FQDN of target servers.


template

protected Request template
The template request (the request we will clone to create new requests)


connectionsLru

protected LRUList connectionsLru
The LRU list of connections.


timeout

protected int timeout

conn_timeout

protected int conn_timeout

conn_count

protected int conn_count

conn_max

protected int conn_max

lenient

protected boolean lenient

keepbody

protected boolean keepbody

_tmp_servers

protected java.util.Hashtable _tmp_servers
Constructor Detail

HttpManager

protected HttpManager()
Create a new HttpManager. FIXME Making this method protected breaks the static method to create HttpManager instances (should use a factory here)

Parameters:
props - The properties from which the manager should initialize itself, or null if none are available.
Method Detail

updateProxy

protected boolean updateProxy()
Update the proxy configuration to match current properties setting.

Returns:
A boolean, true if change was done, false otherwise.

getProperties

public final ObservableProperties getProperties()
Get this manager properties.

Returns:
An ObservableProperties instance.

propertyChanged

public boolean propertyChanged(java.lang.String name)
PropertyMonitoring implementation - Update properties on the fly !

Specified by:
propertyChanged in interface PropertyMonitoring
Parameters:
name - The name of the property that has changed.
Returns:
A boolean, true if change is accepted, false otherwise.

setAllowUserInteraction

public void setAllowUserInteraction(boolean onoff)
Allow the manager to interact with the user if needed. This will, for example, allow prompting for paswords, etc.

Parameters:
onoff - Turn interaction on or off.

getManager

protected static HttpManager getManager(java.lang.Class managerclass,
                                        java.util.Properties p)

getManager

public static HttpManager getManager(java.util.Properties p)
Get an instance of the HTTP manager. This method returns an actual instance of the HTTP manager. It may return different managers, if it decides to distribute the load on different managers (avoid the HttpManager being a bottleneck).

Returns:
An application wide instance of the HTTP manager.

getManager

public static HttpManager getManager()

getServerKey

public final java.lang.String getServerKey(Request request)
Get the String key for the server instance handling that request. This method takes care of any proxy setting (it will return the key to the proxy when required.)

Returns:
A uniq identifier for the handling server, as a String.

lookupServer

protected HttpServer lookupServer(java.lang.String host,
                                  int port)
                           throws HttpException
Get the appropriate server object for handling request to given target.

Parameters:
key - The server's key, as returned by getServerKey.
Returns:
An object complying to the HttpServer interface.
Throws:
HttpException - If the given host name couldn't be resolved.

notifyUse

public void notifyUse(org.w3c.www.protocol.http.HttpConnection conn)
The given connection is about to be used. Update our list of available servers.

Parameters:
conn - The idle connection.

notifyIdle

public void notifyIdle(org.w3c.www.protocol.http.HttpConnection conn)
The given connection can be reused, but is now idle.

Parameters:
conn - The connection that is now idle.

notifyConnection

protected void notifyConnection(org.w3c.www.protocol.http.HttpConnection conn)
The given connection has just been created.

Parameters:
conn - The newly created connection.

deleteConnection

protected void deleteConnection(org.w3c.www.protocol.http.HttpConnection conn)
The given connection has been deleted.

Parameters:
conn - The deleted connection.

tooManyConnections

protected boolean tooManyConnections()

getConnection

protected org.w3c.www.protocol.http.HttpConnection getConnection(HttpServer server)
Try reusing one of the idle connection of that server, if any.

Parameters:
server - The target server.
Returns:
An currently idle connection to the given server.

waitForConnection

protected void waitForConnection(HttpServer server)
                          throws java.lang.InterruptedException
Wait for a connection to come up.

Parameters:
server, - the target server.
Throws:
java.lang.InterruptedException - If interrupted..

closeAnyConnection

protected boolean closeAnyConnection()
Close some connections, but pickling the least recently used ones. One third of the max number of connection is cut. This is done to eliminate the old connections that should be broken already. (no Socket.isAlive());

Returns:
A boolean, true if a connection was closed false otherwise.

negotiateConnection

protected boolean negotiateConnection(HttpServer server)
One of our server handler wants to open a connection.

Parameters:
block - A boolean indicating whether we should block the calling thread until a token is available (otherwise, the method will just peek at the connection count, and return the appropriate result).
Returns:
A boolean, true if the connection can be opened straight, false otherwise.

incrConnCount

protected final void incrConnCount(HttpServer server)
A new client connection has been established. This method will try to maintain a maximum number of established connections, by closing idle connections when possible.

Parameters:
server - The server that has established a new connection.

decrConnCount

protected final void decrConnCount(HttpServer server)
Decrement the number of established connections.

Parameters:
server - The server that has closed one connection to its target.

runRequest

public Reply runRequest(Request request)
                 throws HttpException
Run the given request, in synchronous mode. This method will launch the given request, and block the calling thread until the response headers are available.

Parameters:
request - The request to run.
Returns:
An instance of Reply, containing all the reply informations.
Throws:
HttpException - If something failed during request processing.

getReplyFactory

public MimeParserFactory getReplyFactory()

setFilter

public void setFilter(java.net.URL[] incs,
                      java.net.URL[] exs,
                      RequestFilter filter)
Add a new request filter. Request filters are called before a request is launched, and after the reply headers are available. They allow applications to setup specific request headers (such as PICS, or PEP stuff) on the way in, and check the reply on the way out.

Request filters are application wide: if their scope matches the current request, then they will always be aplied.

Filter scopes are defined inclusively and exclusively

Parameters:
incs - The URL domains for which the filter should be triggered.
exs - The URL domains for which the filter should not be triggered.
filter - The request filter to add.

setFilter

public void setFilter(RequestFilter filter)
Add a global filter. The given filter will always be invoked.

Parameters:
filter - The filter to install.

getGlobalFilter

public RequestFilter getGlobalFilter(java.lang.Class cls)
Find back an instance of a global filter. This methods allow external classes to get a pointer to installed filters of a given class.

Parameters:
cls - The class of the filter to look for.
Returns:
A RequestFilter instance, or null if not found.

createRequest

public Request createRequest()
Create a new default outgoing request. This method should always be used to create outgoing requests. It will initialize the request with appropriate default values for the various headers, and make sure that the request is enhanced by the registered request filters.

Returns:
An instance of Request, suitable to be launched.

setMaxConnections

public void setMaxConnections(int max_conn)
Global settings - Set the max number of allowed connections. Set the maximum number of simultaneous connections that can remain opened. The manager will take care of queuing requests if this number is reached.

This value defaults to the value of the org.w3c.www.http.connections.max property.

Parameters:
max_conn - The allowed maximum simultaneous open connections.

setTimeout

public void setTimeout(int timeout)
Global settings - Set the timeout on the socket

This value defaults to the value of the org.w3c.www.http.connections.timeout property.

Parameters:
timeout - The allowed maximum microsecond before a timeout.

setConnTimeout

public void setConnTimeout(int conn_timeout)
Global settings - Set the connection timeout for the socket

This value defaults to the value of the org.w3c.www.protocol.http.connections.connTimeout property

Parameters:
timeout - The allowed maximum microsecond before a timeout.

setLenient

public void setLenient(boolean lenient)
Global settings - set the HTTP parsing lenient or not.

Parameters:
lenient, - true by default, false to detect wrong servers

isLenient

public boolean isLenient()
Is this manager parsing headers in a lenient way?

Returns:
A boolean.

setProxy

public void setProxy(java.net.URL proxy)
Global settings - Set an optional proxy to use. Set the proxy to which all requests should be targeted. If the org.w3c.www.http.proxy property is defined, it will be used as the default value.

Parameters:
proxy - The URL for the proxy to use.

usingProxy

public boolean usingProxy()
Does this manager uses a proxy to fulfill requests ?

Returns:
A boolean.

setRequestTimeout

public void setRequestTimeout(int ms)
Global settings - Set the request timeout. Once a request has been emited, the HttpManager will sit for this given number of milliseconds before the request is declared to have timed-out.

This timeout value defaults to the value of the org.w3c.www.http.requestTimeout property value.

Parameters:
ms - The timeout value in milliseconds.

setGlobalHeader

public void setGlobalHeader(java.lang.String name,
                            java.lang.String value)
Global settings - Define a global request header. Set a default value for some request header. Once defined, the header will automatically be defined on all outgoing requests created through the createRequest request.

Parameters:
name - The name of the header, case insensitive.
value - It's default value.

getGlobalHeader

public java.lang.String getGlobalHeader(java.lang.String name)
Global settings - Get a global request header default value.

Parameters:
name - The name of the header to get.
Returns:
The value for that header, as a String, or null if undefined.

sync

public void sync()
Dump all in-memory cached state to persistent storage.


toString

public java.lang.String toString()
DEBUGGING !

Overrides:
toString in class java.lang.Object

main

public static void main(java.lang.String[] args)