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.model.Layout;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portal.util.WebKeys;
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<String, Serializable>(
072                            newSessionAttributes);
073    
074                    for (String removedSessionAttributeName :
075                                    removedSessionAttributeNames) {
076    
077                            deltaSessionAttributes.put(removedSessionAttributeName, null);
078                    }
079    
080                    captureThreadLocals();
081            }
082    
083            public void captureResponse(
084                            HttpServletRequest request,
085                            BufferCacheServletResponse bufferCacheServletResponse)
086                    throws IOException {
087    
088                    Boolean portalResiliencyAction = (Boolean)request.getAttribute(
089                            WebKeys.PORTAL_RESILIENCY_ACTION);
090    
091                    if (portalResiliencyAction != Boolean.TRUE) {
092                            portalResiliencyResponse = false;
093    
094                            return;
095                    }
096    
097                    portalResiliencyResponse = true;
098    
099                    metaData = bufferCacheServletResponse.getMetaData();
100    
101                    byteData = null;
102    
103                    if (bufferCacheServletResponse.isByteMode()) {
104                            ByteBuffer byteBuffer = bufferCacheServletResponse.getByteBuffer();
105    
106                            if (byteBuffer.remaining() > 0) {
107                                    if (byteBuffer.hasArray()) {
108                                            byte[] byteArray = byteBuffer.array();
109    
110                                            if (byteBuffer.remaining() == byteArray.length) {
111                                                    byteData = byteArray;
112                                            }
113                                    }
114    
115                                    if (byteData == null) {
116                                            byteData = new byte[byteBuffer.remaining()];
117    
118                                            byteBuffer.get(byteData);
119                                    }
120                            }
121                    }
122    
123                    stringData = null;
124    
125                    if (!bufferCacheServletResponse.isCharMode()) {
126                            return;
127                    }
128    
129                    String content = bufferCacheServletResponse.getString();
130    
131                    if (content.length() == 0) {
132                            return;
133                    }
134    
135                    if (ParamUtil.get(
136                                    request, "portalResiliencyPortletShowFooter",
137                                    PropsValues.PORTAL_RESILIENCY_PORTLET_SHOW_FOOTER)) {
138    
139                            int index = content.lastIndexOf("</div>");
140    
141                            if (index > 0) {
142                                    StringBundler sb = new StringBundler(6);
143    
144                                    sb.append(content.substring(0, index));
145                                    sb.append("<div class=\"alert alert-info\"><strong>");
146                                    sb.append("This portlet is from SPI ");
147                                    sb.append(PortalUtil.getPortalLocalPort(false));
148                                    sb.append("</strong></div>");
149                                    sb.append(content.substring(index));
150    
151                                    content = sb.toString();
152                            }
153                    }
154    
155                    stringData = content;
156            }
157    
158            public void populate(
159                            HttpServletRequest request, HttpServletResponse response)
160                    throws PortalResiliencyException {
161    
162                    if (exception != null) {
163                            throw new PortalResiliencyException("SPI exception", exception);
164                    }
165    
166                    if (!portalResiliencyResponse) {
167                            return;
168                    }
169    
170                    String typeSettings = (String)distributedRequestAttributes.remove(
171                            WebKeys.SPI_AGENT_LAYOUT_TYPE_SETTINGS);
172    
173                    if (typeSettings != null) {
174                            Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
175    
176                            layout.setTypeSettings(typeSettings);
177                    }
178    
179                    for (Map.Entry<String, Serializable> entry :
180                                    distributedRequestAttributes.entrySet()) {
181    
182                            request.setAttribute(entry.getKey(), entry.getValue());
183                    }
184    
185                    HttpSession session = request.getSession();
186    
187                    for (Map.Entry<String, Serializable> entry :
188                                    deltaSessionAttributes.entrySet()) {
189    
190                            session.setAttribute(entry.getKey(), entry.getValue());
191                    }
192    
193                    try {
194                            MetaInfoCacheServletResponse.finishResponse(metaData, response);
195    
196                            if (byteData != null) {
197                                    ServletResponseUtil.write(response, ByteBuffer.wrap(byteData));
198                            }
199    
200                            if (stringData != null) {
201                                    ServletResponseUtil.write(
202                                            response, CharBuffer.wrap(stringData));
203                            }
204                    }
205                    catch (IOException ioe) {
206                            throw new PortalResiliencyException(ioe);
207                    }
208    
209                    restoreThreadLocals();
210            }
211    
212            public void setException(Exception exception) {
213                    this.exception = exception;
214            }
215    
216            protected byte[] byteData;
217            protected Map<String, Serializable> deltaSessionAttributes;
218            protected Map<String, Serializable> distributedRequestAttributes;
219            protected Exception exception;
220            protected MetaData metaData;
221            protected boolean portalResiliencyResponse;
222            protected String stringData;
223    
224    }