org.apache.shindig.gadgets.http
Class BasicHttpFetcher

java.lang.Object
  extended by org.apache.shindig.gadgets.http.BasicHttpFetcher
All Implemented Interfaces:
HttpFetcher

public class BasicHttpFetcher
extends Object
implements HttpFetcher

A simple HTTP fetcher implementation based on Apache httpclient. Not recommended for production deployments until the following issues are addressed:

1. This class potentially allows access to resources behind an organization's firewall. 2. This class does not enforce any limits on what is fetched from remote hosts.


Field Summary
protected  org.apache.http.client.HttpClient FETCHER
           
 
Constructor Summary
BasicHttpFetcher(int maxObjSize, int connectionTimeoutMs, int readTimeoutMs, String basicHttpFetcherProxy)
          Creates a new fetcher for fetching HTTP objects.
BasicHttpFetcher(String basicHttpFetcherProxy)
          Creates a new fetcher using the default maximum object size and timeout -- no limit and 5 seconds.
 
Method Summary
 HttpResponse fetch(HttpRequest request)
          Fetch HTTP content.
 void setConnectionTimeoutMs(int connectionTimeoutMs)
          Change the global connection timeout for all new fetchs.
 void setMaxObjectSizeBytes(int maxObjectSizeBytes)
          Change the global maximum fetch size (in bytes) for all fetches.
 void setReadTimeoutMs(int readTimeoutMs)
          Change the global read timeout for all new fetchs.
 void setSlowResponseWarning(long slowResponseWarning)
          Change the global threshold for warning about slow responses
protected  void slowResponseWarning(HttpRequest request, long started, long finished)
          Called when a request takes too long.
 byte[] toByteArraySafe(org.apache.http.HttpEntity entity)
          This method is Safe replica version of org.apache.http.util.EntityUtils.toByteArray.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FETCHER

protected final org.apache.http.client.HttpClient FETCHER
Constructor Detail

BasicHttpFetcher

@Inject
public BasicHttpFetcher(@Nullable
                               String basicHttpFetcherProxy)
Creates a new fetcher using the default maximum object size and timeout -- no limit and 5 seconds.

Parameters:
basicHttpFetcherProxy - The http proxy to use.

BasicHttpFetcher

public BasicHttpFetcher(int maxObjSize,
                        int connectionTimeoutMs,
                        int readTimeoutMs,
                        String basicHttpFetcherProxy)
Creates a new fetcher for fetching HTTP objects. Not really suitable for production use. Use of an HTTP proxy for security is also necessary for production deployment.

Parameters:
maxObjSize - Maximum size, in bytes, of the object we will fetch, 0 if no limit..
connectionTimeoutMs - timeout, in milliseconds, for connecting to hosts.
readTimeoutMs - timeout, in millseconds, for unresponsive connections
basicHttpFetcherProxy - The http proxy to use.
Method Detail

fetch

public HttpResponse fetch(HttpRequest request)
                   throws GadgetException
Description copied from interface: HttpFetcher
Fetch HTTP content.

Specified by:
fetch in interface HttpFetcher
Parameters:
request - The request to fetch.
Returns:
An HTTP response from the relevant resource, including error conditions.
Throws:
GadgetException - In the event of a failure that can't be mapped to an HTTP result code.

slowResponseWarning

protected void slowResponseWarning(HttpRequest request,
                                   long started,
                                   long finished)
Called when a request takes too long. Consider subclassing this if you want to do something other than logging a warning .

Parameters:
request - the request that generated the slowrequest
started - the time the request started, in milliseconds.
finished - the time the request finished, in milliseconds.

setMaxObjectSizeBytes

@Inject(optional=true)
public void setMaxObjectSizeBytes(int maxObjectSizeBytes)
Change the global maximum fetch size (in bytes) for all fetches.

Parameters:
maxObjectSizeBytes - value for maximum number of bytes, or 0 for no limit

setSlowResponseWarning

@Inject(optional=true)
public void setSlowResponseWarning(long slowResponseWarning)
Change the global threshold for warning about slow responses

Parameters:
slowResponseWarning - time in milliseconds after we issue a warning

setConnectionTimeoutMs

@Inject(optional=true)
public void setConnectionTimeoutMs(int connectionTimeoutMs)
Change the global connection timeout for all new fetchs.

Parameters:
connectionTimeoutMs - new connection timeout in milliseconds

setReadTimeoutMs

@Inject(optional=true)
public void setReadTimeoutMs(int readTimeoutMs)
Change the global read timeout for all new fetchs.

Parameters:
readTimeoutMs - new connection timeout in milliseconds

toByteArraySafe

public byte[] toByteArraySafe(org.apache.http.HttpEntity entity)
                       throws IOException
This method is Safe replica version of org.apache.http.util.EntityUtils.toByteArray. The try block embedding 'instream.read' has a corresponding catch block for 'EOFException' (that's Ignored) and all other IOExceptions are let pass.

Parameters:
entity -
Returns:
byte array containing the entity content. May be empty/null.
Throws:
IOException - if an error occurs reading the input stream


Copyright © 2007-2012. All Rights Reserved.