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