org.owasp.webscarab.model
Class Message

java.lang.Object
  extended by org.owasp.webscarab.model.Message
Direct Known Subclasses:
Request, Response

public class Message
extends java.lang.Object

Message is a class that is used to represent the bulk of an HTTP message, namely the headers, and (possibly null) body. Messages should not be instantiated directly, but should rather be created by a derived class, namely Request or Response.

Author:
rdawes

Field Summary
protected  java.util.logging.Logger _logger
           
 
Constructor Summary
Message()
          Message is a class that is used to represent the bulk of an HTTP message, namely the headers, and (possibly null) body.
 
Method Summary
 void addHeader(NamedValue header)
           
 void addHeader(java.lang.String name, java.lang.String value)
          Adds a header with the specified name and value.
 java.lang.String deleteHeader(java.lang.String name)
          Removes a header
 boolean equals(java.lang.Object obj)
           
 void flushContentStream()
          reads all content from the content stream if one exists.
 byte[] getContent()
          getContent returns the message body that accompanied the request.
 java.lang.String getHeader(java.lang.String name)
          Returns the value of the requested header
 java.lang.String[] getHeaderNames()
          Returns an array of header names
 NamedValue[] getHeaders()
          returns the header names and their values
 java.lang.String[] getHeaders(java.lang.String name)
          Returns all the values of the requested header, if there are multiple items
protected  java.lang.String getLine(java.lang.StringBuffer buffer)
          a protected method to read a line up to and including the CR or CRLF.
 void parse(java.lang.StringBuffer buffer)
          Instructs the class to read the headers and content from the supplied StringBuffer N.B.
 void read(java.io.InputStream is)
          Instructs the class to read the headers from the InputStream, and assign the InputStream as the contentStream, from which the body of the message can be read.
protected  java.lang.String readLine(java.io.InputStream is)
          a protected method to read a line up to and including the CR or CRLF.
 void setContent(byte[] bytes)
          Sets the content of the message body.
 void setHeader(NamedValue header)
           
 void setHeader(java.lang.String name, java.lang.String value)
          sets the value of a header.
 void setHeaders(NamedValue[] headers)
          sets the headers
 void setNoBody()
          sets the message to not have a body.
 java.lang.String toString()
          Returns a String representation of the message, *including* the message body.
 java.lang.String toString(java.lang.String crlf)
          Returns a String representation of the message, *including* the message body.
 void write(java.io.OutputStream os)
          Writes the Message headers and content to the supplied OutputStream
 void write(java.io.OutputStream os, java.lang.String crlf)
          Writes the Message headers and content to the supplied OutputStream
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_logger

protected java.util.logging.Logger _logger
Constructor Detail

Message

public Message()
Message is a class that is used to represent the bulk of an HTTP message, namely the headers, and (possibly null) body. Messages should not be instantiated directly, but should rather be created by a derived class, namely Request or Response.

Method Detail

read

public void read(java.io.InputStream is)
          throws java.io.IOException
Instructs the class to read the headers from the InputStream, and assign the InputStream as the contentStream, from which the body of the message can be read.

Parameters:
is - the InputStream to read the Message headers and body from
Throws:
java.io.IOException - Propagates any IOExceptions thrown by the InputStream read methods

write

public void write(java.io.OutputStream os)
           throws java.io.IOException
Writes the Message headers and content to the supplied OutputStream

Parameters:
os - The OutputStream to write the Message headers and content to
Throws:
java.io.IOException - any IOException thrown by the supplied OutputStream, or any IOException thrown by the InputStream from which this Message was originally read (if any)

write

public void write(java.io.OutputStream os,
                  java.lang.String crlf)
           throws java.io.IOException
Writes the Message headers and content to the supplied OutputStream

Parameters:
os - The OutputStream to write the Message headers and content to
crlf - the line ending to use for the headers
Throws:
java.io.IOException - any IOException thrown by the supplied OutputStream, or any IOException thrown by the InputStream from which this Message was originally read (if any)

