com.ibm.wbi.http
Class HttpResponse

java.lang.Object
  |
  +--com.ibm.wbi.http.HttpBase
        |
        +--com.ibm.wbi.http.HttpResponse

public class HttpResponse
extends HttpBase

This class is used to manipulate HTTP 1.0 responses. It can be constructed with default values, from a response header string, or from a MegInputStream (that comes from a RequestEvent).

The response can then be manipulated through various properties.

Finally, the response can be produced as a String, or written to the output stream. associated with a RequestEvent. see writeResponse(RequestEvent). HttpResponse does not support creating headers with multiple keys of the same name. For example, if you do two add calls on the same HttpResponse object with the same field name, the second add will override the first.


Fields inherited from class com.ibm.wbi.http.HttpBase
content, COPYRIGHT, fieldKeys, headerFields, protocolVersion
 
Constructor Summary
HttpResponse()
          Construct a default HTTP 1.0 response, which has the default parameters.
HttpResponse(MegInputStream in, boolean loadContent)
          Construct an HTTP 1.0 response from the given MegInputStream.
HttpResponse(java.lang.String header)
          Construct an HTTP 1.0 response from the given header string.
 
Method Summary
 void addCookie(java.lang.String name, java.lang.String value, java.util.Date expires, java.lang.String domain, java.lang.String path, boolean secure)
          Add a cookie to this response, which will then be stored in the browser.
 int getResponseCode()
          Access the HTTP response code (e.g.
 java.lang.String getResponseText()
          Access the HTTP response text (e.g.
 java.lang.String getServer()
          Access the name of the server that produced the response.
 boolean isCache()
          Access the Cache property.
 void loadResponseContent(MegInputStream in)
          Load the response content from the given MegInputStream into the Content property of this HTTP Response object.
static void main(java.lang.String[] args)
          Test Driver.
 void parseResponseHeader(MegInputStream in)
          Parse the response header in the given MegInputStream and set the various properties to reflect its contents.
 void parseResponseHeader(java.lang.String header)
          Parse the response header in the given string and set the various properties to reflect its contents.
protected  java.lang.String produceCurrentDate()
          Produce the current date in String format.
 java.lang.String produceHeader()
          Deprecated. Use produceResponseWithoutContent instead.
 java.lang.String produceResponseWithContent()
          Produces response header up through the double \r\n, plus any response content.
 java.lang.String produceResponseWithoutContent()
          Produce the HTTP header in the form of a String.
 void removeContentLength()
          Removes the "content-length" header field.
 void removeWindowTarget()
          Removes the "window-target" header field.
 void setCache(boolean c)
          Set the Cache property, which indicates to the browser whether this page should be cached or not.
 void setResponseCode(int c)
          Set the HTTP response code (e.g.
 void setResponseText(java.lang.String t)
          Set the HTTP response text (e.g.
 void setServer(java.lang.String s)
          Set the name of the server producing the document.
 void writeHeader(RequestEvent e)
          Deprecated. Use writeResponse( RequestEvent e ) instead.
protected  java.lang.String writeHeaderFields()
          Writes all header fields to s string.
 void writeResponse(RequestEvent e)
          Produce the HTTP header and store it in the MegOutputStream associated with this RequestEvent.
 
Methods inherited from class com.ibm.wbi.http.HttpBase
add, change, cleanupHash, get, getAll, getContent, getContentLength, getContentType, getKeys, getProtocolVersion, initialize, remove, remove, removeAll, set, setContent, setContentLength, setContentType, setProtocolVersion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HttpResponse

public HttpResponse()
Construct a default HTTP 1.0 response, which has the default parameters.

HttpResponse

public HttpResponse(MegInputStream in,
                    boolean loadContent)
Construct an HTTP 1.0 response from the given MegInputStream.
Parameters:
in - The input stream containing the header fields for the response
loadContent - If true, the content (if any) will be loaded from the input stream and loaded into the Content property of this object. Otherwise, any content will be left in the input stream. Since the content can be arbitrarily large, caution should be exercised to make sure that you don't load too much stuff into memory.

HttpResponse

public HttpResponse(java.lang.String header)
Construct an HTTP 1.0 response from the given header string.
Parameters:
header - An HTTP 1.0 response header, such as:
     HTTP/1.0 200 Ok \r\n
     content-length: 500 \r\n
     content-type: text/html \r\n
     \r\n
 
Method Detail

removeContentLength

public void removeContentLength()
Removes the "content-length" header field. This operation is useful when the content is being edited and the final length is unknown.

removeWindowTarget

public void removeWindowTarget()
Removes the "window-target" header field. The page will be directed to the current window.

setResponseCode

public void setResponseCode(int c)
Set the HTTP response code (e.g. 200 [=Ok]).
Parameters:
c - The ResponseCode.

setResponseText

public void setResponseText(java.lang.String t)
Set the HTTP response text (e.g. "Ok", "Page Not Found").
Parameters:
t - The ResponseText.

setServer

public void setServer(java.lang.String s)
Set the name of the server producing the document.
Parameters:
s - The name of the server.

setCache

public void setCache(boolean c)
Set the Cache property, which indicates to the browser whether this page should be cached or not.
Parameters:
c - if true, the browser is allowed to cache the page; if false, the browser should not

isCache

public boolean isCache()
Access the Cache property.
Returns:
Cached?
See Also:
setCache(boolean)

addCookie

public void addCookie(java.lang.String name,
                      java.lang.String value,
                      java.util.Date expires,
                      java.lang.String domain,
                      java.lang.String path,
                      boolean secure)
Add a cookie to this response, which will then be stored in the browser.
Parameters:
name - Name of the cookie (required)
value - Value of the cookie (required)
expires - When the cookie expires (may be null)
domain - The domain over which the cookie should be sent back to the server (may be null)
path - The path for which the cookie should be sent back to the server (may be null)
secure - If true, the cookie will only be passed back over secure links

getResponseCode

public int getResponseCode()
Access the HTTP response code (e.g. 200 [=Ok])
Returns:
The ResponseCode.

getResponseText

public java.lang.String getResponseText()
Access the HTTP response text (e.g. "Ok", "Page Not Found")
Returns:
The ResponseText.

getServer

public java.lang.String getServer()
Access the name of the server that produced the response.
Returns:
The server.

produceCurrentDate

protected java.lang.String produceCurrentDate()
Produce the current date in String format.
Returns:
Current data.

produceResponseWithoutContent

public java.lang.String produceResponseWithoutContent()
Produce the HTTP header in the form of a String. Replaces the deprecated method produceHeader().
Returns:
The header.

produceHeader

public java.lang.String produceHeader()
Deprecated. Use produceResponseWithoutContent instead.
Produce the HTTP header in the form of a String.
Returns:
The header.
See Also:
produceResponseWithoutContent()

produceResponseWithContent

public java.lang.String produceResponseWithContent()
Produces response header up through the double \r\n, plus any response content.
Returns:
The HTTP response string.

writeResponse

public void writeResponse(RequestEvent e)
Produce the HTTP header and store it in the MegOutputStream associated with this RequestEvent. Replaces the deprecated method writeHeader(...).
Parameters:
e - The request event.

writeHeader

public void writeHeader(RequestEvent e)
Deprecated. Use writeResponse( RequestEvent e ) instead.
Produce the HTTP header and store it in the MegOutputStream associated with this RequestEvent.
Parameters:
e - The request event.
See Also:
writeResponse( RequestEvent )

parseResponseHeader

public void parseResponseHeader(MegInputStream in)
Parse the response header in the given MegInputStream and set the various properties to reflect its contents.
Parameters:
in - The input stream.

parseResponseHeader

public void parseResponseHeader(java.lang.String header)
Parse the response header in the given string and set the various properties to reflect its contents.
Parameters:
header - The header.

loadResponseContent

public void loadResponseContent(MegInputStream in)
Load the response content from the given MegInputStream into the Content property of this HTTP Response object.
Parameters:
in - The input stream.

writeHeaderFields

protected java.lang.String writeHeaderFields()
Writes all header fields to s string. Each header field is written on a separate line.
Returns:
String containing all header fields.
Overrides:
writeHeaderFields in class HttpBase

main

public static void main(java.lang.String[] args)
Test Driver.
Parameters:
args - Command line arguments.