org.apache.tapestry.request
Class RequestContext

java.lang.Object
  |
  +--org.apache.tapestry.request.RequestContext
All Implemented Interfaces:
IRender

public class RequestContext
extends java.lang.Object
implements IRender

This class encapsulates all the relevant data for one request cycle of an ApplicationServlet. This includes:

It also provides methods for:

If some cases, it is necesary to provide an implementation of IRequestDecoder(often, due to a firewall). If the application specifification provides an extension named org.apache.tapestry.request-decoder then it will be used, instead of a default decoder.

This class is not a component, but does implement IRender. When asked to render (perhaps as the delegate of a Delegatorcomponent} it simply invokes write(IMarkupWriter)to display all debugging output.

This class is derived from the original class com.primix.servlet.RequestContext, part of the ServletUtils framework available from The Giant Java Tree .

Author:
Howard Lewis Ship

Constructor Summary
RequestContext(javax.servlet.http.HttpServlet servlet, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, IApplicationSpecification specification)
          Creates a RequestContext from its components.
 
Method Summary
 void addCookie(javax.servlet.http.Cookie cookie)
          Adds a Cookieto the response.
 void addCookie(java.lang.String name, java.lang.String value)
          Adds a simple Cookie.
 void cleanup()
          Invoked at the end of the request cycle to cleanup and temporary resources.
 javax.servlet.http.HttpSession createSession()
          Like getSession(), but forces the creation of the HttpSession, if necessary.
 java.lang.String getAbsoluteURL(java.lang.String URI)
          Builds an absolute URL from the given URI, using the HttpServletRequestas the source for scheme, server name and port.
 java.lang.String getAbsoluteURL(java.lang.String URI, java.lang.String scheme, java.lang.String server, int port)
          Does some easy checks to turn a path (or URI) into an absolute URL.
 IApplicationSpecification getApplicationSpecification()
           
 java.lang.Object getAttribute(java.lang.String name)
          Convienience method for getting a HttpServletRequestattribute.
 java.lang.String getContextPath()
          Returns the context path, which is the portion of the URL that identifies the web application context.
 javax.servlet.http.Cookie getCookie(java.lang.String name)
          Gets a named Cookie.
 java.lang.String getCookieValue(java.lang.String name)
          Reads the named Cookieand returns its value (if it exists), or null if it does not exist.
 java.lang.String getParameter(java.lang.String name)
          Returns the named parameter from the HttpServletRequest.
 java.lang.String[] getParameterNames()
           
 java.lang.String[] getParameters(java.lang.String name)
          For parameters that are, or are possibly, multi-valued, this method returns all the values as an array of Strings.
 javax.servlet.http.HttpServletRequest getRequest()
          Returns the request which initiated the current request cycle.
 java.lang.String getRequestURI()
          Returns the actual request URI, possibly decoded from the request.
 javax.servlet.http.HttpServletResponse getResponse()
           
 java.lang.String getScheme()
          Returns the actual scheme, possibly decoded from the request.
 java.lang.String getServerName()
          Returns the actual server name, possibly decoded from the request.
 int getServerPort()
          Returns the actual server port, possibly decoded from the request.
 javax.servlet.http.HttpServlet getServlet()
           
 javax.servlet.http.HttpSession getSession()
          Returns the HttpSession, if necessary, invoking HttpServletRequest.getSession(boolean).
 IUploadFile getUploadFile(java.lang.String name)
          Returns the named IUploadFile, if it exists, or null if it doesn't.
protected  IMultipartDecoder obtainMultipartDecoder(javax.servlet.http.HttpServlet servlet, javax.servlet.http.HttpServletRequest request)
          Invoked from the constructor to create a DefaultMultipartDecoderinstance.
 void render(IMarkupWriter writer, IRequestCycle cycle)
          Invokes write(IMarkupWriter), which is used for debugging.
 void setDecoder(IMultipartDecoder decoder)
          Sets the multipart decoder to be used for the request.
 void write(IMarkupWriter writer)
          Writes the state of the context to the writer, typically for inclusion in a HTML page returned to the user.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RequestContext

public RequestContext(javax.servlet.http.HttpServlet servlet,
                      javax.servlet.http.HttpServletRequest request,
                      javax.servlet.http.HttpServletResponse response,
                      IApplicationSpecification specification)
               throws java.io.IOException
Creates a RequestContext from its components.

Method Detail

obtainMultipartDecoder

protected IMultipartDecoder obtainMultipartDecoder(javax.servlet.http.HttpServlet servlet,
                                                   javax.servlet.http.HttpServletRequest request)
                                            throws java.io.IOException
Invoked from the constructor to create a DefaultMultipartDecoderinstance. Applications with specific upload needs may need to override this to provide a subclass instance instead. The caller will invoke IMultipartDecoder.decode(HttpServletRequest) on the returned object.

This implementation checks for application extension Tapestry.MULTIPART_DECODER_EXTENSION_NAME. If that is not defined, a shared instance of DefaultMultipartDecoderis returned.

