org.xlightweb
Class HttpRequestWrapper

Package class diagram package HttpRequestWrapper
java.lang.Object
  extended by org.xlightweb.HttpRequestHeaderWrapper
      extended by org.xlightweb.HttpRequestWrapper
All Implemented Interfaces:
IHeader, IHttpMessage, IHttpMessageHeader, IHttpRequest, IHttpRequestHeader, IPart

public class HttpRequestWrapper
extends HttpRequestHeaderWrapper
implements IHttpRequest

Provides a convenient implementation of the HttpRequest interface that can be subclassed by developers wishing to adapt the request. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.


Field Summary
 
Fields inherited from interface org.xlightweb.IHttpMessage
CONNECT_METHOD, DEFAULT_ENCODING, DEFAULT_ENCODING_KEY, DELETE_METHOD, GET_METHOD, HEAD_METHOD, OPTIONS_METHOD, POST_METHOD, PUT_METHOD, TRACE_METHOD
 
Fields inherited from interface org.xlightweb.IHttpMessageHeader
DEFAULT_ENCODING
 
Constructor Summary
HttpRequestWrapper(IHttpRequest request)
          constructor
 
Method Summary
 void addHeader(String headername, String headervalue)
          Adds a header with the given name and value.
 void addHeaderLine(String line)
          adds a raw header line
 void addHeaderlines(String... lines)
          adds raw header lines
 boolean containsHeader(String headername)
          Returns a boolean indicating whether the named header has already been set.
 BlockingBodyDataSource getBlockingBody()
          
 BodyDataSource getBody()
          returns the blocking body
 String getCharacterEncoding()
          Returns the name of the character encoding used in the body of this message.
 String getContentType()
          Returns the MIME type of the body of the messag, or null if the type is not known.
 String getContextPath()
          Returns the portion of the request URI that indicates the context of the request.
 String getDisposition()
          returns the content disposition header of the part, or null if the type is not known.
 String getDispositionParam(String name)
          returns the content disposition param value or null if not set

see also rfc2183
 String getDispositionType()
          returns the content disposition type or null if not set

see also rfc2183
 String getHeader(String headername)
          Returns the value of the specified header as a String.
 List<String> getHeaderList(String headername)
          Returns all the values of the specified header as an List of String objects.
 Enumeration getHeaderNames()
          Returns an enumeration of all the header names.
 Set<String> getHeaderNameSet()
          Returns an set of all the header names.
 Enumeration getHeaders(String headername)
          Returns all the values of the specified header as an Enumeration of String objects.
 String getHost()
          returns the Host header parameter or null if the header is not set
 IHttpMessageHeader getMessageHeader()
          returns the message header
 NonBlockingBodyDataSource getNonBlockingBody()
          returns the non-blocking body
 IHeader getPartHeader()
          returns the part header
 String getPathInfo()
          Returns any extra path information associated with the URL.
 String getPathInfo(boolean removeSurroundingSlashs)
          Returns any extra path information associated with the URL.
 String getRequestHandlerPath()
          returns the request handler path.
 IHttpRequestHeader getRequestHeader()
          returns the request header
 String getUserAgent()
          returns the User-Agent header parameter or null if the header is not set
protected  IHeader getWrappedHeader()
           
 boolean hasBody()
          returns true if the part has a body
 void removeHeader(String headername)
          removes a header with the given name
 void removeParameter(String parameterName)
          remove a parameter
 void setContextPath(String contextPath)
          sets the context path.
 void setHeader(String headername, String headervalue)
          Sets a header with the given name and value.
 void setHost(String host)
          sets the Host header (e.g. www.gmx.com or www.gmx.com:9900).
 void setParameter(String parameterName, String parameterValue)
          sets a parameter
 void setRequestHandlerPath(String requestHandlerPath)
          sets the request handler path
 void setUserAgent(String userAgent)
          sets the User-Agent header
 String toString()
          
 
