Femto Web Server V1.4-F

dfischer.femtowebserver.httpd
Class HttpResponse

java.lang.Object
  |
  +--java.util.Dictionary
        |
        +--java.util.Hashtable
              |
              +--dfischer.femtowebserver.httpd.HttpResponse
All Implemented Interfaces:
java.lang.Cloneable, java.util.Map, java.io.Serializable

public class HttpResponse
extends java.util.Hashtable

Sets the Weblet HTTP response data (server response) and (optionally) allows a Filter to modify every HTTP response.

Use the methods print() and println() to transmit ASCII-content like HTML data to the agent (browser). For all other type of content, especially for binary data, you must use the method setContent(). Take into consideration that if you use setContent(), you must also set the right MIME type of your content data with the method setContentType().

More information about the HTTP protocol is available at http://www.w3.org

 Programming Example I:

 package mypackage;
 import dfischer.femtowebserver.httpd.*;
 
 public class TestWeblet implements HttpdWebletInterface 
 {	
     public void execute(HttpRequest req, HttpResponse res, HttpdProperties httpdProperties) throws Exception
     {
         res.println("<HTML>");
         res.println("<BODY>");
         res.println("This is a Test Weblet");
         res.println("</BODY>");
         res.println("</HTML>");		
     }
 }
 

 Programming Example II:
 
 package mypackage;
 import dfischer.femtowebserver.httpd.*;

 public class RedirectTestWeblet implements HttpdWebletInterface 
 {
     public void execute(HttpRequest req, HttpResponse res, HttpdProperties httpdProperties) throws Exception
     {
         res.addHeaderField("Location", "http://www.d-fischer.com");
         res.setStatus(302, "Found");
     }
 } 

See Also:
HttpdWebletInterface, HttpdProperties, HttpdFilter, HttpdSession, Serialized Form

Inner classes inherited from class java.util.Map
java.util.Map.Entry
 
Field Summary
static java.lang.String HTTP_DEFAULT_CONTENT_TYPE
          The default content type for the Weblet response is "TEXT/HTML" (without a content subtype).
static int HTTP_DEFAULT_STATUS_CODE
          The default HTTP status code for a Weblet is 200.
static java.lang.String HTTP_DEFAULT_STATUS_TEXT
          The default HTTP status text for a Weblet is "OK".
static java.lang.String HTTP_VERSION
          The Femto Web Server supports only the HTTP protocol version 1.0 .
 
Method Summary
 void addHeaderField(java.lang.String headerFieldName, java.lang.String headerFieldValue)
          Allows to add additional HTTP header fields inside the HTTP response header.
 void clearContent()
          Clears the content.
 void dropCookie(java.lang.String name, java.lang.String startPath)
          Allows to delete cookies, before they are sent.
 void dump()
          Dumps the HTTP response header and the response content to the log output.
 byte[] getContent()
          Returns the current content as byte[] array.
 java.lang.String getContentAsString()
          Returns the current content as string.
 java.lang.String getContentSubtype()
          Returns the current content subtype.
 java.lang.String getContentType()
          Returns the current content type, without the the content subtype.
 java.lang.String getHeaderField(java.lang.String headerFieldName)
          Returns the value of a HTTP header field name.
 java.lang.String[] getHeaderFieldNames()
          Returns a String[] array of all HTTP header field names.
 HttpdSession getSession()
          Returns the current http session (only if a session handler is active).
 int getStatus()
          Returns the current HTTP status code.
 java.lang.String getStatusText()
          Returns the current HTTP status text.
 void print(java.lang.String text)
          Appends (text) content to HTTP response.
 void println()
          Appends a <CR><NL> to the HTTP response.
 void println(java.lang.String text)
          Appends text content to the HTTP response and adds a <CR><NL>.
 void removeHeaderField(java.lang.String headerFieldName)
          Removes a HTTP header field.
 void setAdditionalHeaderField(java.lang.String headerFieldName, java.lang.String headerFieldValue)
          Deprecated. Replaced by addHeaderField()
 void setContent(byte[] content)
          Sets the content data of the HTTP response.
 void setContentType(java.lang.String mimeContentType)
          Sets the MIME content type and (optionally) the content subtype of the content data.
 void setError(int httpStatus, java.lang.String httpStatusText)
          Sets the HTTP status code and the HTTP status text, and appends a small HTML error message as content.
 void setPermanentCookie(java.lang.String name, java.lang.String value, java.lang.String path, int expiresInDays, boolean secure)
          Transfers a new permanent cookie to the agent (browser).
 void setSession(HttpdSession httpdSession)
          Allows to set a HTTP session before the request will be processed by the Femto Web Server.
 void setStatus(int httpStatus, java.lang.String httpStatusText)
          Sets the HTTP status code and the HTTP status text.
 void setTransientCookie(java.lang.String name, java.lang.String value, java.lang.String path, boolean secure)
          Transfers a new transient (session-) cookie to the agent (browser).
 void updateHeaderField(java.lang.String headerFieldName, java.lang.String headerFieldValue)
          Allows to replace a HTTP header field value inside the HTTP response header.
 
