org.crosswire.common.util
Class WebResource

java.lang.Object
  extended by org.crosswire.common.util.WebResource

public class WebResource
extends Object

A WebResource is backed by an URL and potentially the proxy through which it need go. It can get basic information about the resource and it can get the resource. The requests are subject to a timeout, which can be set via the constructor or previously by a call to set the default timeout. The initial default timeout is 750 milliseconds.

Author:
DM Smith
See Also:
for license details.
The copyright to this program is held by it's authors.

Field Summary
private  org.apache.http.client.HttpClient client
           
private static int timeout
          Define a 750 ms timeout to get a connection
private  URI uri
           
 
Constructor Summary
WebResource(URI theURI)
          Construct a WebResource for the given URL, while timing out if too much time has passed.
WebResource(URI theURI, int theTimeout)
          Construct a WebResource for the given URL, while timing out if too much time has passed.
WebResource(URI theURI, String theProxyHost)
          Construct a WebResource for the given URL, going through the optional proxy and default port, while timing out if too much time has passed.
WebResource(URI theURI, String theProxyHost, int theTimeout)
          Construct a WebResource for the given URL, going through the optional proxy and default port, while timing out if too much time has passed.
WebResource(URI theURI, String theProxyHost, Integer theProxyPort)
          Construct a WebResource for the given URL, going through the optional proxy and port, while timing out if too much time has passed.
WebResource(URI theURI, String theProxyHost, Integer theProxyPort, int theTimeout)
          Construct a WebResource for the given URL, going through the optional proxy and port, while timing out if too much time has passed.
 
Method Summary
 void copy(URI dest)
          Copy this WebResource to the destination.
 void copy(URI dest, Progress meter)
          Copy this WebResource to the destination and report progress.
private  long getHeaderAsDate(org.apache.http.HttpResponse response, String field)
          Get the number of seconds since start of epoch for the field in the response headers as a Date.
private  int getHeaderAsInt(org.apache.http.HttpResponse response, String field)
          Get the field as a long.
 long getLastModified()
          Determine the last modified date of this WebResource.
 int getSize()
          Determine the size of this WebResource.
static int getTimeout()
           
static void setTimeout(int timeout)
           
 void shutdown()
          When this WebResource is no longer needed it should be shutdown to return underlying resources back to the OS.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

timeout

private static int timeout
Define a 750 ms timeout to get a connection


uri

private URI uri

client

private org.apache.http.client.HttpClient client
Constructor Detail

WebResource

public WebResource(URI theURI)
Construct a WebResource for the given URL, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP

WebResource

public WebResource(URI theURI,
                   int theTimeout)
Construct a WebResource for the given URL, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP
theTimeout - the length of time in milliseconds to allow a connection to respond before timing out

WebResource

public WebResource(URI theURI,
                   String theProxyHost)
Construct a WebResource for the given URL, going through the optional proxy and default port, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP
theProxyHost - the proxy host or null

WebResource

public WebResource(URI theURI,
                   String theProxyHost,
                   int theTimeout)
Construct a WebResource for the given URL, going through the optional proxy and default port, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP
theProxyHost - the proxy host or null
theTimeout - the length of time in milliseconds to allow a connection to respond before timing out

WebResource

public WebResource(URI theURI,
                   String theProxyHost,
                   Integer theProxyPort)
Construct a WebResource for the given URL, going through the optional proxy and port, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP
theProxyHost - the proxy host or null
theProxyPort - the proxy port or null, where null means use the standard port

WebResource

public WebResource(URI theURI,
                   String theProxyHost,
                   Integer theProxyPort,
                   int theTimeout)
Construct a WebResource for the given URL, going through the optional proxy and port, while timing out if too much time has passed.

Parameters:
theURI - the Resource to get via HTTP
theProxyHost - the proxy host or null
theProxyPort - the proxy port or null, where null means use the standard port
theTimeout - the length of time in milliseconds to allow a connection to respond before timing out
Method Detail

shutdown

public void shutdown()
When this WebResource is no longer needed it should be shutdown to return underlying resources back to the OS.


getTimeout

public static int getTimeout()
Returns:
the timeout in milliseconds

setTimeout

public static void setTimeout(int timeout)
Parameters:
timeout - the timeout to set in milliseconds

getSize

public int getSize()
Determine the size of this WebResource.

Note that the http client may read the entire file to determine this.

Returns:
the size of the file

getLastModified

public long getLastModified()
Determine the last modified date of this WebResource.

Note that the http client may read the entire file.

Returns:
the last mod date of the file

copy

public void copy(URI dest,
                 Progress meter)
          throws LucidException
Copy this WebResource to the destination and report progress.

Parameters:
dest - the URI of the destination, typically a file:///.
meter - the job on which to report progress
Throws:
LucidException

copy

public void copy(URI dest)
          throws LucidException
Copy this WebResource to the destination.

Parameters:
dest -
Throws:
LucidException

getHeaderAsInt

private int getHeaderAsInt(org.apache.http.HttpResponse response,
                           String field)
Get the field as a long.

Parameters:
response - The response from the request
field - the header field to check
Returns:
the int value for the field

getHeaderAsDate

private long getHeaderAsDate(org.apache.http.HttpResponse response,
                             String field)
Get the number of seconds since start of epoch for the field in the response headers as a Date.

Parameters:
response - The response from the request
field - the header field to check
Returns:
number of seconds since start of epoch

Copyright ? 2003-2011