org.w3c.mwi.mobileok.basic
Class HttpResourceRetriever

java.lang.Object
  extended by org.w3c.mwi.mobileok.basic.HttpResourceRetriever
All Implemented Interfaces:
ResourceRetriever

public class HttpResourceRetriever
extends java.lang.Object
implements ResourceRetriever

Resource retriever that handles the HTTP scheme.

Version:
$Revision: 1.1 $
Author:
Francois Daoust

Field Summary
private static org.apache.commons.httpclient.HttpClient client
          HTTP client used to retrieve HTTP resources.
static java.lang.String CONFIG_HTTP_ACCEPT_CHARSET_HEADER
          Name of the configuration key to use in TesterConfiguration to override the Accept-Charset HTTP header sent to retrieve the resource.
static java.lang.String CONFIG_HTTP_ACCEPT_HEADER
          Name of the configuration key to use in TesterConfiguration to override the Accept HTTP header sent to retrieve the resource.
static java.lang.String CONFIG_HTTP_ADDITIONAL_HEADERS
          Name of the configuration key to use in TesterConfiguration to send additional HTTP header fields in the request.
static java.lang.String CONFIG_HTTP_PROXY_HOST
          Hostname of the proxy to use when sending HTTP requests.
static java.lang.String CONFIG_HTTP_PROXY_PORT
          Port of the proxy to use when sending HTTP requests.
static java.lang.String CONFIG_HTTP_UA_HEADER
          Name of the configuration key to use in TesterConfiguration to override the User-Agent HTTP header sent to retrieve the resource.
private static int MAX_REDIRECTS
          Maximum number of redirects before giving up.
private static java.lang.String MOBILEOK_ACCEPT_CHARSET_HEADER
          mobileOK value of the Accept-Charset HTTP header field.
private static java.lang.String MOBILEOK_ACCEPT_HEADER
          mobileOK value of the Accept HTTP header field.
private static java.lang.String MOBILEOK_UA
          mobileOK value of the User-Agent HTTP header field.
 
Constructor Summary
HttpResourceRetriever()
           
 
Method Summary
static void configureHTTPMethod(org.apache.commons.httpclient.HttpMethod method, java.util.Map<java.lang.String,java.lang.String> additionalRequestHeaders)
          Prepares the HTTP request to send.
static void executeMethod(org.apache.commons.httpclient.HttpMethod method)
          Sends the HTTP request and retrieves the response.
 java.util.List<RetrievalElement> getResource(java.net.URI uri)
          Retrieves the Web resource identified by the URI.
private static boolean isAuthorizationStatus(int status)
          Returns true when the HTTP status code is an "authorization required" status.
private static boolean isRedirectStatus(int status)
          Returns true when the HTTP status code is a "redirection" status.
 boolean isSchemeSupported(java.net.URI uri)
          Returns true when the scheme of the given URI is supported by the resource retriever.
private static boolean manageBasicAuthentification(org.apache.commons.httpclient.HttpMethod method)
          Registers user's credentials that may have been set in TesterConfiguration in the HttpClient instance used to retrieve the resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_REDIRECTS

private static final int MAX_REDIRECTS
Maximum number of redirects before giving up.

See Also:
Constant Field Values

MOBILEOK_UA

private static final java.lang.String MOBILEOK_UA
mobileOK value of the User-Agent HTTP header field.

This value is used by default unless overridden in TesterConfiguration.

See Also:
Constant Field Values

MOBILEOK_ACCEPT_HEADER

private static final java.lang.String MOBILEOK_ACCEPT_HEADER
mobileOK value of the Accept HTTP header field.

This value is used by default unless overridden in TesterConfiguration.

See Also:
Constant Field Values

MOBILEOK_ACCEPT_CHARSET_HEADER

private static final java.lang.String MOBILEOK_ACCEPT_CHARSET_HEADER
mobileOK value of the Accept-Charset HTTP header field.

This value is used by default unless overridden in TesterConfiguration.

See Also:
Constant Field Values

CONFIG_HTTP_UA_HEADER

public static final java.lang.String CONFIG_HTTP_UA_HEADER
Name of the configuration key to use in TesterConfiguration to override the User-Agent HTTP header sent to retrieve the resource.

See Also:
Constant Field Values

CONFIG_HTTP_ACCEPT_HEADER

