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