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.url.impl;
018:
019: import java.io.UnsupportedEncodingException;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: import javax.portlet.PortletMode;
024: import javax.portlet.WindowState;
025: import javax.servlet.http.HttpServletRequest;
026:
027: import org.apache.jetspeed.PortalContext;
028: import org.apache.jetspeed.PortalReservedParameters;
029: import org.apache.jetspeed.container.state.NavigationalState;
030: import org.apache.jetspeed.container.url.BasePortalURL;
031: import org.apache.jetspeed.desktop.JetspeedDesktop;
032: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
033: import org.apache.pluto.om.window.PortletWindow;
034: import org.apache.pluto.om.entity.PortletEntity;
035: import org.apache.pluto.om.portlet.PortletDefinition;
036:
037: /**
038: * DesktopEncodingPortalURL encodes action URLs to target desktop specific /action pipeline,
039: * and render URLs to target desktop specific /render pipeline
040: *
041: * The query parameters "entity" and "portlet" are added to each url. These parameters are needed in a /render
042: * request and are used by the desktop javascript code for both /render and /action requests.
043: *
044: * @author <a href="mailto:ate@apache.org">Ate Douma</a>
045: * @version $Id: PathInfoEncodingPortalURL.java 367856 2006-01-11 01:04:09Z taylor $
046: */
047: public class DesktopEncodingPortalURL extends AbstractPortalURL {
048: private String baseActionPath = null;
049: private String baseRenderPath = null;
050:
051: private String desktopActionPipelinePath = null;
052: private String desktopRenderPipelinePath = null;
053:
054: public DesktopEncodingPortalURL(NavigationalState navState,
055: PortalContext portalContext,
056: String desktopRenderPipelinePath,
057: String desktopActionPipelinePath) {
058: super (navState, portalContext);
059: initializePipelinePaths(desktopRenderPipelinePath,
060: desktopActionPipelinePath);
061: }
062:
063: public DesktopEncodingPortalURL(NavigationalState navState,
064: PortalContext portalContext,
065: String desktopRenderPipelinePath,
066: String desktopActionPipelinePath, BasePortalURL base) {
067: super (navState, portalContext, base);
068: initializePipelinePaths(desktopRenderPipelinePath,
069: desktopActionPipelinePath);
070: }
071:
072: public DesktopEncodingPortalURL(String characterEncoding,
073: NavigationalState navState, PortalContext portalContext) {
074: super (characterEncoding, navState, portalContext);
075: initializePipelinePaths(null, null);
076: }
077:
078: public DesktopEncodingPortalURL(HttpServletRequest request,
079: String characterEncoding, NavigationalState navState,
080: PortalContext portalContext) {
081: super (request, characterEncoding, navState, portalContext);
082: initializePipelinePaths(null, null);
083: }
084:
085: private void initializePipelinePaths(
086: String desktopRenderPipelinePath,
087: String desktopActionPipelinePath) {
088: if (desktopActionPipelinePath == null
089: || desktopActionPipelinePath.length() == 0)
090: desktopActionPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_ACTION_PIPELINE_PATH;
091: if (desktopActionPipelinePath.charAt(0) != '/')
092: desktopActionPipelinePath = "/" + desktopActionPipelinePath;
093: if (desktopActionPipelinePath.length() > 1
094: && desktopActionPipelinePath
095: .charAt(desktopActionPipelinePath.length() - 1) == '/')
096: desktopActionPipelinePath = desktopActionPipelinePath
097: .substring(0,
098: desktopActionPipelinePath.length() - 1);
099:
100: if (desktopRenderPipelinePath == null
101: || desktopRenderPipelinePath.length() == 0)
102: desktopRenderPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_RENDER_PIPELINE_PATH;
103: if (desktopRenderPipelinePath.charAt(0) != '/')
104: desktopRenderPipelinePath = "/" + desktopRenderPipelinePath;
105: if (desktopRenderPipelinePath.length() > 1
106: && desktopRenderPipelinePath
107: .charAt(desktopRenderPipelinePath.length() - 1) == '/')
108: desktopRenderPipelinePath = desktopRenderPipelinePath
109: .substring(0,
110: desktopRenderPipelinePath.length() - 1);
111:
112: this .desktopRenderPipelinePath = desktopRenderPipelinePath;
113: this .desktopActionPipelinePath = desktopActionPipelinePath;
114: }
115:
116: protected void decodeBasePath(HttpServletRequest request) {
117: super .decodeBasePath(request);
118: if (this .baseActionPath == null) {
119: this .baseActionPath = contextPath
120: + this .desktopActionPipelinePath;
121: this .baseRenderPath = contextPath
122: + this .desktopRenderPipelinePath;
123: }
124: }
125:
126: protected void decodePathAndNavigationalState(
127: HttpServletRequest request) {
128: String path = null;
129: String encodedNavState = null;
130:
131: String pathInfo = request.getPathInfo();
132: if (pathInfo != null) {
133: StringTokenizer tokenizer = new StringTokenizer(request
134: .getPathInfo(), "/");
135: StringBuffer buffer = new StringBuffer();
136: String token;
137: boolean foundNavState = false;
138: String navStatePrefix = getNavigationalStateParameterName()
139: + ":";
140: while (tokenizer.hasMoreTokens()) {
141: token = tokenizer.nextToken();
142: if (!foundNavState && token.startsWith(navStatePrefix)) {
143: foundNavState = true;
144: if (token.length() > navStatePrefix.length()) {
145: encodedNavState = token
146: .substring(navStatePrefix.length());
147: }
148: } else {
149: buffer.append("/");
150: buffer.append(token);
151: }
152: }
153: if (buffer.length() > 0) {
154: path = buffer.toString();
155: } else {
156: path = "/";
157: }
158: }
159: setPath(path);
160: setEncodedNavigationalState(encodedNavState);
161: }
162:
163: protected String createPortletURL(String encodedNavState,
164: boolean secure) {
165: return createPortletURL(encodedNavState, secure, null, false);
166: }
167:
168: protected String createPortletURL(String encodedNavState,
169: boolean secure, PortletWindow window, boolean action) {
170: return createPortletURL(encodedNavState, secure, window,
171: action, false, false);
172: }
173:
174: protected String createPortletURL(String encodedNavState,
175: boolean secure, PortletWindow window, boolean action,
176: boolean resource, boolean desktopRequestNotAjax) {
177: StringBuffer buffer = new StringBuffer("");
178: buffer.append(getBaseURL(secure));
179: if (action) {
180: buffer.append(this .baseActionPath);
181: } else {
182: buffer.append(this .baseRenderPath);
183: }
184: if (encodedNavState != null) {
185: buffer.append("/");
186: buffer.append(getNavigationalStateParameterName());
187: buffer.append(":");
188: buffer.append(encodedNavState);
189: }
190: if (getPath() != null) {
191: buffer.append(getPath());
192: }
193:
194: if (!resource) {
195: if (!desktopRequestNotAjax) {
196: PortletEntity pe = window.getPortletEntity();
197: buffer.append("?entity=").append(pe.getId());
198:
199: PortletDefinition portlet = pe.getPortletDefinition();
200: MutablePortletApplication app = (MutablePortletApplication) portlet
201: .getPortletApplicationDefinition();
202: String uniqueName = app.getName() + "::"
203: + portlet.getName();
204: buffer.append("&portlet=").append(uniqueName);
205: }
206: } else {
207: buffer.append("?encoder=desktop");
208: }
209:
210: return buffer.toString();
211: }
212:
213: public String createPortletURL(PortletWindow window,
214: Map parameters, PortletMode mode, WindowState state,
215: boolean action, boolean secure) {
216: try {
217: boolean resource = !action
218: && parameters
219: .containsKey(PortalReservedParameters.PORTLET_RESOURCE_URL_REQUEST_PARAMETER);
220: boolean desktopRequestNotAjax = false;
221: if (parameters
222: .containsKey(JetspeedDesktop.DESKTOP_REQUEST_NOT_AJAX_PARAMETER)) {
223: desktopRequestNotAjax = true;
224: parameters
225: .remove(JetspeedDesktop.DESKTOP_REQUEST_NOT_AJAX_PARAMETER);
226: }
227: return createPortletURL(this .getNavigationalState().encode(
228: window, parameters, mode, state, action), secure,
229: window, action, resource, desktopRequestNotAjax);
230: } catch (UnsupportedEncodingException e) {
231: // should never happen
232: e.printStackTrace();
233: // to keep the compiler happy
234: return null;
235: }
236: }
237: }
|