org.apache.anteater.test
Class HttpMessage

java.lang.Object
  |
  +--org.apache.anteater.test.HttpMessage
All Implemented Interfaces:
XMLDocument

public class HttpMessage
extends Object
implements XMLDocument

Describes the common attributes of an HTTP request and response.

Since:
September 28, 2001
Author:
Ovidiu Predescu, Jeff Turner

Field Summary
static String UNSET_BODY
           
static String UNSET_HOST
           
static int UNSET_PORT
           
static String UNSET_PROTOCOL
           
 
Constructor Summary
HttpMessage(javax.servlet.http.HttpServletRequest request)
          Creates a new HttpMessage instance from an HttpServletRequest object.
HttpMessage(org.apache.tools.ant.Project project)
          Creates a new HttpMessage instance.
HttpMessage(org.apache.tools.ant.Project proj, InputStream is)
           
HttpMessage(org.apache.tools.ant.Project proj, InputStream is, int debug)
          Creates a new HttpMessage instance given an InputStream object to read an HTTP response from the server.
HttpMessage(org.apache.tools.ant.Project project, int dbg)
          Creates a new HttpMessage instance.
 
Method Summary
 void addHeader(Header header)
           
 void addParameter(Parameter param)
           
 String getBody()
           
 org.dom4j.Document getDocument()
          Assumes body contains an XML document, and returns a DOM4J Document object representation of it.
 String getDocumentAsString()
          Obtain the string representation of the XML document.
 Header getHeader(String name)
           
 HashMap getHeaders()
           
 String getHost()
           
 String getMethod()
           
 Map getParameters()
           
 String getPassword()
           
 String getPath()
           
 int getPort()
          Returns the port number of this URL.
 String getProtocol()
           
 String getResponseLine()
           
 String getURL()
          Return the URL of the HTTP resource to request.
 String getUser()
           
 boolean isSecure()
          Should operate over SSL?
 void parseHeaders(BufferedReader br)
          Parse the incoming HTTP request headers.
 String printHeaders(HashMap headers)
          Print a set of Headers.
 String readBody(BufferedReader isr)
           
 void setBody(String bd)
           
 void setContent(String content)
          Assumes the value of the content attribute is an href whose content is to be POSTed.
 void setHeaders(String s)
           
 void setHost(String host)
           
 void setHref(String href)
          Set the URL specifying the resource for a HTTP request message.
 void setMethod(String method)
           
 void setPassword(String password)
           
 void setPath(String path)
           
 void setPort(int port)
           
 void setProject(org.apache.tools.ant.Project proj)
           
 void setProtocol(String protocol)
          Set HTTP protocol, typically HTTP/1.0 or HTTP/1.1.
 void setQuery(String query)
          Parse the 'query' part of the URL, ie the bit after the '?', and store in variable "Parameter".
 void setResponseLine(String rsp)
           
 void setSecure(boolean secure)
          Sets whether request/response should be made via SSL.
 void setUser(String user)
           
 void setUseTidy(Boolean yn)
           
 String toString()
           
protected  String urlDecode(String str)
           
protected  String urlEncode(String str)
           
 void writeTo(OutputStream os, org.apache.tools.ant.Project project)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

UNSET_PORT

public static final int UNSET_PORT
See Also:
Constant Field Values

UNSET_HOST

public static final String UNSET_HOST
See Also:
Constant Field Values

UNSET_BODY

public static final String UNSET_BODY
See Also:
Constant Field Values

UNSET_PROTOCOL

public static final String UNSET_PROTOCOL
See Also:
Constant Field Values
Constructor Detail

HttpMessage

public HttpMessage(org.apache.tools.ant.Project proj,
                   InputStream is,
                   int debug)
            throws IOException
Creates a new HttpMessage instance given an InputStream object to read an HTTP response from the server.

Parameters:
is - an InputStream value
debug - an int value
Throws:
IOException - if an error occurs

HttpMessage

public HttpMessage(org.apache.tools.ant.Project proj,
                   InputStream is)
            throws IOException

HttpMessage

public HttpMessage(javax.servlet.http.HttpServletRequest request)
            throws IOException
