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.portal.apache.bridges.struts;
022:
023: import com.liferay.portal.kernel.util.JavaConstants;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portlet.PortletServletRequest;
026: import com.liferay.portlet.PortletServletResponse;
027: import com.liferay.portlet.RenderRequestImpl;
028:
029: import java.io.IOException;
030:
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import javax.portlet.PortletResponse;
035:
036: import javax.servlet.RequestDispatcher;
037: import javax.servlet.ServletException;
038: import javax.servlet.ServletRequest;
039: import javax.servlet.ServletResponse;
040: import javax.servlet.http.HttpServletRequest;
041: import javax.servlet.http.HttpServletResponse;
042:
043: /**
044: * <a href="LiferayRequestDispatcher.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Michael Young
047: * @author Brian Myunghun Kim
048: *
049: */
050: public class LiferayRequestDispatcher implements RequestDispatcher {
051:
052: public LiferayRequestDispatcher(RequestDispatcher rd, String path) {
053: _rd = rd;
054: _path = path;
055: }
056:
057: public void forward(ServletRequest req, ServletResponse res)
058: throws IOException, ServletException {
059:
060: if (req.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) != null) {
061: invoke(req, res, false);
062: } else {
063: _rd.forward(req, res);
064: }
065: }
066:
067: public void include(ServletRequest req, ServletResponse res)
068: throws IOException, ServletException {
069:
070: if (req.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) != null) {
071: invoke(req, res, true);
072: } else {
073: _rd.include(req, res);
074: }
075: }
076:
077: public void invoke(ServletRequest req, ServletResponse res,
078: boolean include) throws IOException, ServletException {
079:
080: String pathInfo = null;
081: String queryString = null;
082: String requestURI = null;
083: String servletPath = null;
084:
085: RenderRequestImpl renderReq = (RenderRequestImpl) req
086: .getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);
087:
088: PortletResponse portletRes = (PortletResponse) req
089: .getAttribute(JavaConstants.JAVAX_PORTLET_RESPONSE);
090:
091: if (_path != null) {
092: String pathNoQueryString = _path;
093:
094: int pos = _path.indexOf(StringPool.QUESTION);
095:
096: if (pos != -1) {
097: pathNoQueryString = _path.substring(0, pos);
098: queryString = _path.substring(pos + 1, _path.length());
099: }
100:
101: List servletURLPatterns = renderReq.getPortlet()
102: .getServletURLPatterns();
103:
104: Iterator itr = servletURLPatterns.iterator();
105:
106: while (itr.hasNext()) {
107: String urlPattern = (String) itr.next();
108:
109: if (urlPattern.endsWith("/*")) {
110: pos = urlPattern.indexOf("/*");
111:
112: urlPattern = urlPattern.substring(0, pos);
113:
114: if (pathNoQueryString.startsWith(urlPattern)) {
115: pathInfo = pathNoQueryString
116: .substring(urlPattern.length());
117: servletPath = urlPattern;
118:
119: break;
120: }
121: }
122: }
123:
124: if ((pathInfo == null) && (servletPath == null)) {
125: pathInfo = StringPool.BLANK;
126: servletPath = pathNoQueryString;
127: }
128:
129: requestURI = renderReq.getContextPath() + pathNoQueryString;
130: }
131:
132: PortletServletRequest portletServletReq = new PortletServletRequest(
133: (HttpServletRequest) req, renderReq, pathInfo,
134: queryString, requestURI, servletPath);
135:
136: PortletServletResponse portletServletRes = new PortletServletResponse(
137: (HttpServletResponse) res, portletRes);
138:
139: if (include) {
140: _rd.include(portletServletReq, portletServletRes);
141: } else {
142: _rd.forward(portletServletReq, portletServletRes);
143: }
144: }
145:
146: private RequestDispatcher _rd;
147: private String _path;
148:
149: }
|