com.sun.slamd.http
Class HTTPRequest

java.lang.Object
  extended by com.sun.slamd.http.HTTPRequest

public class HTTPRequest
extends java.lang.Object

This class defines a means of encapsulating an HTTP request to send to a remote server. The request may use either the GET or POST method.

Author:
Neil A. Wilson

Field Summary
static java.lang.String HTTP_METHOD_GET
          The HTTP method for GET requests.
static java.lang.String HTTP_METHOD_POST
          The HTTP method for POST requests.
 
Constructor Summary
HTTPRequest(boolean isGet, java.net.URL baseURL)
          Creates a new HTTP request with the provided method and base URL.
 
Method Summary
 void addEncodedParameter(java.lang.String name, java.lang.String value)
          Adds the provided parameter to this request with a value that is already properly URL-encoded.
 void addParameter(java.lang.String name, java.lang.String value)
          Adds the specified parameter to this request.
 void addParameter(java.lang.String name, java.lang.String[] values)
          Adds the specified parameter to this request.
 void clearHeaders()
          Removes all header information for this request.
 HTTPRequest clone(java.net.URL baseURL)
          Creates a new HTTP request that is a copy of this request, optionally using a different base URL.
static java.lang.String encodeValue(java.lang.String parameterValue)
          Encodes the provided value in a form suitable for including in an HTTP request.
 java.lang.String generateHTTPRequest(HTTPClient client)
          Creates a string that is suitable for sending to an HTTP server or proxy.
 java.net.URL getBaseURL()
          Retrieves the base URL for this request.
 java.lang.String getHeader(java.lang.String name)
          Retrieves the value of the requested header for this request.
 java.util.LinkedHashMap getHeaderMap()
          Retrieves the map containing information about the headers for this request.
 java.util.ArrayList getParameterNameList()
          Retrieves an array list whose string elements are the names of the parameters associated with this request.
 java.lang.String[] getParameterNames()
          Retrieves the names of the parameters for this request.
 java.lang.String getParameterValue(java.lang.String parameterName)
          Retrieves the value of the parameter with the specified name.
 java.util.ArrayList getParameterValueList()
          Retrieves an array list whose string elements are the values of the parameters associated with this request.
 java.lang.String[] getParameterValues()
          Retrieves the values of the parameters for this request.
 java.lang.String[] getParameterValues(java.lang.String parameterName)
          Retrieves the set of values for the parameter with the specified name.
 java.lang.String getRequestMethod()
          Retrieves the HTTP method associated with this request.
 boolean isGet()
          Indicates whether this is an HTTP GET request.
 void removeAllParameters()
          Removes all parameter information from this request.
 void removeHeader(java.lang.String name)
          Removes the header with the specified name from this request.
 void removeParameter(java.lang.String name)
          Removes all values for the parameter with the given name.
 void removeParameter(java.lang.String name, java.lang.String value)
          Removes the parameter with the specified name and value from this request.
 void replaceParameter(java.lang.String name, java.lang.String newValue)
          Replaces any existing values for the specified parameter in this request with the provided value.
 void replaceParameter(java.lang.String name, java.lang.String[] newValues)
          Replaces any existing values for the specified parameter in this request with the provided set of values.
 void setHeader(java.lang.String name, java.lang.String value)
          Adds the provided name and value to the set of headers for this request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HTTP_METHOD_GET

public static final java.lang.String HTTP_METHOD_GET
The HTTP method for GET requests.

See Also:
Constant Field Values

HTTP_METHOD_POST

public static final java.lang.String HTTP_METHOD_POST
The HTTP method for POST requests.

See Also:
Constant Field Values
Constructor Detail

HTTPRequest

public HTTPRequest(boolean isGet,
                   java.net.URL baseURL)
Creates a new HTTP request with the provided method and base URL. It will not have any request headers or parameters.

Parameters:
isGet - Indicates whether this request should use the HTTP GET method (if not, POST will be used).
baseURL - The base URL to use for the request.
Method Detail

isGet

public boolean isGet()
Indicates whether this is an HTTP GET request.

Returns:
true if this represents a GET request, or false if it represents a POST request.

getRequestMethod

public java.lang.String getRequestMethod()
Retrieves the HTTP method associated with this request.

Returns:
The HTTP method associated with this request.

getBaseURL

public java.net.URL getBaseURL()
Retrieves the base URL for this request.

Returns:
The base URL for this request.

getHeader

public java.lang.String getHeader(java.lang.String name)
Retrieves the value of the requested header for this request.

Parameters:
name - The name of the header to retrieve.
Returns:
The value of the requested header for this request, or null if no such header has been defined.

getHeaderMap

public java.util.LinkedHashMap getHeaderMap()
Retrieves the map containing information about the headers for this request.

Returns:
The map containing information about the headers for this request.

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Adds the provided name and value to the set of headers for this request. If a header already exists with the given name, it will be replaced with the provided value.

