com.pmdesigns.jvc
Class JVCRequestContext

java.lang.Object
  extended by com.pmdesigns.jvc.JVCRequestContext

public final class JVCRequestContext
extends java.lang.Object

This class contains state information for processing an http request. It wraps the standard HttpServletRequest, HttpServletResponse and HttpServlet objects and provides convenience methods for accessing them.

It also provides access to the 'flash' which is a Map for storing temporary key/value strings. The scope of the flash is the current request or the following request in the case of a redirect response.

It also contains some convenience methods for forming links and absolute paths.

Author:
mike dooley

Field Summary
 java.lang.String action
          The action is the last part of the servlet path (before any query arguments)
 java.lang.String controller
          The controller is the servlet path up to the action.
 java.util.Map<java.lang.String,java.lang.String> flash
          The 'flash' map for holding temporary key/value strings.
 javax.servlet.http.HttpServletRequest request
          The actual HttpServletRequest object
 javax.servlet.http.HttpServletResponse response
          The actual HttpServletResponse object
 javax.servlet.http.HttpServlet servlet
          The HttpServlet object (actually this is a JVCDispatcher)
 
Method Summary
 java.lang.String absPath(java.lang.String path)
          Convenience method to prepend the context path onto a servlet path
static java.lang.String getCachedBlock(java.lang.String key)
          Internal method used to retrieve a cached block
 java.lang.String getContextPath()
          Convenience method.
 javax.servlet.http.Cookie getCookie(java.lang.String name)
          Convenience method to get cookie by name
 java.lang.String[] getCookieNames()
          Convenience method to get all the cookie names.
 java.lang.String getFlash(java.lang.String key)
          Convenience method to retrieve a value from the flash
 java.lang.String getMethod()
          Convenience method
 java.lang.String getParam(java.lang.String name)
          Convenience method
 java.util.Map<java.lang.String,java.lang.String> getParamMap(java.lang.String name)
          Convenience method which gathers all parameters of the form '[]' and return then in a map where the keys are the strings and the values are the corresponding parameter values.
 java.lang.String[] getParamNames()
          Convenience method to get all the parameter names.
 java.lang.String[] getParamValues(java.lang.String name)
          Convenience method.
 java.lang.String getQueryString()
          Convenience method.
 java.lang.String getScheme()
          Convenience method
 java.lang.String getServerName()
          Convenience method
 int getServerPort()
          Convenience method
 java.lang.String getServletPath()
          Convenience method.
 java.lang.Object getSessionAttr(java.lang.String name)
          Convenience method
 java.lang.String[] getSessionAttrNames()
          Convenience method
 boolean isGet()
          Convenience method to check if this request is a GET
 boolean isPost()
          Convenience method to check if this request is a POST
 boolean isSecure()
          Convenience method
 java.lang.String makeLink(java.lang.String anchor, java.lang.String path)
          Convenience method to create an html anchor link tag
 java.lang.String makeLink(java.lang.String anchor, java.lang.String path, java.lang.String options)
          Convenience method to create an html anchor link tag
 void nonStandardResponse(int httpCode, java.lang.String arg)
          Cause a non-standard response (ie. redirect) to be returned.
 void redirect(java.lang.String path)
          Cause a redirect response to be returned
 void removeSessionAttr(java.lang.String name)
          Convenience method
static void setCachedBlock(java.lang.String key, java.lang.String val)
          Internal method used to store a cached block
 void setCookie(javax.servlet.http.Cookie cookie)
          Convenience method
 void setFlash(java.lang.String key, java.lang.String val)
          Convenience method to add a value to the flash
 void setSessionAttr(java.lang.String name, java.lang.Object value)
          Convenience method
 java.lang.String toString()
          For debugging
 java.lang.String toString(java.lang.String sep)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

request

public final javax.servlet.http.HttpServletRequest request
The actual HttpServletRequest object


response

public final javax.servlet.http.HttpServletResponse response
The actual HttpServletResponse object


