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.util.servlet.filters;
016    
017    import com.liferay.portal.kernel.servlet.ByteBufferServletResponse;
018    import com.liferay.portal.kernel.servlet.Header;
019    
020    import java.io.Serializable;
021    
022    import java.nio.ByteBuffer;
023    
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * @author Michael Young
030     * @author Shuyang Zhou
031     */
032    public class CacheResponseData implements Serializable {
033    
034            public CacheResponseData(ByteBufferServletResponse byteBufferResponse) {
035                    _byteBuffer = byteBufferResponse.getByteBuffer();
036                    _content = _byteBuffer.array();
037                    _contentType = byteBufferResponse.getContentType();
038                    _headers = byteBufferResponse.getHeaders();
039            }
040    
041            public Object getAttribute(String name) {
042                    return _attributes.get(name);
043            }
044    
045            public ByteBuffer getByteBuffer() {
046                    if (_byteBuffer == null) {
047                            _byteBuffer = ByteBuffer.wrap(_content);
048                    }
049    
050                    return _byteBuffer;
051            }
052    
053            public String getContentType() {
054                    return _contentType;
055            }
056    
057            public Map<String, List<Header>> getHeaders() {
058                    return _headers;
059            }
060    
061            public void setAttribute(String name, Object value) {
062                    _attributes.put(name, value);
063            }
064    
065            private Map<String, Object> _attributes = new HashMap<String, Object>();
066            private transient ByteBuffer _byteBuffer;
067            private byte[] _content;
068            private String _contentType;
069            private Map<String, List<Header>> _headers;
070    
071    }