com.sonalb.net.http
Class HTTPRedirectHandler

java.lang.Object
  extended by com.sonalb.net.http.HTTPRedirectHandler

public class HTTPRedirectHandler
extends java.lang.Object

Convenience class that combines cookie-handling and redirect-handling logic. When the connect() method is invoked, the handler cyclically processes HTTP Redirects (if any), and also takes care of cookie-handling while doing so. The maximum number of redirects defaults to 10. The handler determines a successful run, when the HTTP response code is equal to a specified success code. This code defaults to 200 (OK).

Author:
Sonal Bansal

Constructor Summary
HTTPRedirectHandler(java.net.HttpURLConnection huc)
          Creates a handler for the input HttpURLConnection.
 
Method Summary
 void addCookies(CookieJar cj)
          Adds some Cookies to the existing Cookies in an HTTPRedirectHandler.
 void connect()
          Connects to initial HttpURLConnection (specified during construction), and initiates cookie-handling and redirect-handling.
 java.net.HttpURLConnection getConnection()
          Gets the HttpURLConnection for the final successful response.
 CookieJar getCookieJar()
          Gets the CookieJar containing any pre-existing Cookies, as well as new ones extracted during processing.
 java.io.InputStream getInputStream()
          Gets the InputStream for the final successful response.
 void handleCookies(boolean b)
          Enables/Disables automated cookie-handling.
 boolean isConnected()
          Checks whether this handler has successfully connected.
 void setClient(Client cl)
          Sets the Client to be used for this handler.
 void setCookieJar(CookieJar cj)
          Sets the CookieJar containing Cookies to be used during cookie-handling.
 void setMaxRedirects(int i)
          Sets the maximum number of redirects that will be followed.
 void setSuccessCode(int i)
          Sets the HTTP response code designating a successful run.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HTTPRedirectHandler

public HTTPRedirectHandler(java.net.HttpURLConnection huc)
Creates a handler for the input HttpURLConnection. The HttpURLConnection must NOT be connected yet.

Method Detail

setClient

public void setClient(Client cl)
Sets the Client to be used for this handler.


handleCookies

public void handleCookies(boolean b)
Enables/Disables automated cookie-handling.


setSuccessCode

public void setSuccessCode(int i)
Sets the HTTP response code designating a successful run.

Parameters:
i - the code; non-positive values ignored

setCookieJar

public void setCookieJar(CookieJar cj)
Sets the CookieJar containing Cookies to be used during cookie-handling.


addCookies

public void addCookies(CookieJar cj)
Adds some Cookies to the existing Cookies in an HTTPRedirectHandler.

Parameters:
cj - the Cookies to be added

getCookieJar

public CookieJar getCookieJar()
Gets the CookieJar containing any pre-existing Cookies, as well as new ones extracted during processing.

Returns:
the CookieJar; always non-null

getInputStream

public java.io.InputStream getInputStream()
Gets the InputStream for the final successful response.

Throws:
java.lang.IllegalStateException - when called before successful connection.

getConnection

public java.net.HttpURLConnection getConnection()
Gets the HttpURLConnection for the final successful response.


setMaxRedirects

public void setMaxRedirects(int i)
Sets the maximum number of redirects that will be followed.

Parameters:
i - the max number; non-positive values are ignored

connect

public void connect()
             throws java.io.IOException,
                    MalformedCookieException
Connects to initial HttpURLConnection (specified during construction), and initiates cookie-handling and redirect-handling. It can only be called once per instance.

Throws:
java.io.IOException - if there is an I/O problem
MalformedCookieException - if there was a problem with cookie-handling
java.lang.IllegalStateException - if this method has already been called

isConnected

public boolean isConnected()
Checks whether this handler has successfully connected.