1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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: 651946 $ $Date: 2008-04-27 08:41:38 -0500 (Sun, 27 Apr 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 * Get the current PortletResponse
135 *
136 * @return The current PortletResponse.
137 */
138 public static PortletResponse getResponse() {
139 return (PortletResponse) getContext().get(RESPONSE);
140 }
141
142 /***
143 * Get the phase that the portlet is executing in.
144 *
145 * @return {@link PortletActionConstants#RENDER_PHASE} in render phase, and
146 * {@link PortletActionConstants#EVENT_PHASE} in the event phase.
147 */
148 public static Integer getPhase() {
149 return (Integer) getContext().get(PHASE);
150 }
151
152 /***
153 * @return <code>true</code> if the Portlet is executing in render phase.
154 */
155 public static boolean isRender() {
156 return PortletActionConstants.RENDER_PHASE.equals(getPhase());
157 }
158
159 /***
160 * @return <code>true</code> if the Portlet is executing in the event phase.
161 */
162 public static boolean isEvent() {
163 return PortletActionConstants.EVENT_PHASE.equals(getPhase());
164 }
165
166 /***
167 * @return The current ActionContext.
168 */
169 private static ActionContext getContext() {
170 return ActionContext.getContext();
171 }
172
173 /***
174 * Check to see if the current request is a portlet request.
175 *
176 * @return <code>true</code> if the current request is a portlet request.
177 */
178 public static boolean isPortletRequest() {
179 return getRequest() != null;
180 }
181
182 /***
183 * Get the default action mapping for the current mode.
184 *
185 * @return The default action mapping for the current portlet mode.
186 */
187 public static ActionMapping getDefaultActionForMode() {
188 return (ActionMapping)getContext().get(DEFAULT_ACTION_FOR_MODE);
189 }
190
191 /***
192 * Get the namespace to mode mappings.
193 *
194 * @return The map of the namespaces for each mode.
195 */
196 public static Map getModeNamespaceMap() {
197 return (Map)getContext().get(MODE_NAMESPACE_MAP);
198 }
199
200 /***
201 * Get the portlet context.
202 * @return The portlet context.
203 */
204 public static PortletContext getPortletContext() {
205 return (PortletContext)getContext().get(StrutsStatics.STRUTS_PORTLET_CONTEXT);
206 }
207
208 }