com.bowstreet.util
Class HTTPRequest

java.lang.Object
  extended by com.bowstreet.util.HTTPRequest

public class HTTPRequest
extends java.lang.Object

API for calling out to a specified URL


Field Summary
static int DELETE_REQUEST
          Indicator for setRequestType.
static int GET_REQUEST
          Indicator for setRequestType.
static int POST_REQUEST
          Indicator for setRequestType.
static int PUT_REQUEST
          Indicator for setRequestType.
static java.lang.String ROOT_TAG
          This is the root level tag we place around the HTTP response.
 
Constructor Summary
HTTPRequest()
           
 
Method Summary
 void addCookie(java.lang.String cookie)
          Add a cookie to be sent.
 void addParam(java.lang.String name, IXml value)
          Add an IXml parameter to the request to be sent.
 void addParam(java.lang.String name, java.lang.String value)
          Add a String parameter to the request to be sent.
static void callMethod(java.lang.reflect.Method method, java.lang.Object instance, int value)
          Calls the specified method using reflection.
 void clear()
          Clears out and initializes the HTTPRequest object so it can be safely reused in another request, thereby avoiding the need to instantiate another HTTPRequest object.
 java.lang.String getBodyContentType()
          Gets the HTTP Body content type.
static java.lang.reflect.Method getConnectTimeoutMethod()
          Gets the setConnectTimeout Method on the HttpURLConnection if Java 1.5 or higher.
 java.util.Vector getCookies()
          Get the cookies that were sent back in the reply.
 java.lang.String getEncoding()
          Get the encoding of the response stream
 java.util.HashMap getHeaders()
          Retrieve the Headers that were sent back in the reply.
static java.lang.String getHTTPOperationString(int requestType)
          Gets the HTTP Operation String for the specified request type.
static int getHTTPOperationType(java.lang.String operationType)
          Gets the HTTP request type for the specified operation String.
static java.lang.reflect.Method getMethod(java.lang.Class sourceClass, java.lang.String name)
          Gets the method from the class for the specified name.
 java.lang.String getMimeType()
          Return the MIME type of the response.
 java.lang.String getProxyHost()
           
 java.lang.String getProxyPassword()
           
 int getProxyPort()
           
 java.lang.String getProxyUser()
           
static java.lang.reflect.Method getReadTimeoutMethod()
          Gets the setReadTimeout Method on the HttpURLConnection if Java 1.5 or higher.
 int getResponseCode()
           
 java.lang.String getResponseError()
           
 java.lang.String getResponseMessage()
          Gets the HTTP response message from the server.
 IXml invoke()
          Issues the HTTP request based on values set in the HTTPRequest object.
static void main(java.lang.String[] args)
           
 void overrideHost(java.lang.String host)
          Overrides the hostname in the target URL.
 void overridePort(java.lang.String port)
          Overrides the port in the target URL.
 void setBodyContent(java.lang.Object o)
          Passing this method a String or IXml argument will set it as the body content to be sent if the submission style is POST.
 void setBodyContentType(java.lang.String bodyContentType)
          Sets the HTTP Body content type used when performing a POST or PUT.
 void setContentType(java.lang.String s)
          Optional, override the content type of the response.
 void setDoStats(boolean b)
          Enable/disable the internal stats (timing) logging around the invoke.
 void setEncoding(java.lang.String encoding)
          Set the encoding (eg, "iso-8859-1") to be used to read the response.
 void setFollowRedirect(boolean b)
          Allows connection to follow HTTP redirect.
 void setHeader(java.lang.String name, java.lang.String value)
          Set a HTTP header to be sent.
 void setHttpURLConnectionFactory(com.bowstreet.util.HttpURLConnectionFactory httpURLConnectionFactory)
           
 void setLogRequest(boolean b)
          Sets an indicator to enable/disable logging of the request and response.
 void setOutputStreamEncoding(java.lang.String encoding)
          Set the encoding (eg, "UTF-8") to be used to send POST content.
 void setParseAll(boolean b)
          Enables/Disables parsing of all tags in the document response.
 void setPassword(java.lang.String password)
          Set the password for HTTP Basic Auth.
 void setProxyHost(java.lang.String proxyHost)
           
 void setProxyPassword(java.lang.String proxyPassword)
           
 void setProxyPort(java.lang.String proxyPort)
           
 void setProxyUser(java.lang.String proxyUser)
           
 void setRequestType(int type)
          Specify the request type as HTTP GET or POST.
 void setTimeout(int nSecs)
          Set a timeout value for the request.
 void setTimeout(java.lang.String nSecs)
          Set a timeout value for the request.
 void setURL(java.net.URL url)
          Sets the target URL for the HTTP request.
 void setUsername(java.lang.String username)
          Username for HTTP Basic Auth.
 void setWebAppAccess(WebAppAccess webAppAccess)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELETE_REQUEST

