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.PortletRequestImpl;
30  import com.liferay.portlet.PortletResponseImpl;
31  import com.liferay.portlet.PortletServletRequest;
32  import com.liferay.portlet.PortletServletResponse;
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(
55          RequestDispatcher requestDispatcher, String path) {
56  
57          _requestDispatcher = requestDispatcher;
58          _path = path;
59      }
60  
61      public void forward(
62              ServletRequest servletRequest, ServletResponse servletResponse)
63          throws IOException, ServletException {
64  
65          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
66                  null) {
67  
68              invoke(servletRequest, servletResponse, false);
69          }
70          else {
71              _requestDispatcher.forward(servletRequest, servletResponse);
72          }
73      }
74  
75      public void include(
76              ServletRequest servletRequest, ServletResponse servletResponse)
77          throws IOException, ServletException {
78  
79          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
80                  null) {
81  
82              invoke(servletRequest, servletResponse, true);
83          }
84          else {
85              _requestDispatcher.include(servletRequest, servletResponse);
86          }
87      }
88  
89      public void invoke(
90              ServletRequest servletRequest, ServletResponse servletResponse,
91              boolean include)
92          throws IOException, ServletException {
93  
94          String pathInfo = null;
95          String queryString = null;
96          String requestURI = null;
97          String servletPath = null;
98  
99          PortletRequestImpl portletRequestImpl =
100             (PortletRequestImpl)servletRequest.getAttribute(
101                 JavaConstants.JAVAX_PORTLET_REQUEST);
102 
103         PortletResponseImpl portletResponseImpl =
104             (PortletResponseImpl)servletRequest.getAttribute(
105                 JavaConstants.JAVAX_PORTLET_RESPONSE);
106 
107         if (_path != null) {
108             String pathNoQueryString = _path;
109 
110             int pos = _path.indexOf(StringPool.QUESTION);
111 
112             if (pos != -1) {
113                 pathNoQueryString = _path.substring(0, pos);
114                 queryString = _path.substring(pos + 1, _path.length());
115             }
116 
117             Portlet portlet = portletRequestImpl.getPortlet();
118 
119             PortletApp portletApp = portlet.getPortletApp();
120 
121             Set<String> servletURLPatterns = portletApp.getServletURLPatterns();
122 
123             for (String urlPattern : servletURLPatterns) {
124                 if (urlPattern.endsWith("/*")) {
125                     pos = urlPattern.indexOf("/*");
126 
127                     urlPattern = urlPattern.substring(0, pos);
128 
129                     if (pathNoQueryString.startsWith(urlPattern)) {
130                         pathInfo = pathNoQueryString.substring(
131                             urlPattern.length());
132                         servletPath = urlPattern;
133 
134                         break;
135                     }
136                 }
137             }
138 
139             if ((pathInfo == null) && (servletPath == null)) {
140                 pathInfo = StringPool.BLANK;
141                 servletPath = pathNoQueryString;
142             }
143 
144             requestURI =
145                 portletRequestImpl.getContextPath() + pathNoQueryString;
146         }
147 
148         PortletServletRequest portletServletRequest = new PortletServletRequest(
149             (HttpServletRequest)servletRequest, portletRequestImpl, pathInfo,
150             queryString, requestURI, servletPath, false, include);
151 
152         PortletServletResponse portletServletResponse =
153             new PortletServletResponse(
154                 (HttpServletResponse)servletResponse, portletResponseImpl,
155                 include);
156 
157         if (include) {
158             _requestDispatcher.include(
159                 portletServletRequest, portletServletResponse);
160         }
161         else {
162             _requestDispatcher.forward(
163                 portletServletRequest, portletServletResponse);
164         }
165     }
166 
167     private RequestDispatcher _requestDispatcher;
168     private String _path;
169 
170 }