View Javadoc

1   /*
2    * $Id: PortletActionContext.java 724030 2008-12-06 19:32:29Z 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.context;
23  
24  import java.util.Map;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletContext;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletResponse;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts2.StrutsStatics;
36  import org.apache.struts2.dispatcher.mapper.ActionMapping;
37  import org.apache.struts2.portlet.PortletActionConstants;
38  
39  import com.opensymphony.xwork2.ActionContext;
40  
41  
42  /***
43   * PortletActionContext. ActionContext thread local for the portlet environment.
44   *
45   * @version $Revision: 724030 $ $Date: 2008-12-06 14:32:29 -0500 (Sat, 06 Dec 2008) $
46   */
47  public class PortletActionContext implements PortletActionConstants {
48  
49      /***
50       * Get the PortletConfig of the portlet that is executing.
51       *
52       * @return The PortletConfig of the executing portlet.
53       */
54      public static PortletConfig getPortletConfig() {
55          return (PortletConfig) getContext().get(PORTLET_CONFIG);
56      }
57  
58      /***
59       * Get the RenderRequest. Can only be invoked in the render phase.
60       *
61       * @return The current RenderRequest.
62       * @throws IllegalStateException If the method is invoked in the wrong phase.
63       */
64      public static RenderRequest getRenderRequest() {
65          if (!isRender()) {
66              throw new IllegalStateException(
67                      "RenderRequest cannot be obtained in event phase");
68          }
69          return (RenderRequest) getContext().get(REQUEST);
70      }
71  
72      /***
73       * Get the RenderResponse. Can only be invoked in the render phase.
74       *
75       * @return The current RenderResponse.
76       * @throws IllegalStateException If the method is invoked in the wrong phase.
77       */
78      public static RenderResponse getRenderResponse() {
79          if (!isRender()) {
80              throw new IllegalStateException(
81                      "RenderResponse cannot be obtained in event phase");
82          }
83          return (RenderResponse) getContext().get(RESPONSE);
84      }
85  
86      /***
87       * Get the ActionRequest. Can only be invoked in the event phase.
88       *
89       * @return The current ActionRequest.
90       * @throws IllegalStateException If the method is invoked in the wrong phase.
91       */
92      public static ActionRequest getActionRequest() {
93          if (!isEvent()) {
94              throw new IllegalStateException(
95                      "ActionRequest cannot be obtained in render phase");
96          }
97          return (ActionRequest) getContext().get(REQUEST);
98      }
99  
100     /***
101      * Get the ActionRequest. Can only be invoked in the event phase.
102      *
103      * @return The current ActionRequest.
104      * @throws IllegalStateException If the method is invoked in the wrong phase.
105      */
106     public static ActionResponse getActionResponse() {
107         if (!isEvent()) {
108             throw new IllegalStateException(
109                     "ActionResponse cannot be obtained in render phase");
110         }
111         return (ActionResponse) getContext().get(RESPONSE);
112     }
113 
114     /***
115      * Get the action namespace of the portlet. Used to organize actions for multiple portlets in
116      * the same portlet application.
117      *
118      * @return The portlet namespace as defined in <code>portlet.xml</code> and <code>struts.xml</code>
119      */
120     public static String getPortletNamespace() {
121         return (String)getContext().get(PORTLET_NAMESPACE);
122     }
123 
124     /***
125      * Get the current PortletRequest.
126      *
127      * @return The current PortletRequest.
128      */
129     public static PortletRequest getRequest() {
130         return (PortletRequest) getContext().get(REQUEST);
131     }
132     
133     /***
134      * Convenience setter for the portlet request.
135      * @param request
136      */
137     public static void setRequest(PortletRequest request) {
138     	getContext().put(REQUEST, request);
139     }
140 
141     /***
142      * Get the current PortletResponse
143      *
144      * @return The current PortletResponse.
145      */
146     public static PortletResponse getResponse() {
147         return (PortletResponse) getContext().get(RESPONSE);
148     }
149     
150     /***
151      * Convenience setter for the portlet response.
152      * @param response
153      */
154     public static void setResponse(PortletResponse response) {
155     	getContext().put(RESPONSE, response);
156     }
157 
158     /***
159      * Get the phase that the portlet is executing in.
160      *
161      * @return {@link PortletActionConstants#RENDER_PHASE} in render phase, and
162      * {@link PortletActionConstants#EVENT_PHASE} in the event phase.
163      */
164     public static Integer getPhase() {
165         return (Integer) getContext().get(PHASE);
166     }
167 
168     /***
169      * @return <code>true</code> if the Portlet is executing in render phase.
170      */
171     public static boolean isRender() {
172         return PortletActionConstants.RENDER_PHASE.equals(getPhase());
173     }
174 
175     /***
176      * @return <code>true</code> if the Portlet is executing in the event phase.
177      */
178     public static boolean isEvent() {
179         return PortletActionConstants.EVENT_PHASE.equals(getPhase());
180     }
181 
182     /***
183      * @return The current ActionContext.
184      */
185     private static ActionContext getContext() {
186         return ActionContext.getContext();
187     }
188 
189     /***
190      * Check to see if the current request is a portlet request.
191      *
192      * @return <code>true</code> if the current request is a portlet request.
193      */
194     public static boolean isPortletRequest() {
195         return getRequest() != null;
196     }
197 
198     /***
199      * Get the default action mapping for the current mode.
200      *
201      * @return The default action mapping for the current portlet mode.
202      */
203     public static ActionMapping getDefaultActionForMode() {
204         return (ActionMapping)getContext().get(DEFAULT_ACTION_FOR_MODE);
205     }
206 
207     /***
208      * Get the namespace to mode mappings.
209      *
210      * @return The map of the namespaces for each mode.
211      */
212     public static Map getModeNamespaceMap() {
213         return (Map)getContext().get(MODE_NAMESPACE_MAP);
214     }
215     
216     /***
217      * Get the portlet context.
218      * @return The portlet context.
219      */
220     public static PortletContext getPortletContext() {
221     	return (PortletContext)getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
222     }
223     
224     /***
225      * Convenience setter for the portlet context.
226      * @param context
227      */
228     public static void setPortletContext(PortletContext context) {
229     	getContext().put(StrutsStatics.STRUTS_PORTLET_CONTEXT, context);
230     }
231     
232     /***
233      * Gets the action mapping for this context
234      *
235      * @return The action mapping
236      */
237     public static ActionMapping getActionMapping() {
238         return (ActionMapping) getContext().get(ACTION_MAPPING);
239     }
240 
241 }