1   /**
2    * Copyright (c) 2000-2008 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.EventRequest;
35  import javax.portlet.EventResponse;
36  import javax.portlet.PortletException;
37  import javax.portlet.PortletRequest;
38  import javax.portlet.PortletResponse;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  import javax.portlet.ResourceRequest;
42  import javax.portlet.ResourceResponse;
43  import javax.portlet.filter.FilterChain;
44  
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServlet;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import javax.servlet.http.HttpSession;
50  
51  /**
52   * <a href="PortletServlet.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class PortletServlet extends HttpServlet {
58  
59      public static final String PORTLET_CLASS_LOADER = "PORTLET_CLASS_LOADER";
60  
61      public static final String PORTLET_SERVLET_CONFIG =
62          "com.liferay.portal.kernel.servlet.PortletServletConfig";
63  
64      public static final String PORTLET_SERVLET_FILTER_CHAIN =
65          "com.liferay.portal.kernel.servlet.PortletServletFilterChain";
66  
67      public static final String PORTLET_SERVLET_REQUEST =
68          "com.liferay.portal.kernel.servlet.PortletServletRequest";
69  
70      public static final String PORTLET_SERVLET_RESPONSE =
71          "com.liferay.portal.kernel.servlet.PortletServletResponse";
72  
73      public void service(HttpServletRequest req, HttpServletResponse res)
74          throws IOException, ServletException {
75  
76          PortletRequest portletReq = (PortletRequest)req.getAttribute(
77              JavaConstants.JAVAX_PORTLET_REQUEST);
78  
79          PortletResponse portletRes = (PortletResponse)req.getAttribute(
80              JavaConstants.JAVAX_PORTLET_RESPONSE);
81  
82          FilterChain filterChain = (FilterChain)req.getAttribute(
83              PORTLET_SERVLET_FILTER_CHAIN);
84  
85          LiferayPortletSession portletSes =
86              (LiferayPortletSession)portletReq.getPortletSession();
87  
88          portletReq.setAttribute(PORTLET_SERVLET_CONFIG, getServletConfig());
89          portletReq.setAttribute(PORTLET_SERVLET_REQUEST, req);
90          portletReq.setAttribute(PORTLET_SERVLET_RESPONSE, res);
91  
92          HttpSession ses = req.getSession();
93  
94          PortletSessionTracker.add(ses);
95  
96          portletSes.setHttpSession(ses);
97  
98          try {
99              if (portletReq instanceof ActionRequest) {
100                 ActionRequest actionReq = (ActionRequest)portletReq;
101                 ActionResponse actionRes = (ActionResponse)portletRes;
102 
103                 filterChain.doFilter(actionReq, actionRes);
104             }
105             else if (portletReq instanceof EventRequest) {
106                 EventRequest eventReq = (EventRequest)portletReq;
107                 EventResponse eventRes = (EventResponse)portletRes;
108 
109                 filterChain.doFilter(eventReq, eventRes);
110             }
111             else if (portletReq instanceof RenderRequest) {
112                 RenderRequest renderReq = (RenderRequest)portletReq;
113                 RenderResponse renderRes = (RenderResponse)portletRes;
114 
115                 filterChain.doFilter(renderReq, renderRes);
116             }
117             else if (portletReq instanceof ResourceRequest) {
118                 ResourceRequest resourceReq = (ResourceRequest)portletReq;
119                 ResourceResponse resourceRes = (ResourceResponse)portletRes;
120 
121                 filterChain.doFilter(resourceReq, resourceRes);
122             }
123         }
124         catch (PortletException pe) {
125             _log.error(pe, pe);
126 
127             throw new ServletException(pe);
128         }
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(PortletServlet.class);
132 
133 }