public abstract class PortletUtils
extends java.lang.Object
Constructor and Description |
---|
PortletUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
clearAllRenderParameters(ActionResponse response)
Clear all the render parameters from the
ActionResponse . |
static void |
exposeRequestAttributes(PortletRequest request,
java.util.Map<java.lang.String,?> attributes)
Expose the given Map as request attributes, using the keys as attribute names
and the values as corresponding attribute values.
|
static Cookie |
getCookie(PortletRequest request,
java.lang.String name)
Retrieve the first cookie with the given name.
|
static <T> T |
getNativeRequest(PortletRequest request,
java.lang.Class<T> requiredType)
Return an appropriate request object of the specified type, if available,
unwrapping the given request as far as necessary.
|
static <T> T |
getNativeResponse(PortletResponse response,
java.lang.Class<T> requiredType)
Return an appropriate response object of the specified type, if available,
unwrapping the given response as far as necessary.
|
static java.lang.Object |
getOrCreateSessionAttribute(PortletSession session,
java.lang.String name,
java.lang.Class<?> clazz)
Get the specified session attribute under the
PortletSession.PORTLET_SCOPE ,
creating and setting a new attribute if no existing found. |
static java.lang.Object |
getOrCreateSessionAttribute(PortletSession session,
java.lang.String name,
java.lang.Class<?> clazz,
int scope)
Get the specified session attribute in the given scope,
creating and setting a new attribute if no existing found.
|
static java.util.Map<java.lang.String,java.lang.Object> |
getParametersStartingWith(PortletRequest request,
java.lang.String prefix)
Return a map containing all parameters with the given prefix.
|
static java.lang.String |
getRealPath(PortletContext portletContext,
java.lang.String path)
Return the real path of the given path within the web application,
as provided by the portlet container.
|
static java.lang.Object |
getRequiredSessionAttribute(PortletRequest request,
java.lang.String name)
Check the given request for a session attribute of the given name
under the
PortletSession.PORTLET_SCOPE . |
static java.lang.Object |
getRequiredSessionAttribute(PortletRequest request,
java.lang.String name,
int scope)
Check the given request for a session attribute of the given name in the given scope.
|
static java.lang.Object |
getSessionAttribute(PortletRequest request,
java.lang.String name)
Check the given request for a session attribute of the given name under the
PortletSession.PORTLET_SCOPE . |
static java.lang.Object |
getSessionAttribute(PortletRequest request,
java.lang.String name,
int scope)
Check the given request for a session attribute of the given name in the given scope.
|
static java.lang.Object |
getSessionMutex(PortletSession session)
Return the best available mutex for the given session:
that is, an object to synchronize on for the given session.
|
static java.lang.String |
getSubmitParameter(PortletRequest request,
java.lang.String name)
Return the full name of a specific input type="submit" parameter
if it was sent in the request, either via a button (directly with name)
or via an image (name + ".x" or name + ".y").
|
static int |
getTargetPage(PortletRequest request,
java.lang.String paramPrefix,
int currentPage)
Return the target page specified in the request.
|
static java.io.File |
getTempDir(PortletContext portletContext)
Return the temporary directory for the current web application,
as provided by the portlet container.
|
static boolean |
hasSubmitParameter(PortletRequest request,
java.lang.String name)
Check if a specific input type="submit" parameter was sent in the request,
either via a button (directly with name) or via an image (name + ".x" or
name + ".y").
|
static void |
passAllParametersToRenderPhase(ActionRequest request,
ActionResponse response)
Pass all the action request parameters to the render phase by putting them into
the action response object.
|
static void |
serveResource(ResourceRequest request,
ResourceResponse response,
PortletContext context)
Serve the resource as specified in the given request to the given response,
using the PortletContext's request dispatcher.
|
static void |
setSessionAttribute(PortletRequest request,
java.lang.String name,
java.lang.Object value)
Set the session attribute with the given name to the given value under the
PortletSession.PORTLET_SCOPE . |
static void |
setSessionAttribute(PortletRequest request,
java.lang.String name,
java.lang.Object value,
int scope)
Set the session attribute with the given name to the given value in the given scope.
|
public static java.io.File getTempDir(PortletContext portletContext)
portletContext
- the portlet context of the web applicationpublic static java.lang.String getRealPath(PortletContext portletContext, java.lang.String path) throws java.io.FileNotFoundException
Prepends a slash if the path does not already start with a slash,
and throws a FileNotFoundException
if the path cannot
be resolved to a resource (in contrast to
PortletContext's {@code getRealPath}
,
which simply returns null
).
portletContext
- the portlet context of the web applicationpath
- the relative path within the web applicationjava.io.FileNotFoundException
- if the path cannot be resolved to a resourcePortletContext.getRealPath(java.lang.String)
public static java.lang.Object getSessionAttribute(PortletRequest request, java.lang.String name)
PortletSession.PORTLET_SCOPE
.
Returns null
if there is no session or if the session has no such attribute in that scope.
Does not create a new session if none has existed before!request
- current portlet requestname
- the name of the session attributenull
if not foundpublic static java.lang.Object getSessionAttribute(PortletRequest request, java.lang.String name, int scope)
null
if there is no session or if the session has no such attribute in that scope.
Does not create a new session if none has existed before!request
- current portlet requestname
- the name of the session attributescope
- session scope of this attributenull
if not foundpublic static java.lang.Object getRequiredSessionAttribute(PortletRequest request, java.lang.String name) throws java.lang.IllegalStateException
PortletSession.PORTLET_SCOPE
.
Throws an exception if there is no session or if the session has
no such attribute in that scope.
Does not create a new session if none has existed before!
request
- current portlet requestname
- the name of the session attributejava.lang.IllegalStateException
- if the session attribute could not be foundpublic static java.lang.Object getRequiredSessionAttribute(PortletRequest request, java.lang.String name, int scope) throws java.lang.IllegalStateException
Does not create a new session if none has existed before!
request
- current portlet requestname
- the name of the session attributescope
- session scope of this attributejava.lang.IllegalStateException
- if the session attribute could not be foundpublic static void setSessionAttribute(PortletRequest request, java.lang.String name, java.lang.Object value)
PortletSession.PORTLET_SCOPE
.
Removes the session attribute if value is null
, if a session existed at all.
Does not create a new session if not necessary!request
- current portlet requestname
- the name of the session attributevalue
- the value of the session attributepublic static void setSessionAttribute(PortletRequest request, java.lang.String name, java.lang.Object value, int scope)
null
, if a session existed at all.
Does not create a new session if not necessary!request
- current portlet requestname
- the name of the session attributevalue
- the value of the session attributescope
- session scope of this attributepublic static java.lang.Object getOrCreateSessionAttribute(PortletSession session, java.lang.String name, java.lang.Class<?> clazz) throws java.lang.IllegalArgumentException
PortletSession.PORTLET_SCOPE
,
creating and setting a new attribute if no existing found. The given class
needs to have a public no-arg constructor.
Useful for on-demand state objects in a web tier, like shopping carts.session
- current portlet sessionname
- the name of the session attributeclazz
- the class to instantiate for a new attributejava.lang.IllegalArgumentException
- if the session attribute could not be instantiatedpublic static java.lang.Object getOrCreateSessionAttribute(PortletSession session, java.lang.String name, java.lang.Class<?> clazz, int scope) throws java.lang.IllegalArgumentException
session
- current portlet sessionname
- the name of the session attributeclazz
- the class to instantiate for a new attributescope
- the session scope of this attributejava.lang.IllegalArgumentException
- if the session attribute could not be instantiatedpublic static java.lang.Object getSessionMutex(PortletSession session)
Returns the session mutex attribute if available; usually,
this means that the
HttpSessionMutexListener
needs to be defined in web.xml
. Falls back to the
PortletSession
itself if no mutex attribute found.
The session mutex is guaranteed to be the same object during
the entire lifetime of the session, available under the key defined
by the WebUtils.SESSION_MUTEX_ATTRIBUTE
constant. It serves as a safe reference to synchronize on for locking
on the current session.
In many cases, the PortletSession
reference
itself is a safe mutex as well, since it will always be the same
object reference for the same active logical session. However, this is
not guaranteed across different servlet containers; the only 100% safe
way is a session mutex.
session
- the HttpSession to find a mutex fornull
)WebUtils.SESSION_MUTEX_ATTRIBUTE
,
HttpSessionMutexListener
public static <T> T getNativeRequest(PortletRequest request, java.lang.Class<T> requiredType)
request
- the portlet request to introspectrequiredType
- the desired type of request objectnull
if none
of that type is availablepublic static <T> T getNativeResponse(PortletResponse response, java.lang.Class<T> requiredType)
response
- the portlet response to introspectrequiredType
- the desired type of response objectnull
if none
of that type is availablepublic static void exposeRequestAttributes(PortletRequest request, java.util.Map<java.lang.String,?> attributes)
request
- current portlet requestattributes
- the attributes Mappublic static Cookie getCookie(PortletRequest request, java.lang.String name)
request
- current portlet requestname
- cookie namenull
if none is foundpublic static boolean hasSubmitParameter(PortletRequest request, java.lang.String name)
request
- current portlet requestname
- name of the parameterWebUtils.SUBMIT_IMAGE_SUFFIXES
public static java.lang.String getSubmitParameter(PortletRequest request, java.lang.String name)
request
- current portlet requestname
- name of the parameterWebUtils.SUBMIT_IMAGE_SUFFIXES
public static java.util.Map<java.lang.String,java.lang.Object> getParametersStartingWith(PortletRequest request, java.lang.String prefix)
For example, with a prefix of "spring_", "spring_param1" and "spring_param2" result in a Map with "param1" and "param2" as keys.
Similar to portlet
PortletRequest.getParameterMap()
,
but more flexible.
request
- portlet request in which to look for parametersprefix
- the beginning of parameter names
(if this is null
or the empty string, all parameters will match)PortletRequest.getParameterNames()
,
PortletRequest.getParameterValues(java.lang.String)
,
PortletRequest.getParameterMap()
public static int getTargetPage(PortletRequest request, java.lang.String paramPrefix, int currentPage)
request
- current portlet requestparamPrefix
- the parameter prefix to check for
(e.g. "_target" for parameters like "_target1" or "_target2")currentPage
- the current page, to be returned as fallback
if no target page specifiedpublic static void passAllParametersToRenderPhase(ActionRequest request, ActionResponse response)
sendRedirect
.request
- the current action requestresponse
- the current action responseStateAwareResponse.setRenderParameter(java.lang.String, java.lang.String)
public static void clearAllRenderParameters(ActionResponse response)
ActionResponse
.
This may not be called when the action will call
sendRedirect
.response
- the current action responseStateAwareResponse.setRenderParameters(java.util.Map<java.lang.String, java.lang.String[]>)
public static void serveResource(ResourceRequest request, ResourceResponse response, PortletContext context) throws PortletException, java.io.IOException
This is roughly equivalent to Portlet 2.0 GenericPortlet.
request
- the current resource requestresponse
- the current resource responsecontext
- the current Portlet's PortletContextPortletException
- propagated from Portlet API's forward methodjava.io.IOException
- propagated from Portlet API's forward method