net.noderunner.http
Class RetryHttpClient

java.lang.Object
  extended by net.noderunner.http.RetryHttpClient
All Implemented Interfaces:
HttpClient

public class RetryHttpClient
extends java.lang.Object
implements HttpClient

A HttpClient implementation that retries contacting a remote URL and allows for persistant connections. For each retry, this client waits an additional second before attempting a new connection. Note that if a failure occurs reading the server response, retries may or may not be made only for non-POST and PUT methods. Refer to readResponse on this is handled.

Also, this client handles most redirects, on either the same host or different host. By default, note that redirects will only be followed for non-POST and PUT methods. Refer to setFollowRedirects on this.

HTTPS (HTTP over SSL) relies on the javax.net.ssl library, and may require com.sun.net.ssl.internal.ssl.Provider to be installed using the java.security.Security.addProvider method.

Many of the protected methods may be overriden to supply specific functionality. For example, by default the underlying HttpClient is the BasicHttpClient, this can be easily changed.


Field Summary
static int DEFAULT_MAX_TRIES
          Default number of tries.
 
Constructor Summary
RetryHttpClient(java.net.URL url)
          Constructs a RetryHttpClient that retries 3 times.
RetryHttpClient(java.net.URL url, int maxTries)
          Constructs a RetryHttpClient that retries a number of times.
 
Method Summary
 void close()
          Closes the wrapped HttpClient.
 java.io.OutputStream getOutputStream()
          Returns a stream for writing data to, if data is to be sent to the server.
protected  HttpClient makeHttpClient(java.net.URL url)
          Returns a newly constructed HTTP client for a given URL.
protected  java.net.Socket makeSocket(java.net.URL url)
          Returns a newly constructed socket for a given URL.
protected  java.net.Socket makeSSLSocket(java.net.URL url)
          Returns a newly constructed SSL socket for a given URL.
 ClientResponse readResponse()
          Reads the response data from the HTTP server.
protected  void retrySendRequest(java.io.IOException e, int failures)
          Callback that indicates the connection failed and will be retried.
 void setFollowRedirects(boolean followRedirects)
          Sets whether HTTP redirects should be followed.
 void setSkipContinues(boolean skipContinues)
          Sets whether HTTP continue responses should be skipped.
protected  void setSocketOptions(java.net.Socket socket)
          Sets the socket options to use.
 java.lang.String toString()
          Returns debug information.
 void writeRequest(ClientRequest request)
          Attempts to send an HTTP request, and may retry to send a certain number of times.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MAX_TRIES

public static final int DEFAULT_MAX_TRIES
Default number of tries.

See Also:
Constant Field Values
Constructor Detail

RetryHttpClient

public RetryHttpClient(java.net.URL url,
                       int maxTries)
Constructs a RetryHttpClient that retries a number of times.

Parameters:
maxTries - must be greater than zero

RetryHttpClient

public RetryHttpClient(java.net.URL url)
Constructs a RetryHttpClient that retries 3 times.

Method Detail

retrySendRequest

protected void retrySendRequest(java.io.IOException e,
                                int failures)
Callback that indicates the connection failed and will be retried. The default implementation simply closes the connection.

Parameters:
failures - number of failures (counting from 0)

makeSocket

protected java.net.Socket makeSocket(java.net.URL url)
                              throws java.io.IOException
Returns a newly constructed socket for a given URL. By default, the HTTP port to use will be 80.

Throws:
java.io.IOException

makeSSLSocket

protected java.net.Socket makeSSLSocket(java.net.URL url)
                                 throws java.io.IOException
Returns a newly constructed SSL socket for a given URL. By default, the HTTPS port to use will be 443.

Throws:
java.io.IOException

setSocketOptions

protected void setSocketOptions(java.net.Socket socket)
                         throws java.io.IOException
Sets the socket options to use. By default, sets the read timeout to 30 seconds.

Throws:
java.io.IOException

makeHttpClient

protected HttpClient makeHttpClient(java.net.URL url)
                             throws java.io.IOException
Returns a newly constructed HTTP client for a given URL. Calls makeSocket(java.net.URL) to construct a network socket for HTTP, makeSSLSocket(java.net.URL) for HTTPS. Calls setSocketOptions(java.net.Socket) to set socket options.

Throws:
java.io.IOException

writeRequest

public void writeRequest(ClientRequest request)
                  throws java.io.IOException
Attempts to send an HTTP request, and may retry to send a certain number of times. Calls makeHttpClient(java.net.URL) to construct a new client.

Specified by:
writeRequest in interface HttpClient
Throws:
java.io.IOException

getOutputStream

public java.io.OutputStream getOutputStream()
Returns a stream for writing data to, if data is to be sent to the server.

Specified by:
getOutputStream in interface HttpClient

readResponse

public ClientResponse readResponse()
                            throws java.io.IOException
Reads the response data from the HTTP server.

If the response data cannot be read, then the connection will be closed, and writeRequest will be called up to the maximum of retries as previously set. The original request will not retried if the request method was PUT or POST and ClientRequest.getDataPoster() returns null.

If a redirection response was read, it is followed only if redirection is enabled.

Specified by:
readResponse in interface HttpClient
Throws:
HttpException - if the server returned an invalid HTTP response
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the wrapped HttpClient. The underlying client may not be re-used.

Specified by:
close in interface HttpClient
Throws:
java.io.IOException

setFollowRedirects

public void setFollowRedirects(boolean followRedirects)
Sets whether HTTP redirects should be followed. These are 300-level response codes. By default, redirects are followed. The max number of redirects followed is equal to the number of retries, as was set in the constructor.


setSkipContinues

public void setSkipContinues(boolean skipContinues)
Sets whether HTTP continue responses should be skipped. These are 100-level response codes. By default, continue responses are ignored. There is no limit to the number of these that will be read.


toString

public java.lang.String toString()
Returns debug information.

Overrides:
toString in class java.lang.Object


Copyright © 2010. All Rights Reserved.