Creates a new HttpMessage instance from an HttpServletRequest object. This is the preferred constructor when creating instances within a servlet container.

Note that if this constructor is used, getURL() will not have variables interpolated, as there is no access to the Ant project.

Parameters:
request - a HttpServletRequest value
Throws:
IOException - if an error occurs

HttpMessage

public HttpMessage(org.apache.tools.ant.Project project)
Creates a new HttpMessage instance.


HttpMessage

public HttpMessage(org.apache.tools.ant.Project project,
                   int dbg)
Creates a new HttpMessage instance.

Method Detail

setProject

public void setProject(org.apache.tools.ant.Project proj)

toString

public String toString()
Overrides:
toString in class Object

getURL

public String getURL()
Return the URL of the HTTP resource to request.


setMethod

public void setMethod(String method)

getMethod

public String getMethod()

setProtocol

public void setProtocol(String protocol)
Set HTTP protocol, typically HTTP/1.0 or HTTP/1.1.


getProtocol

public String getProtocol()

setSecure

public void setSecure(boolean secure)
Sets whether request/response should be made via SSL.


isSecure

public boolean isSecure()
Should operate over SSL?


setHref

public void setHref(String href)
             throws MalformedURLException
Set the URL specifying the resource for a HTTP request message. Shorthand for setting the host, port, path, query etc.

Parameters:
href - URL to request, eg http://whatever/blah?foo=bar. The query string (foo=bar) part provides an alternative to nested <parameter> elements.
MalformedURLException

setHost

public void setHost(String host)

getHost

public String getHost()

setPort

public void setPort(int port)

getPort

public int getPort()
Returns the port number of this URL.

Returns:
the port number

setPath

public void setPath(String path)

getPath

public String getPath()

setQuery

public void setQuery(String query)
Parse the 'query' part of the URL, ie the bit after the '?', and store in variable "Parameter".

Parameters:
query - The Query string. Can be null.

setUser

public void setUser(String user)

getUser

public String getUser()

setPassword

public void setPassword(String password)

getPassword

public String getPassword()

getResponseLine

public String getResponseLine()

setResponseLine

public void setResponseLine(String rsp)

addParameter

public void addParameter(Parameter param)

getParameters

public Map getParameters()

addHeader

public void addHeader(Header header)

getHeader

public Header getHeader(String name)

getHeaders

public HashMap getHeaders()

setBody

public void setBody(String bd)

getBody

public String getBody()

setUseTidy

public void setUseTidy(Boolean yn)

setHeaders

public void setHeaders(String s)

getDocument

public org.dom4j.Document getDocument()
                               throws org.dom4j.DocumentException
Assumes body contains an XML document, and returns a DOM4J Document object representation of it.

Specified by:
getDocument in interface XMLDocument
Returns:
a Document object that's the DOM4J representation of the document.
org.dom4j.DocumentException

getDocumentAsString

public String getDocumentAsString()
Description copied from interface: XMLDocument
Obtain the string representation of the XML document.

Specified by:
getDocumentAsString in interface XMLDocument
Returns:
a String value

parseHeaders

public void parseHeaders(BufferedReader br)
                  throws IOException
Parse the incoming HTTP request headers.

Throws:
IOException - if an input/output error occurs

readBody

public String readBody(BufferedReader isr)
                throws IOException
IOException

setContent

public void setContent(String content)
Assumes the value of the content attribute is an href whose content is to be POSTed.


writeTo

public void writeTo(OutputStream os,
                    org.apache.tools.ant.Project project)
             throws IOException
IOException

printHeaders

public String printHeaders(HashMap headers)
Print a set of Headers.

Parameters:
headers - HashMap of name->Header objects.
Returns:
a String like this:
 Headers:
   X-Cocoon-Version: 2.1-dev
   Content-Length: 187
   Content-Type: text/html
   Server: Tomcat Web Server/3.3.1 Final ( JSP 1.1; Servlet 2.2 )
   Date: Mon, 21 Oct 2002 08:39:22 GMT
   Location: http://localhost:8787/cocoon-cvs/samples/
 

urlEncode

protected String urlEncode(String str)

urlDecode

protected String urlDecode(String str)


Ant Functional Tester -- see Project home page for details