1
22
23 package com.liferay.portal.servlet.filters.strip;
24
25 import com.liferay.portal.kernel.util.ByteArrayMaker;
26 import com.liferay.portal.kernel.util.StringPool;
27
28 import java.io.IOException;
29 import java.io.OutputStreamWriter;
30 import java.io.PrintWriter;
31
32 import javax.servlet.ServletOutputStream;
33 import javax.servlet.http.HttpServletResponse;
34 import javax.servlet.http.HttpServletResponseWrapper;
35
36
42 public class StripResponse extends HttpServletResponseWrapper {
43
44 public StripResponse(HttpServletResponse response) {
45 super(response);
46 }
47
48 public void finishResponse() {
49 try {
50 if (_writer != null) {
51 _writer.close();
52 }
53 else if (_stream != null) {
54 _stream.close();
55 }
56 }
57 catch (IOException e) {
58 }
59 }
60
61 public void flushBuffer() throws IOException {
62 if (_stream != null) {
63 _stream.flush();
64 }
65 }
66
67 public ServletOutputStream getOutputStream() {
68 if (_writer != null) {
69 throw new IllegalStateException();
70 }
71
72 if (_stream == null) {
73 _stream = createOutputStream();
74 }
75
76 return _stream;
77 }
78
79 public PrintWriter getWriter() throws IOException {
80 if (_writer != null) {
81 return _writer;
82 }
83
84 if (_stream != null) {
85 throw new IllegalStateException();
86 }
87
88 _stream = createOutputStream();
89
90 _writer = new PrintWriter(new OutputStreamWriter(
91 _stream, StringPool.UTF8));
93
94 return _writer;
95 }
96
97 public boolean isCommitted() {
98 if (_stream != null) {
99 return true;
100 }
101 else {
102 return super.isCommitted();
103 }
104 }
105
106 public String getContentType() {
107 return _contentType;
108 }
109
110 public void setContentType(String contentType) {
111 _contentType = contentType;
112
113 super.setContentType(contentType);
114 }
115
116 public byte[] getData() {
117 finishResponse();
118
119 return _bam.toByteArray();
120 }
121
122 protected ServletOutputStream createOutputStream() {
123 return new StripStream(_bam);
124 }
125
126 private ByteArrayMaker _bam = new ByteArrayMaker();
127 private ServletOutputStream _stream = null;
128 private PrintWriter _writer = null;
129 private String _contentType;
130
131 }