001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.portlet.LiferayPortletSession;
020    import com.liferay.portal.kernel.portlet.PortletFilterUtil;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.WebKeys;
023    
024    import java.io.IOException;
025    
026    import javax.portlet.PortletException;
027    import javax.portlet.PortletRequest;
028    import javax.portlet.PortletResponse;
029    import javax.portlet.filter.FilterChain;
030    
031    import javax.servlet.ServletException;
032    import javax.servlet.http.HttpServlet;
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    import javax.servlet.http.HttpSession;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class PortletServlet extends HttpServlet {
041    
042            public static final String PORTLET_APP =
043                    "com.liferay.portal.model.PortletApp";
044    
045            public static final String PORTLET_CLASS_LOADER = "PORTLET_CLASS_LOADER";
046    
047            public static final String PORTLET_SERVLET_CONFIG =
048                    "com.liferay.portal.kernel.servlet.PortletServletConfig";
049    
050            public static final String PORTLET_SERVLET_FILTER_CHAIN =
051                    "com.liferay.portal.kernel.servlet.PortletServletFilterChain";
052    
053            public static final String PORTLET_SERVLET_REQUEST =
054                    "com.liferay.portal.kernel.servlet.PortletServletRequest";
055    
056            public static final String PORTLET_SERVLET_RESPONSE =
057                    "com.liferay.portal.kernel.servlet.PortletServletResponse";
058    
059            @Override
060            public void service(
061                            HttpServletRequest request, HttpServletResponse response)
062                    throws IOException, ServletException {
063    
064                    String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
065    
066                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
067                            JavaConstants.JAVAX_PORTLET_REQUEST);
068    
069                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
070                            JavaConstants.JAVAX_PORTLET_RESPONSE);
071    
072                    String lifecycle = (String)request.getAttribute(
073                            PortletRequest.LIFECYCLE_PHASE);
074    
075                    FilterChain filterChain = (FilterChain)request.getAttribute(
076                            PORTLET_SERVLET_FILTER_CHAIN);
077    
078                    LiferayPortletSession portletSession =
079                            (LiferayPortletSession)portletRequest.getPortletSession();
080    
081                    portletRequest.setAttribute(WebKeys.PORTLET_ID, portletId);
082                    portletRequest.setAttribute(PORTLET_SERVLET_CONFIG, getServletConfig());
083                    portletRequest.setAttribute(PORTLET_SERVLET_REQUEST, request);
084                    portletRequest.setAttribute(PORTLET_SERVLET_RESPONSE, response);
085    
086                    HttpSession session = request.getSession();
087    
088                    PortletSessionTracker.add(session);
089    
090                    portletSession.setHttpSession(session);
091    
092                    try {
093                            PortletFilterUtil.doFilter(
094                                    portletRequest, portletResponse, lifecycle, filterChain);
095                    }
096                    catch (PortletException pe) {
097                            _log.error(pe, pe);
098    
099                            throw new ServletException(pe);
100                    }
101            }
102    
103            private static Log _log = LogFactoryUtil.getLog(PortletServlet.class);
104    
105    }