com.marringtons.net.HTTP
Class Request

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

public class Request
extends Object

This class reads a request from a remote client and evaluate it so that the calling program can retrieve the different forms of information contained in the request. Information in a request includes the HTTP header, the request URL used, arguments, cookies and parameters.

 Request request = new Request( socket);
 Header header = request.getHeader();
 String accept = header.get( "Accept");
 accept = request.getHeader( "Accept");
 Properties parameters = request.getParameters();
 int iv = parameters.get( "integerValue", 0);
 boolean bv = parameters.get( "booleanValue", false);
 if (request.parameterExists( "action")) action( request.getParameter( "action"));
 
 // To record a session
 request.startRecorder();
 String tape = request.stopRecorder();
 

Author:
Paul Marrington

Constructor Summary
Request()
           
 
Method Summary
static String decode(String encoded)
          Decode a string sent from the browser.
 boolean fetch()
          Fetch and process a browser request from any source.
 boolean fetch(String requestString)
          Called ny unit tests to create an artificial request from a string.
 String fullURL(String partial)
          Given a partial URL, return a fully defined URL (http://server.com/path/url).
 String[] getArguments()
          Return the query string with special characters processed and split into arguments.
 String getCookie(String key)
          Get a cookie from the request.
 Header getHeader()
          Retrieve a pointer to the header sent with this request.
 String getHeader(String key)
          Retrieve an item from the HTTP header that came as part of the request.
 String getHeader(String key, String defaultValue)
          Retrieve an item from the HTTP header that came as part of the request - with a default if the item does not exist.
 String getMethod()
          Get the method set in the header - usually GET or POST.
 String getParameter(String key)
          Return the value for a single key.
 String getParameter(String key, String defaultValue)
          Return the value for a single key.
 String[] getParameterList(String key)
          Return the values for a single key.
 Properties getParameters()
          Divide up the arguments into key/value pairs (name=value&name=value).
 String getProtocol()
          The protocol os HTTP/1.1 or HTTP/1.0.
 String getQueryString()
          Retrieve the query string in raw form (with & between name=value pairs).
 String getReferer()
          Return the page that referred this page.
 String getURL()
          The URL retrieved is relative to the server root.
 boolean parameterExists(String key)
          Confirm whether a particular parameter is in the request.
 void removeParameter(String key)
          Remove a parameter from the list so that it will not show up on a subsequent search or list.
 void setQueryString(String queryString)
          Set the query string - used to reset after unsuccessfully trying defaults like CGI?
 String setQueryString(String[] arguments)
          encode and Set the query string given the individual arguments.
 String setQueryString(String[] arguments, int startIndex)
          encode and Set the query string given the individual arguments.
 void setReader(BufferedReader newReader)
          Another data source raw reader (typically socket)
 void setReader(Reader newReader)
          Another data source raw reader (typically socket)
 void setReader(Socket socket)
          Before fetching request queries from the browser we will need to tell it which socket to use.
 void setReader(String requestString)
          Junit tests often provide the request data in a string.
 void setURL(String url)
          While looking for the file/class to run/serve on the local system we need to change the URL - usually to add bits (like /index.html).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Request

public Request()
Method Detail

fetch

public boolean fetch()
              throws IOException
Fetch and process a browser request from any source.

Returns:
true if OK, false if socket stream closed
Throws:
IOException

fetch

public boolean fetch(String requestString)
Called ny unit tests to create an artificial request from a string.

Parameters:
requestString - contents of request for testing.
Returns:
true if all worked OK, false on IO error.

fullURL

public String fullURL(String partial)
Given a partial URL, return a fully defined URL (http://server.com/path/url).

Parameters:
partial - may be relative (url.html), partial (/bin/url.cgi) or absolute (http://localhost:1958/bin/url.cgi)
Returns:
relative URL.

getArguments

public String[] getArguments()
Return the query string with special characters processed and split into arguments.

Returns:
arguments/parameters as name=value strings.

decode

public static String decode(String encoded)
Decode a string sent from the browser. (percent)2f becomes /, etc.

Parameters:
encoded - string from browser or other HTTP source.
Returns:
decoded string.

getHeader

public Header getHeader()
Retrieve a pointer to the header sent with this request.

Returns:
name/value pairs in the HTTP header (like Host and Cache-Control).

getHeader

public String getHeader(String key)
Retrieve an item from the HTTP header that came as part of the request.

Parameters:
key - for entry in request header.
Returns:
value for key in header or empty string if no entry

getReferer

public String getReferer()
Return the page that referred this page.

Returns:
referer string returned by the browser.

getHeader

public String getHeader(String key,
                        String defaultValue)
Retrieve an item from the HTTP header that came as part of the request - with a default if the item does not exist.

Parameters:
key - for entry in request header.
defaultValue - if key not in header.
Returns:
value for key in header or defaultValue if no entry

getMethod

public String getMethod()
Get the method set in the header - usually GET or POST.

Returns:
String - GET, HEAD, POST, etc

getParameter

public String getParameter(String key)
Return the value for a single key. An empty key and a non-existent one will both return empty strings.

Parameters:
key - for entry in request header.
Returns:
value for key or null if it does not exist.
See Also:
parameterExists(String)

getParameter

public String getParameter(String key,
                           String defaultValue)
Return the value for a single key. An empty key and a non-existent one will both return empty strings. Use getParameters() when retrieving values other than strings (integers, boolean, etc).

Parameters:
key - for entry in request header.
defaultValue - if key not in header.
Returns:
- value for key or default if the key does not exist
See Also:
parameterExists(String)

getParameterList

public String[] getParameterList(String key)
Return the values for a single key. Each time the parameter is used it will extend the array of results. Very handy for a table of <input> fields.

Parameters:
key - for entry in request header.
Returns:
an array of resulting elements (zero long if none exist).

getParameters

public Properties getParameters()
Divide up the arguments into key/value pairs (name=value&name=value).

Returns:
a map of the parameter name/value pairs.

removeParameter

public void removeParameter(String key)
Remove a parameter from the list so that it will not show up on a subsequent search or list.

Parameters:
key - Name of parameter to remove.

getProtocol

public String getProtocol()
The protocol os HTTP/1.1 or HTTP/1.0.

Returns:
File protocol (HTTP/1.1)

getQueryString

public String getQueryString()
Retrieve the query string in raw form (with & between name=value pairs).

Returns:
query string in raw form as found on command or in post.

getURL

public String getURL()
The URL retrieved is relative to the server root. Does not include queryString (? and after).

Returns:
URL relative to HTTP route (i.e. /index.html)

parameterExists

public boolean parameterExists(String key)
Confirm whether a particular parameter is in the request.

Parameters:
key - for entry in request header.
Returns:
true if the query string contains a named parameter - whether or not it has a value

setQueryString

public void setQueryString(String queryString)
Set the query string - used to reset after unsuccessfully trying defaults like CGI?index.cgi.

Parameters:
queryString - value to be set as query string.

setQueryString

public String setQueryString(String[] arguments)
encode and Set the query string given the individual arguments.

Parameters:
arguments - name=value pairs to become http arguments on command line.
Returns:
a pointer to the newly set query string.

setQueryString

public String setQueryString(String[] arguments,
                             int startIndex)
encode and Set the query string given the individual arguments.

Parameters:
arguments - name=value pairs to become http arguments on command line.
startIndex - the starting index in arguments (used to skip command if 1st in string)
Returns:
a pointer to the newly set query string.

setReader

public void setReader(BufferedReader newReader)
Another data source raw reader (typically socket)

Parameters:
newReader - stack another data source (typically socket) to be read from.

setReader

public void setReader(Reader newReader)
Another data source raw reader (typically socket)

Parameters:
newReader - stack another data source (typically socket) to be read from.

setReader

public void setReader(Socket socket)
Before fetching request queries from the browser we will need to tell it which socket to use.

Parameters:
socket - stack a socket data source to be read from.

setReader

public void setReader(String requestString)
Junit tests often provide the request data in a string. So do recording replays.

Parameters:
requestString - for testing.

setURL

public void setURL(String url)
While looking for the file/class to run/serve on the local system we need to change the URL - usually to add bits (like /index.html). Only sets query string if it is changes - otherwise leaves it as was.

Parameters:
url - to update the URL that made the request.

getCookie

public String getCookie(String key)
Get a cookie from the request. Does not create a map of cookies as it is meant for VERY infrequent use. In fact it should only get the session and nothing else.

Parameters:
key - cookie key.
Returns:
the cookie contents or null of cookie does not exist.


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