com.marringtons.net.HTTP
Class Header

java.lang.Object
  extended bycom.marringtons.net.HTTP.Header

public class Header
extends Object

This class provides basic support for interpreting and creating HTTP headers. HTTP requests and responses include a header that is basically a list of name/value pairs that ends in a blank line. This class can be used by client and server components to both receive and send a header.

 Header header = new Header(clientSocket.getReader());
 int contentLength = header.contentLength();
 
 Request request = new Request(socket);
 Header header = request.getHeader();
 String accept = header.get("Accept");
 header.clear();
 header.add("Server", "Adept - http://adept.marringtons.com");
 header.add("P3P", "CP=\"NOI?DSP?COR?CURa?TAIa?OUR?STP?PRE\"");
 header.add("Cache-Control", "max-age=" + maxAge); // 1 day
 writer.write(header.toString());
 

Author:
Paul Marrington

Field Summary
 boolean staticContent
          Set true if data to send is static content.
 
Constructor Summary
Header()
          Default initialisation.
Header(BufferedReader in)
          Initialise a header and read it in from a stream at the same time.
 
Method Summary
 boolean add(String line)
          Used by connection to add header values to request object.
 void add(String key, int value)
          Used to add arbitrary integer values to the header.
 void add(String key, String value)
          Used to add arbitrary string values to the header.
 void clear()
          Clear the header of content - typically ready to load a new header.
 int contentLength()
          Use to retrieve the length of the message content to be sent.
 void contentLength(int length)
          Set the content length - presumably for a write.
 boolean exists(String key)
          See if the header includes a specific entry.
 String get(String key)
          Get a header entry - and an empty string if it doesn't exist.
 String get(String key, String defaultValue)
          Get a header entry - and a specified default value if it doesn't exist.
 void read(BufferedReader in)
          Read the header, leaving the stream ready to read the body on request.
 String[] toArray()
          Used by exec as the CGI environment.
 String toString()
          Convert HTTP header to a viable string ready to send to the browser.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

staticContent

public boolean staticContent
Set true if data to send is static content. Then when header is written, cache controls. are informed that they can keep a local copy.

Constructor Detail

Header

public Header()
Default initialisation.


Header

public Header(BufferedReader in)
       throws IOException
Initialise a header and read it in from a stream at the same time.

Parameters:
in - stream reader to retrieve header from.
Throws:
IOException - if the stream has a problem.
Method Detail

exists

public boolean exists(String key)
See if the header includes a specific entry.

Parameters:
key - to look for in header
Returns:
True if header contains a key of this name

get

public String get(String key)
Get a header entry - and an empty string if it doesn't exist.

Parameters:
key - to look for in header
Returns:
value for key in header or empty string if no entry

get

public String get(String key,
                  String defaultValue)
Get a header entry - and a specified default value if it doesn't exist.

Parameters:
key - to look for in header
defaultValue - if the key does not exist.
Returns:
- value for key in header or defaultValue if no entry

toArray

public String[] toArray()
Used by exec as the CGI environment.

Returns:
Each string holds one "name=value" pair

add

public boolean add(String line)
Used by connection to add header values to request object.

Parameters:
line - of the form "key: value"
Returns:
true if request in valid form

add

public void add(String key,
                String value)
Used to add arbitrary string values to the header.

Parameters:
key - to add to header
value - to add for key to header

add

public void add(String key,
                int value)
Used to add arbitrary integer values to the header.

Parameters:
key - to add to header
value - to add for key to header

read

public void read(BufferedReader in)
          throws IOException
Read the header, leaving the stream ready to read the body on request.

Parameters:
in - reader to retrieve header from server.
Throws:
IOException

toString

public String toString()
Convert HTTP header to a viable string ready to send to the browser.

Returns:
String representation of a HTTP header.
See Also:
Object.toString()

clear

public void clear()
Clear the header of content - typically ready to load a new header.


contentLength

public int contentLength()
Use to retrieve the length of the message content to be sent.

Returns:
length of the expected content (-1 if none found)

contentLength

public void contentLength(int length)
Set the content length - presumably for a write.

Parameters:
length - of the http response


Copyright © 2005 Paul Marrington http://library.marringtons.com