fc.web.servlet
Class WebUtil

java.lang.Object
  extended by fc.web.servlet.WebUtil

public final class WebUtil
extends Object

Misc web related utility functions


Constructor Summary
WebUtil()
           
 
Method Summary
static String absoluteEncPath(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String path)
          URL encodes and returns the path obtained via absolutePath(javax.servlet.http.HttpServletRequest, java.lang.String).
static String absolutePath(javax.servlet.http.HttpServletRequest req, String path)
          Creates an absolute path starting from the web document root directory.
static void clientRedirect(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String location)
          Redirects the browser to the specified URL via a client side redirect URL.
static void debugRequestParams(javax.servlet.http.HttpServletRequest req)
          writes all request params received in the specied request to System.out.
static void debugRequestParams(javax.servlet.http.HttpServletRequest req, OutputStream out)
          writes all request params received in the specied request to the specified output stream.
static void forward(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res, String URL)
          Redirects the request (server side redirect) to the specified relative URL via the ServletRequest.getRequestDispatcher(java.lang.String).
static Object getAttribute(javax.servlet.http.HttpServletRequest request, String name, String backup)
          Gets the specified attribute from the request object.
static Object getAttribute(javax.servlet.ServletContext context, String name, String backup)
          Gets the specified attribute from the servlet context.
static boolean getBooleanParameter(javax.servlet.http.HttpServletRequest request, String name)
          Convenience method that returns the specified parameter as a boolean value.
static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest req, String cookieName)
          Returns the cookie with the specified name or null if no cookie was found.
static String getParam(javax.servlet.http.HttpServletRequest request, String name, String backup)
          Gets the specified parameter from the request object.
static String getParam(javax.servlet.ServletContext context, String name, String backup)
          Gets the specified initialization parameter from the servlet context.
