org.apache.struts.util
Class RequestUtils

java.lang.Object
  extended by org.apache.struts.util.RequestUtils

public class RequestUtils
extends java.lang.Object

General purpose utility methods related to processing a servlet request in the Struts controller framework.

Version:
$Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $

Field Summary
protected static org.apache.commons.logging.Log log
          Commons Logging instance.
 
Constructor Summary
RequestUtils()
           
 
Method Summary
static java.net.URL absoluteURL(javax.servlet.http.HttpServletRequest request, java.lang.String path)
          Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request.
static java.lang.String actionIdURL(ForwardConfig forward, javax.servlet.http.HttpServletRequest request, ActionServlet servlet)
          Returns the true path of the destination action if the specified forward is an action-aliased URL.
static java.lang.String actionIdURL(java.lang.String originalPath, ModuleConfig moduleConfig, ActionServlet servlet)
          Returns the true path of the destination action if the specified forward is an action-aliased URL.
static java.lang.String actionURL(javax.servlet.http.HttpServletRequest request, ActionConfig action, java.lang.String pattern)
          Return the context-relative URL that corresponds to the specified ActionConfig, relative to the module associated with the current modules's ModuleConfig.
static java.lang.Class applicationClass(java.lang.String className)
          Return the Class object for the specified fully qualified class name, from this web application's class loader.
static java.lang.Class applicationClass(java.lang.String className, java.lang.ClassLoader classLoader)
          Return the Class object for the specified fully qualified class name, from this web application's class loader.
static java.lang.Object applicationInstance(java.lang.String className)
          Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader.
static java.lang.Object applicationInstance(java.lang.String className, java.lang.ClassLoader classLoader)
          Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader.
static ActionForm createActionForm(FormBeanConfig config, ActionServlet servlet)
          Create and return an ActionForm instance appropriate to the information in config.
static ActionForm createActionForm(javax.servlet.http.HttpServletRequest request, ActionMapping mapping, ModuleConfig moduleConfig, ActionServlet servlet)
          Create (if necessary) and return an ActionForm instance appropriate for this request.
static java.lang.StringBuffer createServerStringBuffer(java.lang.String scheme, java.lang.String server, int port)
          Return StringBuffer representing the scheme, server, and port number of the current request.
static java.lang.StringBuffer createServerUriStringBuffer(java.lang.String scheme, java.lang.String server, int port, java.lang.String uri)
          Return StringBuffer representing the scheme, server, and port number of the current request.
static java.lang.String forwardURL(javax.servlet.http.HttpServletRequest request, ForwardConfig forward)
          Return the context-relative URL that corresponds to the specified ForwardConfig.
static java.lang.String forwardURL(javax.servlet.http.HttpServletRequest request, ForwardConfig forward, ModuleConfig moduleConfig)
          Return the context-relative URL that corresponds to the specified ForwardConfig.
static java.lang.String getServletMapping(ActionServlet servlet)
          Retrieves the servlet mapping pattern for the specified ActionServlet.
static java.util.Locale getUserLocale(javax.servlet.http.HttpServletRequest request, java.lang.String locale)
          Look up and return current user locale, based on the specified parameters.
static void populate(java.lang.Object bean, javax.servlet.http.HttpServletRequest request)
          Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name against the corresponding JavaBeans "property setter" methods in the bean's class.
static void populate(java.lang.Object bean, java.lang.String prefix, java.lang.String suffix, javax.servlet.http.HttpServletRequest request)
          Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name (plus an optional prefix and/or suffix) against the corresponding JavaBeans "property setter" methods in the bean's class.
static java.lang.String printableURL(java.net.URL url)
          Compute the printable representation of a URL, leaving off the scheme/host/port part if no host is specified.
static java.lang.StringBuffer requestToServerStringBuffer(javax.servlet.http.HttpServletRequest request)
          Return StringBuffer representing the scheme, server, and port number of the current request.
static java.lang.StringBuffer requestToServerUriStringBuffer(javax.servlet.http.HttpServletRequest request)
          Return the string representing the scheme, server, and port number of the current request.
static java.net.URL requestURL(javax.servlet.http.HttpServletRequest request)
          Return the URL representing the current request.
static java.net.URL serverURL(javax.servlet.http.HttpServletRequest request)
          Return the URL representing the scheme, server, and port number of the current request.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static org.apache.commons.logging.Log log

Commons Logging instance.

Constructor Detail

RequestUtils

public RequestUtils()
Method Detail

absoluteURL

public static java.net.URL absoluteURL(javax.servlet.http.HttpServletRequest request,
                                       java.lang.String path)
                                throws java.net.MalformedURLException

Create and return an absolute URL for the specified context-relative path, based on the server and context information in the specified request.

Parameters:
request - The servlet request we are processing
path - The context-relative path (must start with '/')
Returns:
absolute URL based on context-relative path
Throws:
java.net.MalformedURLException - if we cannot create an absolute URL

applicationClass

public static java.lang.Class applicationClass(java.lang.String className)
                                        throws java.lang.ClassNotFoundException

Return the Class object for the specified fully qualified class name, from this web application's class loader.

