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.resiliency.spi.agent;
016    
017    import com.liferay.portal.kernel.resiliency.PortalResiliencyException;
018    import com.liferay.portal.kernel.resiliency.spi.agent.annotation.Direction;
019    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
020    import com.liferay.portal.kernel.servlet.MetaInfoCacheServletResponse;
021    import com.liferay.portal.kernel.servlet.MetaInfoCacheServletResponse.MetaData;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.StringBundler;
025    import com.liferay.portal.kernel.util.WebKeys;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.io.IOException;
031    import java.io.Serializable;
032    
033    import java.nio.ByteBuffer;
034    import java.nio.CharBuffer;
035    
036    import java.util.HashMap;
037    import java.util.Map;
038    import java.util.Set;
039    
040    import javax.servlet.http.HttpServletRequest;
041    import javax.servlet.http.HttpServletResponse;
042    import javax.servlet.http.HttpSession;
043    
044    /**
045     * @author Shuyang Zhou
046     */
047    public class SPIAgentResponse extends SPIAgentSerializable {
048    
049            public SPIAgentResponse(String servletContextName) {
050                    super(servletContextName);
051            }
052    
053            public void captureRequestSessionAttributes(HttpServletRequest request) {
054                    distributedRequestAttributes = extractDistributedRequestAttributes(
055                            request, Direction.RESPONSE);
056    
057                    SPIAgentRequest spiAgentRequest = (SPIAgentRequest)request.getAttribute(
058                            WebKeys.SPI_AGENT_REQUEST);
059    
060                    Map<String, Serializable> originalSessionAttributes =
061                            spiAgentRequest.getOriginalSessionAttributes();
062    
063                    Map<String, Serializable> newSessionAttributes =
064                            extractSessionAttributes(request);
065    
066                    Set<String> removedSessionAttributeNames =
067                            originalSessionAttributes.keySet();
068    
069                    removedSessionAttributeNames.removeAll(newSessionAttributes.keySet());
070    
071                    deltaSessionAttributes = new HashMap<>(newSessionAttributes);
072    
073                    for (String removedSessionAttributeName :
074                                    removedSessionAttributeNames) {
075    
076                            deltaSessionAttributes.put(removedSessionAttributeName, null);
077                    }
078    
079                    captureThreadLocals();
080            }
081    
082            public void captureResponse(
083                            HttpServletRequest request,
084                            BufferCacheServletResponse bufferCacheServletResponse)
085                    throws IOException {
086    
087                    Boolean portalResiliencyAction = (Boolean)request.getAttribute(
088                            WebKeys.PORTAL_RESILIENCY_ACTION);
089    
090                    if (portalResiliencyAction != Boolean.TRUE) {
091                            portalResiliencyResponse = false;
092    
093                            return;
094                    }
095    
096                    portalResiliencyResponse = true;
097    
098                    metaData = bufferCacheServletResponse.getMetaData();
099    
100                    byteData = null;
101    
102                    if (bufferCacheServletResponse.isByteMode()) {
103                            ByteBuffer byteBuffer = bufferCacheServletResponse.getByteBuffer();
104    
105                            if (byteBuffer.remaining() > 0) {
106                                    if (byteBuffer.hasArray()) {
107                                            byte[] byteArray = byteBuffer.array();
108    
109                                            if (byteBuffer.remaining() == byteArray.length) {
110                                                    byteData = byteArray;
111                                            }
112                                    }
113    
114                                    if (byteData == null) {
115                                            byteData = new byte[byteBuffer.remaining()];
116    
117                                            byteBuffer.get(byteData);
118                                    }
119                            }
120                    }
121    
122                    stringData = null;
123    
124                    if (!bufferCacheServletResponse.isCharMode()) {
125                            return;
126                    }
127    
128                    String content = bufferCacheServletResponse.getString();
129    
130                    if (content.length() == 0) {
131                            return;
132                    }
133    
134                    if (ParamUtil.get(
135                                    request, "portalResiliencyPortletShowFooter",
136                                    PropsValues.PORTAL_RESILIENCY_PORTLET_SHOW_FOOTER)) {
137    
138                            int index = content.lastIndexOf("</div>");
139    
140                            if (index > 0) {
141                                    StringBundler sb = new StringBundler(6);
142    
143                                    sb.append(content.substring(0, index));
144                                    sb.append("<div class=\"alert alert-info\"><strong>");
145                                    sb.append("This portlet is from SPI ");
146                                    sb.append(PortalUtil.getPortalLocalPort(false));
147                                    sb.append("</strong></div>");
148                                    sb.append(content.substring(index));
149    
150                                    content = sb.toString();
151                            }
152                    }
153    
154                    stringData = content;
155            }
156    
157            public void populate(
158                            HttpServletRequest request, HttpServletResponse response)
159                    throws PortalResiliencyException {
160    
161                    if (exception != null) {
162                            throw new PortalResiliencyException("SPI exception", exception);
163                    }
164    
165                    if (!portalResiliencyResponse) {
166                            return;
167                    }
168    
169                    String typeSettings = (String)distributedRequestAttributes.remove(
170                            WebKeys.SPI_AGENT_LAYOUT_TYPE_SETTINGS);
171    
172                    if (typeSettings != null) {
173                            Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
174    
175                            layout.setTypeSettings(typeSettings);
176                    }
177    
178                    for (Map.Entry<String, Serializable> entry :
179                                    distributedRequestAttributes.entrySet()) {
180    
181                            request.setAttribute(entry.getKey(), entry.getValue());
182                    }
183    
184                    HttpSession session = request.getSession();
185    
186                    for (Map.Entry<String, Serializable> entry :
187                                    deltaSessionAttributes.entrySet()) {
188    
189                            session.setAttribute(entry.getKey(), entry.getValue());
190                    }
191    
192                    try {
193                            MetaInfoCacheServletResponse.finishResponse(metaData, response);
194    
195                            if (byteData != null) {
196                                    ServletResponseUtil.write(response, ByteBuffer.wrap(byteData));
197                            }
198    
199                            if (stringData != null) {
200                                    ServletResponseUtil.write(
201                                            response, CharBuffer.wrap(stringData));
202                            }
203                    }
204                    catch (IOException ioe) {
205                            throw new PortalResiliencyException(ioe);
206                    }
207    
208                    restoreThreadLocals();
209            }
210    
211            public void setException(Exception exception) {
212                    this.exception = exception;
213            }
214    
215            protected byte[] byteData;
216            protected Map<String, Serializable> deltaSessionAttributes;
217            protected Map<String, Serializable> distributedRequestAttributes;
218            protected Exception exception;
219            protected MetaData metaData;
220            protected boolean portalResiliencyResponse;
221            protected String stringData;
222    
223    }