View Javadoc

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