1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.servlet.filters;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.nio.charset.CharsetEncoderUtil;
19  import com.liferay.portal.kernel.servlet.Header;
20  import com.liferay.portal.kernel.servlet.StringServletResponse;
21  import com.liferay.portal.kernel.util.StringPool;
22  
23  import java.io.Serializable;
24  
25  import java.nio.ByteBuffer;
26  
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  
31  /**
32   * <a href="CacheResponseData.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Michael Young
35   */
36  public class CacheResponseData implements Serializable {
37  
38      public CacheResponseData(StringServletResponse stringResponse) {
39          if (stringResponse.isCalledGetOutputStream()) {
40              UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
41                  stringResponse.getUnsyncByteArrayOutputStream();
42  
43              _content = unsyncByteArrayOutputStream.unsafeGetByteArray();
44              _contentLength = unsyncByteArrayOutputStream.size();
45          }
46          else {
47              String content = stringResponse.getString();
48  
49              ByteBuffer contentByteBuffer = CharsetEncoderUtil.encode(
50                  StringPool.UTF8, content);
51  
52              _content = contentByteBuffer.array();
53              _contentLength = contentByteBuffer.limit();
54          }
55  
56          _contentType = stringResponse.getContentType();
57          _headers = stringResponse.getHeaders();
58      }
59  
60      public CacheResponseData(
61          byte[] content, int contentLength, String contentType,
62          Map<String, List<Header>> headers) {
63  
64          _content = content;
65          _contentLength = contentLength;
66          _contentType = contentType;
67          _headers = headers;
68      }
69  
70      public Object getAttribute(String name) {
71          return _attributes.get(name);
72      }
73  
74      public byte[] getContent() {
75          return _content;
76      }
77  
78      public int getContentLength() {
79          return _contentLength;
80      }
81  
82      public String getContentType() {
83          return _contentType;
84      }
85  
86      public Map<String, List<Header>> getHeaders() {
87          return _headers;
88      }
89  
90      public void setAttribute(String name, Object value) {
91          _attributes.put(name, value);
92      }
93  
94      private Map<String, Object> _attributes = new HashMap<String, Object>();
95      private byte[] _content;
96      private int _contentLength;
97      private String _contentType;
98      private Map<String, List<Header>> _headers;
99  
100 }