Parameters:
className - Fully qualified class name to be loaded
Returns:
Class object
Throws:
java.lang.ClassNotFoundException - if the class cannot be found

applicationClass

public static java.lang.Class applicationClass(java.lang.String className,
                                               java.lang.ClassLoader classLoader)
                                        throws java.lang.ClassNotFoundException

Return the Class object for the specified fully qualified class name, from this web application's class loader.

Parameters:
className - Fully qualified class name to be loaded
classLoader - The desired classloader to use
Returns:
Class object
Throws:
java.lang.ClassNotFoundException - if the class cannot be found

applicationInstance

public static java.lang.Object applicationInstance(java.lang.String className)
                                            throws java.lang.ClassNotFoundException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.InstantiationException

Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class MUST have a public zero-arguments constructor.

Parameters:
className - Fully qualified class name to use
Returns:
new instance of class
Throws:
java.lang.ClassNotFoundException - if the class cannot be found
java.lang.IllegalAccessException - if the class or its constructor is not accessible
java.lang.InstantiationException - if this class represents an abstract class, an interface, an array class, a primitive type, or void
java.lang.InstantiationException - if this class has no zero-arguments constructor

applicationInstance

public static java.lang.Object applicationInstance(java.lang.String className,
                                                   java.lang.ClassLoader classLoader)
                                            throws java.lang.ClassNotFoundException,
                                                   java.lang.IllegalAccessException,
                                                   java.lang.InstantiationException

Return a new instance of the specified fully qualified class name, after loading the class from this web application's class loader. The specified class MUST have a public zero-arguments constructor.

Parameters:
className - Fully qualified class name to use
classLoader - The desired classloader to use
Returns:
new instance of class
Throws:
java.lang.ClassNotFoundException - if the class cannot be found
java.lang.IllegalAccessException - if the class or its constructor is not accessible
java.lang.InstantiationException - if this class represents an abstract class, an interface, an array class, a primitive type, or void
java.lang.InstantiationException - if this class has no zero-arguments constructor

createActionForm

public static ActionForm createActionForm(javax.servlet.http.HttpServletRequest request,
                                          ActionMapping mapping,
                                          ModuleConfig moduleConfig,
                                          ActionServlet servlet)

Create (if necessary) and return an ActionForm instance appropriate for this request. If no ActionForm instance is required, return null.

Parameters:
request - The servlet request we are processing
mapping - The action mapping for this request
moduleConfig - The configuration for this module
servlet - The action servlet
Returns:
ActionForm instance associated with this request

createActionForm

public static ActionForm createActionForm(FormBeanConfig config,
                                          ActionServlet servlet)

Create and return an ActionForm instance appropriate to the information in config.

Does not perform any checks to see if an existing ActionForm exists which could be reused.

Parameters:
config - The configuration for the Form bean which is to be created.
servlet - The action servlet
Returns:
ActionForm instance associated with this request

getServletMapping

public static java.lang.String getServletMapping(ActionServlet servlet)

Retrieves the servlet mapping pattern for the specified ActionServlet.

Returns:
the servlet mapping
Since:
Struts 1.3.6
See Also:
Globals.SERVLET_KEY

getUserLocale

public static java.util.Locale getUserLocale(javax.servlet.http.HttpServletRequest request,
                                             java.lang.String locale)

Look up and return current user locale, based on the specified parameters.

Parameters:
request - The request used to lookup the Locale
locale - Name of the session attribute for our user's Locale. If this is null, the default locale key is used for the lookup.
Returns:
current user locale
Since:
Struts 1.2

populate

public static void populate(java.lang.Object bean,
                            javax.servlet.http.HttpServletRequest request)
                     throws javax.servlet.ServletException

Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name against the corresponding JavaBeans "property setter" methods in the bean's class. Suitable conversion is done for argument types as described under convert().

Parameters:
bean - The JavaBean whose properties are to be set
request - The HTTP request whose parameters are to be used to populate bean properties
Throws:
javax.servlet.ServletException - if an exception is thrown while setting property values

populate

public static void populate(java.lang.Object bean,
                            java.lang.String prefix,
                            java.lang.String suffix,
                            javax.servlet.http.HttpServletRequest request)
                     throws javax.servlet.ServletException

Populate the properties of the specified JavaBean from the specified HTTP request, based on matching each parameter name (plus an optional prefix and/or suffix) against the corresponding JavaBeans "property setter" methods in the bean's class. Suitable conversion is done for argument types as described under setProperties.

If you specify a non-null prefix and a non-null suffix, the parameter name must match both conditions for its value(s) to be used in populating bean properties. If the request's content type is "multipart/form-data" and the method is "POST", the HttpServletRequest object will be wrapped in a MultipartRequestWrapper

Parameters:
bean - The JavaBean whose properties are to be set
prefix - The prefix (if any) to be prepend to bean property names when looking for matching parameters
suffix - The suffix (if any) to be appended to bean property names when looking for matching parameters
request - The HTTP request whose parameters are to be used to populate bean properties
Throws:
javax.servlet.ServletException - if an exception is thrown while setting property values

printableURL

public static java.lang.String printableURL(java.net.URL url)

