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.resiliency.spi.agent;
016    
017    import com.liferay.portal.kernel.portlet.ActionResult;
018    import com.liferay.portal.kernel.resiliency.PortalResiliencyException;
019    import com.liferay.portal.kernel.resiliency.spi.SPI;
020    import com.liferay.portal.kernel.resiliency.spi.agent.SPIAgent;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.WebKeys;
023    
024    import java.io.IOException;
025    import java.io.PrintWriter;
026    
027    import java.util.Collections;
028    
029    import javax.portlet.Event;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    /**
035     * @author Shuyang Zhou
036     */
037    public class ErrorSPIAgent implements SPIAgent {
038    
039            @Override
040            public void destroy() {
041            }
042    
043            @Override
044            public void init(SPI spi) {
045            }
046    
047            @Override
048            public HttpServletRequest prepareRequest(HttpServletRequest request) {
049                    return request;
050            }
051    
052            @Override
053            public HttpServletResponse prepareResponse(
054                    HttpServletRequest request, HttpServletResponse response) {
055    
056                    return response;
057            }
058    
059            @Override
060            public void service(
061                            HttpServletRequest request, HttpServletResponse response)
062                    throws PortalResiliencyException {
063    
064                    SPIAgent.Lifecycle lifecycle = (SPIAgent.Lifecycle)request.getAttribute(
065                            WebKeys.SPI_AGENT_LIFECYCLE);
066    
067                    if (lifecycle.equals(SPIAgent.Lifecycle.ACTION)) {
068                            request.setAttribute(
069                                    WebKeys.SPI_AGENT_ACTION_RESULT,
070                                    new ActionResult(
071                                            Collections.<Event>emptyList(), StringPool.SLASH));
072                    }
073                    else if (lifecycle.equals(SPIAgent.Lifecycle.EVENT)) {
074                            request.setAttribute(
075                                    WebKeys.SPI_AGENT_EVENT_RESULT, Collections.emptyList());
076                    }
077                    else if (lifecycle.equals(SPIAgent.Lifecycle.RENDER)) {
078                            try {
079                                    PrintWriter printWriter = response.getWriter();
080    
081                                    printWriter.write("<div class=\"alert alert-error\">");
082                                    printWriter.write("SPI is temporarily unavailable.");
083                                    printWriter.write("</div>");
084                            }
085                            catch (IOException ioe) {
086                                    throw new PortalResiliencyException(ioe);
087                            }
088                    }
089            }
090    
091            @Override
092            public void transferResponse(
093                    HttpServletRequest request, HttpServletResponse response, Exception e) {
094            }
095    
096    }