servlet

public final javax.servlet.http.HttpServlet servlet
The HttpServlet object (actually this is a JVCDispatcher)


controller

public final java.lang.String controller
The controller is the servlet path up to the action.


action

public final java.lang.String action
The action is the last part of the servlet path (before any query arguments)


flash

public final java.util.Map<java.lang.String,java.lang.String> flash
The 'flash' map for holding temporary key/value strings.

Method Detail

isSecure

public boolean isSecure()
Convenience method

Returns:
true if the request is secure

getScheme

public java.lang.String getScheme()
Convenience method

Returns:
the request scheme, ie. http, https

getMethod

public java.lang.String getMethod()
Convenience method

Returns:
the request method, ie. GET, POST

getServerName

public java.lang.String getServerName()
Convenience method

Returns:
the server name for this request

getServerPort

public int getServerPort()
Convenience method

Returns:
the server port for this request

getContextPath

public java.lang.String getContextPath()
Convenience method. The request url path is broken into:

Returns:
the context path part of the url path
See Also:
getServletPath(), getQueryString()

getServletPath

public java.lang.String getServletPath()
Convenience method. The request url path is broken into:

Returns:
the servlet path part of the url path
See Also:
getContextPath(), getQueryString()

getQueryString

public java.lang.String getQueryString()
Convenience method. The request url path is broken into:

Returns:
the query string part of the url path
See Also:
getContextPath(), getServletPath()

getParam

public java.lang.String getParam(java.lang.String name)
Convenience method

Parameters:
name - which parameter to get
Returns:
the parameter value or null
See Also:
getParamMap(java.lang.String), getParamValues(java.lang.String), getParamNames()

getParamMap

public java.util.Map<java.lang.String,java.lang.String> getParamMap(java.lang.String name)
Convenience method which gathers all parameters of the form '[]' and return then in a map where the keys are the strings and the values are the corresponding parameter values.
 For example, if there are parameters:
   'foo[bar1]' = 'baz1'
   'foo[bar2]' = 'baz2'
 then getParamMap('foo') will returned the map:
   map['bar1'] = 'baz1'
   map['bar2'] = 'baz2'
 

Parameters:
name - the prefix to be used to select parameters
Returns:
a map of all paramters of the form 'name[]' where the strings are the keys of the map and the values are the corresponding parameter values.
See Also:
getParam(java.lang.String), getParamValues(java.lang.String), getParamNames()

getParamValues

public java.lang.String[] getParamValues(java.lang.String name)
Convenience method. Use this if you expect a parameter name to map to multiple values.

Parameters:
name - the name of the parameter(s) to get
Returns:
an array of paramter values corresponding to the indicated name
See Also:
getParam(java.lang.String), getParamMap(java.lang.String), getParamNames()

getParamNames

public java.lang.String[] getParamNames()
Convenience method to get all the parameter names.

Returns:
an array of all parameter names
See Also:
getParam(java.lang.String), getParamMap(java.lang.String), getParamValues(java.lang.String)

getSessionAttr

public java.lang.Object getSessionAttr(java.lang.String name)
Convenience method

Parameters:
name - the name of the attribute to retrieve
Returns:
the attribute associated with the indicated name or null
See Also:
setSessionAttr(java.lang.String, java.lang.Object), getSessionAttrNames()

getSessionAttrNames

public java.lang.String[] getSessionAttrNames()
Convenience method

Returns:
all the session attribute names.
See Also:
getSessionAttr(java.lang.String), setSessionAttr(java.lang.String, java.lang.Object), removeSessionAttr(java.lang.String)

setSessionAttr

public void setSessionAttr(java.lang.String name,
                           java.lang.Object value)
Convenience method

Parameters:
name - the key to store the indicated value
value - the object to be stored
See Also:
getSessionAttr(java.lang.String), removeSessionAttr(java.lang.String)

removeSessionAttr