public static final int DELETE_REQUEST
Indicator for setRequestType. Sets up the invoke to be a HTTP DELETE.

See Also:
Constant Field Values

GET_REQUEST

public static final int GET_REQUEST
Indicator for setRequestType. Sets up the invoke to be a HTTP GET. This is the default for a new or cleared HTTPRequest object.

See Also:
Constant Field Values

POST_REQUEST

public static final int POST_REQUEST
Indicator for setRequestType. Sets up the invoke to be a HTTP POST.

See Also:
Constant Field Values

PUT_REQUEST

public static final int PUT_REQUEST
Indicator for setRequestType. Sets up the invoke to be a HTTP PUT.

See Also:
Constant Field Values

ROOT_TAG

public static final java.lang.String ROOT_TAG
This is the root level tag we place around the HTTP response.

See Also:
Constant Field Values
Constructor Detail

HTTPRequest

public HTTPRequest()
Method Detail

addCookie

public void addCookie(java.lang.String cookie)
Add a cookie to be sent.

Parameters:
cookie - the cookie to be added

addParam

public void addParam(java.lang.String name,
                     IXml value)
Add an IXml parameter to the request to be sent.

Parameters:
name - name of the arg
value - value of the arg

addParam

public void addParam(java.lang.String name,
                     java.lang.String value)
Add a String parameter to the request to be sent.

Parameters:
name - name of the arg
value - value of the arg

callMethod

public static void callMethod(java.lang.reflect.Method method,
                              java.lang.Object instance,
                              int value)
Calls the specified method using reflection.

Parameters:
method - The class Method to call.
instance - The class instance to call the method on.
value - The value to pass the method.

clear

public void clear()
Clears out and initializes the HTTPRequest object so it can be safely reused in another request, thereby avoiding the need to instantiate another HTTPRequest object.


getBodyContentType

public java.lang.String getBodyContentType()
Gets the HTTP Body content type.

Returns:
The HTTP Body content type.

getConnectTimeoutMethod

public static java.lang.reflect.Method getConnectTimeoutMethod()
Gets the setConnectTimeout Method on the HttpURLConnection if Java 1.5 or higher.

Returns:
The setConnectTimeout Method or null if not Java 1.5 or higher.

getCookies

public java.util.Vector getCookies()
Get the cookies that were sent back in the reply. Will return an empty vector if none. These are unparsed cookie strings which may contain domain and path info. Example: name=value; domain=foobar; path=/

Returns:
vector of cookie strings as they came back in the HTTP response

getEncoding

public java.lang.String getEncoding()
Get the encoding of the response stream

Returns:
encoding string

getHeaders

public java.util.HashMap getHeaders()
Retrieve the Headers that were sent back in the reply.

Returns:
Hashmap of keys and values.

getHTTPOperationString

public static final java.lang.String getHTTPOperationString(int requestType)
Gets the HTTP Operation String for the specified request type.

Parameters:
requestType - The HTTP request type (GET_REQUEST, PUT_REQUEST, DELETE_REQUEST, POST_REQUEST)
Returns:
The HTTP Operation String for the specified request type.

getHTTPOperationType

