1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LiferayRequestDispatcher implements RequestDispatcher {
52  
53      public LiferayRequestDispatcher(
54          RequestDispatcher requestDispatcher, String path) {
55  
56          _requestDispatcher = requestDispatcher;
57          _path = path;
58      }
59  
60      public void forward(
61              ServletRequest servletRequest, ServletResponse servletResponse)
62          throws IOException, ServletException {
63  
64          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
65                  null) {
66  
67              invoke(servletRequest, servletResponse, false);
68          }
69          else {
70              _requestDispatcher.forward(servletRequest, servletResponse);
71          }
72      }
73  
74      public void include(
75              ServletRequest servletRequest, ServletResponse servletResponse)
76          throws IOException, ServletException {
77  
78          if (servletRequest.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST) !=
79                  null) {
80  
81              invoke(servletRequest, servletResponse, true);
82          }
83          else {
84              _requestDispatcher.include(servletRequest, servletResponse);
85          }
86      }
87  
88      public void invoke(
89              ServletRequest servletRequest, ServletResponse servletResponse,
90              boolean include)
91          throws IOException, ServletException {
92  
93          String pathInfo = null;
94          String queryString = null;
95          String requestURI = null;
96          String servletPath = null;
97  
98          PortletRequestImpl portletRequestImpl =
99              (PortletRequestImpl)servletRequest.getAttribute(
100                 JavaConstants.JAVAX_PORTLET_REQUEST);
101 
102         PortletResponseImpl portletResponseImpl =
103             (PortletResponseImpl)servletRequest.getAttribute(
104                 JavaConstants.JAVAX_PORTLET_RESPONSE);
105 
106         if (_path != null) {
107             String pathNoQueryString = _path;
108 
109             int pos = _path.indexOf(StringPool.QUESTION);
110 
111             if (pos != -1) {
112                 pathNoQueryString = _path.substring(0, pos);
113                 queryString = _path.substring(pos + 1, _path.length());
114             }
115 
116             Portlet portlet = portletRequestImpl.getPortlet();
117 
118             PortletApp portletApp = portlet.getPortletApp();
119 
120             Set<String> servletURLPatterns = portletApp.getServletURLPatterns();
121 
122             for (String urlPattern : servletURLPatterns) {
123                 if (urlPattern.endsWith("/*")) {
124                     pos = urlPattern.indexOf("/*");
125 
126                     urlPattern = urlPattern.substring(0, pos);
127 
128                     if (pathNoQueryString.startsWith(urlPattern)) {
129                         pathInfo = pathNoQueryString.substring(
130                             urlPattern.length());
131                         servletPath = urlPattern;
132 
133                         break;
134                     }
135                 }
136             }
137 
138             if ((pathInfo == null) && (servletPath == null)) {
139                 pathInfo = StringPool.BLANK;
140                 servletPath = pathNoQueryString;
141             }
142 
143             requestURI =
144                 portletRequestImpl.getContextPath() + pathNoQueryString;
145         }
146 
147         PortletServletRequest portletServletRequest = new PortletServletRequest(
148             (HttpServletRequest)servletRequest, portletRequestImpl, pathInfo,
149             queryString, requestURI, servletPath, false, include);
150 
151         PortletServletResponse portletServletResponse =
152             new PortletServletResponse(
153                 (HttpServletResponse)servletResponse, portletResponseImpl,
154                 include);
155 
156         if (include) {
157             _requestDispatcher.include(
158                 portletServletRequest, portletServletResponse);
159         }
160         else {
161             _requestDispatcher.forward(
162                 portletServletRequest, portletServletResponse);
163         }
164     }
165 
166     private RequestDispatcher _requestDispatcher;
167     private String _path;
168 
169 }