public void removeSessionAttr(java.lang.String name)
Convenience method

Parameters:
name - the key of
See Also:
setSessionAttr(java.lang.String, java.lang.Object)

getCookie

public javax.servlet.http.Cookie getCookie(java.lang.String name)
Convenience method to get cookie by name

Parameters:
name - the name of the cookie to get
Returns:
the cookie with the indicated name, or null
See Also:
setCookie(javax.servlet.http.Cookie), getCookieNames()

getCookieNames

public java.lang.String[] getCookieNames()
Convenience method to get all the cookie names.

Returns:
an array of the names of all the cookies in the request, possibly empty
See Also:
getCookie(java.lang.String), setCookie(javax.servlet.http.Cookie)

setCookie

public void setCookie(javax.servlet.http.Cookie cookie)
Convenience method

Parameters:
cookie - the cookie to be added to the response
See Also:
getCookie(java.lang.String), getCookieNames()

nonStandardResponse

public void nonStandardResponse(int httpCode,
                                java.lang.String arg)
Cause a non-standard response (ie. redirect) to be returned.

Parameters:
httpCode - the http response code to be returned
arg - an argument or message to be returned (depends on response code)
Throws:
NonStandardResponseException - which is a subclass of RuntimeException. If you call this method inside a try/catch block make sure you re-throw the NonStandardResponseException.
See Also:
redirect(java.lang.String)

redirect

public void redirect(java.lang.String path)
Cause a redirect response to be returned

Parameters:
path - where to redirect to. If the path starts with a '/' character then its assumed that this is an absolute link and the context path will be prepended to it
Throws:
NonStandardResponseException - which is a subclass of RuntimeException. If you call this method inside a try/catch block make sure you re-throw the NonStandardResponseException.
See Also:
nonStandardResponse(int, java.lang.String)

absPath

public java.lang.String absPath(java.lang.String path)
Convenience method to prepend the context path onto a servlet path

Parameters:
path - a servlet path
Returns:
the context path plus the indicated path

makeLink

public java.lang.String makeLink(java.lang.String anchor,
                                 java.lang.String path)
Convenience method to create an html anchor link tag

Parameters:
anchor - the text in an anchor link
path - the anchor target, if the path starts with a '/' character then its assumed that this is an absolute link and the context path will be prepended to it
See Also:
makeLink(java.lang.String, java.lang.String), absPath(java.lang.String)

makeLink

public java.lang.String makeLink(java.lang.String anchor,
                                 java.lang.String path,
                                 java.lang.String options)
Convenience method to create an html anchor link tag

Parameters:
anchor - the text in an anchor link
path - the anchor target, if the path starts with a '/' character then its assumed that this is an absolute link and the context path will be prepended to it
options - extra html options to add to the anchor tag
See Also:
makeLink(java.lang.String, java.lang.String), absPath(java.lang.String)

isPost

public boolean isPost()
Convenience method to check if this request is a POST


isGet

public boolean isGet()
Convenience method to check if this request is a GET


getFlash

public java.lang.String getFlash(java.lang.String key)
Convenience method to retrieve a value from the flash

Parameters:
key - the key to use to lookup a flash value
Returns:
the flash value associated with the indicated key or null
See Also:
setFlash(java.lang.String, java.lang.String)

setFlash

public void setFlash(java.lang.String key,
                     java.lang.String val)
Convenience method to add a value to the flash

Parameters:
key - the key to use to associate with the flash value
val - the value to store
See Also:
getFlash(java.lang.String)

getCachedBlock

public static java.lang.String getCachedBlock(java.lang.String key)
Internal method used to retrieve a cached block

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

setCachedBlock

public static void setCachedBlock(java.lang.String key,
                                  java.lang.String val)
Internal method used to store a cached block

See Also:
getCachedBlock(java.lang.String)

toString

public java.lang.String toString()
For debugging

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this request context

toString

public java.lang.String toString(java.lang.String sep)