Methods inherited from class java.util.Hashtable
clear, clone, contains, containsKey, containsValue, elements, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, remove, size, toString, values
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

HTTP_VERSION

public static final java.lang.String HTTP_VERSION
The Femto Web Server supports only the HTTP protocol version 1.0 .
This means that the network connection will be closed after processing a request.

HTTP_DEFAULT_STATUS_CODE

public static final int HTTP_DEFAULT_STATUS_CODE
The default HTTP status code for a Weblet is 200.
This means that the server response is valid.
See Also:
setStatus(int, java.lang.String), setError(int, java.lang.String)

HTTP_DEFAULT_STATUS_TEXT

public static final java.lang.String HTTP_DEFAULT_STATUS_TEXT
The default HTTP status text for a Weblet is "OK".
See Also:
setStatus(int, java.lang.String), setError(int, java.lang.String)

HTTP_DEFAULT_CONTENT_TYPE

public static final java.lang.String HTTP_DEFAULT_CONTENT_TYPE
The default content type for the Weblet response is "TEXT/HTML" (without a content subtype).
See Also:
setContentType(java.lang.String)
Method Detail

setSession

public void setSession(HttpdSession httpdSession)
Allows to set a HTTP session before the request will be processed by the Femto Web Server. Used in filters.

Parameters:
httpdSession - the HTTP session
See Also:
HttpdFilter, HttpSessionCookieHandler

getSession

public HttpdSession getSession()
Returns the current http session (only if a session handler is active).

Returns:
the current http session, if a session handler is active. Or null, if no session is available.
See Also:
HttpSessionCookieHandler

setStatus

public void setStatus(int httpStatus,
                      java.lang.String httpStatusText)
Sets the HTTP status code and the HTTP status text. Use this method only if you don't want to transfer a "200 OK" to the agent (browser).
Parameters:
httpStatus - HTTP status code
httpStatusText - HTTP status text, or set this value to null, if you don't want to transfer a status text
See Also:
setError(int, java.lang.String)

getStatus

public int getStatus()
Returns the current HTTP status code. Used in filters.
Returns:
the current HTTP status code.
See Also:
HttpdFilter

getStatusText

public java.lang.String getStatusText()
Returns the current HTTP status text. Used in filters.
Returns:
the current HTTP status text.
See Also:
HttpdFilter

setError

public void setError(int httpStatus,
                     java.lang.String httpStatusText)
Sets the HTTP status code and the HTTP status text, and appends a small HTML error message as content. Use this method only if you don't want to transfer a "200 OK" to the agent (browser).

Important Hint: all content data that you have set by print(), println() or setContent() and setContentType() will be overwritten.

Parameters:
httpStatus - HTTP status code
httpStatusText - HTTP status text (must not be null).
See Also:
setStatus(int, java.lang.String)

setAdditionalHeaderField

public void setAdditionalHeaderField(java.lang.String headerFieldName,
                                     java.lang.String headerFieldValue)
Deprecated. Replaced by addHeaderField()

See Also:
addHeaderField(java.lang.String, java.lang.String)

addHeaderField

public void addHeaderField(java.lang.String headerFieldName,
                           java.lang.String headerFieldValue)
Allows to add additional HTTP header fields inside the HTTP response header.
The following header fields are set by the Femto Web Server itself and can not be added:
Parameters:
headerFieldName - the header field name to add
headerFieldValue - the value of the header field
See Also:
HttpdFilter

updateHeaderField

public void updateHeaderField(java.lang.String headerFieldName,
                              java.lang.String headerFieldValue)
Allows to replace a HTTP header field value inside the HTTP response header. Used in filters.
If the header field name doesn't already exist, the name and value will be added to the header by calling addHeaderField().
The following header fields are set by the Femto Web Server itself and can not be modified:
Parameters:
headerFieldName - the header field name whose value should be modified
headerFieldValue - the new value of the header field
See Also:
addHeaderField(java.lang.String, java.lang.String), getHeaderField(java.lang.String), HttpdFilter

getHeaderField

public java.lang.String getHeaderField(java.lang.String headerFieldName)
Returns the value of a HTTP header field name. Used in filters.
The following header fields are set by the Femto Web Server itself and can not be demanded:
Parameters:
headerFieldName - the name of the HTTP header field
Returns:
the value of the HTTP header field or null, if the header field name was not found.
See Also:
getContentType(), getContent(), getContentAsString(), addHeaderField(java.lang.String, java.lang.String), updateHeaderField(java.lang.String, java.lang.String), HttpdFilter

getHeaderFieldNames

public java.lang.String[] getHeaderFieldNames()
Returns a String[] array of all HTTP header field names. Used in filters.
The following header field names are set by the Femto Web Server itself and will never be returned:
Returns:
a String[] array of all HTTP header field names. If no header fields are found a empty String[] array is returns: the method result will never be null.
See Also:
getHeaderField(java.lang.String), getContentType(), getContent(), getContentAsString(), HttpdFilter

removeHeaderField