public static final java.lang.String CONFIG_HTTP_ACCEPT_HEADER
Name of the configuration key to use in TesterConfiguration to override the Accept HTTP header sent to retrieve the resource.

See Also:
Constant Field Values

CONFIG_HTTP_ACCEPT_CHARSET_HEADER

public static final java.lang.String CONFIG_HTTP_ACCEPT_CHARSET_HEADER
Name of the configuration key to use in TesterConfiguration to override the Accept-Charset HTTP header sent to retrieve the resource.

See Also:
Constant Field Values

CONFIG_HTTP_ADDITIONAL_HEADERS

public static final java.lang.String CONFIG_HTTP_ADDITIONAL_HEADERS
Name of the configuration key to use in TesterConfiguration to send additional HTTP header fields in the request.

See Also:
Constant Field Values

CONFIG_HTTP_PROXY_HOST

public static final java.lang.String CONFIG_HTTP_PROXY_HOST
Hostname of the proxy to use when sending HTTP requests.

See Also:
Constant Field Values

CONFIG_HTTP_PROXY_PORT

public static final java.lang.String CONFIG_HTTP_PROXY_PORT
Port of the proxy to use when sending HTTP requests.

See Also:
Constant Field Values

client

private static final org.apache.commons.httpclient.HttpClient client
HTTP client used to retrieve HTTP resources.

Constructor Detail

HttpResourceRetriever

public HttpResourceRetriever()
Method Detail

getResource

public java.util.List<RetrievalElement> getResource(java.net.URI uri)
Description copied from interface: ResourceRetriever
Retrieves the Web resource identified by the URI.

The exact class of the returned RetrievalElement elements may depend on the scheme and Content-Type of the resource being retrieved.

Specified by:
getResource in interface ResourceRetriever
Parameters:
uri - Absolute URI of the Web resource to retrieve
Returns:
The list of client/server exchanges that were needed to actually retrieve the resource, null when the scheme of the URI is not supported.

isSchemeSupported

public boolean isSchemeSupported(java.net.URI uri)
Description copied from interface: ResourceRetriever
Returns true when the scheme of the given URI is supported by the resource retriever.

Specified by:
isSchemeSupported in interface ResourceRetriever
Parameters:
uri - Absolute URI of the Web resource to check
Returns:
true when the scheme is supported, false otherwise.

configureHTTPMethod

public static void configureHTTPMethod(org.apache.commons.httpclient.HttpMethod method,
                                       java.util.Map<java.lang.String,java.lang.String> additionalRequestHeaders)
Prepares the HTTP request to send.

Note the function needs to access the tester configuration per-thread singleton. Since retrieval is typically done using multithreading, make sure the singleton is set in the thread that calls this function!

Parameters:
method - HTTP exchange to complete
additionalRequestHeaders - potential additional HTTP headers to set in the request. Cannot be null.

manageBasicAuthentification

private static boolean manageBasicAuthentification(org.apache.commons.httpclient.HttpMethod method)
                                            throws TestException
Registers user's credentials that may have been set in TesterConfiguration in the HttpClient instance used to retrieve the resource.

Note the function needs to access the tester configuration per-thread singleton. Since retrieval is typically done using multithreading, make sure the singleton is set in the thread that calls this function!

Parameters:
method - the HTTP exchange that requires authentication.
Returns:
true when credentials could be set, false otherwise.
Throws:
TestException

isRedirectStatus

private static boolean isRedirectStatus(int status)
Returns true when the HTTP status code is a "redirection" status.

Parameters:
status - HTTP response status code to parse.
Returns:
true when the status code asks for a redirect, false otherwise.

isAuthorizationStatus

private static boolean isAuthorizationStatus(int status)
Returns true when the HTTP status code is an "authorization required" status.

Parameters:
status - HTTP response status code to parse.
Returns:
true when the status code asks for authentication parameters, false otherwise.

executeMethod

public static void executeMethod(org.apache.commons.httpclient.HttpMethod method)
                          throws java.io.IOException
Sends the HTTP request and retrieves the response.

Note the function needs to access the tester configuration per-thread singleton. Since retrieval is typically done using multithreading, make sure the singleton is set in the thread that calls this function!

Parameters:
method - prepared HTTP exchange.
Throws:
java.io.IOException - a network error occurred.