org.springframework.web.portlet.mvc.annotation
Class AnnotationMethodHandlerAdapter

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.portlet.context.PortletApplicationObjectSupport
          extended by org.springframework.web.portlet.handler.PortletContentGenerator
              extended by org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter
All Implemented Interfaces:
BeanFactoryAware, ApplicationContextAware, Ordered, PortletContextAware, HandlerAdapter

public class AnnotationMethodHandlerAdapter
extends PortletContentGenerator
implements HandlerAdapter, Ordered, BeanFactoryAware

Implementation of the HandlerAdapter interface that maps handler methods based on portlet modes, action/render phases and request parameters expressed through the RequestMapping annotation.

Supports request parameter binding through the RequestParam annotation. Also supports the ModelAttribute annotation for exposing model attribute values to the view, as well as InitBinder for binder initialization methods and SessionAttributes for automatic session management of specific attributes.

This adapter can be customized through various bean properties. A common use case is to apply shared binder initialization logic through a custom WebBindingInitializer.

Since:
2.5
Author:
Juergen Hoeller, Arjen Poutsma
See Also:
setWebBindingInitializer(org.springframework.web.bind.support.WebBindingInitializer), setSessionAttributeStore(org.springframework.web.bind.support.SessionAttributeStore)

Nested Class Summary
private  class AnnotationMethodHandlerAdapter.PortletHandlerMethodInvoker
          Portlet-specific subclass of HandlerMethodInvoker.
private static class AnnotationMethodHandlerAdapter.PortletHandlerMethodResolver
          Portlet-specific subclass of HandlerMethodResolver.
private static class AnnotationMethodHandlerAdapter.RequestMappingInfo
          Holder for request mapping metadata.
 
Field Summary
private  ConfigurableBeanFactory beanFactory
           
private  int cacheSecondsForSessionAttributeHandlers
           
private  WebArgumentResolver[] customArgumentResolvers
           
private  ModelAndViewResolver[] customModelAndViewResolvers
           
private  BeanExpressionContext expressionContext
           
static java.lang.String IMPLICIT_MODEL_RENDER_PARAMETER
           
static java.lang.String IMPLICIT_MODEL_SESSION_ATTRIBUTE
           
private  java.util.Map<java.lang.Class<?>,AnnotationMethodHandlerAdapter.PortletHandlerMethodResolver> methodResolverCache
           
private  int order
           
private  ParameterNameDiscoverer parameterNameDiscoverer
           
private  SessionAttributeStore sessionAttributeStore
           
private  boolean synchronizeOnSession
           
private  WebBindingInitializer webBindingInitializer
           
 
Fields inherited from class org.springframework.context.support.ApplicationObjectSupport
logger
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
AnnotationMethodHandlerAdapter()
           
 
Method Summary
protected  PortletRequestDataBinder createBinder(PortletRequest request, java.lang.Object target, java.lang.String objectName)
          Template method for creating a new PortletRequestDataBinder instance.
protected  ModelAndView doHandle(PortletRequest request, PortletResponse response, java.lang.Object handler)
           
private  AnnotationMethodHandlerAdapter.PortletHandlerMethodResolver getMethodResolver(java.lang.Object handler)
          Build a HandlerMethodResolver for the given handler type.
 int getOrder()
          Return the order value of this object, with a higher value meaning greater in terms of sorting.
 void handleAction(ActionRequest request, ActionResponse response, java.lang.Object handler)
          Use the given handler to handle this action request.
 void handleEvent(EventRequest request, EventResponse response, java.lang.Object handler)
          Use the given handler to handle this event request.
 ModelAndView handleRender(RenderRequest request, RenderResponse response, java.lang.Object handler)
          Use the given handler to handle this render request.
 ModelAndView handleResource(ResourceRequest request, ResourceResponse response, java.lang.Object handler)
          Use the given handler to handle this resource request.
