001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.servlet.filters;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
019    import com.liferay.portal.kernel.servlet.Header;
020    import com.liferay.portal.kernel.servlet.StringServletResponse;
021    import com.liferay.portal.kernel.util.StringPool;
022    
023    import java.io.Serializable;
024    
025    import java.nio.ByteBuffer;
026    
027    import java.util.HashMap;
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * @author Michael Young
033     */
034    public class CacheResponseData implements Serializable {
035    
036            public CacheResponseData(StringServletResponse stringResponse) {
037                    if (stringResponse.isCalledGetOutputStream()) {
038                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
039                                    stringResponse.getUnsyncByteArrayOutputStream();
040    
041                            _content = unsyncByteArrayOutputStream.unsafeGetByteArray();
042                            _contentLength = unsyncByteArrayOutputStream.size();
043                    }
044                    else {
045                            String content = stringResponse.getString();
046    
047                            ByteBuffer contentByteBuffer = CharsetEncoderUtil.encode(
048                                    StringPool.UTF8, content);
049    
050                            _content = contentByteBuffer.array();
051                            _contentLength = contentByteBuffer.limit();
052                    }
053    
054                    _contentType = stringResponse.getContentType();
055                    _headers = stringResponse.getHeaders();
056            }
057    
058            public CacheResponseData(
059                    byte[] content, int contentLength, String contentType,
060                    Map<String, List<Header>> headers) {
061    
062                    _content = content;
063                    _contentLength = contentLength;
064                    _contentType = contentType;
065                    _headers = headers;
066            }
067    
068            public Object getAttribute(String name) {
069                    return _attributes.get(name);
070            }
071    
072            public byte[] getContent() {
073                    return _content;
074            }
075    
076            public int getContentLength() {
077                    return _contentLength;
078            }
079    
080            public String getContentType() {
081                    return _contentType;
082            }
083    
084            public Map<String, List<Header>> getHeaders() {
085                    return _headers;
086            }
087    
088            public void setAttribute(String name, Object value) {
089                    _attributes.put(name, value);
090            }
091    
092            private Map<String, Object> _attributes = new HashMap<String, Object>();
093            private byte[] _content;
094            private int _contentLength;
095            private String _contentType;
096            private Map<String, List<Header>> _headers;
097    
098    }