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.kernel.servlet;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.portlet.LiferayPortletSession;
28  import com.liferay.portal.kernel.util.JavaConstants;
29  
30  import java.io.IOException;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.Portlet;
35  import javax.portlet.PortletException;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.PortletResponse;
38  import javax.portlet.RenderRequest;
39  import javax.portlet.RenderResponse;
40  
41  import javax.servlet.ServletException;
42  import javax.servlet.http.HttpServlet;
43  import javax.servlet.http.HttpServletRequest;
44  import javax.servlet.http.HttpServletResponse;
45  import javax.servlet.http.HttpSession;
46  
47  /**
48   * <a href="PortletServlet.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class PortletServlet extends HttpServlet {
54  
55      public static final String PORTLET_CLASS_LOADER = "PORTLET_CLASS_LOADER";
56  
57      public static final String PORTLET_SERVLET_CONFIG =
58          "com.liferay.portal.kernel.servlet.PortletServletConfig";
59  
60      public static final String PORTLET_SERVLET_REQUEST =
61          "com.liferay.portal.kernel.servlet.PortletServletRequest";
62  
63      public static final String PORTLET_SERVLET_RESPONSE =
64          "com.liferay.portal.kernel.servlet.PortletServletResponse";
65  
66      public void service(HttpServletRequest req, HttpServletResponse res)
67          throws IOException, ServletException {
68  
69          PortletRequest portletReq = (PortletRequest)req.getAttribute(
70              JavaConstants.JAVAX_PORTLET_REQUEST);
71  
72          PortletResponse portletRes = (PortletResponse)req.getAttribute(
73              JavaConstants.JAVAX_PORTLET_RESPONSE);
74  
75          Portlet portlet = (Portlet)req.getAttribute(
76              JavaConstants.JAVAX_PORTLET_PORTLET);
77  
78          LiferayPortletSession portletSes =
79              (LiferayPortletSession)portletReq.getPortletSession();
80  
81          portletReq.setAttribute(PORTLET_SERVLET_CONFIG, getServletConfig());
82          portletReq.setAttribute(PORTLET_SERVLET_REQUEST, req);
83          portletReq.setAttribute(PORTLET_SERVLET_RESPONSE, res);
84  
85          HttpSession ses = req.getSession();
86  
87          PortletSessionTracker.add(ses);
88  
89          portletSes.setHttpSession(ses);
90  
91          try {
92              if (portletReq instanceof ActionRequest) {
93                  ActionRequest actionReq = (ActionRequest)portletReq;
94                  ActionResponse actionRes = (ActionResponse)portletRes;
95  
96                  portlet.processAction(actionReq, actionRes);
97              }
98              else {
99                  RenderRequest renderReq = (RenderRequest)portletReq;
100                 RenderResponse renderRes = (RenderResponse)portletRes;
101 
102                 portlet.render(renderReq, renderRes);
103             }
104         }
105         catch (PortletException pe) {
106             _log.error(pe, pe);
107 
108             throw new ServletException(pe);
109         }
110     }
111 
112     private static Log _log = LogFactoryUtil.getLog(PortletServlet.class);
113 
114 }