Save This Page
Home » struts-2.0.11.2-src » org.apache » struts2 » interceptor » [javadoc | source]
    1   /*
    2    * $Id: ServletConfigInterceptor.java 502196 2007-02-01 11:28:57Z rgielen $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   package org.apache.struts2.interceptor;
   22   
   23   import java.util.Map;
   24   
   25   import javax.servlet.ServletContext;
   26   import javax.servlet.http.HttpServletRequest;
   27   import javax.servlet.http.HttpServletResponse;
   28   import javax.portlet.PortletRequest;
   29   
   30   import org.apache.struts2.StrutsStatics;
   31   import org.apache.struts2.servlet.interceptor.ServletPrincipalProxy;
   32   import org.apache.struts2.portlet.PortletActionConstants;
   33   import org.apache.struts2.portlet.interceptor.PortletPrincipalProxy;
   34   import org.apache.struts2.util.ServletContextAware;
   35   
   36   import com.opensymphony.xwork2.ActionContext;
   37   import com.opensymphony.xwork2.ActionInvocation;
   38   import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
   39   
   40   
   41   /**
   42    * <!-- START SNIPPET: description -->
   43    *
   44    * An interceptor which sets action properties based on the interfaces an action implements. For example, if the action
   45    * implements {@link ParameterAware} then the action context's parameter map will be set on it.
   46    *
   47    * <p/> This interceptor is designed to set all properties an action needs if it's aware of servlet parameters, the
   48    * servlet context, the session, etc. Interfaces that it supports are:
   49    *
   50    * <ul>
   51    *
   52    * <li>{@link ServletContextAware}</li>
   53    *
   54    * <li>{@link ServletRequestAware}</li>
   55    *
   56    * <li>{@link ServletResponseAware}</li>
   57    *
   58    * <li>{@link ParameterAware}</li>
   59    *
   60    * <li>{@link RequestAware}</li>
   61    *
   62    * <li>{@link SessionAware}</li>
   63    *
   64    * <li>{@link ApplicationAware}</li>
   65    *
   66    * <li>{@link PrincipalAware}</li>
   67    *
   68    * </ul>
   69    *
   70    * <!-- END SNIPPET: description -->
   71    *
   72    * <p/> <u>Interceptor parameters:</u>
   73    *
   74    * <!-- START SNIPPET: parameters -->
   75    *
   76    * <ul>
   77    *
   78    * <li>None</li>
   79    *
   80    * </ul>
   81    *
   82    * <!-- END SNIPPET: parameters -->
   83    *
   84    * <p/> <u>Extending the interceptor:</u>
   85    *
   86    * <p/>
   87    *
   88    * <!-- START SNIPPET: extending -->
   89    *
   90    * There are no known extension points for this interceptor.
   91    *
   92    * <!-- END SNIPPET: extending -->
   93    *
   94    * <p/> <u>Example code:</u>
   95    *
   96    * <pre>
   97    * <!-- START SNIPPET: example -->
   98    * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
   99    *     &lt;interceptor-ref name="servlet-config"/&gt;
  100    *     &lt;interceptor-ref name="basicStack"/&gt;
  101    *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
  102    * &lt;/action&gt;
  103    * <!-- END SNIPPET: example -->
  104    * </pre>
  105    *
  106    * @see ServletContextAware
  107    * @see ServletRequestAware
  108    * @see ServletResponseAware
  109    * @see ParameterAware
  110    * @see SessionAware
  111    * @see ApplicationAware
  112    * @see PrincipalAware
  113    */
  114   public class ServletConfigInterceptor extends AbstractInterceptor implements StrutsStatics {
  115   
  116       private static final long serialVersionUID = 605261777858676638L;
  117   
  118       /**
  119        * Sets action properties based on the interfaces an action implements. Things like application properties,
  120        * parameters, session attributes, etc are set based on the implementing interface.
  121        *
  122        * @param invocation an encapsulation of the action execution state.
  123        * @throws Exception if an error occurs when setting action properties.
  124        */
  125       public String intercept(ActionInvocation invocation) throws Exception {
  126           final Object action = invocation.getAction();
  127           final ActionContext context = invocation.getInvocationContext();
  128   
  129           if (action instanceof ServletRequestAware) {
  130               HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
  131               ((ServletRequestAware) action).setServletRequest(request);
  132           }
  133   
  134           if (action instanceof ServletResponseAware) {
  135               HttpServletResponse response = (HttpServletResponse) context.get(HTTP_RESPONSE);
  136               ((ServletResponseAware) action).setServletResponse(response);
  137           }
  138   
  139           if (action instanceof ParameterAware) {
  140               ((ParameterAware) action).setParameters(context.getParameters());
  141           }
  142   
  143           if (action instanceof RequestAware) {
  144               ((RequestAware) action).setRequest((Map) context.get("request"));
  145           }
  146   
  147           if (action instanceof SessionAware) {
  148               ((SessionAware) action).setSession(context.getSession());
  149           }
  150   
  151           if (action instanceof ApplicationAware) {
  152               ((ApplicationAware) action).setApplication(context.getApplication());
  153           }
  154   
  155           if (action instanceof PrincipalAware) {
  156               HttpServletRequest request = (HttpServletRequest) context.get(HTTP_REQUEST);
  157               Object portletRequest = context.get(PortletActionConstants.REQUEST);
  158               if (portletRequest != null) {
  159                   // We are in portlet environment, so principal information resides in PortletRequest
  160                   ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy((PortletRequest) portletRequest));
  161               } else {
  162                   // We are in servtlet environment, so principal information resides in HttpServletRequest
  163                   ((PrincipalAware) action).setPrincipalProxy(new ServletPrincipalProxy(request));
  164               }
  165           }
  166           if (action instanceof ServletContextAware) {
  167               ServletContext servletContext = (ServletContext) context.get(SERVLET_CONTEXT);
  168               ((ServletContextAware) action).setServletContext(servletContext);
  169           }
  170           return invocation.invoke();
  171       }
  172   }

Save This Page
Home » struts-2.0.11.2-src » org.apache » struts2 » interceptor » [javadoc | source]