public void removeHeaderField(java.lang.String headerFieldName)
Removes a HTTP header field. Used in filters.
The following header fields are set by the Femto Web Server itself and can not be removed:
Parameters:
headerFieldName - the name of the HTTP header field to remove. If the field name is not found inside the HTTP response header, this method will also complete successful.
See Also:
HttpdFilter

setTransientCookie

public void setTransientCookie(java.lang.String name,
                               java.lang.String value,
                               java.lang.String path,
                               boolean secure)
Transfers a new transient (session-) cookie to the agent (browser).
Parameters:
name - the cookie name
value - the cookie value
path - the subset of URLs for which the cookie is valid. Set this value to "/" to transfer a transient cookie that is valid for all URL paths.
secure - this value must always set to false, because encrypted HTTPS/SSL connections are currently not supported.
See Also:
setPermanentCookie(java.lang.String, java.lang.String, java.lang.String, int, boolean)

setPermanentCookie

public void setPermanentCookie(java.lang.String name,
                               java.lang.String value,
                               java.lang.String path,
                               int expiresInDays,
                               boolean secure)
Transfers a new permanent cookie to the agent (browser).
Parameters:
name - the cookie name
value - the cookie value
path - the subset of URLs for which the cookie is valid. Set this value to "/" to transfer a permanent cookie that is valid for all URL paths.
expiresInDays - valid life time of that cookie in days (from now on).
secure - this value must always set to false, because encrypted HTTPS/SSL connections are currently not supported.
See Also:
setTransientCookie(java.lang.String, java.lang.String, java.lang.String, boolean)

dropCookie

public void dropCookie(java.lang.String name,
                       java.lang.String startPath)
Allows to delete cookies, before they are sent. Used in filters.
Parameters:
name - the name of the cookie(s) to delete - (only) if the startPath matches. If several cookies have te same name, all of these cookies are deleted.
startPath - start-string of the path. All cookies with the selected name - whose paths starts with this string - are deletetd. Set this value to "/" (used as wildcard) to select all cookies with the same name.

setContentType

public void setContentType(java.lang.String mimeContentType)
Sets the MIME content type and (optionally) the content subtype of the content data. The default content type is "TEXT/HTML" without a content subtype. Exaples: "IMAGE/GIF" (only content type) or "TEXT/HTML; charset=ISO-8859-4" (content type with content subtype).
Parameters:
mimeContentType - the MIME content type and (optionally) the content subtype of the content data
See Also:
HTTP_DEFAULT_CONTENT_TYPE, setContent(byte[])

getContentType

public java.lang.String getContentType()
Returns the current content type, without the the content subtype. Used in filters.
Returns:
the current content type without the content subtype
See Also:
HttpdFilter, getContentSubtype()

getContentSubtype

public java.lang.String getContentSubtype()
Returns the current content subtype. Used in filters.
Returns:
the current content subtype. Or null, if no content subtype was set.
See Also:
HttpdFilter, getContentType()

setContent

public void setContent(byte[] content)
Sets the content data of the HTTP response.

Important Hint: this method will overwrite all content data of the methods print() and println(). Consider also that you must set the appropriate content type if you use this method.

Parameters:
content - content data of the HTTP response
See Also:
setContentType(java.lang.String), print(java.lang.String), println()

print

public void print(java.lang.String text)
Appends (text) content to HTTP response. You may call this method as often as you want to append (more and more) text.

Important Hint: this method will clear all content that have already set with setContent().

Parameters:
text - text to append to the response content (must not be null)
See Also:
println(String), setContent(byte[])

println

public void println()
Appends a <CR><NL> to the HTTP response. You may call this method as often as you want to append (more and more) text.

Important Hint: this method will clear all content that have already set with setContent().

See Also:
print(java.lang.String), println(String), setContent(byte[])

println

public void println(java.lang.String text)
Appends text content to the HTTP response and adds a <CR><NL>. You may call this method as often as you want to append (more and more) text.

Important Hint: this method will clear all content that have already set with setContent().

Parameters:
text - text to append to the response content (must not be null)
See Also:
print(java.lang.String), println(), setContent(byte[])

getContent

public byte[] getContent()
Returns the current content as byte[] array. Used in filters.
Returns:
The content as byte[] array or null, if no content is available
See Also:
getContentAsString(), HttpdFilter

getContentAsString

public java.lang.String getContentAsString()
Returns the current content as string. Used in filters.
Returns:
The content as string or null, if no content is available
See Also:
getContent(), HttpdFilter

clearContent

public void clearContent()
Clears the content. Used in filters.
See Also:
HttpdFilter

dump

public void dump()
          throws java.io.IOException
Dumps the HTTP response header and the response content to the log output. Or - if the log output has been disabled - to System.out. Commonly used to debug HTTP responses. All non printable ASCII characters and also all characters greater than 126(decimal) are shown as "[<char-number>(decimal)]"
See Also:
Httpd.setLogStream(java.io.PrintStream)

Femto Web Server V1.4-F

Copyright 2002, 2003, 2006 by Ingenieurbüro David Fischer GmbH, Switzerland. All rights reserved.