1   /**
2    * Copyright (c) 2000-2007 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.portlet.PortletServletRequest;
28  import com.liferay.portlet.PortletServletResponse;
29  import com.liferay.portlet.RenderRequestImpl;
30  
31  import java.io.IOException;
32  
33  import java.util.Iterator;
34  import java.util.List;
35  
36  import javax.portlet.PortletResponse;
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          PortletResponse portletRes = (PortletResponse)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             List servletURLPatterns =
106                 renderReq.getPortlet().getServletURLPatterns();
107 
108             Iterator itr = servletURLPatterns.iterator();
109 
110             while (itr.hasNext()) {
111                 String urlPattern = (String)itr.next();
112 
113                 if (urlPattern.endsWith("/*")) {
114                     pos = urlPattern.indexOf("/*");
115 
116                     urlPattern = urlPattern.substring(0, pos);
117 
118                     if (pathNoQueryString.startsWith(urlPattern)) {
119                         pathInfo = pathNoQueryString.substring(
120                             urlPattern.length());
121                         servletPath = urlPattern;
122 
123                         break;
124                     }
125                 }
126             }
127 
128             if ((pathInfo == null) && (servletPath == null)) {
129                 pathInfo = StringPool.BLANK;
130                 servletPath = pathNoQueryString;
131             }
132 
133             requestURI = renderReq.getContextPath() + pathNoQueryString;
134         }
135 
136         PortletServletRequest portletServletReq = new PortletServletRequest(
137             (HttpServletRequest)req, renderReq, pathInfo, queryString,
138             requestURI, servletPath);
139 
140         PortletServletResponse portletServletRes =
141             new PortletServletResponse((HttpServletResponse)res, portletRes);
142 
143         if (include) {
144             _rd.include(portletServletReq, portletServletRes);
145         }
146         else {
147             _rd.forward(portletServletReq, portletServletRes);
148         }
149     }
150 
151     private RequestDispatcher _rd;
152     private String _path;
153 
154 }