View Javadoc

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