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.kernel.servlet;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
25  import com.liferay.portal.kernel.portlet.PortletFilterUtil;
26  import com.liferay.portal.kernel.util.JavaConstants;
27  import com.liferay.portal.kernel.util.WebKeys;
28  
29  import java.io.IOException;
30  
31  import javax.portlet.PortletException;
32  import javax.portlet.PortletRequest;
33  import javax.portlet.PortletResponse;
34  import javax.portlet.filter.FilterChain;
35  
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServlet;
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.http.HttpServletResponse;
40  import javax.servlet.http.HttpSession;
41  
42  /**
43   * <a href="PortletServlet.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class PortletServlet extends HttpServlet {
49  
50      public static final String PORTLET_CLASS_LOADER = "PORTLET_CLASS_LOADER";
51  
52      public static final String PORTLET_SERVLET_CONFIG =
53          "com.liferay.portal.kernel.servlet.PortletServletConfig";
54  
55      public static final String PORTLET_SERVLET_FILTER_CHAIN =
56          "com.liferay.portal.kernel.servlet.PortletServletFilterChain";
57  
58      public static final String PORTLET_SERVLET_REQUEST =
59          "com.liferay.portal.kernel.servlet.PortletServletRequest";
60  
61      public static final String PORTLET_SERVLET_RESPONSE =
62          "com.liferay.portal.kernel.servlet.PortletServletResponse";
63  
64      public void service(
65              HttpServletRequest request, HttpServletResponse response)
66          throws IOException, ServletException {
67  
68          String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
69  
70          PortletRequest portletRequest = (PortletRequest)request.getAttribute(
71              JavaConstants.JAVAX_PORTLET_REQUEST);
72  
73          PortletResponse portletResponse = (PortletResponse)request.getAttribute(
74              JavaConstants.JAVAX_PORTLET_RESPONSE);
75  
76          String lifecycle = (String)request.getAttribute(
77              PortletRequest.LIFECYCLE_PHASE);
78  
79          FilterChain filterChain = (FilterChain)request.getAttribute(
80              PORTLET_SERVLET_FILTER_CHAIN);
81  
82          LiferayPortletSession portletSession =
83              (LiferayPortletSession)portletRequest.getPortletSession();
84  
85          portletRequest.setAttribute(WebKeys.PORTLET_ID, portletId);
86          portletRequest.setAttribute(PORTLET_SERVLET_CONFIG, getServletConfig());
87          portletRequest.setAttribute(PORTLET_SERVLET_REQUEST, request);
88          portletRequest.setAttribute(PORTLET_SERVLET_RESPONSE, response);
89  
90          HttpSession session = request.getSession();
91  
92          PortletSessionTracker.add(session);
93  
94          portletSession.setHttpSession(session);
95  
96          try {
97              PortletFilterUtil.doFilter(
98                  portletRequest, portletResponse, lifecycle, filterChain);
99          }
100         catch (PortletException pe) {
101             _log.error(pe, pe);
102 
103             throw new ServletException(pe);
104         }
105     }
106 
107     private static Log _log = LogFactoryUtil.getLog(PortletServlet.class);
108 
109 }