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.servlet.Header;
19  import com.liferay.portal.kernel.servlet.StringServletResponse;
20  
21  import java.io.Serializable;
22  
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  /**
28   * <a href="CacheResponseData.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Michael Young
31   */
32  public class CacheResponseData implements Serializable {
33  
34      public CacheResponseData(StringServletResponse stringResponse) {
35          if (stringResponse.isCalledGetOutputStream()) {
36              UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
37                  stringResponse.getUnsyncByteArrayOutputStream();
38  
39              _content = unsyncByteArrayOutputStream.unsafeGetByteArray();
40              _contentLength = unsyncByteArrayOutputStream.size();
41          }
42          else {
43              String content = stringResponse.getString();
44  
45              _content = content.getBytes();
46              _contentLength = _content.length;
47          }
48  
49          _contentType = stringResponse.getContentType();
50          _headers = stringResponse.getHeaders();
51      }
52  
53      public CacheResponseData(
54          byte[] content, int contentLength, String contentType,
55          Map<String, List<Header>> headers) {
56  
57          _content = content;
58          _contentLength = contentLength;
59          _contentType = contentType;
60          _headers = headers;
61      }
62  
63      public Object getAttribute(String name) {
64          return _attributes.get(name);
65      }
66  
67      public byte[] getContent() {
68          return _content;
69      }
70  
71      public int getContentLength() {
72          return _contentLength;
73      }
74  
75      public String getContentType() {
76          return _contentType;
77      }
78  
79      public Map<String, List<Header>> getHeaders() {
80          return _headers;
81      }
82  
83      public void setAttribute(String name, Object value) {
84          _attributes.put(name, value);
85      }
86  
87      private Map<String, Object> _attributes = new HashMap<String, Object>();
88      private byte[] _content;
89      private int _contentLength;
90      private String _contentType;
91      private Map<String, List<Header>> _headers;
92  
93  }