public static final int getHTTPOperationType(java.lang.String operationType)
Gets the HTTP request type for the specified operation String.

Parameters:
operationType - The HTTP operation ("GET", "PUT", "DELETE", "POST")
Returns:
The HTTP request type for the specified operation String.

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class sourceClass,
                                                 java.lang.String name)
Gets the method from the class for the specified name.

Parameters:
sourceClass - The class to get the method from.
name - The name of the method.
Returns:
The Method, or null if not found.

getMimeType

public java.lang.String getMimeType()
Return the MIME type of the response.

Returns:
MIME type of the response.

getProxyHost

public java.lang.String getProxyHost()
Returns:
the proxyHost

getProxyPassword

public java.lang.String getProxyPassword()
Returns:
the proxyPassword

getProxyPort

public int getProxyPort()
Returns:
the proxyPort

getProxyUser

public java.lang.String getProxyUser()
Returns:
the proxyUser

getReadTimeoutMethod

public static java.lang.reflect.Method getReadTimeoutMethod()
Gets the setReadTimeout Method on the HttpURLConnection if Java 1.5 or higher.

Returns:
The setReadTimeout Method or null if not Java 1.5 or higher.

getResponseCode

public int getResponseCode()
Returns:
the responseCode

getResponseError

public java.lang.String getResponseError()
Returns:
the responseError

getResponseMessage

public java.lang.String getResponseMessage()
Gets the HTTP response message from the server.
This is only valid after calling the invoke() method.

Returns:
the HTTP response message from the server.

invoke

public IXml invoke()
            throws java.net.UnknownHostException,
                   java.io.IOException,
                   com.bowstreet.util.parser.ParserException
Issues the HTTP request based on values set in the HTTPRequest object. Typical call sequence:

HTTPRequest h = new HTTPRequest();
h.setURL(new URL("http://homie.codebump.com:8080/services/zipcodes/zipcodelookup.asmx/GetZipCodesWithin"));
h.setRequestType(HTTPRequest.GET_REQUEST);
h.addParam("zip", "03801");
h.addParam("distance", "10");
h.setTimeout(10);
IXml doc = h.invoke();
System.out.println("mime type =" + h.getMimeType());
if (doc != null)
System.out.println("htmlDoc = " + doc.toString());

If it fails an exception will be thrown. If the call succeeds the data will be accessible via getDocument() and the mime type by getMimeType().

Returns:
the result (document content) as IXml with a top <htmlDocument> element containing the HTML, XHTML response, depending on content type.
Throws:
java.net.UnknownHostException - if the URL given is invalid
java.io.IOException - if there's a communication error
com.bowstreet.util.parser.ParserException - if the response could not be interpreted

main

public static void main(java.lang.String[] args)

overrideHost

public void overrideHost(java.lang.String host)
Overrides the hostname in the target URL. This replaces just the host part of the URL, not the whole URL, so it can be used like this:

http.setURL(new URL("http://www.xmethods.net/aservice/s.asmx"));
http.overrideHost("localhost");
http.overridePort("9090");
IXml x = http.invoke();

Then if you start TcpTunnel on localhost:9090 and point it at xmethods.net port 80, the HTTP request will be tunnelled through TcpTunnel and you can see everything that goes back and forth.

Parameters:
host - the hostname, eg, www.xmethods.net, or localhost

overridePort

public void overridePort(java.lang.String port)
Overrides the port in the target URL. See also overrideHost and setURL.

Parameters:
port - the port number, eg 9090

setBodyContent

public void setBodyContent(java.lang.Object o)
Passing this method a String or IXml argument will set it as the body content to be sent if the submission style is POST. Alternatively the Body content can be set by passing an argument name-value pair with name="BODY_CONTENT". Setting this will cause any other arguments passed to the HTTP request to be ignored.

Parameters:
o - the String or IXml data that is to be sent in the Body of the HTTP POST

setBodyContentType

