001    /**
002     * Copyright (c) 2000-present 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.kernel.model.PortletApp";
044    
045            public static final String PORTLET_SERVLET_CONFIG =
046                    "com.liferay.portal.kernel.servlet.PortletServletConfig";
047    
048            public static final String PORTLET_SERVLET_FILTER_CHAIN =
049                    "com.liferay.portal.kernel.servlet.PortletServletFilterChain";
050    
051            public static final String PORTLET_SERVLET_REQUEST =
052                    "com.liferay.portal.kernel.servlet.PortletServletRequest";
053    
054            public static final String PORTLET_SERVLET_RESPONSE =
055                    "com.liferay.portal.kernel.servlet.PortletServletResponse";
056    
057            @Override
058            public void service(
059                            HttpServletRequest request, HttpServletResponse response)
060                    throws IOException, ServletException {
061    
062                    if (request.getAttribute(WebKeys.EXTEND_SESSION) != null) {
063                            request.removeAttribute(WebKeys.EXTEND_SESSION);
064    
065                            HttpSession session = request.getSession(false);
066    
067                            if (session != null) {
068                                    session.setAttribute(WebKeys.EXTEND_SESSION, Boolean.TRUE);
069    
070                                    session.removeAttribute(WebKeys.EXTEND_SESSION);
071                            }
072    
073                            return;
074                    }
075    
076                    String portletId = (String)request.getAttribute(WebKeys.PORTLET_ID);
077    
078                    PortletRequest portletRequest = (PortletRequest)request.getAttribute(
079                            JavaConstants.JAVAX_PORTLET_REQUEST);
080    
081                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
082                            JavaConstants.JAVAX_PORTLET_RESPONSE);
083    
084                    String lifecycle = (String)request.getAttribute(
085                            PortletRequest.LIFECYCLE_PHASE);
086    
087                    FilterChain filterChain = (FilterChain)request.getAttribute(
088                            PORTLET_SERVLET_FILTER_CHAIN);
089    
090                    LiferayPortletSession portletSession =
091                            (LiferayPortletSession)portletRequest.getPortletSession();
092    
093                    portletRequest.setAttribute(WebKeys.PORTLET_ID, portletId);
094                    portletRequest.setAttribute(PORTLET_SERVLET_CONFIG, getServletConfig());
095                    portletRequest.setAttribute(PORTLET_SERVLET_REQUEST, request);
096                    portletRequest.setAttribute(PORTLET_SERVLET_RESPONSE, response);
097    
098                    HttpSession session = request.getSession();
099    
100                    PortletSessionTracker.add(session);
101    
102                    portletSession.setHttpSession(session);
103    
104                    try {
105                            PortletFilterUtil.doFilter(
106                                    portletRequest, portletResponse, lifecycle, filterChain);
107                    }
108                    catch (PortletException pe) {
109                            _log.error(pe, pe);
110    
111                            throw new ServletException(pe);
112                    }
113            }
114    
115            private static final Log _log = LogFactoryUtil.getLog(PortletServlet.class);
116    
117    }