HttpClient preference architecture
Quality and extent of the
HTTP parametersAs of version 3 HttpClient sports a new preference API based on HttpParams interface. All major components of the HttpClient toolkit (agents, host configurations, methods, connections, connection managers) contain a collection of HTTP parameters, which determine the runtime behavior of those components.
HttpClient httpclient = new HttpClient();
HttpVersion ver = (HttpVersion)httpclient.getParams().getParameter("http.protocol.version");
In a nutshell HTTP parameters is a collection of name/object pairs that can be linked with other collections to form a hierarchy. If a particular parameter value has not been explicitly defined in the collection itself, its value will be drawn from the upper level collection of parameters.
HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
httpclient.getParams().setParameter("http.socket.timeout", new Integer(1000));
httpclient.getParams().setParameter("http.protocol.content-charset", "UTF-8");
HostConfiguration hostconfig = new HostConfiguration();
hostconfig.setHost("www.yahoo.com");
hostconfig.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_0);
GetMethod httpget = new GetMethod("/");
httpget.getParams().setParameter("http.socket.timeout", new Integer(5000));
try {
// Internally the parameter collections will be linked together
// by performing the following operations:
// hostconfig.getParams().setDefaults(httpclient.getParams());
// httpget.getParams().setDefaults(hostconfig.getParams());
httpclient.executeMethod(hostconfig, httpget);
System.out.println(httpget.getParams().getParameter("http.protocol.version"));
System.out.println(httpget.getParams().getParameter("http.socket.timeout"));
System.out.println(httpget.getParams().getParameter("http.protocol.content-charset"));
} finally {
httpget.releaseConnection();
}
The code above will produce the following output: HTTP/1.0 5000 UTF-8 When resolving a parameter HttpClient uses the following algorithm:
This architecture enables the users to define generic parameters at a higher level (for instance, at the agent level or host level) and selectively override specific parameters at a lower level (for instance, at the method level). Whenever a parameter is not explicitly defined at a given level, the defaults of the upper levels will apply. HTTP parameter hierarchyPresently HttpClient provides the following parameter hierarchy:
global--+ | DefaultHttpParams
| |
client | HttpClient
| |
+-- connection manager | HttpConnectionManager
| | |
| +-- connection | HttpConnection
| |
+-- host | HostConfiguration
| |
+-- method | HttpMethod
Supported parametersHTTP method parametersApplicable at the following levels: global -> client -> host -> method
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers. HTTP connection parametersApplicable at the following levels: global -> client -> connection manager -> connection
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers. HTTP connection manager parametersApplicable at the following levels: global -> client -> connection manager
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers. Host configuration parametersApplicable at the following levels: global -> client -> host
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers. HTTP client parametersApplicable at the following levels: global -> client
Whenever a parameter is left undefined (no value is explicitly set anywhere in the parameter hierarchy) HttpClient will use its best judgment to pick up a value. This default behavior is likely to provide the best compatibility with widely used HTTP servers. |