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.action;
016    
017    import com.liferay.portal.kernel.portlet.ActionResult;
018    import com.liferay.portal.kernel.portlet.PortletContainer;
019    import com.liferay.portal.kernel.portlet.PortletContainerUtil;
020    import com.liferay.portal.kernel.resiliency.spi.agent.SPIAgent;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.resiliency.spi.agent.SPIAgentRequest;
025    import com.liferay.portal.resiliency.spi.agent.SPIAgentResponse;
026    import com.liferay.portal.security.auth.PrincipalThreadLocal;
027    import com.liferay.portal.util.WebKeys;
028    
029    import java.util.List;
030    
031    import javax.portlet.Event;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    import javax.servlet.http.HttpSession;
036    
037    import org.apache.struts.action.Action;
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Shuyang Zhou
044     */
045    public class PortalResiliencyAction extends Action {
046    
047            @Override
048            public ActionForward execute(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            HttpServletRequest request, HttpServletResponse response)
051                    throws Exception {
052    
053                    SPIAgentRequest spiAgentRequest = (SPIAgentRequest)request.getAttribute(
054                            WebKeys.SPI_AGENT_REQUEST);
055    
056                    HttpSession session = request.getSession();
057    
058                    spiAgentRequest.populateSessionAttributes(session);
059    
060                    PrincipalThreadLocal.setPassword(
061                            (String)session.getAttribute(WebKeys.USER_PASSWORD));
062    
063                    try {
064                            _doExecute(request, response);
065                    }
066                    finally {
067                            SPIAgentResponse spiAgentResponse =
068                                    (SPIAgentResponse)request.getAttribute(
069                                            WebKeys.SPI_AGENT_RESPONSE);
070    
071                            spiAgentResponse.captureRequestSessionAttributes(request);
072    
073                            request.setAttribute(
074                                    WebKeys.PORTAL_RESILIENCY_ACTION, Boolean.TRUE);
075                    }
076    
077                    return null;
078            }
079    
080            private void _doExecute(
081                            HttpServletRequest request, HttpServletResponse response)
082                    throws Exception {
083    
084                    PortletContainer portletContainer =
085                            PortletContainerUtil.getPortletContainer();
086    
087                    Portlet portlet = (Portlet)request.getAttribute(
088                            WebKeys.SPI_AGENT_PORTLET);
089    
090                    String portletId = ParamUtil.getString(request, "p_p_id");
091    
092                    if (portletId.equals(portlet.getPortletId())) {
093                            portletContainer.preparePortlet(request, portlet);
094                    }
095    
096                    SPIAgent.Lifecycle lifecycle = (SPIAgent.Lifecycle)request.getAttribute(
097                            WebKeys.SPI_AGENT_LIFECYCLE);
098    
099                    switch (lifecycle) {
100                            case ACTION :
101    
102                                    // Action and event requests can make a transient change to a
103                                    // layout's type settings. The fix should be done via a regular
104                                    // request attribute, but to avoid a massive refactor, the SPI
105                                    // simply sends back the modified layout type settings to the
106                                    // MPI.
107    
108                                    Layout requestLayout = (Layout)request.getAttribute(
109                                            WebKeys.LAYOUT);
110    
111                                    String typeSettings = requestLayout.getTypeSettings();
112    
113                                    ActionResult actionResult = portletContainer.processAction(
114                                            request, response, portlet);
115    
116                                    String newTypeSettings = requestLayout.getTypeSettings();
117    
118                                    if (!newTypeSettings.equals(typeSettings)) {
119                                            request.setAttribute(
120                                                    WebKeys.SPI_AGENT_LAYOUT_TYPE_SETTINGS,
121                                                    newTypeSettings);
122                                    }
123    
124                                    request.setAttribute(
125                                            WebKeys.SPI_AGENT_ACTION_RESULT, actionResult);
126    
127                                    break;
128    
129                            case EVENT :
130                                    requestLayout = (Layout)request.getAttribute(WebKeys.LAYOUT);
131    
132                                    typeSettings = requestLayout.getTypeSettings();
133    
134                                    Layout layout = (Layout)request.getAttribute(
135                                            WebKeys.SPI_AGENT_LAYOUT);
136    
137                                    Event event = (Event)request.getAttribute(
138                                            WebKeys.SPI_AGENT_EVENT);
139    
140                                    List<Event> events = portletContainer.processEvent(
141                                            request, response, portlet, layout, event);
142    
143                                    newTypeSettings = requestLayout.getTypeSettings();
144    
145                                    if (!newTypeSettings.equals(typeSettings)) {
146                                            request.setAttribute(
147                                                    WebKeys.SPI_AGENT_LAYOUT_TYPE_SETTINGS,
148                                                    newTypeSettings);
149                                    }
150    
151                                    request.setAttribute(WebKeys.SPI_AGENT_EVENT_RESULT, events);
152    
153                                    break;
154    
155                            case RENDER :
156                                    portletContainer.render(request, response, portlet);
157    
158                                    break;
159    
160                            case RESOURCE :
161                                    portletContainer.serveResource(request, response, portlet);
162    
163                                    break;
164    
165                            default :
166                                    throw new IllegalArgumentException(
167                                            "Unkown lifecycle " + lifecycle);
168                    }
169            }
170    
171    }