Methods inherited from class org.xlightweb.HttpRequestHeaderWrapper
addMatrixParameter, addParameter, copy, copyHeaderFrom, getAccept, getAttribute, getAttributeNames, getAttributeNameSet, getBooleanParameter, getBooleanParameter, getContentLength, getDoubleParameter, getDoubleParameter, getFloatParameter, getFloatParameter, getIntParameter, getIntParameter, getLongParameter, getLongParameter, getMatrixParameter, getMatrixParameterNameSet, getMatrixParameterValues, getMethod, getParameter, getParameter, getParameterNames, getParameterNameSet, getParameterValues, getProtocol, getProtocolVersion, getQueryString, getRemoteAddr, getRemoteHost, getRemotePort, getRequestURI, getRequestUrl, getRequiredBooleanParameter, getRequiredDoubleParameter, getRequiredFloatParameter, getRequiredIntParameter, getRequiredLongParameter, getRequiredStringParameter, getScheme, getServerName, getServerPort, getTransferEncoding, getWrappedRequestHeader, isSecure, removeHopByHopHeaders, removeMatrixParameter, setAttribute, setContentLength, setContentType, setMatrixParameter, setMethod, setRequestURI, setRequestUrl, setTransferEncoding
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xlightweb.IHttpRequest
addMatrixParameter, addParameter, getAccept, getBooleanParameter, getBooleanParameter, getDoubleParameter, getDoubleParameter, getFloatParameter, getFloatParameter, getIntParameter, getIntParameter, getLongParameter, getLongParameter, getMatrixParameter, getMatrixParameterNameSet, getMatrixParameterValues, getMethod, getParameter, getParameter, getParameterNames, getParameterNameSet, getParameterValues, getQueryString, getRemoteAddr, getRemoteHost, getRemotePort, getRequestURI, getRequestUrl, getRequiredBooleanParameter, getRequiredDoubleParameter, getRequiredFloatParameter, getRequiredIntParameter, getRequiredLongParameter, getRequiredStringParameter, getScheme, getServerName, getServerPort, isSecure, removeMatrixParameter, setMatrixParameter, setMethod, setRequestURI, setRequestUrl
 
Methods inherited from interface org.xlightweb.IHttpMessage
getAttribute, getAttributeNames, getAttributeNameSet, getCharacterEncoding, getContentLength, getContentType, getProtocol, getProtocolVersion, getTransferEncoding, removeHopByHopHeaders, setAttribute, setContentLength, setContentType, setTransferEncoding
 
Methods inherited from interface org.xlightweb.IHeader
addHeader, addHeaderLine, addHeaderlines, containsHeader, getDisposition, getDispositionParam, getDispositionType, getHeader, getHeaderList, getHeaderNames, getHeaderNameSet, getHeaders, removeHeader, setHeader
 

Constructor Detail

HttpRequestWrapper

public HttpRequestWrapper(IHttpRequest request)
constructor

Parameters:
request - the request to wrap
Method Detail

getContextPath

public String getContextPath()
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. For the default (root) context, this method returns "".

Specified by:
getContextPath in interface IHttpRequest
Returns:
the context path
See Also:
IHttpRequest.getRequestHandlerPath()

getPathInfo

public String getPathInfo()
Returns any extra path information associated with the URL. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.

Specified by:
getPathInfo in interface IHttpRequest
Returns:
a String specifying extra path information that comes after the request handler and context path but before the query string in the request URL; or null if the URL does not have any extra path information

getPathInfo

public String getPathInfo(boolean removeSurroundingSlashs)
Returns any extra path information associated with the URL. The extra path information follows the servlet path but precedes the query string

Specified by:
getPathInfo in interface IHttpRequest
Parameters:
removeSurroundingSlashs - true, if surrounding slashs wil lbe removed
Returns:
a String specifying extra path information that comes after the request handler and context path but before the query string in the request URL; or null if the URL does not have any extra path information

getRequestHandlerPath

public String getRequestHandlerPath()
returns the request handler path. By default this method returns "".

Example:

 class MyHandler implements IHttpRequestHandler {
 
    public void onRequest(IHttpExchange exchange) throws IOException,BadMessageException {
       IHttpRequest req = exchange.getRequest();
       
       StringBuilder sb = new StringBuilder();
       sb.append("path=" + req.getRequestHandlerPath() + "\r\n");
       sb.append("ctx=" + req.getContextPath() + "\r\n");
       sb.append("uri=" + req.getRequestURI() + "\r\n");
       
       exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
    }
 }

 
  ...
  Context ctx = new Context("/ctx/subctx");
  ctx.addHandler("/hdlPath/*", new MyHandler());
  HttpServer server = new HttpServer(ctx);
  server.start();
  
  HttpClient httpClient = new HttpClient();
  IHttpResponse response = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/ctx/subctx/hdlPath/test?param=value"));
  
  System.out.println(response.getBlockingBody().readString());
  // prints out:
  // >path=/hdlPath
  // >ctx=/ctx/subctx
  // >uri=/ctx/subctx/hdlPath/test

  ...
 
 

