1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.apache.bridges.struts;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.util.JavaConstants;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.model.PortletApp;
30  import com.liferay.portal.portletcontainer.WindowInvokerUtil;
31  import com.liferay.portal.service.PortletLocalServiceUtil;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portlet.PortletRequestImpl;
35  import com.liferay.portlet.PortletResponseImpl;
36  import com.liferay.portlet.PortletServletRequest;
37  import com.liferay.portlet.PortletServletResponse;
38  
39  import com.sun.portal.portletcontainer.portlet.impl.RDRequestWrapper;
40  import com.sun.portal.portletcontainer.portlet.impl.RDResponseWrapper;
41  
42  import java.io.IOException;
43  
44  import java.util.Set;
45  
46  import javax.portlet.PortletRequest;
47  import javax.portlet.PortletResponse;
48  
49  import javax.servlet.RequestDispatcher;
50  import javax.servlet.ServletException;
51  import javax.servlet.ServletRequest;
52  import javax.servlet.ServletResponse;
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  /**
57   * <a href="LiferayRequestDispatcher.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Michael Young
60   * @author Brian Myunghun Kim
61   * @author Brian Wing Shun Chan
62   * @author Deepak Gothe
63   *
64   */
65  public class LiferayRequestDispatcher implements RequestDispatcher {
66  
67      public LiferayRequestDispatcher(
68          RequestDispatcher requestDispatcher, String path) {
69  
70          _requestDispatcher = requestDispatcher;
71          _path = path;
72      }
73  
74      public void forward(
75              ServletRequest servletRequest, ServletResponse servletResponse)
76          throws IOException, ServletException {
77  
78          PortletRequest portletRequest =
79              (PortletRequest)servletRequest.getAttribute(
80                  JavaConstants.JAVAX_PORTLET_REQUEST);
81  
82          if (portletRequest != null) {
83              invoke(servletRequest, servletResponse, false);
84          }
85          else {
86              _requestDispatcher.forward(servletRequest, servletResponse);
87          }
88      }
89  
90      public void include(
91              ServletRequest servletRequest, ServletResponse servletResponse)
92          throws IOException, ServletException {
93  
94          PortletRequest portletRequest =
95              (PortletRequest)servletRequest.getAttribute(
96                  JavaConstants.JAVAX_PORTLET_REQUEST);
97  
98          if (portletRequest != null) {
99              invoke(servletRequest, servletResponse, true);
100         }
101         else {
102             _requestDispatcher.include(servletRequest, servletResponse);
103         }
104     }
105 
106     public void invoke(
107             ServletRequest servletRequest, ServletResponse servletResponse,
108             boolean include)
109         throws IOException, ServletException {
110 
111         String pathInfo = null;
112         String queryString = null;
113         String requestURI = null;
114         String servletPath = null;
115 
116         PortletRequest portletRequest =
117             (PortletRequest)servletRequest.getAttribute(
118                 JavaConstants.JAVAX_PORTLET_REQUEST);
119 
120         PortletResponse portletResponse =
121             (PortletResponse)servletRequest.getAttribute(
122                 JavaConstants.JAVAX_PORTLET_RESPONSE);
123 
124         if (_path != null) {
125             String pathNoQueryString = _path;
126 
127             int pos = _path.indexOf(StringPool.QUESTION);
128 
129             if (pos != -1) {
130                 pathNoQueryString = _path.substring(0, pos);
131                 queryString = _path.substring(pos + 1, _path.length());
132             }
133 
134             Set<String> servletURLPatterns = getServletURLPatterns(
135                 servletRequest, portletRequest, portletResponse);
136 
137             for (String urlPattern : servletURLPatterns) {
138                 if (urlPattern.endsWith("/*")) {
139                     pos = urlPattern.indexOf("/*");
140 
141                     urlPattern = urlPattern.substring(0, pos);
142 
143                     if (pathNoQueryString.startsWith(urlPattern)) {
144                         pathInfo = pathNoQueryString.substring(
145                             urlPattern.length());
146                         servletPath = urlPattern;
147 
148                         break;
149                     }
150                 }
151             }
152 
153             if ((pathInfo == null) && (servletPath == null)) {
154                 pathInfo = StringPool.BLANK;
155                 servletPath = pathNoQueryString;
156             }
157 
158             requestURI = portletRequest.getContextPath() + pathNoQueryString;
159         }
160 
161         HttpServletRequest portletServletRequest = getPortletServletRequest(
162             servletRequest, portletRequest, pathInfo, queryString, requestURI,
163             servletPath, include);
164 
165         HttpServletResponse portletServletResponse =
166             getPortletServletResponse(
167                 servletResponse, portletRequest, portletResponse, include);
168 
169         if (include) {
170             _requestDispatcher.include(
171                 portletServletRequest, portletServletResponse);
172         }
173         else {
174             _requestDispatcher.forward(
175                 portletServletRequest, portletServletResponse);
176         }
177     }
178 
179     protected HttpServletRequest getPortletServletRequest(
180         ServletRequest servletRequest, PortletRequest portletRequest,
181         String pathInfo, String queryString, String requestURI,
182         String servletPath, boolean include) {
183 
184         HttpServletRequest request = (HttpServletRequest)servletRequest;
185         boolean named = false;
186 
187         if (PropsValues.PORTLET_CONTAINER_IMPL_SUN) {
188             String lifecyclePhase = (String)portletRequest.getAttribute(
189                 PortletRequest.LIFECYCLE_PHASE);
190 
191             return new RDRequestWrapper(
192                 null, request, portletRequest, requestURI, servletPath,
193                 pathInfo, queryString, lifecyclePhase, named, include);
194         }
195         else {
196             PortletRequestImpl portletRequestImpl =
197                 (PortletRequestImpl)portletRequest;
198 
199             return new PortletServletRequest(
200                 request, portletRequestImpl, pathInfo, queryString, requestURI,
201                 servletPath, named, include);
202         }
203     }
204 
205     protected HttpServletResponse getPortletServletResponse(
206         ServletResponse servletResponse, PortletRequest portletRequest,
207         PortletResponse portletResponse, boolean include) {
208 
209         HttpServletResponse response = (HttpServletResponse)servletResponse;
210 
211         if (PropsValues.PORTLET_CONTAINER_IMPL_SUN) {
212             String lifecyclePhase = (String)portletRequest.getAttribute(
213                 PortletRequest.LIFECYCLE_PHASE);
214 
215             return new RDResponseWrapper(
216                 response, portletResponse, lifecyclePhase, include);
217         }
218         else {
219             PortletResponseImpl portletResponseImpl =
220                 (PortletResponseImpl)portletResponse;
221 
222             return new PortletServletResponse(
223                 response, portletResponseImpl, include);
224         }
225     }
226 
227     protected Set<String> getServletURLPatterns(
228             ServletRequest servletRequest, PortletRequest portletRequest,
229             PortletResponse portletResponse)
230         throws ServletException {
231 
232         if (PropsValues.PORTLET_CONTAINER_IMPL_SUN) {
233             try {
234                 HttpServletRequest request = (HttpServletRequest)servletRequest;
235 
236                 long companyId = PortalUtil.getCompanyId(request);
237                 String portletId = WindowInvokerUtil.getPortletId(
238                     portletResponse.getNamespace());
239 
240                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
241                     companyId, portletId);
242 
243                 PortletApp portletApp = portlet.getPortletApp();
244 
245                 return portletApp.getServletURLPatterns();
246             }
247             catch (SystemException se) {
248                 throw new ServletException(se);
249             }
250         }
251         else {
252             PortletRequestImpl portletRequestImpl =
253                 (PortletRequestImpl)portletRequest;
254 
255             Portlet portlet = portletRequestImpl.getPortlet();
256 
257             PortletApp portletApp = portlet.getPortletApp();
258 
259             return portletApp.getServletURLPatterns();
260         }
261     }
262 
263     private RequestDispatcher _requestDispatcher;
264     private String _path;
265 
266 }