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