001: /*
002: * $Id: PortletActionContext.java 568895 2007-08-23 08:57:36Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.context;
022:
023: import java.util.Map;
024:
025: import javax.portlet.*;
026:
027: import org.apache.struts2.dispatcher.mapper.ActionMapping;
028: import org.apache.struts2.portlet.PortletActionConstants;
029: import org.apache.struts2.StrutsStatics;
030:
031: import com.opensymphony.xwork2.ActionContext;
032:
033: /**
034: * PortletActionContext. ActionContext thread local for the portlet environment.
035: *
036: * @version $Revision: 568895 $ $Date: 2007-08-23 04:57:36 -0400 (Thu, 23 Aug 2007) $
037: */
038: public class PortletActionContext implements PortletActionConstants {
039:
040: /**
041: * Get the PortletConfig of the portlet that is executing.
042: *
043: * @return The PortletConfig of the executing portlet.
044: */
045: public static PortletConfig getPortletConfig() {
046: return (PortletConfig) getContext().get(PORTLET_CONFIG);
047: }
048:
049: /**
050: * Get the RenderRequest. Can only be invoked in the render phase.
051: *
052: * @return The current RenderRequest.
053: * @throws IllegalStateException If the method is invoked in the wrong phase.
054: */
055: public static RenderRequest getRenderRequest() {
056: if (!isRender()) {
057: throw new IllegalStateException(
058: "RenderRequest cannot be obtained in event phase");
059: }
060: return (RenderRequest) getContext().get(REQUEST);
061: }
062:
063: /**
064: * Get the RenderResponse. Can only be invoked in the render phase.
065: *
066: * @return The current RenderResponse.
067: * @throws IllegalStateException If the method is invoked in the wrong phase.
068: */
069: public static RenderResponse getRenderResponse() {
070: if (!isRender()) {
071: throw new IllegalStateException(
072: "RenderResponse cannot be obtained in event phase");
073: }
074: return (RenderResponse) getContext().get(RESPONSE);
075: }
076:
077: /**
078: * Get the ActionRequest. Can only be invoked in the event phase.
079: *
080: * @return The current ActionRequest.
081: * @throws IllegalStateException If the method is invoked in the wrong phase.
082: */
083: public static ActionRequest getActionRequest() {
084: if (!isEvent()) {
085: throw new IllegalStateException(
086: "ActionRequest cannot be obtained in render phase");
087: }
088: return (ActionRequest) getContext().get(REQUEST);
089: }
090:
091: /**
092: * Get the ActionRequest. Can only be invoked in the event phase.
093: *
094: * @return The current ActionRequest.
095: * @throws IllegalStateException If the method is invoked in the wrong phase.
096: */
097: public static ActionResponse getActionResponse() {
098: if (!isEvent()) {
099: throw new IllegalStateException(
100: "ActionResponse cannot be obtained in render phase");
101: }
102: return (ActionResponse) getContext().get(RESPONSE);
103: }
104:
105: /**
106: * Get the action namespace of the portlet. Used to organize actions for multiple portlets in
107: * the same portlet application.
108: *
109: * @return The portlet namespace as defined in <code>portlet.xml</code> and <code>struts.xml</code>
110: */
111: public static String getPortletNamespace() {
112: return (String) getContext().get(PORTLET_NAMESPACE);
113: }
114:
115: /**
116: * Get the current PortletRequest.
117: *
118: * @return The current PortletRequest.
119: */
120: public static PortletRequest getRequest() {
121: return (PortletRequest) getContext().get(REQUEST);
122: }
123:
124: /**
125: * Get the current PortletResponse
126: *
127: * @return The current PortletResponse.
128: */
129: public static PortletResponse getResponse() {
130: return (PortletResponse) getContext().get(RESPONSE);
131: }
132:
133: /**
134: * Get the phase that the portlet is executing in.
135: *
136: * @return {@link PortletActionConstants#RENDER_PHASE} in render phase, and
137: * {@link PortletActionConstants#EVENT_PHASE} in the event phase.
138: */
139: public static Integer getPhase() {
140: return (Integer) getContext().get(PHASE);
141: }
142:
143: /**
144: * @return <code>true</code> if the Portlet is executing in render phase.
145: */
146: public static boolean isRender() {
147: return PortletActionConstants.RENDER_PHASE.equals(getPhase());
148: }
149:
150: /**
151: * @return <code>true</code> if the Portlet is executing in the event phase.
152: */
153: public static boolean isEvent() {
154: return PortletActionConstants.EVENT_PHASE.equals(getPhase());
155: }
156:
157: /**
158: * @return The current ActionContext.
159: */
160: private static ActionContext getContext() {
161: return ActionContext.getContext();
162: }
163:
164: /**
165: * Check to see if the current request is a portlet request.
166: *
167: * @return <code>true</code> if the current request is a portlet request.
168: */
169: public static boolean isPortletRequest() {
170: return getRequest() != null;
171: }
172:
173: /**
174: * Get the default action mapping for the current mode.
175: *
176: * @return The default action mapping for the current portlet mode.
177: */
178: public static ActionMapping getDefaultActionForMode() {
179: return (ActionMapping) getContext()
180: .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(
198: StrutsStatics.STRUTS_PORTLET_CONTEXT);
199: }
200:
201: }
|