Version 4.10.0

hirondelle.web4j.util
Class WebUtil

Object
  extended by hirondelle.web4j.util.WebUtil

public final class WebUtil
extends Object

Static convenience methods for common web-related tasks, which eliminate code duplication.

Similar to Util, but for methods particular to the web.


Method Summary
static Object findAttribute(String aKey, HttpServletRequest aRequest)
          Find an attribute by searching request scope, session scope (if it exists), and application scope (in that order).
static String getFileExtension(String aURL)
          Returns the 'file extension' for a given URL.
static String getOriginalRequestURL(HttpServletRequest aRequest, HttpServletResponse aResponse)
          Return the original, complete URL submitted by the browser.
static String getURLWithQueryString(HttpServletRequest aRequest)
          Return HttpServletRequest.getRequestURL(), optionally concatenated with ? and HttpServletRequest.getQueryString().
static void init(ServletConfig aConfig)
          Called only upon startup, by the framework.
static boolean isValidEmailAddress(String aEmailAddress)
          Validate the form of an email address.
static String setQueryParam(String aURL, String aParamName, String aParamValue)
          Ensure a particular name-value pair is present in a URL.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

init

public static void init(ServletConfig aConfig)
Called only upon startup, by the framework.


isValidEmailAddress

public static boolean isValidEmailAddress(String aEmailAddress)
Validate the form of an email address.

Return true only if

The second condition arises since local email addresses, simply of the form "albert", for example, are valid for InternetAddress, but almost always undesired.


setQueryParam

public static String setQueryParam(String aURL,
                                   String aParamName,
                                   String aParamValue)
Ensure a particular name-value pair is present in a URL.

If the parameter does not currently exist in the URL, then the name-value pair is appended to the URL; if the parameter is already present in the URL, however, then its value is changed.

Any number of query parameters can be added to a URL, one after the other. Any special characters in aParamName and aParamValue will be escaped by this method using EscapeChars.forURL(java.lang.String).

This method is intended for cases in which an Action requires a redirect after processing, and the redirect in turn requires dynamic query parameters. (With a redirect, this is the only way to pass data to the destination page. Items placed in request scope for the original request will no longer be available to the second request caused by the redirect.)

Example 1, where a new parameter is added :

setQueryParam("blah.do", "artist", "Tom Thomson")
returns the value :
blah.do?artist=Tom+Thomson

Example 2, where an existing parameter is updated :

setQueryParam("blah.do?artist=Tom+Thomson", "artist", "A Y Jackson")
returns the value :
blah.do?artist=A+Y+Jackson

Example 3, with a parameter name of slightly different form :

setQueryParam("blah.do?Favourite+Artist=Tom+Thomson", "Favourite Artist", "A Y Jackson")
returns the value :
blah.do?Favourite+Artist=A+Y+Jackson

Parameters:
aURL - a base URL, with escaped parameter names and values
aParamName - unescaped parameter name
aParamValue - unescaped parameter value

getURLWithQueryString

public static String getURLWithQueryString(HttpServletRequest aRequest)
Return HttpServletRequest.getRequestURL(), optionally concatenated with ? and HttpServletRequest.getQueryString().

Query parameters are added only if they are present.

If the underlying method is a GET which does NOT edit the database, then presenting the return value of this method in a link is usually acceptable.

If the underlying method is a POST, or if it is a GET which (erroneously) edits the database, it is recommended that the return value of this method NOT be placed in a link.

Warning : if this method is called in JSP or custom tag, then it is likely that the original query string has been overwritten by the server, as result of an internal forward operation.


getOriginalRequestURL

public static String getOriginalRequestURL(HttpServletRequest aRequest,
                                           HttpServletResponse aResponse)
Return the original, complete URL submitted by the browser.

Session id is included in the return value.

Somewhat frustratingly, the original client request is not directly available from the Servlet API.

This implementation is based on an example in the Java Almanac.


findAttribute

public static Object findAttribute(String aKey,
                                   HttpServletRequest aRequest)
Find an attribute by searching request scope, session scope (if it exists), and application scope (in that order).

If there is no session, then this method will not create one.

If no Object corresponding to aKey is found, then null is returned.


getFileExtension

public static String getFileExtension(String aURL)
Returns the 'file extension' for a given URL.

Some example return values for this method :

URL'File Extension'
.../VacationAction.do do
.../VacationAction.fetchForChange?Id=103 fetchForChange
.../VacationAction.list?Start=Now&End=Never list
.../SomethingAction.show;jsessionid=32131?SomeId=123456 show

Parameters:
aURL - has content, and contains a '.' character (which defines the start of the 'file extension'.)

Version 4.10.0

Copyright Hirondelle Systems. Published October 19, 2013 - User Guide - All Docs.