001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.portlet.context;
006:
007: import java.util.Map;
008:
009: import javax.portlet.ActionRequest;
010: import javax.portlet.ActionResponse;
011: import javax.portlet.PortletConfig;
012: import javax.portlet.PortletRequest;
013: import javax.portlet.PortletResponse;
014: import javax.portlet.RenderRequest;
015: import javax.portlet.RenderResponse;
016:
017: import com.opensymphony.webwork.portlet.PortletActionConstants;
018: import com.opensymphony.xwork.ActionContext;
019:
020: /**
021: * PortletActionContext. ActionContext thread local for the portlet environment.
022: *
023: * @author Nils-Helge Garli
024: * @version $Revision: 2338 $ $Date: 2006-03-08 21:22:35 +0100 (Wed, 08 Mar 2006) $
025: */
026: public class PortletActionContext implements PortletActionConstants {
027:
028: /**
029: * Get the PortletConfig of the portlet that is executing.
030: *
031: * @return The PortletConfig of the executing portlet.
032: */
033: public static PortletConfig getPortletConfig() {
034: return (PortletConfig) getContext().get(PORTLET_CONFIG);
035: }
036:
037: /**
038: * Get the RenderRequest. Can only be invoked in the render phase.
039: *
040: * @return The current RenderRequest.
041: * @throws IllegalStateException If the method is invoked in the wrong phase.
042: */
043: public static RenderRequest getRenderRequest() {
044: if (!isRender()) {
045: throw new IllegalStateException(
046: "RenderRequest cannot be obtained in event phase");
047: }
048: return (RenderRequest) getContext().get(REQUEST);
049: }
050:
051: /**
052: * Get the RenderResponse. Can only be invoked in the render phase.
053: *
054: * @return The current RenderResponse.
055: * @throws IllegalStateException If the method is invoked in the wrong phase.
056: */
057: public static RenderResponse getRenderResponse() {
058: if (!isRender()) {
059: throw new IllegalStateException(
060: "RenderResponse cannot be obtained in event phase");
061: }
062: return (RenderResponse) getContext().get(RESPONSE);
063: }
064:
065: /**
066: * Get the ActionRequest. Can only be invoked in the event phase.
067: *
068: * @return The current ActionRequest.
069: * @throws IllegalStateException If the method is invoked in the wrong phase.
070: */
071: public static ActionRequest getActionRequest() {
072: if (!isEvent()) {
073: throw new IllegalStateException(
074: "ActionRequest cannot be obtained in render phase");
075: }
076: return (ActionRequest) getContext().get(REQUEST);
077: }
078:
079: /**
080: * Get the ActionRequest. Can only be invoked in the event phase.
081: *
082: * @return The current ActionRequest.
083: * @throws IllegalStateException If the method is invoked in the wrong phase.
084: */
085: public static ActionResponse getActionResponse() {
086: if (!isEvent()) {
087: throw new IllegalStateException(
088: "ActionResponse cannot be obtained in render phase");
089: }
090: return (ActionResponse) getContext().get(RESPONSE);
091: }
092:
093: /**
094: * Get the action namespace of the portlet. Used to organize actions for multiple portlets in
095: * the same portlet application.
096: *
097: * @return The portlet namespace as defined in <code>portlet.xml</code> and <code>xwork.xml</code>
098: */
099: public static String getPortletNamespace() {
100: return (String) getContext().get(PORTLET_NAMESPACE);
101: }
102:
103: /**
104: * Get the current PortletRequest.
105: *
106: * @return The current PortletRequest.
107: */
108: public static PortletRequest getRequest() {
109: return (PortletRequest) getContext().get(REQUEST);
110: }
111:
112: /**
113: * Get the current PortletResponse
114: *
115: * @return The current PortletResponse.
116: */
117: public static PortletResponse getResponse() {
118: return (PortletResponse) getContext().get(RESPONSE);
119: }
120:
121: /**
122: * Get the phase that the portlet is executing in.
123: *
124: * @return {@link PortletActionConstants#RENDER_PHASE} in render phase, and
125: * {@link PortletActionConstants#EVENT_PHASE} in the event phase.
126: */
127: public static Integer getPhase() {
128: return (Integer) getContext().get(PHASE);
129: }
130:
131: /**
132: * @return <code>true</code> if the Portlet is executing in render phase.
133: */
134: public static boolean isRender() {
135: return PortletActionConstants.RENDER_PHASE.equals(getPhase());
136: }
137:
138: /**
139: * @return <code>true</code> if the Portlet is executing in the event phase.
140: */
141: public static boolean isEvent() {
142: return PortletActionConstants.EVENT_PHASE.equals(getPhase());
143: }
144:
145: /**
146: * @return The current ActionContext.
147: */
148: private static ActionContext getContext() {
149: return ActionContext.getContext();
150: }
151:
152: /**
153: * Check to see if the current request is a portlet request.
154: *
155: * @return <code>true</code> if the current request is a portlet request.
156: */
157: public static boolean isPortletRequest() {
158: return getRequest() != null;
159: }
160:
161: /**
162: * Get the default action name for the current mode.
163: *
164: * @return The default action name for the current portlet mode.
165: */
166: public static String getDefaultActionForMode() {
167: return (String) getContext().get(DEFAULT_ACTION_FOR_MODE);
168: }
169:
170: /**
171: * Get the namespace to mode mappings.
172: *
173: * @return The map of the namespaces for each mode.
174: */
175: public static Map getModeNamespaceMap() {
176: return (Map) getContext().get(MODE_NAMESPACE_MAP);
177: }
178:
179: }
|