1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.apache.bridges.struts;
21  
22  import com.liferay.portal.kernel.util.JavaConstants;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.model.Portlet;
25  import com.liferay.portal.model.PortletApp;
26  import com.liferay.portlet.PortletRequestImpl;
27  import com.liferay.portlet.PortletResponseImpl;
28  import com.liferay.portlet.PortletServletRequest;
29  import com.liferay.portlet.PortletServletResponse;
30  
31  import java.io.IOException;
32  
33  import java.util.Set;
34  
35  import javax.servlet.RequestDispatcher;
36  import javax.servlet.ServletException;
37  import javax.servlet.ServletRequest;
38  import javax.servlet.ServletResponse;
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  /**
43   * <a href="LiferayRequestDispatcher.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Michael Young
46   * @author Brian Myunghun Kim
47   *
48   */
49  public class LiferayRequestDispatcher implements RequestDispatcher {
50  
51      public LiferayRequestDispatcher(
52          RequestDispatcher requestDispatcher, String path) {
53  
54          _requestDispatcher = requestDispatcher;
55          _path = path;
56      }
57  
58      public void forward(
59              ServletRequest servletRequest, ServletResponse servletResponse)
60          throws IOException, ServletException {
61  
62          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
63                  null) {
64  
65              invoke(servletRequest, servletResponse, false);
66          }
67          else {
68              _requestDispatcher.forward(servletRequest, servletResponse);
69          }
70      }
71  
72      public void include(
73              ServletRequest servletRequest, ServletResponse servletResponse)
74          throws IOException, ServletException {
75  
76          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
77                  null) {
78  
79              invoke(servletRequest, servletResponse, true);
80          }
81          else {
82              _requestDispatcher.include(servletRequest, servletResponse);
83          }
84      }
85  
86      public void invoke(
87              ServletRequest servletRequest, ServletResponse servletResponse,
88              boolean include)
89          throws IOException, ServletException {
90  
91          String pathInfo = null;
92          String queryString = null;
93          String requestURI = null;
94          String servletPath = null;
95  
96          PortletRequestImpl portletRequestImpl =
97              (PortletRequestImpl)servletRequest.getAttribute(
98                  JavaConstants.JAVAX_PORTLET_REQUEST);
99  
100         PortletResponseImpl portletResponseImpl =
101             (PortletResponseImpl)servletRequest.getAttribute(
102                 JavaConstants.JAVAX_PORTLET_RESPONSE);
103 
104         if (_path != null) {
105             String pathNoQueryString = _path;
106 
107             int pos = _path.indexOf(StringPool.QUESTION);
108 
109             if (pos != -1) {
110                 pathNoQueryString = _path.substring(0, pos);
111                 queryString = _path.substring(pos + 1, _path.length());
112             }
113 
114             Portlet portlet = portletRequestImpl.getPortlet();
115 
116             PortletApp portletApp = portlet.getPortletApp();
117 
118             Set<String> servletURLPatterns = portletApp.getServletURLPatterns();
119 
120             for (String urlPattern : servletURLPatterns) {
121                 if (urlPattern.endsWith("/*")) {
122                     pos = urlPattern.indexOf("/*");
123 
124                     urlPattern = urlPattern.substring(0, pos);
125 
126                     if (pathNoQueryString.startsWith(urlPattern)) {
127                         pathInfo = pathNoQueryString.substring(
128                             urlPattern.length());
129                         servletPath = urlPattern;
130 
131                         break;
132                     }
133                 }
134             }
135 
136             if ((pathInfo == null) && (servletPath == null)) {
137                 pathInfo = StringPool.BLANK;
138                 servletPath = pathNoQueryString;
139             }
140 
141             requestURI =
142                 portletRequestImpl.getContextPath() + pathNoQueryString;
143         }
144 
145         PortletServletRequest portletServletRequest = new PortletServletRequest(
146             (HttpServletRequest)servletRequest, portletRequestImpl, pathInfo,
147             queryString, requestURI, servletPath, false, include);
148 
149         PortletServletResponse portletServletResponse =
150             new PortletServletResponse(
151                 (HttpServletResponse)servletResponse, portletResponseImpl,
152                 include);
153 
154         if (include) {
155             _requestDispatcher.include(
156                 portletServletRequest, portletServletResponse);
157         }
158         else {
159             _requestDispatcher.forward(
160                 portletServletRequest, portletServletResponse);
161         }
162     }
163 
164     private RequestDispatcher _requestDispatcher;
165     private String _path;
166 
167 }