Specified by:
getRequestHandlerPath in interface IHttpRequest
Returns:
the request handler path

getRequestHeader

public IHttpRequestHeader getRequestHeader()
returns the request header

Specified by:
getRequestHeader in interface IHttpRequest
Returns:
the request header

getHost

public String getHost()
returns the Host header parameter or null if the header is not set

Specified by:
getHost in interface IHttpRequest
Specified by:
getHost in interface IHttpRequestHeader
Overrides:
getHost in class HttpRequestHeaderWrapper
Returns:
the Host header parameter or null if the header is not set

getUserAgent

public String getUserAgent()
returns the User-Agent header parameter or null if the header is not set

Specified by:
getUserAgent in interface IHttpRequest
Specified by:
getUserAgent in interface IHttpRequestHeader
Overrides:
getUserAgent in class HttpRequestHeaderWrapper
Returns:
the User-Agent header parameter or null if the header is not set

setHost

public void setHost(String host)
sets the Host header (e.g. www.gmx.com or www.gmx.com:9900). If the port value is -1, it will be removed, automatically

Specified by:
setHost in interface IHttpRequest
Specified by:
setHost in interface IHttpRequestHeader
Overrides:
setHost in class HttpRequestHeaderWrapper
Parameters:
host - the Host header

setUserAgent

public void setUserAgent(String userAgent)
sets the User-Agent header

Specified by:
setUserAgent in interface IHttpRequest
Specified by:
setUserAgent in interface IHttpRequestHeader
Overrides:
setUserAgent in class HttpRequestHeaderWrapper
Parameters:
userAgent - the User-Agent header

setContextPath

public void setContextPath(String contextPath)
sets the context path.

Specified by:
setContextPath in interface IHttpRequest
Parameters:
contextPath - the context path

setParameter

public void setParameter(String parameterName,
                         String parameterValue)
sets a parameter

Specified by:
setParameter in interface IHttpRequest
Specified by:
setParameter in interface IHttpRequestHeader
Overrides:
setParameter in class HttpRequestHeaderWrapper
Parameters:
parameterName - the parameter name
parameterValue - the parameter value

removeParameter

public void removeParameter(String parameterName)
remove a parameter

Specified by:
removeParameter in interface IHttpRequest
Specified by:
removeParameter in interface IHttpRequestHeader
Overrides:
removeParameter in class HttpRequestHeaderWrapper
Parameters:
parameterName - the parameter name

setRequestHandlerPath

public void setRequestHandlerPath(String requestHandlerPath)
sets the request handler path

Specified by:
setRequestHandlerPath in interface IHttpRequest
Parameters:
requestHandlerPath - the request handler path

getBlockingBody

public BlockingBodyDataSource getBlockingBody()
                                       throws IOException

Specified by:
getBlockingBody in interface IPart
Throws:
IOException

getBody

public BodyDataSource getBody()
                       throws IOException
Description copied from interface: IPart
returns the blocking body

Specified by:
getBody in interface IPart
Returns:
the body or null if part is bodyless
Throws:
IOException

getMessageHeader

public IHttpMessageHeader getMessageHeader()
returns the message header

Specified by:
getMessageHeader in interface IHttpMessage
Returns:
the message header

getPartHeader

public IHeader getPartHeader()
returns the part header

Specified by:
getPartHeader in interface IPart
Returns:
the part header

getNonBlockingBody

public NonBlockingBodyDataSource getNonBlockingBody()
                                             throws IOException
returns the non-blocking body

Specified by:
getNonBlockingBody in interface IPart
Returns:
the body or null if parte is bodyless
Throws:
IOException

hasBody

public boolean hasBody()
returns true if the part has a body

Specified by:
hasBody in interface IPart
Returns:
true, if the part has a body

