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