no.sesat.search.http
Class HTTPClient

java.lang.Object
  extended by no.sesat.search.http.HTTPClient

public final class HTTPClient
extends Object

Utility class to fetch URLs and return them as either BufferedReaders or XML documents. Keeps statistics on connection times and failures. XXX redesign into multiple classes with less static methods.

Supports protocols http, https, ftp, jar, and file. If no protocol is specified in the host it defaults to http. Provides support for URL Jars loaded with request properties as Sun's JVM does not. http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6270774

Version:
$Id: HTTPClient.java 7225 2009-04-09 00:32:20Z ssmiweve $

Method Summary
static void addConnectionStatistic(URLConnection conn, long time)
           
 boolean exists(String path)
           
 BufferedReader getBufferedReader(String path)
           
 BufferedReader getBufferedReader(String path, String encoding)
           
 BufferedInputStream getBufferedStream(String path)
           
 long getLastModified(String path)
           
 Document getXmlDocument(String path)
           
static HTTPClient instance(String host, int port)
          Returns client for specified host and port for HTTP protocol.
static HTTPClient instance(String host, int port, String physicalHost)
          Returns client for specified host, port and physical host (if the host is virtual).
static HTTPClient instance(URL url)
          Returns client instance for the specified URL.
static HTTPClient instance(URL url, String physicalHost)
          Returns client instance for the specified URL and physical host.
 IOException interceptIOException(IOException ioe)
           
static IOException interceptIOException(URLConnection conn, IOException ioe)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

instance

public static HTTPClient instance(String host,
                                  int port)
Returns client for specified host and port for HTTP protocol.

Parameters:
host - The host to use. If no protocol is given then http is assumed.
port - The port to use.
Returns:
a client.

instance

public static HTTPClient instance(String host,
                                  int port,
                                  String physicalHost)
Returns client for specified host, port and physical host (if the host is virtual). Useful if you need to use a virtual host different from the physical host. Defaults to the http protocol if the host argument doesn't specify it.

Parameters:
host - the physical host to use.
port - the port to use.
physicalHost - virtual host to use.
Returns:
a client.

instance

public static HTTPClient instance(URL url)
Returns client instance for the specified URL. The URL can either be complete or just contain the host. Note that only the host and port and used since the url must be supplied again against the HTTPClient instance. The path can be supplied later when using the querying methods like getBufferedStream(String path).

Parameters:
url - The URL.
Returns:
a client.

instance

public static HTTPClient instance(URL url,
                                  String physicalHost)
Returns client instance for the specified URL and physical host. Use this if the virtual host is different from the physcical host. The original host in the URL will be replaced by the supplied physical host and and the original host will instead be used as a host header.

Parameters:
url - The url.
physicalHost - The physical host.
Returns:
a client.

getXmlDocument

public Document getXmlDocument(String path)
                        throws IOException,
                               SAXException
Parameters:
path -
Returns:
Throws:
IOException
SAXException

getBufferedStream

public BufferedInputStream getBufferedStream(String path)
                                      throws IOException
Parameters:
path -
Returns:
Throws:
IOException

getBufferedReader

public BufferedReader getBufferedReader(String path)
                                 throws IOException
Parameters:
path -
Returns:
Throws:
IOException

getBufferedReader

public BufferedReader getBufferedReader(String path,
                                        String encoding)
                                 throws IOException
Parameters:
path -
encoding -
Returns:
Throws:
IOException

getLastModified

public long getLastModified(String path)
                     throws IOException
Parameters:
path -
Returns:
Throws:
IOException

exists

public boolean exists(String path)
               throws IOException
Parameters:
path -
Returns:
Throws:
IOException

interceptIOException

public IOException interceptIOException(IOException ioe)
Parameters:
ioe -
Returns:

interceptIOException

public static IOException interceptIOException(URLConnection conn,
                                               IOException ioe)
Parameters:
conn -
ioe -
Returns:

addConnectionStatistic

public static void addConnectionStatistic(URLConnection conn,
                                          long time)
Parameters:
conn -
time -


Copyright © 2005-2009 Schibsted ASA. All Rights Reserved.