private  ModelAndView invokeHandlerMethod(PortletRequest request, PortletResponse response, java.lang.Object handler, ExtendedModelMap implicitModel)
           
 void setBeanFactory(BeanFactory beanFactory)
          Callback that supplies the owning factory to a bean instance.
 void setCacheSecondsForSessionAttributeHandlers(int cacheSecondsForSessionAttributeHandlers)
          Cache content produced by @SessionAttributes annotated handlers for the given number of seconds.
 void setCustomArgumentResolver(WebArgumentResolver argumentResolver)
          Set a custom WebArgumentResolver to use for special method parameter types.
 void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers)
          Set one or more custom WebArgumentResolvers to use for special method parameter types.
 void setCustomModelAndViewResolver(ModelAndViewResolver customModelAndViewResolver)
          Set a custom ModelAndViewResolvers to use for special method return types.
 void setCustomModelAndViewResolvers(ModelAndViewResolver[] customModelAndViewResolvers)
          Set one or more custom ModelAndViewResolvers to use for special method return types.
 void setOrder(int order)
          Specify the order value for this HandlerAdapter bean.
 void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)
          Set the ParameterNameDiscoverer to use for resolving method parameter names if needed (e.g.
 void setSessionAttributeStore(SessionAttributeStore sessionAttributeStore)
          Specify the strategy to store session attributes with.
 void setSynchronizeOnSession(boolean synchronizeOnSession)
          Set if controller execution should be synchronized on the session, to serialize parallel invocations from the same client.
 void setWebBindingInitializer(WebBindingInitializer webBindingInitializer)
          Specify a WebBindingInitializer which will apply pre-configured configuration to every DataBinder that this controller uses.
 boolean supports(java.lang.Object handler)
          Given a handler instance, return whether or not this HandlerAdapter can support it.
 
Methods inherited from class org.springframework.web.portlet.handler.PortletContentGenerator
applyCacheSeconds, cacheForSeconds, check, checkAndPrepare, checkAndPrepare, getCacheSeconds, isRequireSession, preventCaching, setCacheSeconds, setRequireSession
 
Methods inherited from class org.springframework.web.portlet.context.PortletApplicationObjectSupport
getPortletContext, getTempDir, isContextRequired, setPortletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, initApplicationContext, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IMPLICIT_MODEL_SESSION_ATTRIBUTE

public static final java.lang.String IMPLICIT_MODEL_SESSION_ATTRIBUTE

IMPLICIT_MODEL_RENDER_PARAMETER

public static final java.lang.String IMPLICIT_MODEL_RENDER_PARAMETER
See Also:
Constant Field Values

webBindingInitializer

private WebBindingInitializer webBindingInitializer

sessionAttributeStore

private SessionAttributeStore sessionAttributeStore

cacheSecondsForSessionAttributeHandlers

private int cacheSecondsForSessionAttributeHandlers

synchronizeOnSession

private boolean synchronizeOnSession

parameterNameDiscoverer

private ParameterNameDiscoverer parameterNameDiscoverer

customArgumentResolvers

private WebArgumentResolver[] customArgumentResolvers

customModelAndViewResolvers

private ModelAndViewResolver[] customModelAndViewResolvers

order

private int order

beanFactory

private ConfigurableBeanFactory beanFactory

expressionContext

private BeanExpressionContext expressionContext

methodResolverCache

private final java.util.Map<java.lang.Class<?>,AnnotationMethodHandlerAdapter.PortletHandlerMethodResolver> methodResolverCache
Constructor Detail

AnnotationMethodHandlerAdapter

public AnnotationMethodHandlerAdapter()
Method Detail

setWebBindingInitializer

public void setWebBindingInitializer(WebBindingInitializer webBindingInitializer)
Specify a WebBindingInitializer which will apply pre-configured configuration to every DataBinder that this controller uses.


setSessionAttributeStore

public void setSessionAttributeStore(SessionAttributeStore sessionAttributeStore)
Specify the strategy to store session attributes with.

Default is DefaultSessionAttributeStore, storing session attributes in the PortletSession, using the same attribute name as in the model.


setCacheSecondsForSessionAttributeHandlers

public void setCacheSecondsForSessionAttributeHandlers(int cacheSecondsForSessionAttributeHandlers)
Cache content produced by @SessionAttributes annotated handlers for the given number of seconds. Default is 0, preventing caching completely.

In contrast to the "cacheSeconds" property which will apply to all general handlers (but not to @SessionAttributes annotated handlers), this setting will apply to @SessionAttributes annotated handlers only.

See Also:
PortletContentGenerator.setCacheSeconds(int), SessionAttributes

setSynchronizeOnSession

public void setSynchronizeOnSession(boolean synchronizeOnSession)
Set if controller execution should be synchronized on the session, to serialize parallel invocations from the same client.

More specifically, the execution of each handler method will get synchronized if this flag is "true". The best available session mutex will be used for the synchronization; ideally, this will be a mutex exposed by HttpSessionMutexListener.

The session mutex is guaranteed to be the same object during the entire lifetime of the session, available under the key defined by the 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.

See Also:
HttpSessionMutexListener, org.springframework.web.portlet.util.PortletUtils#getSessionMutex(javax.portlet.PortletSession)

setParameterNameDiscoverer

public void setParameterNameDiscoverer(ParameterNameDiscoverer parameterNameDiscoverer)
Set the ParameterNameDiscoverer to use for resolving method parameter names if needed (e.g. for default attribute names).

Default is a LocalVariableTableParameterNameDiscoverer.


setCustomArgumentResolver

public void setCustomArgumentResolver(WebArgumentResolver argumentResolver)
Set a custom WebArgumentResolver to use for special method parameter types. Such a custom WebArgumentResolver will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.


setCustomArgumentResolvers

public void setCustomArgumentResolvers(WebArgumentResolver[] argumentResolvers)
Set one or more custom WebArgumentResolvers to use for special method parameter types. Any such custom WebArgumentResolver will kick in first, having a chance to resolve an argument value before the standard argument handling kicks in.


setCustomModelAndViewResolver

public void setCustomModelAndViewResolver(ModelAndViewResolver customModelAndViewResolver)
Set a custom ModelAndViewResolvers to use for special method return types. Such a custom ModelAndViewResolver will kick in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.


setCustomModelAndViewResolvers

public void setCustomModelAndViewResolvers(ModelAndViewResolver[] customModelAndViewResolvers)
Set one or more custom ModelAndViewResolvers to use for special method return types. Any such custom ModelAndViewResolver will kick in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.


setOrder

public void setOrder(int order)
Specify the order value for this HandlerAdapter bean.

Default value is Integer.MAX_VALUE, meaning that it's non-ordered.

See Also:
Ordered.getOrder()

getOrder

public int getOrder()
Description copied from interface: Ordered
Return the order value of this object, with a higher value meaning greater in terms of sorting.

Normally starting with 0, with Integer.MAX_VALUE indicating the greatest value. Same order values will result in arbitrary positions for the affected objects.

Higher values can be interpreted as lower priority. As a consequence, the object with the lowest value has highest priority (somewhat analogous to Servlet "load-on-startup" values).

Specified by:
getOrder in interface Ordered
Returns:
the order value

setBeanFactory

public void setBeanFactory(BeanFactory beanFactory)
Description copied from interface: BeanFactoryAware
Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean.afterPropertiesSet() or a custom init-method.

Specified by:
setBeanFactory in interface BeanFactoryAware
Parameters:
beanFactory - owning BeanFactory (never null). The bean can immediately call methods on the factory.
See Also:
BeanInitializationException

supports

public boolean supports(java.lang.Object handler)
Description copied from interface: HandlerAdapter
Given a handler instance, return whether or not this HandlerAdapter can support it. Typical HandlerAdapters will base the decision on the handler type. HandlerAdapters will usually only support one handler type each.

A typical implementation:

return (handler instanceof MyHandler);

Specified by:
supports in interface HandlerAdapter
Parameters:
handler - handler object to check
Returns:
whether or not this object can use the given handler

handleAction

public void handleAction(ActionRequest request,
                         ActionResponse response,
                         java.lang.Object handler)
                  throws java.lang.Exception
Description copied from interface: HandlerAdapter
Use the given handler to handle this action request. The workflow that is required may vary widely.

Specified by:
handleAction in interface HandlerAdapter
Parameters:
request - current action request
response - current action response
handler - handler to use. This object must have previously been passed to the supports method of this interface, which must have returned true.
Throws:
java.lang.Exception - in case of errors
See Also:
javax.portlet.Portlet#processAction

handleRender

public ModelAndView handleRender(RenderRequest request,
                                 RenderResponse response,
                                 java.lang.Object handler)
                          throws java.lang.Exception
Description copied from interface: HandlerAdapter
Use the given handler to handle this render request. The workflow that is required may vary widely.

Specified by:
handleRender in interface HandlerAdapter
Parameters:
request - current render request
response - current render response
handler - handler to use. This object must have previously been passed to the supports method of this interface, which must have returned true.
Returns:
ModelAndView object with the name of the view and the required model data, or null if the request has been handled directly
Throws:
java.lang.Exception - in case of errors
See Also:
javax.portlet.Portlet#render

handleResource

public ModelAndView handleResource(ResourceRequest request,
                                   ResourceResponse response,
                                   java.lang.Object handler)
                            throws java.lang.Exception
Description copied from interface: HandlerAdapter
Use the given handler to handle this resource request. The workflow that is required may vary widely.

Specified by:
handleResource in interface HandlerAdapter
Parameters:
request - current render request
response - current render response
handler - handler to use. This object must have previously been passed to the supports method of this interface, which must have returned true.
Returns:
ModelAndView object with the name of the view and the required model data, or null if the request has been handled directly
Throws:
java.lang.Exception - in case of errors
See Also:
javax.portlet.ResourceServingPortlet#serveResource

handleEvent

public void handleEvent(EventRequest request,
                        EventResponse response,
                        java.lang.Object handler)
                 throws java.lang.Exception
Description copied from interface: HandlerAdapter
Use the given handler to handle this event request. The workflow that is required may vary widely.

Specified by:
handleEvent in interface HandlerAdapter
Parameters:
request - current action request
response - current action response
handler - handler to use. This object must have previously been passed to the supports method of this interface, which must have returned true.
Throws:
java.lang.Exception - in case of errors
See Also:
javax.portlet.EventPortlet#processEvent

doHandle

protected ModelAndView doHandle(PortletRequest request,
                                PortletResponse response,
                                java.lang.Object handler)
                         throws java.lang.Exception
Throws:
java.lang.Exception

invokeHandlerMethod

private ModelAndView invokeHandlerMethod(PortletRequest request,
                                         PortletResponse response,
                                         java.lang.Object handler,
                                         ExtendedModelMap implicitModel)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

getMethodResolver

private AnnotationMethodHandlerAdapter.PortletHandlerMethodResolver getMethodResolver(java.lang.Object handler)
Build a HandlerMethodResolver for the given handler type.


createBinder

protected PortletRequestDataBinder createBinder(PortletRequest request,
                                                java.lang.Object target,
                                                java.lang.String objectName)
                                         throws java.lang.Exception
Template method for creating a new PortletRequestDataBinder instance.

The default implementation creates a standard PortletRequestDataBinder. This can be overridden for custom PortletRequestDataBinder subclasses.

Parameters:
request - current portlet request
target - the target object to bind onto (or null if the binder is just used to convert a plain parameter value)
objectName - the objectName of the target object
Returns:
the PortletRequestDataBinder instance to use
Throws:
java.lang.Exception - in case of invalid state or arguments
See Also:
PortletRequestDataBinder#bind(javax.portlet.PortletRequest)