001    /**
002     * Copyright (c) 2000-2013 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.resiliency.spi.agent;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.resiliency.spi.SPI;
020    import com.liferay.portal.kernel.resiliency.spi.SPIUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.util.PortalUtil;
023    
024    import java.io.IOException;
025    
026    import javax.servlet.RequestDispatcher;
027    import javax.servlet.ServletContext;
028    import javax.servlet.http.HttpServlet;
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.http.HttpServletResponse;
031    import javax.servlet.http.HttpSession;
032    
033    /**
034     * @author Shuyang Zhou
035     */
036    public class AcceptorServlet extends HttpServlet {
037    
038            protected void doService(
039                            HttpServletRequest request, HttpServletResponse response)
040                    throws IOException {
041    
042                    PortalUtil.setPortalPort(request);
043    
044                    ServletContext servletContext = getServletContext();
045    
046                    String uriPath = PortalUtil.getPathContext();
047    
048                    if (uriPath.isEmpty()) {
049                            uriPath = StringPool.SLASH;
050                    }
051    
052                    ServletContext portalServletContext = servletContext.getContext(
053                            uriPath);
054    
055                    RequestDispatcher requestDispatcher =
056                            portalServletContext.getRequestDispatcher("/c/portal/resiliency");
057    
058                    SPI spi = SPIUtil.getSPI();
059    
060                    SPIAgent spiAgent = spi.getSPIAgent();
061    
062                    HttpServletRequest spiAgentHttpServletRequest = spiAgent.prepareRequest(
063                            request);
064    
065                    HttpServletResponse spiAgentHttpServletResponse =
066                            spiAgent.prepareResponse(request, response);
067    
068                    Exception exception = null;
069    
070                    try {
071                            requestDispatcher.forward(
072                                    spiAgentHttpServletRequest, spiAgentHttpServletResponse);
073                    }
074                    catch (Exception e) {
075                            exception = e;
076                    }
077    
078                    spiAgent.transferResponse(
079                            spiAgentHttpServletRequest, spiAgentHttpServletResponse, exception);
080    
081                    HttpSession session = spiAgentHttpServletRequest.getSession();
082    
083                    session.invalidate();
084            }
085    
086            @Override
087            protected void service(
088                            HttpServletRequest request, HttpServletResponse response)
089                    throws IOException {
090    
091                    try {
092                            doService(request, response);
093                    }
094                    catch (IOException ioe) {
095                            _log.error(ioe, ioe);
096    
097                            throw ioe;
098                    }
099                    catch (RuntimeException re) {
100                            _log.error(re, re);
101    
102                            throw re;
103                    }
104            }
105    
106            private static Log _log = LogFactoryUtil.getLog(AcceptorServlet.class);
107    
108    }