View Javadoc

1   /*
2    * $Id: PortletAwareInterceptor.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.portlet.interceptor;
23  
24  import javax.portlet.PortletContext;
25  import javax.portlet.PortletRequest;
26  import javax.portlet.PortletResponse;
27  
28  import org.apache.struts2.StrutsStatics;
29  import org.apache.struts2.interceptor.PrincipalAware;
30  import org.apache.struts2.portlet.PortletActionConstants;
31  
32  import com.opensymphony.xwork2.ActionContext;
33  import com.opensymphony.xwork2.ActionInvocation;
34  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
35  import com.opensymphony.xwork2.util.logging.Logger;
36  import com.opensymphony.xwork2.util.logging.LoggerFactory;
37  
38  public class PortletAwareInterceptor extends AbstractInterceptor implements PortletActionConstants, StrutsStatics {
39  
40  	private static final long serialVersionUID = 2476509721059587700L;
41  	
42  	private static final Logger LOG = LoggerFactory.getLogger(PortletAwareInterceptor.class);
43  
44  	/***
45       * Sets action properties based on the interfaces an action implements. Things like application properties,
46       * parameters, session attributes, etc are set based on the implementing interface.
47       *
48       * @param invocation an encapsulation of the action execution state.
49       * @throws Exception if an error occurs when setting action properties.
50       */
51      public String intercept(ActionInvocation invocation) throws Exception {
52          final Object action = invocation.getAction();
53          final ActionContext context = invocation.getInvocationContext();
54  
55          if (action instanceof PortletRequestAware) {
56              PortletRequest request = (PortletRequest) context.get(REQUEST);
57              ((PortletRequestAware) action).setPortletRequest(request);
58          }
59  
60          if (action instanceof PortletResponseAware) {
61              PortletResponse response = (PortletResponse) context.get(RESPONSE);
62              ((PortletResponseAware) action).setPortletResponse(response);
63          }
64          if (action instanceof PrincipalAware) {
65              PortletRequest request = (PortletRequest) context.get(REQUEST);
66              ((PrincipalAware) action).setPrincipalProxy(new PortletPrincipalProxy(request));
67          }
68          if (action instanceof PortletContextAware) {
69              PortletContext portletContext = (PortletContext) context.get(STRUTS_PORTLET_CONTEXT);
70              ((PortletContextAware) action).setPortletContext(portletContext);
71          }
72          if (action instanceof PortletPreferencesAware) {
73          	PortletRequest request = (PortletRequest) context.get(REQUEST);
74              
75              // Check if running in a servlet environment
76              if (request == null) {
77                  LOG.warn("This portlet preferences implementation should only be used during development");
78                  ((PortletPreferencesAware)action).setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession()));
79              } else {
80              	((PortletPreferencesAware)action).setPortletPreferences(request.getPreferences());
81              }
82          }
83          return invocation.invoke();
84      }
85  }