1   /**
2    * Copyright (c) 2000-2008 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.kernel.util.JavaConstants;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Portlet;
28  import com.liferay.portal.model.PortletApp;
29  import com.liferay.portlet.PortletResponseImpl;
30  import com.liferay.portlet.PortletServletRequest;
31  import com.liferay.portlet.PortletServletResponse;
32  import com.liferay.portlet.RenderRequestImpl;
33  
34  import java.io.IOException;
35  
36  import java.util.Set;
37  
38  import javax.servlet.RequestDispatcher;
39  import javax.servlet.ServletException;
40  import javax.servlet.ServletRequest;
41  import javax.servlet.ServletResponse;
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="LiferayRequestDispatcher.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Michael Young
49   * @author Brian Myunghun Kim
50   *
51   */
52  public class LiferayRequestDispatcher implements RequestDispatcher {
53  
54      public LiferayRequestDispatcher(RequestDispatcher rd, String path) {
55          _rd = rd;
56          _path = path;
57      }
58  
59      public void forward(ServletRequest req, ServletResponse res)
60          throws IOException, ServletException {
61  
62          if (req.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) != null) {
63              invoke(req, res, false);
64          }
65          else {
66              _rd.forward(req, res);
67          }
68      }
69  
70      public void include(ServletRequest req, ServletResponse res)
71          throws IOException, ServletException {
72  
73          if (req.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) != null) {
74              invoke(req, res, true);
75          }
76          else {
77              _rd.include(req, res);
78          }
79      }
80  
81      public void invoke(ServletRequest req, ServletResponse res, boolean include)
82          throws IOException, ServletException {
83  
84          String pathInfo = null;
85          String queryString = null;
86          String requestURI = null;
87          String servletPath = null;
88  
89          RenderRequestImpl renderReq = (RenderRequestImpl)req.getAttribute(
90              JavaConstants.JAVAX_PORTLET_REQUEST);
91  
92          PortletResponseImpl portletRes = (PortletResponseImpl)req.getAttribute(
93              JavaConstants.JAVAX_PORTLET_RESPONSE);
94  
95          if (_path != null) {
96              String pathNoQueryString = _path;
97  
98              int pos = _path.indexOf(StringPool.QUESTION);
99  
100             if (pos != -1) {
101                 pathNoQueryString = _path.substring(0, pos);
102                 queryString = _path.substring(pos + 1, _path.length());
103             }
104 
105             Portlet portlet = renderReq.getPortlet();
106 
107             PortletApp portletApp = portlet.getPortletApp();
108 
109             Set<String> servletURLPatterns = portletApp.getServletURLPatterns();
110 
111             for (String urlPattern : servletURLPatterns) {
112                 if (urlPattern.endsWith("/*")) {
113                     pos = urlPattern.indexOf("/*");
114 
115                     urlPattern = urlPattern.substring(0, pos);
116 
117                     if (pathNoQueryString.startsWith(urlPattern)) {
118                         pathInfo = pathNoQueryString.substring(
119                             urlPattern.length());
120                         servletPath = urlPattern;
121 
122                         break;
123                     }
124                 }
125             }
126 
127             if ((pathInfo == null) && (servletPath == null)) {
128                 pathInfo = StringPool.BLANK;
129                 servletPath = pathNoQueryString;
130             }
131 
132             requestURI = renderReq.getContextPath() + pathNoQueryString;
133         }
134 
135         PortletServletRequest portletServletReq = new PortletServletRequest(
136             (HttpServletRequest)req, renderReq, pathInfo, queryString,
137             requestURI, servletPath, false, include);
138 
139         PortletServletResponse portletServletRes = new PortletServletResponse(
140             (HttpServletResponse)res, portletRes, include);
141 
142         if (include) {
143             _rd.include(portletServletReq, portletServletRes);
144         }
145         else {
146             _rd.forward(portletServletReq, portletServletRes);
147         }
148     }
149 
150     private RequestDispatcher _rd;
151     private String _path;
152 
153 }