org.apache.commons.latka.http
Class RequestImpl

java.lang.Object
  extended by org.apache.commons.latka.http.RequestImpl
All Implemented Interfaces:
Request

public class RequestImpl
extends Object
implements Request

An implementation of a Latka Request interface based on the Apache Commons HttpClient package.

Version:
$Id: RequestImpl.java 561366 2007-07-31 15:58:29Z rahul $
Author:
Doug Sale, Morgan Delagrange, dIon Gillard
See Also:
Request

Field Summary
protected  Credentials _credentials
          credentials for the request
protected  boolean _followRedirects
          whether or not redirect responses should be followed as part of the request processing
protected  String _host
          host the request is being made on
protected  org.apache.commons.httpclient.HttpClient _httpClient
          object used to perform the http requests
protected  org.apache.commons.httpclient.HttpMethod _httpMethod
          http method implementation
protected  String _httpVersion
           
protected  String _label
          name given to the request
protected static org.apache.log4j.Category _log
          log4j category used when logging messages
protected  int _method
          http method being used to make the request
protected  Parameters _parameters
          parameters sent with this request
protected  int _port
          port the request is being made on
protected  String _query
          query string portion of the URL
protected  String _requestBody
          manually constructed post body
protected  RequestHeaders _requestHeaders
          headers sent with this request
protected  long _requestTiming
          time taken for the request in milliseconds
protected  SessionImpl _session
          http session the request is part of
protected  URL _targetURL
          URL being requested
protected  List _visitedURLs
           
static int HTTP_PORT
          Standard HTTP Port
static int HTTPS_PORT
          Standard HTTPS Port
 
Fields inherited from interface org.apache.commons.latka.http.Request
HTTP_METHOD_GET, HTTP_METHOD_HEAD, HTTP_METHOD_POST
 
Constructor Summary
protected RequestImpl(String label, URL url, int httpMethod, org.apache.commons.httpclient.HttpState state, SessionImpl session, boolean followRedirects)
          Create a RequestImpl
protected RequestImpl(URL url, int httpMethod, org.apache.commons.httpclient.HttpState state, SessionImpl session)
          Create a request for the specified URL, process using the supplied method and use the supplied state and session for persistent data
 
Method Summary
 void addHeader(String headerName, String headerValue)
          Set a header in the request
protected  void addHttpClientParameter(String name, String value)
          Associate a parameter with this request.
 void addParameter(String name, String value)
          Add a parameter (name and value) to the request
protected  void closeConnection()
          Closes the http connection associated with the request
 Response execute()
          Executes the request.
protected  Response executeRequestPerHost()
          Execute the request - perform the http interaction.
 boolean followRedirects()
          Defined in the interface
 Credentials getCredentials()
          Return the credentials for this request
 RequestHeaders getHeaders()
          Defined in the implemented interface
protected  org.apache.commons.httpclient.HttpMethod getHttpMethod()
          Returns the object implementing the HttpMethod (get, post, etc.) for this request.
 String getLabel()
          Get the label used for the request
 int getMethod()
          Defined in the interface
 Parameters getParameters()
          Defined in the implemented interface
 Proxy getProxy()
          Getter for property proxy.
 int getRequestTiming()
          The amount of time it took to execute the request in milliseconds, or -1 if the request has not been executed successfully
 Session getSession()
          Retrieve the session associated with this request.
 URL getURL()
          Get the URL used for the request
 String getVersion()
          Get the HTTP version to use in this request.
protected  void openConnection()
          opens an HTTP connection.
 void setCredentials(Credentials credentials)
          Defined in the implemented interface
 void setHeaders(RequestHeaders requestHeaders)
          Defined in the implemented interface
 void setParameters(Parameters parameters)
          Defined in the implemented interface
 void setProxy(Proxy proxy)
          Setter for property proxy.
 void setRequestBody(String body)
          Defined in the implemented interface
 void setVersion(String version)
          Sets the HTTP version for this request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_PORT

public static final int HTTP_PORT
Standard HTTP Port

See Also:
Constant Field Values

HTTPS_PORT

public static final int HTTPS_PORT
Standard HTTPS Port

See Also:
Constant Field Values

_host

protected String _host
host the request is being made on


_port

protected int _port
port the request is being made on


_method

protected int _method
http method being used to make the request


_session

protected SessionImpl _session
http session the request is part of


_credentials

protected Credentials _credentials
credentials for the request


_httpMethod

protected org.apache.commons.httpclient.HttpMethod _httpMethod
http method implementation


_targetURL

protected URL _targetURL
URL being requested


_query

protected String _query
query string portion of the URL


_requestTiming

protected long _requestTiming
time taken for the request in milliseconds


_label

protected String _label
name given to the request


_requestHeaders

protected RequestHeaders _requestHeaders
headers sent with this request


_parameters

protected Parameters _parameters
parameters sent with this request


_requestBody

protected String _requestBody
manually constructed post body


_followRedirects

protected boolean _followRedirects
whether or not redirect responses should be followed as part of the request processing


_httpClient

protected org.apache.commons.httpclient.HttpClient _httpClient
object used to perform the http requests


_log

protected static final org.apache.log4j.Category _log
log4j category used when logging messages


_httpVersion

protected String _httpVersion

_visitedURLs

protected List _visitedURLs
Constructor Detail

RequestImpl

protected RequestImpl(URL url,
                      int httpMethod,
                      org.apache.commons.httpclient.HttpState state,
                      SessionImpl session)
Create a request for the specified URL, process using the supplied method and use the supplied state and session for persistent data

Parameters:
url - the URL to send the request to
httpMethod - the http method to use in sending the request, as defined in Request
state - shared information across requests
session - state shared across servers and requests

