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.jetspeed.container;
018:
019: import java.io.IOException;
020: import java.util.Map;
021:
022: import javax.portlet.PortletMode;
023: import javax.portlet.WindowState;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.pluto.PortletContainer;
028: import org.apache.pluto.PortletContainerImpl;
029: import org.apache.pluto.core.InternalActionResponse;
030: import org.apache.pluto.om.window.PortletWindow;
031: import org.apache.pluto.services.information.DynamicInformationProvider;
032: import org.apache.pluto.services.information.InformationProviderAccess;
033: import org.apache.pluto.services.information.PortletURLProvider;
034:
035: import org.apache.jetspeed.desktop.JetspeedDesktop;
036:
037: /**
038: * Desktop Portlet Container implementation. This implementation
039: * redirects only if the query paramater encoder=desktop is NOT specified.
040: * When the encoder=desktop parameter is specified, the 'redirect' URL
041: * is returned in the response body for use by desktop javascript code.
042: *
043: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
044: * @version $Id: $
045: */
046: public class DesktopPortletContainerImpl extends PortletContainerImpl
047: implements PortletContainer {
048: private String desktopPipelinePath = null;
049: private String desktopActionPipelinePath = null;
050: private String desktopRenderPipelinePath = null;
051:
052: public DesktopPortletContainerImpl(String desktopPipelinePath,
053: String desktopActionPipelinePath,
054: String desktopRenderPipelinePath) {
055: if (desktopPipelinePath == null
056: || desktopPipelinePath.length() == 0)
057: desktopPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_PIPELINE_PATH;
058: if (desktopPipelinePath.charAt(0) != '/')
059: desktopPipelinePath = "/" + desktopPipelinePath;
060: if (desktopPipelinePath
061: .charAt(desktopPipelinePath.length() - 1) != '/')
062: desktopPipelinePath = desktopPipelinePath + "/";
063:
064: if (desktopActionPipelinePath == null
065: || desktopActionPipelinePath.length() == 0)
066: desktopActionPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_ACTION_PIPELINE_PATH;
067: if (desktopActionPipelinePath.charAt(0) != '/')
068: desktopActionPipelinePath = "/" + desktopActionPipelinePath;
069: if (desktopActionPipelinePath.charAt(desktopActionPipelinePath
070: .length() - 1) != '/')
071: desktopActionPipelinePath = desktopActionPipelinePath + "/";
072:
073: if (desktopRenderPipelinePath == null
074: || desktopRenderPipelinePath.length() == 0)
075: desktopRenderPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_RENDER_PIPELINE_PATH;
076: if (desktopRenderPipelinePath.charAt(0) != '/')
077: desktopRenderPipelinePath = "/" + desktopRenderPipelinePath;
078: if (desktopRenderPipelinePath.charAt(desktopRenderPipelinePath
079: .length() - 1) != '/')
080: desktopRenderPipelinePath = desktopRenderPipelinePath + "/";
081:
082: this .desktopPipelinePath = desktopPipelinePath;
083: this .desktopActionPipelinePath = desktopActionPipelinePath;
084: this .desktopRenderPipelinePath = desktopRenderPipelinePath;
085: }
086:
087: /**
088: * This redirect does not redirect, instead returns the redirect URL in the response
089: */
090: protected void redirect(String location,
091: PortletWindow portletWindow,
092: HttpServletRequest servletRequest,
093: HttpServletResponse servletResponse,
094: InternalActionResponse _actionResponse) throws IOException {
095: String encoding = servletRequest
096: .getParameter(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER);
097: boolean requestHasDesktopEncoding = false;
098: boolean requestIsDesktopAjax = false;
099: if (encoding != null
100: && encoding
101: .equals(JetspeedDesktop.DESKTOP_ENCODER_REQUEST_PARAMETER_VALUE)) { // used in cases where action request cannot be made via ajax (e.g. form has <input type=file/> element)
102: requestHasDesktopEncoding = true;
103: requestIsDesktopAjax = true;
104: String ajaxOverride = servletRequest
105: .getParameter(JetspeedDesktop.DESKTOP_AJAX_REQUEST_PARAMETER);
106: if (ajaxOverride != null && ajaxOverride.equals("false")) {
107: requestIsDesktopAjax = false;
108: }
109: }
110:
111: if (location == null && _actionResponse != null) {
112: DynamicInformationProvider provider = InformationProviderAccess
113: .getDynamicProvider(servletRequest);
114:
115: // TODO: don't send changes in case of exception -> PORTLET:SPEC:17
116:
117: PortletMode portletMode = provider
118: .getPortletMode(portletWindow);
119: WindowState windowState = provider
120: .getWindowState(portletWindow);
121:
122: // get the changings of this portlet entity that might be set during
123: // action handling
124: // change portlet mode
125: if (_actionResponse.getChangedPortletMode() != null) {
126: portletMode = _actionResponse.getChangedPortletMode();
127: InformationProviderAccess.getDynamicProvider(
128: servletRequest).getPortletActionProvider(
129: portletWindow).changePortletMode(portletMode);
130: }
131: // change window state
132: if (_actionResponse.getChangedWindowState() != null) {
133: windowState = _actionResponse.getChangedWindowState();
134: InformationProviderAccess.getDynamicProvider(
135: servletRequest).getPortletActionProvider(
136: portletWindow).changePortletWindowState(
137: windowState);
138: }
139: // get render parameters
140: Map renderParameter = _actionResponse.getRenderParameters();
141:
142: PortletURLProvider redirectURL = provider
143: .getPortletURLProvider(portletWindow);
144:
145: if (provider.getPortletMode(portletWindow) != null) {
146: redirectURL.setPortletMode(portletMode);
147: }
148: if (provider.getWindowState(portletWindow) != null) {
149: redirectURL.setWindowState(windowState);
150: }
151: if (servletRequest.isSecure()) {
152: redirectURL.setSecure(); // TBD
153: }
154:
155: if (requestHasDesktopEncoding && !requestIsDesktopAjax) { // add parameter to tell DesktopEncodingPortalURL that it should not add extra desktop parameters (e.g. entity and portlet)
156: renderParameter
157: .put(
158: JetspeedDesktop.DESKTOP_REQUEST_NOT_AJAX_PARAMETER,
159: Boolean.TRUE);
160: }
161:
162: redirectURL.clearParameters();
163: redirectURL.setParameters(renderParameter);
164:
165: location = servletResponse.encodeRedirectURL(redirectURL
166: .toString());
167: }
168:
169: javax.servlet.http.HttpServletResponse redirectResponse = servletResponse;
170: while (redirectResponse instanceof javax.servlet.http.HttpServletResponseWrapper) {
171: redirectResponse = (javax.servlet.http.HttpServletResponse) ((javax.servlet.http.HttpServletResponseWrapper) redirectResponse)
172: .getResponse();
173: }
174:
175: if (requestIsDesktopAjax) { // no real redirect will occur; instead, return the redirect URL in the response body
176: location = location.replaceAll(
177: this .desktopActionPipelinePath,
178: this .desktopRenderPipelinePath);
179: redirectResponse.getWriter().print(location);
180: } else { // do real redirect
181: location = location.replaceAll(
182: this .desktopActionPipelinePath,
183: this .desktopPipelinePath);
184: location = location.replaceAll(
185: this .desktopRenderPipelinePath,
186: this .desktopPipelinePath);
187: redirectResponse.sendRedirect(location);
188: }
189: // System.out.println("+++ >>>> location is " + location);
190:
191: }
192:
193: }
|