org.ferris.net.runner
Class HttpRequestRunner<M>

java.lang.Object
  extended by org.ferris.net.runner.HttpRequestRunner<M>
Direct Known Subclasses:
HttpsRequestRunner

public class HttpRequestRunner<M>
extends Object

This class makes it easier to make a GET or POST call to a URL, pass request parameters, and process the results which come back. An instance of this class can be used to make multiple requests to the server and the cookies for the server are automatically manged. This is especially useful if some sort of session cookie must be generated or a non-HTTP BASIC authentication login must be performed before making the request.

Author:
Mike Remijan

Field Summary
static org.apache.http.auth.AuthScope proxyAuthScope
           
static org.apache.http.auth.Credentials proxyCredentials
           
 
Constructor Summary
HttpRequestRunner(org.apache.http.auth.AuthScope authScope)
          This constructor is used if the HTTP resource is NOT protected by BASIC authentication.
HttpRequestRunner(org.apache.http.auth.AuthScope authScope, org.apache.http.auth.UsernamePasswordCredentials credentials)
          This constructor is used if the HTTP resource IS protected by BASIC authentication.
 
Method Summary
 void addCookie(String name, String content, String domain, String path)
          Add the cookie to the request.
 void addParameter(File parameter)
          Add the File value of a request parameter
 void addParameter(String parameter)
          Add the String value of a request parameter and URLEncoder is automatically used to make sure special characters are escaped properly.
 void addParameters(File[] parameters)
           
 void addParameters(String[] parameters)
           
 List<M> get(String request, HttpResponseHandler<M> handler)
          Make the HTTP request using GET
 String getCookiePolicy()
          Get the cookie policy, if none is set the default is CookiePolicy.RFC_2109
protected  String getHost()
          The "localhost" of the URL "http://localhost:8080/some/path/search.php".
protected  int getPort()
          The "8080" of the URL "http://localhost:8080/some/path/search.php".
protected  String getScheme()
          The "http" of the URL "http://localhost:8080/some/path/search.php".
 List<M> post(String request, HttpResponseHandler<M> handler)
          Make the HTTP request using POST
 void setCookiePolicy(String cookiePolicy)
          Set the cookie policy using a value from CookiePolicy.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

proxyAuthScope

public static org.apache.http.auth.AuthScope proxyAuthScope

proxyCredentials

public static org.apache.http.auth.Credentials proxyCredentials
Constructor Detail

HttpRequestRunner

public HttpRequestRunner(org.apache.http.auth.AuthScope authScope)
This constructor is used if the HTTP resource is NOT protected by BASIC authentication.

Parameters:
authScope -

HttpRequestRunner

public HttpRequestRunner(org.apache.http.auth.AuthScope authScope,
                         org.apache.http.auth.UsernamePasswordCredentials credentials)
This constructor is used if the HTTP resource IS protected by BASIC authentication.

Parameters:
authScope -
Method Detail

getScheme

protected String getScheme()
The "http" of the URL "http://localhost:8080/some/path/search.php". By default returns "http". Override this method in sub-classes if "https" is needed instead.


getHost

protected String getHost()
The "localhost" of the URL "http://localhost:8080/some/path/search.php". By default returns getAuthScope().getHost(). Override this method in sub-classes if needed.


getPort

protected int getPort()
The "8080" of the URL "http://localhost:8080/some/path/search.php". By default returns getAuthScope().getPort() if the value is >= 0, otherwise returns -1. Override this method in sub-classes if needed.


setCookiePolicy

public void setCookiePolicy(String cookiePolicy)
Set the cookie policy using a value from CookiePolicy.

Parameters:
cookiePolicy -

getCookiePolicy

public String getCookiePolicy()
Get the cookie policy, if none is set the default is CookiePolicy.RFC_2109

Returns:

addCookie

public void addCookie(String name,
                      String content,
                      String domain,
                      String path)
Add the cookie to the request.


addParameter

public void addParameter(String parameter)
Add the String value of a request parameter and URLEncoder is automatically used to make sure special characters are escaped properly.


addParameter

public void addParameter(File parameter)
Add the File value of a request parameter


addParameters

public void addParameters(String[] parameters)
See Also:
addParameter(String)

addParameters

public void addParameters(File[] parameters)
See Also:
addParameter(File)

get

public List<M> get(String request,
                   HttpResponseHandler<M> handler)
Make the HTTP request using GET

Parameters:
request - - This value is something like: /some/kind/of/path/to/resource.jsp?fname=?&lname=? It must begin with a "/" character. The query parameters are normal query parameters with the "?" character in place of the actual value. This class will take care of replacing and properly encoding the value of the parameter before replacing the "?" character with it.
handler - - What to do on each iteration of the response.

post

public List<M> post(String request,
                    HttpResponseHandler<M> handler)
Make the HTTP request using POST

Parameters:
pathAndResource - - This value is something like: /some/kind/of/path/to/resource.jsp?fname=?&lname=? It must begin with a "/" character. The query parameters are normal query parameters with the "?" character in place of the actual value. This class will take care of replacing and properly encoding the value of the parameter before replacing the "?" character with it. If you call addParameter() with a File object then the contents of the file will be posted to the http server.
handler - - What to do on each iteration of the response.


Copyright © Jun 11, 2009-2011 The Ferris Foundation. All Rights Reserved.