java.io.IOException
Since:
3.0
See Also:
ApplicationServlet#createRequestContext(HttpServletRequest, HttpServletResponse)

addCookie

public void addCookie(java.lang.String name,
                      java.lang.String value)
Adds a simple Cookie. To set a Cookie with attributes, use addCookie(Cookie).


addCookie

public void addCookie(javax.servlet.http.Cookie cookie)
Adds a Cookieto the response. Once added, the Cookie will also be available to getCookie(String)method.

Cookies should only be added before invoking ServletResponse.getWriter()..


getScheme

public java.lang.String getScheme()
Returns the actual scheme, possibly decoded from the request.

Since:
2.2
See Also:
IRequestDecoder, ServletRequest.getScheme()

getServerName

public java.lang.String getServerName()
Returns the actual server name, possibly decoded from the request.

Since:
2.2
See Also:
IRequestDecoder, ServletRequest.getServerName()

getServerPort

public int getServerPort()
Returns the actual server port, possibly decoded from the request.

Since:
2.2
See Also:
IRequestDecoder, ServletRequest.getServerPort()

getRequestURI

public java.lang.String getRequestURI()
Returns the actual request URI, possibly decoded from the request.

Since:
2.2
See Also:
IRequestDecoder, HttpServletRequest.getRequestURI()

getAbsoluteURL

public java.lang.String getAbsoluteURL(java.lang.String URI)
Builds an absolute URL from the given URI, using the HttpServletRequestas the source for scheme, server name and port.

See Also:
getAbsoluteURL(String, String, String, int)

getAbsoluteURL

public java.lang.String getAbsoluteURL(java.lang.String URI,
                                       java.lang.String scheme,
                                       java.lang.String server,
                                       int port)
Does some easy checks to turn a path (or URI) into an absolute URL. We assume


getCookie

public javax.servlet.http.Cookie getCookie(java.lang.String name)
Gets a named Cookie.

Parameters:
name - The name of the Cookie.
Returns:
The Cookie, or null if no Cookie with that name exists.

getCookieValue

public java.lang.String getCookieValue(java.lang.String name)
Reads the named Cookieand returns its value (if it exists), or null if it does not exist.


getParameter

public java.lang.String getParameter(java.lang.String name)
Returns the named parameter from the HttpServletRequest.

Use getParameters(String)for parameters that may include multiple values.

This is the preferred way to obtain parameter values (rather than obtaining the HttpServletRequestitself). For form/multipart-data encoded requests, this method will still work.


getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Convienience method for getting a HttpServletRequestattribute.

Since:
2.3

getParameters

public java.lang.String[] getParameters(java.lang.String name)
For parameters that are, or are possibly, multi-valued, this method returns all the values as an array of Strings.

See Also:
getParameter(String)

getParameterNames

public java.lang.String[] getParameterNames()

getUploadFile

public IUploadFile getUploadFile(java.lang.String name)
Returns the named IUploadFile, if it exists, or null if it doesn't. Uploads require an encoding of multipart/form-data (this is specified in the form's enctype attribute). If the encoding type is not so, or if no upload matches the name, then this method returns null.


cleanup

public void cleanup()
Invoked at the end of the request cycle to cleanup and temporary resources. This is chained to the DefaultMultipartDecoder, if there is one.

Since:
2.0.1

getRequest

public javax.servlet.http.HttpServletRequest getRequest()
Returns the request which initiated the current request cycle. Note that the methods getParameter(String)and getParameters(String)should be used, rather than obtaining parameters directly from the request (since the RequestContext handles the differences between normal and multipart/form requests).


getResponse

public javax.servlet.http.HttpServletResponse getResponse()

getServlet

public javax.servlet.http.HttpServlet getServlet()

getSession

public javax.servlet.http.HttpSession getSession()
Returns the HttpSession, if necessary, invoking HttpServletRequest.getSession(boolean). However, this method will not create a session.


createSession

public javax.servlet.http.HttpSession createSession()
Like getSession(), but forces the creation of the HttpSession, if necessary.


write

public void write(IMarkupWriter writer)
Writes the state of the context to the writer, typically for inclusion in a HTML page returned to the user. This is useful when debugging. The Inspector uses this as well.


render

public void render(IMarkupWriter writer,
                   IRequestCycle cycle)
Invokes write(IMarkupWriter), which is used for debugging. Does nothing if the cycle is rewinding.

Specified by:
render in interface IRender

setDecoder

public void setDecoder(IMultipartDecoder decoder)
Sets the multipart decoder to be used for the request.

Parameters:
decoder - the multipart decoder
Since:
3.0

getApplicationSpecification

public IApplicationSpecification getApplicationSpecification()
Since:
3.1 --- but probably pretty temporary!

getContextPath

public java.lang.String getContextPath()
Returns the context path, which is the portion of the URL that identifies the web application context. This value may be blank (but not null) and does not end in a slash.

Since:
3.1