001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.pluto.internal.impl;
018:
019: import java.util.Enumeration;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import javax.portlet.ActionResponse;
025: import javax.portlet.PortalContext;
026: import javax.portlet.PortletMode;
027: import javax.portlet.PortletModeException;
028: import javax.portlet.WindowState;
029: import javax.portlet.WindowStateException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032: import javax.servlet.http.HttpServletResponseWrapper;
033:
034: import org.apache.pluto.PortletContainer;
035: import org.apache.pluto.descriptors.portlet.PortletDD;
036: import org.apache.pluto.descriptors.portlet.SupportsDD;
037: import org.apache.pluto.internal.InternalActionResponse;
038: import org.apache.pluto.internal.InternalPortletWindow;
039: import org.apache.pluto.spi.ResourceURLProvider;
040: import org.apache.pluto.spi.PortalCallbackService;
041: import org.apache.pluto.util.StringUtils;
042:
043: public class ActionResponseImpl extends PortletResponseImpl implements
044: ActionResponse, InternalActionResponse {
045:
046: /**
047: * Is it still allowed to invoke the method sendRedirect() ?
048: */
049: boolean redirectAllowed = true;
050:
051: private boolean redirected;
052: private String redirectLocation;
053:
054: private Map renderParameters = new HashMap();
055: private WindowState windowState = null;
056: private PortletMode portletMode = null;
057:
058: private PortalCallbackService callback;
059: private PortalContext context;
060:
061: public ActionResponseImpl(PortletContainer container,
062: InternalPortletWindow internalPortletWindow,
063: HttpServletRequest servletRequest,
064: HttpServletResponse servletResponse) {
065: super (container, internalPortletWindow, servletRequest,
066: servletResponse);
067: context = container.getRequiredContainerServices()
068: .getPortalContext();
069: callback = container.getRequiredContainerServices()
070: .getPortalCallbackService();
071: }
072:
073: //
074: // javax.portlet.ActionResponse
075: //
076: public void setWindowState(WindowState windowState)
077: throws WindowStateException {
078: if (redirected) {
079: throw new IllegalStateException(
080: "it is not allowed to invoke setWindowState after sendRedirect has been called");
081: }
082:
083: if (isWindowStateAllowed(windowState)) {
084: this .windowState = windowState;
085: } else {
086: throw new WindowStateException(
087: "Can't set this WindowState", windowState);
088: }
089: redirectAllowed = false;
090: }
091:
092: public void setPortletMode(PortletMode portletMode)
093: throws PortletModeException {
094: if (redirected) {
095: throw new IllegalStateException(
096: "it is not allowed to invoke setPortletMode after sendRedirect has been called");
097: }
098:
099: // check if portal supports portlet mode
100: boolean supported = isPortletModeAllowed(portletMode);
101:
102: // if porlet mode is allowed
103: if (supported) {
104: this .portletMode = portletMode;
105: } else {
106: throw new PortletModeException(
107: "Can't set this PortletMode", portletMode);
108: }
109:
110: redirectAllowed = false;
111:
112: }
113:
114: public void sendRedirect(String location)
115: throws java.io.IOException {
116: if (redirectAllowed) {
117: if (location != null) {
118: HttpServletResponse redirectResponse = getHttpServletResponse();
119: while (redirectResponse instanceof HttpServletResponseWrapper) {
120: redirectResponse = (HttpServletResponse) ((HttpServletResponseWrapper) redirectResponse)
121: .getResponse();
122: }
123:
124: ResourceURLProvider provider = callback
125: .getResourceURLProvider(
126: getHttpServletRequest(),
127: getInternalPortletWindow());
128:
129: if (location.indexOf("://") != -1) {
130: provider.setAbsoluteURL(location);
131: } else {
132: provider.setFullPath(location);
133: }
134: location = redirectResponse.encodeRedirectURL(provider
135: .toString());
136: //redirectResponse.sendRedirect(location);
137: redirectLocation = location;
138: redirected = true;
139: }
140: } else {
141: throw new java.lang.IllegalStateException(
142: "Can't invoke sendRedirect() after certain methods have been called");
143: }
144:
145: }
146:
147: public void setRenderParameters(Map parameters) {
148: if (redirected) {
149: throw new IllegalStateException(
150: "Can't invoke setRenderParameters() after sendRedirect() has been called");
151: }
152: if (parameters == null) {
153: throw new IllegalArgumentException(
154: "Render parameters must not be null.");
155: }
156: for (Iterator iter = parameters.entrySet().iterator(); iter
157: .hasNext();) {
158: Map.Entry entry = (Map.Entry) iter.next();
159: if (!(entry.getKey() instanceof String)) {
160: throw new IllegalArgumentException(
161: "Key must not be null and of type java.lang.String.");
162: }
163: if (!(entry.getValue() instanceof String[])) {
164: throw new IllegalArgumentException(
165: "Value must not be null and of type java.lang.String[].");
166: }
167: }
168:
169: renderParameters = StringUtils.copyParameters(parameters);
170:
171: redirectAllowed = false;
172: }
173:
174: public void setRenderParameter(String key, String value) {
175: if (redirected) {
176: throw new IllegalStateException(
177: "Can't invoke setRenderParameter() after sendRedirect() has been called");
178: }
179:
180: if ((key == null) || (value == null)) {
181: throw new IllegalArgumentException(
182: "Render parameter key or value must not be null.");
183: }
184:
185: renderParameters.put(key, new String[] { value });
186:
187: redirectAllowed = false;
188: }
189:
190: public void setRenderParameter(String key, String[] values) {
191: if (redirected) {
192: throw new IllegalStateException(
193: "Can't invoke setRenderParameter() after sendRedirect() has been called");
194: }
195:
196: if (key == null || values == null || values.length == 0) {
197: throw new IllegalArgumentException(
198: "Render parameter key or value must not be null or values be an empty array.");
199: }
200:
201: renderParameters.put(key, StringUtils.copy(values));
202:
203: redirectAllowed = false;
204: }
205:
206: // --------------------------------------------------------------------------------------------
207:
208: // org.apache.pluto.core.InternalActionResponse implementation --------------------------------
209: public Map getRenderParameters() {
210: return renderParameters;
211: }
212:
213: public PortletMode getChangedPortletMode() {
214: return this .portletMode;
215: }
216:
217: public WindowState getChangedWindowState() {
218: return this .windowState;
219: }
220:
221: public String getRedirectLocation() {
222: return redirectLocation;
223: }
224:
225: private boolean isPortletModeAllowed(PortletMode mode) {
226: return isPortletModeAllowedByPortlet(mode)
227: && isPortletModeAllowedByPortal(mode);
228: }
229:
230: private boolean isPortletModeAllowedByPortlet(PortletMode mode) {
231: PortletDD dd = getInternalPortletWindow().getPortletEntity()
232: .getPortletDefinition();
233:
234: Iterator supports = dd.getSupports().iterator();
235: while (supports.hasNext()) {
236: SupportsDD sup = (SupportsDD) supports.next();
237: Iterator modes = sup.getPortletModes().iterator();
238: while (modes.hasNext()) {
239: if (modes.next().toString().equalsIgnoreCase(
240: mode.toString())) {
241: return true;
242: }
243: }
244: }
245: return false;
246: }
247:
248: private boolean isPortletModeAllowedByPortal(PortletMode mode) {
249: Enumeration supportedModes = context.getSupportedPortletModes();
250: while (supportedModes.hasMoreElements()) {
251: if (supportedModes.nextElement().toString()
252: .equalsIgnoreCase((mode.toString()))) {
253: return true;
254: }
255: }
256: return false;
257: }
258:
259: private boolean isWindowStateAllowed(WindowState state) {
260: Enumeration supportedStates = context
261: .getSupportedWindowStates();
262: while (supportedStates.hasMoreElements()) {
263: if (supportedStates.nextElement().toString()
264: .equalsIgnoreCase((state.toString()))) {
265: return true;
266: }
267: }
268: return false;
269: }
270:
271: }
|