Compute the printable representation of a URL, leaving off the scheme/host/port part if no host is specified. This will typically be the case for URLs that were originally created from relative or context-relative URIs.

Parameters:
url - URL to render in a printable representation
Returns:
printable representation of a URL

actionURL

public static java.lang.String actionURL(javax.servlet.http.HttpServletRequest request,
                                         ActionConfig action,
                                         java.lang.String pattern)

Return the context-relative URL that corresponds to the specified ActionConfig, relative to the module associated with the current modules's ModuleConfig.

Parameters:
request - The servlet request we are processing
action - ActionConfig to be evaluated
pattern - URL pattern used to map the controller servlet
Returns:
context-relative URL relative to the module
Since:
Struts 1.1

forwardURL

public static java.lang.String forwardURL(javax.servlet.http.HttpServletRequest request,
                                          ForwardConfig forward)

Return the context-relative URL that corresponds to the specified ForwardConfig. The URL is calculated based on the properties of the ForwardConfig instance as follows:

Parameters:
request - The servlet request we are processing
forward - ForwardConfig to be evaluated
Returns:
context-relative URL
Since:
Struts 1.1

forwardURL

public static java.lang.String forwardURL(javax.servlet.http.HttpServletRequest request,
                                          ForwardConfig forward,
                                          ModuleConfig moduleConfig)

Return the context-relative URL that corresponds to the specified ForwardConfig. The URL is calculated based on the properties of the ForwardConfig instance as follows:

Parameters:
request - The servlet request we are processing
forward - ForwardConfig to be evaluated
moduleConfig - Base forward on this module config.
Returns:
context-relative URL
Since:
Struts 1.2

requestURL

public static java.net.URL requestURL(javax.servlet.http.HttpServletRequest request)
                               throws java.net.MalformedURLException

Return the URL representing the current request. This is equivalent to HttpServletRequest.getRequestURL in Servlet 2.3.

Parameters:
request - The servlet request we are processing
Returns:
URL representing the current request
Throws:
java.net.MalformedURLException - if a URL cannot be created

serverURL

public static java.net.URL serverURL(javax.servlet.http.HttpServletRequest request)
                              throws java.net.MalformedURLException

Return the URL representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.

Parameters:
request - The servlet request we are processing
Returns:
URL representing the scheme, server, and port number of the current request
Throws:
java.net.MalformedURLException - if a URL cannot be created

requestToServerUriStringBuffer

public static java.lang.StringBuffer requestToServerUriStringBuffer(javax.servlet.http.HttpServletRequest request)

Return the string representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.

Parameters:
request - The servlet request we are processing
Returns:
URL representing the scheme, server, and port number of the current request
Since:
Struts 1.2.0

requestToServerStringBuffer

public static java.lang.StringBuffer requestToServerStringBuffer(javax.servlet.http.HttpServletRequest request)

Return StringBuffer representing the scheme, server, and port number of the current request. Server-relative URLs can be created by simply appending the server-relative path (starting with '/') to this.

Parameters:
request - The servlet request we are processing
Returns:
URL representing the scheme, server, and port number of the current request
Since:
Struts 1.2.0

createServerStringBuffer

public static java.lang.StringBuffer createServerStringBuffer(java.lang.String scheme,
                                                              java.lang.String server,
                                                              int port)

Return StringBuffer representing the scheme, server, and port number of the current request.

Parameters:
scheme - The scheme name to use
server - The server name to use
port - The port value to use
Returns:
StringBuffer in the form scheme: server: port
Since:
Struts 1.2.0

createServerUriStringBuffer

public static java.lang.StringBuffer createServerUriStringBuffer(java.lang.String scheme,
                                                                 java.lang.String server,
                                                                 int port,
                                                                 java.lang.String uri)

Return StringBuffer representing the scheme, server, and port number of the current request.

Parameters:
scheme - The scheme name to use
server - The server name to use
port - The port value to use
uri - The uri value to use
Returns:
StringBuffer in the form scheme: server: port
Since:
Struts 1.2.0

actionIdURL

public static java.lang.String actionIdURL(ForwardConfig forward,
                                           javax.servlet.http.HttpServletRequest request,
                                           ActionServlet servlet)

Returns the true path of the destination action if the specified forward is an action-aliased URL. This method version forms the URL based on the current request; selecting the current module if the forward does not explicitly contain a module path.

Parameters:
forward - the forward config
request - the current request
servlet - the servlet handling the current request
Returns:
the context-relative URL of the action if the forward has an action identifier; otherwise null.
Since:
Struts 1.3.6

actionIdURL

public static java.lang.String actionIdURL(java.lang.String originalPath,
                                           ModuleConfig moduleConfig,
                                           ActionServlet servlet)

Returns the true path of the destination action if the specified forward is an action-aliased URL. This method version forms the URL based on the specified module.

Parameters:
originalPath - the action-aliased path
moduleConfig - the module config for this request
servlet - the servlet handling the current request
Returns:
the context-relative URL of the action if the path has an action identifier; otherwise null.
Since:
Struts 1.3.6


Copyright © 2000-2007 Apache Software Foundation. All Rights Reserved.