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