parse

public void parse(java.lang.StringBuffer buffer)
           throws java.text.ParseException
Instructs the class to read the headers and content from the supplied StringBuffer N.B. The "Content-length" header is updated automatically to reflect the true size of the content, if one exists

Parameters:
buffer - The StringBuffer to parse the headers and content from. This buffer is "consumed" i.e. characters are removed from the buffer as the Message is parsed.
Throws:
java.text.ParseException - if there is an error parsing the Message structure

toString

public java.lang.String toString()
Returns a String representation of the message, *including* the message body.

Overrides:
toString in class java.lang.Object
Returns:
The string representation of the message

toString

public java.lang.String toString(java.lang.String crlf)
Returns a String representation of the message, *including* the message body. Lines of the header are separated by the supplied "CarriageReturnLineFeed" string.

Parameters:
crlf - The required line separator string
Returns:
a String representation of the Message.

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
sets the value of a header. This overwrites any previous values of headers with the same name.

Parameters:
name - the name of the header (without a colon)
value - the value of the header

setHeader

public void setHeader(NamedValue header)

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Adds a header with the specified name and value. This preserves any previous headers with the same name, and adds another header with the same name.

Parameters:
name - the name of the header (without a colon)
value - the value of the header

addHeader

public void addHeader(NamedValue header)

deleteHeader

public java.lang.String deleteHeader(java.lang.String name)
Removes a header

Parameters:
name - the name of the header (without a colon)
Returns:
the value of the header that was deleted

getHeaderNames

public java.lang.String[] getHeaderNames()
Returns an array of header names

Returns:
an array of the header names

getHeader

public java.lang.String getHeader(java.lang.String name)
Returns the value of the requested header

Parameters:
name - the name of the header (without a colon)
Returns:
the value of the header in question (null if the header did not exist)

getHeaders

public java.lang.String[] getHeaders(java.lang.String name)
Returns all the values of the requested header, if there are multiple items

Parameters:
name - the name of the header (without a colon)
Returns:
the values of the header in question (null if the header did not exist)

getHeaders

public NamedValue[] getHeaders()
returns the header names and their values

Returns:
an array of NamedValue's representing the names and values of the headers

setHeaders

public void setHeaders(NamedValue[] headers)
sets the headers

Parameters:
table - a two dimensional array of Strings, where table[i][0] is the header name and table[i][1] is the header value

readLine

protected java.lang.String readLine(java.io.InputStream is)
                             throws java.io.IOException
a protected method to read a line up to and including the CR or CRLF. We don't use a BufferedInputStream so that we don't read further than we should i.e. into the message body, or next message!

Parameters:
is - The InputStream to read the line from
Returns:
the line that was read, WITHOUT the CR or CRLF
Throws:
java.io.IOException - if an IOException occurs while reading from the supplied InputStream

getLine

protected java.lang.String getLine(java.lang.StringBuffer buffer)
a protected method to read a line up to and including the CR or CRLF. Removes the line from the supplied StringBuffer.

Parameters:
buffer - the StringBuffer to read the line from
Returns:
the line that was read, WITHOUT the CR or CRLF

getContent

public byte[] getContent()
getContent returns the message body that accompanied the request. if the message was read from an InputStream, it reads the content from the InputStream and returns a copy of it. If the message body was chunked, or gzipped (according to the headers) it returns the unchunked and unzipped content.

Returns:
Returns a byte array containing the message body

flushContentStream

public void flushContentStream()
reads all content from the content stream if one exists. Bytes read are stored internally, and returned via getContent()


setNoBody

public void setNoBody()
sets the message to not have a body. This is typical for a CONNECT request or response, which should not read any body.


setContent

public void setContent(byte[] bytes)
Sets the content of the message body. If the message headers indicate that the content is gzipped, the content is automatically compressed

Parameters:
bytes - a byte array containing the message body

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object