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;
16  
17  import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18  import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
19  
20  import java.io.PrintWriter;
21  
22  import javax.servlet.ServletOutputStream;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.http.HttpServletResponseWrapper;
25  
26  /**
27   * <a href="GenericServletResponse.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class GenericServletResponse extends HttpServletResponseWrapper {
32  
33      public GenericServletResponse(HttpServletResponse response) {
34          super(response);
35  
36          _ubaos = new UnsyncByteArrayOutputStream();
37      }
38  
39      public byte[] getData() {
40          return _ubaos.toByteArray();
41      }
42  
43      public int getContentLength() {
44          return _contentLength;
45      }
46  
47      public void setContentLength(int length) {
48          super.setContentLength(length);
49  
50          _contentLength = length;
51      }
52  
53      public String getContentType() {
54          return _contentType;
55      }
56  
57      public void setContentType(String type) {
58          super.setContentType(type);
59  
60          _contentType = type;
61      }
62  
63      public ServletOutputStream getOutputStream() {
64          return new GenericServletOutputStream(_ubaos);
65      }
66  
67      public PrintWriter getWriter() {
68          return new UnsyncPrintWriter(getOutputStream(), true);
69      }
70  
71      private int _contentLength;
72      private String _contentType;
73      private UnsyncByteArrayOutputStream _ubaos;
74  
75  }