public void setBodyContentType(java.lang.String bodyContentType)
Sets the HTTP Body content type used when performing a POST or PUT.
Alternatively the Body content type can be set by passing a parameter name-value pair with name="BODY_CONTENT_TYPE".

Parameters:
bodyContentType - The HTTP Body content type to set (e.g. application/atom+xml)

setContentType

public void setContentType(java.lang.String s)
Optional, override the content type of the response.

Parameters:
s - a valid content type string, eg "text/xml"

setDoStats

public void setDoStats(boolean b)
Enable/disable the internal stats (timing) logging around the invoke. Default is false (disabled).

Parameters:
b - true/false to enable/disable tracing

setEncoding

public void setEncoding(java.lang.String encoding)
Set the encoding (eg, "iso-8859-1") to be used to read the response. Overrides the default and any encoding info from the actual response. Use setOutputStreamEncoding to set the encoding of the request stream.

Parameters:
encoding - string

setFollowRedirect

public void setFollowRedirect(boolean b)
Allows connection to follow HTTP redirect. Default is true.

Parameters:
b - boolean to enable/disable

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Set a HTTP header to be sent.

Parameters:
name - name of the HTTP header
value - value of the HTTP header

setHttpURLConnectionFactory

public void setHttpURLConnectionFactory(com.bowstreet.util.HttpURLConnectionFactory httpURLConnectionFactory)
Parameters:
httpURLConnectionFactory - the httpURLConnectionFactory to set

setLogRequest

public void setLogRequest(boolean b)
Sets an indicator to enable/disable logging of the request and response. Default is disabled.

Parameters:
b - true/false indicator to enable/disable logging of request and response.

setOutputStreamEncoding

public void setOutputStreamEncoding(java.lang.String encoding)
Set the encoding (eg, "UTF-8") to be used to send POST content. Overrides the default. When "setBodyContent" is being used the default is "UTF-8". If "setBodyContent" is not being used (so the params are being sent Form-URLEncoded) then the default encoding is "iso-8859-1" (picked up from the underlying HttpURLConnection).

Parameters:
encoding - desired encoding, eg "iso-8859-1"

setParseAll

public void setParseAll(boolean b)
Enables/Disables parsing of all tags in the document response. Default is to parse all tags.

Parameters:
b - boolean to enable/disable all tag parsing

setPassword

public void setPassword(java.lang.String password)
Set the password for HTTP Basic Auth.

Parameters:
password - password

setProxyHost

public void setProxyHost(java.lang.String proxyHost)
Parameters:
proxyHost - the proxyHost to set

setProxyPassword

public void setProxyPassword(java.lang.String proxyPassword)
Parameters:
proxyPassword - the proxyPassword to set

setProxyPort

public void setProxyPort(java.lang.String proxyPort)
Parameters:
proxyPort - the proxyPort to set

setProxyUser

public void setProxyUser(java.lang.String proxyUser)
Parameters:
proxyUser - the proxyUser to set

setRequestType

public void setRequestType(int type)
Specify the request type as HTTP GET or POST. Default if not specified is GET.

Parameters:
type - Must be either GET_REQUEST or POST_REQUEST as defined in this class.

setTimeout

public void setTimeout(int nSecs)
Set a timeout value for the request. Default is 60 seconds.

Parameters:
nSecs - timeout value in seconds

setTimeout

public void setTimeout(java.lang.String nSecs)
Set a timeout value for the request. Default is 60 seconds.

Parameters:
nSecs - timeout value string in seconds

setURL

public void setURL(java.net.URL url)
Sets the target URL for the HTTP request. This is the service endpoint. Subsequent calls to overrideHost and overridePort can be subsequently used to override those components of the URL (eg, for redirecting via TcpTunnel).

Parameters:
url - the target URL (endpoint URL) of the HTTP request

setUsername

public void setUsername(java.lang.String username)
Username for HTTP Basic Auth.

Parameters:
username - username

setWebAppAccess

public void setWebAppAccess(WebAppAccess webAppAccess)
Parameters:
servletRequest - the servletRequest to set


Copyright © 2009 IBM. All Rights Reserved.