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