View Javadoc

1   /*
2    * $Id: PortletPreferencesInterceptor.java 502294 2007-02-01 17:28:00Z niallp $
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.PortletRequest;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.struts2.StrutsStatics;
28  import org.apache.struts2.portlet.context.PortletActionContext;
29  
30  import com.opensymphony.xwork2.ActionContext;
31  import com.opensymphony.xwork2.ActionInvocation;
32  import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
33  
34  
35  /***
36   * <!-- START SNIPPET: description -->
37   *
38   * An interceptor which provides an implementation of PortletPreferences if the Action implements
39   * PortletPreferencesAware.
40   * 
41   * If running in a servlet environment, a testing implementation of PortletPreferences will be
42   * created and provided, but it should not be used in a production environment.
43   *
44   * <!-- END SNIPPET: description -->
45   *
46   * <p/> <u>Interceptor parameters:</u>
47   *
48   * <!-- START SNIPPET: parameters -->
49   *
50   * <ul>
51   *
52   * <li>None</li>
53   *
54   * </ul>
55   *
56   * <!-- END SNIPPET: parameters -->
57   *
58   * <p/> <u>Extending the interceptor:</u>
59   *
60   * <p/>
61   *
62   * <!-- START SNIPPET: extending -->
63   *
64   * There are no known extension points for this interceptor.
65   *
66   * <!-- END SNIPPET: extending -->
67   *
68   * <p/> <u>Example code:</u>
69   *
70   * <pre>
71   * <!-- START SNIPPET: example -->
72   * &lt;action name="someAction" class="com.examples.SomeAction"&gt;
73   *     &lt;interceptor-ref name="portlet-preferences"/&gt;
74   *     &lt;interceptor-ref name="basicStack"/&gt;
75   *     &lt;result name="success"&gt;good_result.ftl&lt;/result&gt;
76   * &lt;/action&gt;
77   * <!-- END SNIPPET: example -->
78   * </pre>
79   *
80   * @see PortletPreferencesAware
81   */
82  public class PortletPreferencesInterceptor extends AbstractInterceptor implements StrutsStatics {
83  
84      private static final Log LOG = LogFactory.getLog(PortletPreferencesInterceptor.class);
85      
86      public String intercept(ActionInvocation invocation) throws Exception {
87          final Object action = invocation.getAction();
88          final ActionContext context = invocation.getInvocationContext();
89  
90          if (action instanceof PortletPreferencesAware) {
91              PortletRequest request = PortletActionContext.getRequest();
92              PortletPreferencesAware awareAction = (PortletPreferencesAware) action;
93              
94              // Check if running in a servlet environment
95              if (request == null) {
96                  LOG.warn("This portlet preferences implementation should only be used during development");
97                  awareAction.setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession()));
98              } else {
99                  awareAction.setPortletPreferences(request.getPreferences());
100             }
101         }
102         return invocation.invoke();
103     }
104 }