public class RequestUtils
extends java.lang.Object
General purpose utility methods related to processing a servlet request in the Struts controller framework.
Modifier and Type | Field and Description |
---|---|
protected static java.util.regex.Pattern |
CLASS_ACCESS_PATTERN
Pattern matching 'class' access.
|
protected static org.apache.commons.logging.Log |
log
Commons Logging instance.
|
Constructor and Description |
---|
RequestUtils() |
Modifier and Type | Method and Description |
---|---|
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.
|
protected static org.apache.commons.logging.Log log
Commons Logging instance.
protected static final java.util.regex.Pattern CLASS_ACCESS_PATTERN
Pattern matching 'class' access.
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.
request
- The servlet request we are processingpath
- The context-relative path (must start with '/')java.net.MalformedURLException
- if we cannot create an absolute URLpublic 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.
className
- Fully qualified class name to be loadedjava.lang.ClassNotFoundException
- if the class cannot be foundpublic 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.
className
- Fully qualified class name to be loadedclassLoader
- The desired classloader to usejava.lang.ClassNotFoundException
- if the class cannot be foundpublic 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.
className
- Fully qualified class name to usejava.lang.ClassNotFoundException
- if the class cannot be foundjava.lang.IllegalAccessException
- if the class or its constructor is not
accessiblejava.lang.InstantiationException
- if this class represents an abstract
class, an interface, an array class, a
primitive type, or voidjava.lang.InstantiationException
- if this class has no zero-arguments
constructorpublic 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.
className
- Fully qualified class name to useclassLoader
- The desired classloader to usejava.lang.ClassNotFoundException
- if the class cannot be foundjava.lang.IllegalAccessException
- if the class or its constructor is not
accessiblejava.lang.InstantiationException
- if this class represents an abstract
class, an interface, an array class, a
primitive type, or voidjava.lang.InstantiationException
- if this class has no zero-arguments
constructorpublic 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
.
request
- The servlet request we are processingmapping
- The action mapping for this requestmoduleConfig
- The configuration for this moduleservlet
- The action servletpublic 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.
config
- The configuration for the Form bean which is to be
created.servlet
- The action servletpublic static java.lang.String getServletMapping(ActionServlet servlet)
Retrieves the servlet mapping pattern for the specified ActionServlet
.
Globals.SERVLET_KEY
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.
request
- The request used to lookup the Localelocale
- Name of the session attribute for our user's Locale. If
this is null
, the default locale key is
used for the lookup.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()
.
bean
- The JavaBean whose properties are to be setrequest
- The HTTP request whose parameters are to be used to
populate bean propertiesjavax.servlet.ServletException
- if an exception is thrown while setting
property valuespublic 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
bean
- The JavaBean whose properties are to be setprefix
- The prefix (if any) to be prepend to bean property names
when looking for matching parameterssuffix
- The suffix (if any) to be appended to bean property
names when looking for matching parametersrequest
- The HTTP request whose parameters are to be used to
populate bean propertiesjavax.servlet.ServletException
- if an exception is thrown while setting
property valuespublic 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.
url
- URL to render in a printable representationpublic 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
.
request
- The servlet request we are processingaction
- ActionConfig to be evaluatedpattern
- URL pattern used to map the controller servletpublic 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:
contextRelative
property is set, it is assumed
that the path
property contains a path that is already
context-relative:
path
property value starts with a slash, it is
returned unmodified.path
property value
does not start with a slash, a slash is prepended.forwardPattern
property from the
ControllerConfig
for the application module used to
process this request. If no pattern was configured, default to a
pattern of $M$P
, which is compatible with the hard-coded
mapping behavior in Struts 1.0.forwardPattern
, performing the
following substitutions:
path
property of
the specified ForwardConfig
, prepended with a slash if it does
not start with one.request
- The servlet request we are processingforward
- ForwardConfig to be evaluatedpublic 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:
contextRelative
property is set, it is assumed
that the path
property contains a path that is already
context-relative: path
property value starts with a slash, it is
returned unmodified.path
property value
does not start with a slash, a slash is prepended.forwardPattern
property from the
ControllerConfig
for the application module used to
process this request. If no pattern was configured, default to a
pattern of $M$P
, which is compatible with the hard-coded
mapping behavior in Struts 1.0.forwardPattern
, performing the
following substitutions: path
property of
the specified ForwardConfig
, prepended with a slash if it does
not start with one.request
- The servlet request we are processingforward
- ForwardConfig to be evaluatedmoduleConfig
- Base forward on this module config.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.
request
- The servlet request we are processingjava.net.MalformedURLException
- if a URL cannot be createdpublic 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.
request
- The servlet request we are processingjava.net.MalformedURLException
- if a URL cannot be createdpublic 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.
request
- The servlet request we are processingpublic 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.
request
- The servlet request we are processingpublic 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.
scheme
- The scheme name to useserver
- The server name to useport
- The port value to usepublic 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.
scheme
- The scheme name to useserver
- The server name to useport
- The port value to useuri
- The uri value to usepublic 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.
forward
- the forward configrequest
- the current requestservlet
- the servlet handling the current requestnull
.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.
originalPath
- the action-aliased pathmoduleConfig
- the module config for this requestservlet
- the servlet handling the current requestnull
.Copyright © 2000–2024 Apache Software Foundation. All rights reserved.