toString

public String toString()

Overrides:
toString in class HttpRequestHeaderWrapper

getWrappedHeader

protected IHeader getWrappedHeader()

addHeader

public void addHeader(String headername,
                      String headervalue)
Adds a header with the given name and value. This method allows headers to have multiple values.

Specified by:
addHeader in interface IHeader
Parameters:
headername - the name of the header
headervalue - the additional header value. If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)

addHeaderLine

public void addHeaderLine(String line)
adds a raw header line

Specified by:
addHeaderLine in interface IHeader
Parameters:
line - the headerline

addHeaderlines

public void addHeaderlines(String... lines)
adds raw header lines

Specified by:
addHeaderlines in interface IHeader
Parameters:
lines - the headerlines

containsHeader

public boolean containsHeader(String headername)
Returns a boolean indicating whether the named header has already been set.

Specified by:
containsHeader in interface IHeader
Returns:
true if the named header has already been set; false otherwise

getCharacterEncoding

public String getCharacterEncoding()
Returns the name of the character encoding used in the body of this message. If the header defines no encoding, the default encoding will be returned

Specified by:
getCharacterEncoding in interface IHeader
Returns:
a String containing the name of the character encoding

getContentType

public String getContentType()
Returns the MIME type of the body of the messag, or null if the type is not known.

Specified by:
getContentType in interface IHeader
Returns:
a String containing the name of the MIME type of the message, or null if the type is not known

getDisposition

public String getDisposition()
returns the content disposition header of the part, or null if the type is not known.

see also rfc2183

Specified by:
getDisposition in interface IHeader
Returns:
a String containing the name of the content disposition of the part, or null if the type is not known

getDispositionType

public String getDispositionType()
returns the content disposition type or null if not set

see also rfc2183

Specified by:
getDispositionType in interface IHeader
Returns:
the content disposition type or null if not set

getDispositionParam

public String getDispositionParam(String name)
returns the content disposition param value or null if not set

see also rfc2183

Specified by:
getDispositionParam in interface IHeader
Parameters:
name - the parameter name
Returns:
the content disposition param value or null if not set

getHeader

public String getHeader(String headername)
Returns the value of the specified header as a String. If the part did not include a header of the specified name, this method returns null. If there are multiple headers with the same name, this method returns the first head in the part.

Specified by:
getHeader in interface IHeader
Parameters:
headername - a String specifying the header name
Returns:
a String containing the value of the requested header, or null if the part does not have a header of that name

getHeaderList

public List<String> getHeaderList(String headername)
Returns all the values of the specified header as an List of String objects.

Some headers, such as Accept-Language can be set by part producer as several headers each with a different value rather than sending the header as a comma separated list.

Specified by:
getHeaderList in interface IHeader
Parameters:
headername - a String specifying the header name
Returns:
an List containing the values of the requested header. If the part does not have any headers of that name return an empty enumeration.

getHeaderNameSet

public Set<String> getHeaderNameSet()
Returns an set of all the header names. If the part has no headers, this method returns an empty Set.

Specified by:
getHeaderNameSet in interface IHeader
Returns:
an Set of all the header names

getHeaderNames

public Enumeration getHeaderNames()
Returns an enumeration of all the header names. If the part has no headers, this method returns an empty enumeration.

Specified by:
getHeaderNames in interface IHeader
Returns:
an enumeration of all the header names

getHeaders

public Enumeration getHeaders(String headername)
Returns all the values of the specified header as an Enumeration of String objects.

Some headers, such as Accept-Language can be set by part producer as several headers each with a different value rather than sending the header as a comma separated list.

Specified by:
getHeaders in interface IHeader
Parameters:
headername - a String specifying the header name
Returns:
an Enumeration containing the values of the requested header. If the part does not have any headers of that name return an empty enumeration.

removeHeader

public void removeHeader(String headername)
removes a header with the given name

Specified by:
removeHeader in interface IHeader
Parameters:
headername - the name of the header

setHeader

public void setHeader(String headername,
                      String headervalue)
Sets a header with the given name and value. If the header had already been set, the new value overwrites the previous one.

Specified by:
setHeader in interface IHeader
Parameters:
headername - the name of the header
headervalue - the header value If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)