static String getParam(javax.servlet.Servlet servlet, String name, String backup)
          Gets the specified initialization parameter for the specified servlet (via it's ServletConfig).
static StringBuffer getRequestURL(javax.servlet.http.HttpServletRequest req)
           
static Object getRequiredAttribute(javax.servlet.http.HttpServletRequest request, String name)
          Gets the specified required attribute from the request object.
static Object getRequiredAttribute(javax.servlet.ServletContext context, String name)
          Gets the specified required attribute from the servlet context.
static String getRequiredParam(javax.servlet.http.HttpServletRequest request, String name)
          Gets the specified required parameter from the request object.
static String getRequiredParam(javax.servlet.ServletContext context, String name)
          Gets the specified required initialization parameter from the servlet context.
static String getRequiredParam(javax.servlet.Servlet servlet, String name)
          Gets the specified initialization parameter for the specified servlet (via it's ServletConfig).
static String getSessionString(javax.servlet.http.HttpSession session, String name)
          Returns a HttpSession attribute as a String or null if the attribute was absent.
static boolean isIOSClient(javax.servlet.http.HttpServletRequest req)
          Returns true if the client is an iOS mobile device.
static boolean isMobileClient(javax.servlet.http.HttpServletRequest req)
          Returns true if the client is a mobile device
static boolean isSelected(javax.servlet.http.HttpServletRequest request, String name)
          The returned value will be true if the specified parameter is present in the request and is a non-null non-empty string (of any value).
static Map parseQuery(String query)
           
static boolean requestCancelled(javax.servlet.http.HttpServletRequest req)
          Returns true only if a request parameter with the name confirm is seen.
static boolean requestConfirmed(javax.servlet.http.HttpServletRequest req)
          Returns true only if a request parameter with the name cancel is seen.
static void setExpireHeaders(javax.servlet.http.HttpServletResponse res)
          set expire headers to cover all cases
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WebUtil

public WebUtil()
Method Detail

getRequiredParam

public static final String getRequiredParam(javax.servlet.ServletContext context,
                                            String name)
                                     throws javax.servlet.ServletException
Gets the specified required initialization parameter from the servlet context. If this param is not found, throws a permanent UnavailableException.

Throws:
javax.servlet.ServletException

getParam

public static final String getParam(javax.servlet.ServletContext context,
                                    String name,
                                    String backup)
Gets the specified initialization parameter from the servlet context. If this param is not found, then the specified backup value is returned.


getRequiredParam

public static final String getRequiredParam(javax.servlet.Servlet servlet,
                                            String name)
                                     throws javax.servlet.ServletException
Gets the specified initialization parameter for the specified servlet (via it's ServletConfig). Note, servlet init parameters are servlet specific whereas context parameters are shared by all servlets within that context. If this param is not found, throws a permanent UnavailableException.

Throws:
javax.servlet.ServletException

getParam

public static final String getParam(javax.servlet.Servlet servlet,
                                    String name,
                                    String backup)
Gets the specified initialization parameter for the specified servlet (via it's ServletConfig). Note, servlet init parameters are servlet specific whereas context parameters are shared by all servlets within that context. If this param is not found, then the specified backup value is returned.


getRequiredParam

public static final String getRequiredParam(javax.servlet.http.HttpServletRequest request,
                                            String name)
                                     throws javax.servlet.ServletException
Gets the specified required parameter from the request object. If this param is not found, throws a ServletException

Throws:
javax.servlet.ServletException

getParam

public static final String getParam(javax.servlet.http.HttpServletRequest request,
                                    String name,
                                    String backup)
Gets the specified parameter from the request object. If this param is not found, returns the backup value.


isSelected

public static final boolean isSelected(javax.servlet.http.HttpServletRequest request,
                                       String name)
The returned value will be true if the specified parameter is present in the request and is a non-null non-empty string (of any value). This is useful for radioboxes and checkboxes.


getBooleanParameter

public static final boolean getBooleanParameter(javax.servlet.http.HttpServletRequest request,
                                                String name)
Convenience method that returns the specified parameter as a boolean value. The returned value will be converted via the Boolean.parseBoolean(String) (for true, the value should be non-null and equal ignore case to "true").


getRequiredAttribute

public static final Object getRequiredAttribute(javax.servlet.ServletContext context,
                                                String name)
                                         throws javax.servlet.ServletException
Gets the specified required attribute from the servlet context. If this attribute is not found, throws a permanent UnavailableException.

Throws:
javax.servlet.ServletException

getAttribute

public static final Object getAttribute(javax.servlet.ServletContext context,
                                        String name,
                                        String backup)
                                 throws javax.servlet.ServletException
Gets the specified attribute from the servlet context. If this attribute is not found, then the specified backup value is returned.

Throws:
javax.servlet.ServletException

getRequiredAttribute

public static final Object getRequiredAttribute(javax.servlet.http.HttpServletRequest request,
                                                String name)
                                         throws javax.servlet.ServletException
Gets the specified required attribute from the request object. If this param is not found, throws a ServletException.

Throws:
javax.servlet.ServletException

getAttribute

public static final Object getAttribute(javax.servlet.http.HttpServletRequest request,
                                        String name,
                                        String backup)
                                 throws javax.servlet.ServletException
Gets the specified attribute from the request object. If this attribute is not found, returns the backup value.

Throws:
javax.servlet.ServletException

getSessionString

public static final String getSessionString(javax.servlet.http.HttpSession session,
                                            String name)
Returns a HttpSession attribute as a String or null if the attribute was absent.

Parameters:
name - the name of the session attribute
Throws:
ClassCastException - if the attribute was not a String
NullPointerException - if the specified session or name parameters were null

getCookie

public static javax.servlet.http.Cookie getCookie(javax.servlet.http.HttpServletRequest req,
                                                  String cookieName)
Returns the cookie with the specified name or null if no cookie was found.


forward

public static final void forward(javax.servlet.http.HttpServletRequest req,
                                 javax.servlet.http.HttpServletResponse res,
                                 String URL)
                          throws javax.servlet.ServletException,
                                 IOException
Redirects the request (server side redirect) to the specified relative URL via the ServletRequest.getRequestDispatcher(java.lang.String). See RequestDispatcher.forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) for restrictions while forwarding to another page (in particular, the response must not have been committed before calling this method).

The calling code must have a return statement immediately after calling this method, otherwise RequestDispatching will probably not work as intended.

Parameters:
req - the current request
res - the current response
URL - The pathname specified may be relative, although it cannot extend outside the current servlet context. If the path begins with a "/" it is interpreted as starting from the root of the current context. (i.e., the webapp context, if present, does not need to be specified).
Throws:
javax.servlet.ServletException
IOException

clientRedirect

public static final void clientRedirect(javax.servlet.http.HttpServletRequest req,
                                        javax.servlet.http.HttpServletResponse res,
                                        String location)
                                 throws javax.servlet.ServletException,
                                        IOException
Redirects the browser to the specified URL via a client side redirect URL. Automatically creates a full URL (including the webapp context path) suitable for this purpose. This method is a thin wrapper around Response.sentRedirect

Session information is preserved if using container provided URL-based sessions.

If the response has already been committed, this method throws an IllegalStateException. After using this method, the response should be considered to be committed and should not be written to.

Parameters:
req - the current request
res - the current response
location - See javax.servlet.http.HttpResponse#sendRedirect. Essentially, the location can be relative to the specified request's URI or relative to the context root if it contains a leading '/'
Throws:
javax.servlet.ServletException
IOException

absolutePath

public static final String absolutePath(javax.servlet.http.HttpServletRequest req,
                                        String path)
Creates an absolute path starting from the web document root directory. It does so by prepending the webapp context path (if any) of the specified request to the specified path. The path is not URL encoded with session informtion (see absoluteEncPath(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String)).

This method should be used for all HTML links (in html pages and forms) and also for client side redirects. This ensures the ability to move the calling code to a different web app (other than the default root ("/") web app). If it's certain that there will always only be 1 web app (the default) then calling this method is not necessary.

Note also that server-side redirects and includes (with RequestDispatcher) should *not* use this method since all such server side redirects etc automatically work as expected inside the web app context and are always relative to the web app context itself.

Parameters:
req - the current HttpServletRequest
path - an absolute path starting from the root of the context associated with the request. If the current webapp (context) is /foo and the specified path is /bar, then this method will return /foo/bar

absoluteEncPath

public static final String absoluteEncPath(javax.servlet.http.HttpServletRequest req,
                                           javax.servlet.http.HttpServletResponse res,
                                           String path)
URL encodes and returns the path obtained via absolutePath(javax.servlet.http.HttpServletRequest, java.lang.String).


getRequestURL

public static final StringBuffer getRequestURL(javax.servlet.http.HttpServletRequest req)
Returns:
the full requested URL including any query parameters. (the RequestURL method in HttpServletRequest does not return the query part of the URL)

requestCancelled

public static final boolean requestCancelled(javax.servlet.http.HttpServletRequest req)
Returns true only if a request parameter with the name confirm is seen. Forms can set a submit button with this name to signal a confirm request.


requestConfirmed

public static final boolean requestConfirmed(javax.servlet.http.HttpServletRequest req)
Returns true only if a request parameter with the name cancel is seen. Forms can set a submit button with this name to signal a cancel request.


debugRequestParams

public static final void debugRequestParams(javax.servlet.http.HttpServletRequest req)
writes all request params received in the specied request to System.out. Useful for debugging.


debugRequestParams

public static final void debugRequestParams(javax.servlet.http.HttpServletRequest req,
                                            OutputStream out)
writes all request params received in the specied request to the specified output stream. Useful for debugging.


setExpireHeaders

public static final void setExpireHeaders(javax.servlet.http.HttpServletResponse res)
set expire headers to cover all cases


parseQuery

public static Map parseQuery(String query)
                      throws UnsupportedEncodingException
Throws:
UnsupportedEncodingException

isMobileClient

public static boolean isMobileClient(javax.servlet.http.HttpServletRequest req)
Returns true if the client is a mobile device


isIOSClient

public static boolean isIOSClient(javax.servlet.http.HttpServletRequest req)
Returns true if the client is an iOS mobile device.