RequestImpl

protected RequestImpl(String label,
                      URL url,
                      int httpMethod,
                      org.apache.commons.httpclient.HttpState state,
                      SessionImpl session,
                      boolean followRedirects)
Create a RequestImpl

Parameters:
label - a name for the request
url - the url that this request embodies
httpMethod - the method by which this request should be executed
state - shared information across requests
session - the session that the request should be executed in
followRedirects - whether http redirect responses should be honoured
Method Detail

getHttpMethod

protected org.apache.commons.httpclient.HttpMethod getHttpMethod()
Returns the object implementing the HttpMethod (get, post, etc.) for this request.

Returns:
the underlying HttpMethod object representing the request/ response pair.

getHeaders

public RequestHeaders getHeaders()
Defined in the implemented interface

Specified by:
getHeaders in interface Request
Returns:
the headers used for this request
See Also:
Request.getHeaders()

setHeaders

public void setHeaders(RequestHeaders requestHeaders)
Defined in the implemented interface

Specified by:
setHeaders in interface Request
Parameters:
requestHeaders - new value for the headers property
See Also:
Request.setHeaders(RequestHeaders)

getParameters

public Parameters getParameters()
Defined in the implemented interface

Specified by:
getParameters in interface Request
Returns:
the parameters property
See Also:
Request.getParameters()

setRequestBody

public void setRequestBody(String body)
Defined in the implemented interface

Specified by:
setRequestBody in interface Request
Parameters:
body - Body to POST
See Also:
Request.setRequestBody(String)

setParameters

public void setParameters(Parameters parameters)
Defined in the implemented interface

Specified by:
setParameters in interface Request
Parameters:
parameters - new value for parameters property
See Also:
Request.setParameters(Parameters)

setCredentials

public void setCredentials(Credentials credentials)
Defined in the implemented interface

Specified by:
setCredentials in interface Request
Parameters:
credentials - username and password to use
See Also:
Request.setCredentials(Credentials)

getCredentials

public Credentials getCredentials()
Description copied from interface: Request
Return the credentials for this request

Specified by:
getCredentials in interface Request
Returns:
Request credentials

executeRequestPerHost

protected Response executeRequestPerHost()
                                  throws IOException
Execute the request - perform the http interaction. Since HttpClient doesn't follow off-server redirects, execute() may have to construct further requests that call this method.

Returns:
a Response detailing the html etc
Throws:
IOException - when there are problems reading and writing
See Also:
Request.execute()

execute

public Response execute()
                 throws IOException
Executes the request. In the event of a 301 or 302, this method may need to create a new Request, due to an idiosyncracy of HttpClient. In that case, the Response.getRequest() method will return the actual Request that was executed.

Specified by:
execute in interface Request
Returns:
a Response object represnting the HTTP response to the request
Throws:
IOException - if the remote server could not be reached

getURL

public URL getURL()
Get the URL used for the request

Specified by:
getURL in interface Request
Returns:
the URL of the request
See Also:
Request.getURL()

getLabel

public String getLabel()
Get the label used for the request

Specified by:
getLabel in interface Request
Returns:
the label used to create the request
See Also:
Request.getLabel()

addParameter

public void addParameter(String name,
                         String value)
Add a parameter (name and value) to the request

Specified by:
addParameter in interface Request
Parameters:
name - the name of the parameter to be added
value - the value of the parameter to be added
See Also:
Request.addParameter(String,String)

addHttpClientParameter

protected void addHttpClientParameter(String name,
                                      String value)
Associate a parameter with this request.

Parameters:
name - the lvalue of the parameter - must not be null
value - the rvalue of the parameter - must not be null

addHeader

public void addHeader(String headerName,
                      String headerValue)
Set a header in the request

Specified by:
addHeader in interface Request
Parameters:
headerName - name of any HTTP request header
headerValue - value of that header

getSession

public Session getSession()
Retrieve the session associated with this request.

Specified by:
getSession in interface Request
Returns:
a Session object

getRequestTiming

public int getRequestTiming()
Description copied from interface: Request
The amount of time it took to execute the request in milliseconds, or -1 if the request has not been executed successfully

Specified by:
getRequestTiming in interface Request
Returns:
the time it took to execute this request in milliseconds or -1 if the request has not yet been executed

openConnection

protected void openConnection()
                       throws IOException
opens an HTTP connection. This method is called before executing the method.

Throws:
IOException - if the server could not be contacted

closeConnection

protected void closeConnection()
                        throws IOException
Closes the http connection associated with the request

Throws:
IOException - if there are problems closing the connection

followRedirects

public boolean followRedirects()
Defined in the interface

Specified by:
followRedirects in interface Request
Returns:
whether the request will honour http redirect status codes
See Also:
Request.followRedirects()

getMethod

public int getMethod()
Defined in the interface

Specified by:
getMethod in interface Request
Returns:
the http method being used for the request, as defined in the interface
See Also:
Request.getMethod()

getProxy

public Proxy getProxy()
Getter for property proxy.

Returns:
Value of property proxy.

setProxy

public void setProxy(Proxy proxy)
Setter for property proxy.

Parameters:
proxy - New value of property proxy.

setVersion

public void setVersion(String version)
Sets the HTTP version for this request. If the version provided is not 1.0 or 1.1, then 1.1 will be used.

Specified by:
setVersion in interface Request
Parameters:
version - HTTP version

getVersion

public String getVersion()
Get the HTTP version to use in this request.

Specified by:
getVersion in interface Request
Returns:
HTTP version for the request


Copyright © 2001-2010 The Apache Software Foundation. All Rights Reserved.