FileDocCategorySizeDatePackage
ServletConfigInterceptor.javaAPI DocExample6246Mon Jul 23 13:26:52 BST 2007org.apache.struts2.interceptor

ServletConfigInterceptor

public class ServletConfigInterceptor extends com.opensymphony.xwork2.interceptor.AbstractInterceptor implements org.apache.struts2.StrutsStatics
An interceptor which sets action properties based on the interfaces an action implements. For example, if the action implements {@link ParameterAware} then the action context's parameter map will be set on it.

This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the servlet context, the session, etc. Interfaces that it supports are:

  • {@link ServletContextAware}
  • {@link ServletRequestAware}
  • {@link ServletResponseAware}
  • {@link ParameterAware}
  • {@link RequestAware}
  • {@link SessionAware}
  • {@link ApplicationAware}
  • {@link PrincipalAware}

Interceptor parameters:

  • None

Extending the interceptor:

There are no known extension points for this interceptor.

Example code:


<action name="someAction" class="com.examples.SomeAction">
<interceptor-ref name="servlet-config"/>
<interceptor-ref name="basicStack"/>
<result name="success">good_result.ftl</result>
</action>

see
ServletContextAware
see
ServletRequestAware
see
ServletResponseAware
see
ParameterAware
see
SessionAware
see
ApplicationAware
see
PrincipalAware

Fields Summary
private static final long
serialVersionUID
Constructors Summary
Methods Summary
public java.lang.Stringintercept(com.opensymphony.xwork2.ActionInvocation invocation)
Sets action properties based on the interfaces an action implements. Things like application properties, parameters, session attributes, etc are set based on the implementing interface.

param
invocation an encapsulation of the action execution state.
throws
Exception if an error occurs when setting action properties.


                                                     
          
        final Object action = invocation.getAction();
        final ActionContext context = invocation.getInvocationContext();

        if (action instanceof ServletRequestAware) {
            HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
            ((ServletRequestAware) action).setServletRequest(request);
        }

        if (action instanceof ServletResponseAware) {
            HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
            ((ServletResponseAware) action).setServletResponse(response);
        }

        if (action instanceof ParameterAware) {
            ((ParameterAware) action).setParameters(context.getParameters());
        }

        if (action instanceof RequestAware) {
            ((RequestAware) action).setRequest((Map) context.get("request"));
        }

        if (action instanceof SessionAware) {
            ((SessionAware) action).setSession(context.getSession());
        }

        if (action instanceof ApplicationAware) {
            ((ApplicationAware) action).setApplication(context.getApplication());
        }

        if (action instanceof PrincipalAware) {
            HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
            Object portletRequest = context.get(PortletActionConstants.REQUEST);
            if (portletRequest != null) {
                // We are in portlet environment, so principal information resides in PortletRequest
                ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy((PortletRequest) portletRequest));
            } else {
                // We are in servtlet environment, so principal information resides in HttpServletRequest
                ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
            }
        }
        if (action instanceof ServletContextAware) {
            ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
            ((ServletContextAware) action).setServletContext(servletContext);
        }
        return invocation.invoke();