Parameters:
name - The name of the header to add to the request.
value - The value of the header to add.

removeHeader

public void removeHeader(java.lang.String name)
Removes the header with the specified name from this request.

Parameters:
name - The name of the header to remove from this request.

clearHeaders

public void clearHeaders()
Removes all header information for this request.


getParameterNameList

public java.util.ArrayList getParameterNameList()
Retrieves an array list whose string elements are the names of the parameters associated with this request.

Returns:
An array list whose string elements are the names of the parameters associated with this request.

getParameterNames

public java.lang.String[] getParameterNames()
Retrieves the names of the parameters for this request.

Returns:
The names of the parameters for this request.

getParameterValueList

public java.util.ArrayList getParameterValueList()
Retrieves an array list whose string elements are the values of the parameters associated with this request. The order of the values will correspond to the order in which the names are provided.

Returns:
An array list whose string elements are the values of the parameters associated with this request.

getParameterValues

public java.lang.String[] getParameterValues()
Retrieves the values of the parameters for this request. The order of the values will correspond to the order in which the names are provided.

Returns:
The values of the parameters for this request.

getParameterValue

public java.lang.String getParameterValue(java.lang.String parameterName)
Retrieves the value of the parameter with the specified name. If the given parameter has multiple values, then the first value will be returned. If there is no such parameter for this entry, then null will be returned.

Parameters:
parameterName - The name of the parameter for which to retrieve the value.
Returns:
The value for the requested parameter.

getParameterValues

public java.lang.String[] getParameterValues(java.lang.String parameterName)
Retrieves the set of values for the parameter with the specified name. If there is no such parameter for this entry, then an empty array will be returned.

Parameters:
parameterName - The name of the parameter for which to retrieve the set of values.
Returns:
The set of values for the requested parameter.

addParameter

public void addParameter(java.lang.String name,
                         java.lang.String value)
Adds the specified parameter to this request. If a parameter already exists with the given name, then another value will be added.

Parameters:
name - The name of the value to add to this request.
value - The value of the parameter to add to this request.

addEncodedParameter

public void addEncodedParameter(java.lang.String name,
                                java.lang.String value)
                         throws java.io.UnsupportedEncodingException
Adds the provided parameter to this request with a value that is already properly URL-encoded.

Parameters:
name - The name to use for the parameter.
value - The pre-encoded value to for the parameter.
Throws:
java.io.UnsupportedEncodingException - If a problem occurs while attempting to decode the value as UTF-8.

addParameter

public void addParameter(java.lang.String name,
                         java.lang.String[] values)
Adds the specified parameter to this request. If a parameter already exists with the given name, then another value will be added.

Parameters:
name - The name of the value to add to this request.
values - The set of value for the parameter to add to this request.

replaceParameter

public void replaceParameter(java.lang.String name,
                             java.lang.String newValue)
Replaces any existing values for the specified parameter in this request with the provided value. If no values exist with the specified name, a new parameter will be added.

Parameters:
name - The name of the parameter for which to replace any existing values.
newValue - The new value to use in place of any existing value(s).

replaceParameter

public void replaceParameter(java.lang.String name,
                             java.lang.String[] newValues)
Replaces any existing values for the specified parameter in this request with the provided set of values. If no values exist with the specified name, a new parameter will be added.

Parameters:
name - The name of the parameter for which to replace any existing values.
newValues - The set of values to use in place of any existing value(s).

removeParameter

public void removeParameter(java.lang.String name)
Removes all values for the parameter with the given name. If no values for the specified parameter exist in the entry, then no action will be performed.

Parameters:
name - The name of the header for which to remove any values from the request.

removeParameter

public void removeParameter(java.lang.String name,
                            java.lang.String value)
Removes the parameter with the specified name and value from this request. If the specified parameter does not exist with the given value, then no action will be performed.

Parameters:
name - The name of the parameter from which to remove the specified value.
value - The value of the parameter to remove.

removeAllParameters

public void removeAllParameters()
Removes all parameter information from this request.


generateHTTPRequest

public java.lang.String generateHTTPRequest(HTTPClient client)
Creates a string that is suitable for sending to an HTTP server or proxy.

Parameters:
client - The client that will be sending the request.
Returns:
The string for sending to the HTTP server or proxy.

encodeValue

public static java.lang.String encodeValue(java.lang.String parameterValue)
Encodes the provided value in a form suitable for including in an HTTP request. Any unsafe characters will be escaped.

Parameters:
parameterValue - The value to be encoded.
Returns:
The properly encoded value.

clone

public HTTPRequest clone(java.net.URL baseURL)
Creates a new HTTP request that is a copy of this request, optionally using a different base URL. This is particularly useful when following redirects generated in response to POST requests.

Parameters:
baseURL - The base URL to use for the new request. If this is null then the base URL from this request will be used.
Returns:
The generated HTTP request.