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