View Javadoc

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