001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet;
022:
023: import com.liferay.portal.kernel.servlet.URLEncoder;
024: import com.liferay.portal.kernel.util.ArrayUtil;
025: import com.liferay.portal.kernel.util.StringPool;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.model.Portlet;
028: import com.liferay.portal.servlet.NamespaceServletRequest;
029: import com.liferay.portal.struts.StrutsURLEncoder;
030: import com.liferay.portal.theme.ThemeDisplay;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portal.util.WebKeys;
033: import com.liferay.util.servlet.DynamicServletRequest;
034:
035: import java.io.IOException;
036:
037: import java.util.HashMap;
038: import java.util.Iterator;
039: import java.util.List;
040: import java.util.Map;
041:
042: import javax.portlet.PortletException;
043: import javax.portlet.PortletRequestDispatcher;
044: import javax.portlet.RenderRequest;
045: import javax.portlet.RenderResponse;
046:
047: import javax.servlet.RequestDispatcher;
048: import javax.servlet.ServletException;
049: import javax.servlet.http.HttpServletRequest;
050: import javax.servlet.http.HttpServletResponse;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054: import org.apache.struts.Globals;
055:
056: /**
057: * <a href="PortletRequestDispatcherImpl.java.html"><b><i>View Source</i></b>
058: * </a>
059: *
060: * @author Brian Wing Shun Chan
061: * @author Brian Myunghun Kim
062: *
063: */
064: public class PortletRequestDispatcherImpl implements
065: PortletRequestDispatcher {
066:
067: public PortletRequestDispatcherImpl(RequestDispatcher rd,
068: PortletContextImpl portletCtxImpl) {
069:
070: this (rd, portletCtxImpl, null);
071: }
072:
073: public PortletRequestDispatcherImpl(RequestDispatcher rd,
074: PortletContextImpl portletCtxImpl, String path) {
075:
076: _rd = rd;
077: _portlet = portletCtxImpl.getPortlet();
078: _portletCtxImpl = portletCtxImpl;
079: _path = path;
080: }
081:
082: public void include(RenderRequest req, RenderResponse res)
083: throws IOException, PortletException {
084:
085: include(req, res, false);
086: }
087:
088: public void include(RenderRequest req, RenderResponse res,
089: boolean strutsURLEncoder) throws IOException,
090: PortletException {
091:
092: try {
093: RenderRequestImpl reqImpl = (RenderRequestImpl) req;
094: RenderResponseImpl resImpl = PortalUtil
095: .getRenderResponseImpl(res);
096:
097: HttpServletRequest httpReq = PortalUtil
098: .getHttpServletRequest(req);
099: HttpServletResponse httpRes = PortalUtil
100: .getHttpServletResponse(res);
101:
102: String pathInfo = null;
103: String queryString = null;
104: String requestURI = null;
105: String servletPath = null;
106:
107: if (_path != null) {
108: /*if (ServerDetector.isJetty()) {
109: int pos = _path.indexOf(StringPool.QUESTION);
110:
111: if (pos != -1) {
112: _path = _path.substring(0, pos);
113: }
114: }*/
115:
116: String pathNoQueryString = _path;
117:
118: int pos = _path.indexOf(StringPool.QUESTION);
119:
120: if (pos != -1) {
121: pathNoQueryString = _path.substring(0, pos);
122: queryString = _path.substring(pos + 1, _path
123: .length());
124:
125: Map queryParams = new HashMap();
126:
127: String[] queryParamsArray = StringUtil.split(
128: queryString, StringPool.AMPERSAND);
129:
130: for (int i = 0; i < queryParamsArray.length; i++) {
131: String[] nameValuePair = StringUtil.split(
132: queryParamsArray[i], StringPool.EQUAL);
133: String name = nameValuePair[0];
134: String value = nameValuePair[1];
135:
136: String[] values = (String[]) queryParams
137: .get(name);
138:
139: if (values == null) {
140: queryParams.put(name,
141: new String[] { value });
142: } else {
143: String[] newValues = new String[values.length + 1];
144:
145: System.arraycopy(values, 0, newValues, 0,
146: values.length);
147:
148: newValues[newValues.length - 1] = value;
149:
150: queryParams.put(name, newValues);
151: }
152: }
153:
154: DynamicServletRequest dynamicReq = null;
155:
156: if (reqImpl.isPrivateRequestAttributes()) {
157: String portletNamespace = PortalUtil
158: .getPortletNamespace(reqImpl
159: .getPortletName());
160:
161: dynamicReq = new NamespaceServletRequest(
162: httpReq, portletNamespace,
163: portletNamespace);
164: } else {
165: dynamicReq = new DynamicServletRequest(httpReq);
166: }
167:
168: Iterator itr = queryParams.entrySet().iterator();
169:
170: while (itr.hasNext()) {
171: Map.Entry entry = (Map.Entry) itr.next();
172:
173: String name = (String) entry.getKey();
174: String[] values = (String[]) entry.getValue();
175:
176: String[] oldValues = dynamicReq
177: .getParameterValues(name);
178:
179: if (oldValues == null) {
180: dynamicReq.setParameterValues(name, values);
181: } else {
182: String[] newValues = ArrayUtil.append(
183: values, oldValues);
184:
185: dynamicReq.setParameterValues(name,
186: newValues);
187: }
188: }
189:
190: httpReq = dynamicReq;
191: }
192:
193: List servletURLPatterns = reqImpl.getPortlet()
194: .getServletURLPatterns();
195:
196: Iterator itr = servletURLPatterns.iterator();
197:
198: while (itr.hasNext()) {
199: String urlPattern = (String) itr.next();
200:
201: if (urlPattern.endsWith("/*")) {
202: pos = urlPattern.indexOf("/*");
203:
204: urlPattern = urlPattern.substring(0, pos);
205:
206: if (pathNoQueryString.startsWith(urlPattern)) {
207: pathInfo = pathNoQueryString
208: .substring(urlPattern.length());
209: servletPath = urlPattern;
210:
211: break;
212: }
213: }
214: }
215:
216: if ((pathInfo == null) && (servletPath == null)) {
217: pathInfo = StringPool.BLANK;
218: servletPath = pathNoQueryString;
219: }
220:
221: requestURI = req.getContextPath() + pathNoQueryString;
222: }
223:
224: PortletServletRequest portletServletReq = new PortletServletRequest(
225: httpReq, reqImpl, pathInfo, queryString,
226: requestURI, servletPath);
227:
228: PortletServletResponse portletServletRes = new PortletServletResponse(
229: httpRes, resImpl);
230:
231: URLEncoder urlEncoder = _portlet.getURLEncoderInstance();
232:
233: if (urlEncoder != null) {
234: resImpl.setURLEncoder(urlEncoder);
235: } else if (strutsURLEncoder) {
236: ThemeDisplay themeDisplay = (ThemeDisplay) req
237: .getAttribute(WebKeys.THEME_DISPLAY);
238:
239: resImpl.setURLEncoder(new StrutsURLEncoder(
240: portletServletReq.getContextPath(),
241: themeDisplay.getPathMain(),
242: (String) _portletCtxImpl
243: .getAttribute(Globals.SERVLET_KEY),
244: (com.liferay.portlet.PortletURLImpl) res
245: .createRenderURL()));
246: }
247:
248: _rd.include(portletServletReq, portletServletRes);
249: } catch (ServletException se) {
250: _log.error(se, se);
251:
252: throw new PortletException(se);
253: }
254: }
255:
256: private static Log _log = LogFactory
257: .getLog(PortletRequestDispatcherImpl.class);
258:
259: private RequestDispatcher _rd;
260: private Portlet _portlet;
261: private PortletContextImpl _portletCtxImpl;
262: private String _path;
263:
264: }
|