1
14
15 package com.liferay.portal.kernel.servlet;
16
17 import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
18 import com.liferay.portal.kernel.io.unsync.UnsyncPrintWriter;
19 import com.liferay.portal.kernel.io.unsync.UnsyncStringWriter;
20 import com.liferay.portal.kernel.log.Log;
21 import com.liferay.portal.kernel.log.LogFactoryUtil;
22 import com.liferay.portal.kernel.util.StringPool;
23
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.io.UnsupportedEncodingException;
27
28 import java.util.Locale;
29
30 import javax.servlet.ServletOutputStream;
31 import javax.servlet.http.HttpServletResponse;
32
33
39 public class StringServletResponse extends HeaderCacheServletResponse {
40
41 public StringServletResponse(HttpServletResponse response) {
42 super(response);
43 }
44
45 public int getBufferSize() {
46 return _bufferSize;
47 }
48
49 public String getContentType() {
50 return _contentType;
51 }
52
53 public void flushBuffer() throws IOException {
54 if (_servletOutputStream != null) {
55 _unsyncByteArrayOutputStream.flush();
56 }
57 else if (_printWriter != null) {
58 _unsyncStringWriter.flush();
59 }
60 }
61
62 public ServletOutputStream getOutputStream() {
63 if (_printWriter != null) {
64 throw new IllegalStateException(
65 "Cannot obtain OutputStream because Writer is already in use");
66 }
67
68 if (_servletOutputStream == null) {
69 _unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();
70 _servletOutputStream = new PipingServletOutputStream(
71 _unsyncByteArrayOutputStream);
72 }
73
74 return _servletOutputStream;
75 }
76
77 public int getStatus() {
78 return _status;
79 }
80
81 public String getString() {
82 if (_string != null) {
83 return _string;
84 }
85
86 if (_servletOutputStream != null) {
87 try {
88 _string = _unsyncByteArrayOutputStream.toString(
89 StringPool.UTF8);
90 }
91 catch (UnsupportedEncodingException uee) {
92 _log.error(uee, uee);
93
94 _string = StringPool.BLANK;
95 }
96 }
97 else if (_printWriter != null) {
98 _string = _unsyncStringWriter.toString();
99 }
100 else {
101 _string = StringPool.BLANK;
102 }
103
104 return _string;
105 }
106
107 public UnsyncByteArrayOutputStream getUnsyncByteArrayOutputStream() {
108 return _unsyncByteArrayOutputStream;
109 }
110
111 public PrintWriter getWriter() {
112 if (_servletOutputStream != null) {
113 throw new IllegalStateException(
114 "Cannot obtain Writer because OutputStream is already in use");
115 }
116
117 if (_printWriter == null) {
118 _unsyncStringWriter = new UnsyncStringWriter();
119 _printWriter = new UnsyncPrintWriter(_unsyncStringWriter);
120 }
121
122 return _printWriter;
123 }
124
125 public boolean isCalledGetOutputStream() {
126 if (_servletOutputStream != null) {
127 return true;
128 }
129 else {
130 return false;
131 }
132 }
133
134 public void recycle() {
135 _status = SC_OK;
136 _string = null;
137
138 resetBuffer();
139 }
140
141 public void resetBuffer() {
142 if (_servletOutputStream != null) {
143 _unsyncByteArrayOutputStream.reset();
144 }
145 else if (_printWriter != null) {
146 _unsyncStringWriter.reset();
147 }
148 }
149
150 public void sendError(int status) throws IOException {
151 _status = status;
152
153 super.sendError(status);
154 }
155
156 public void sendError(int status, String msg) throws IOException {
157 _status = status;
158
159 super.sendError(status, msg);
160 }
161
162 public void setBufferSize(int bufferSize) {
163 _bufferSize = bufferSize;
164 }
165
166 public void setContentType(String contentType) {
167 _contentType = contentType;
168
169 super.setContentType(contentType);
170 }
171
172 public void setLocale(Locale locale) {
173 }
174
175 public void setStatus(int status) {
176 _status = status;
177
178 super.setStatus(_status);
179 }
180
181 public void setString(String string) {
182 _string = string;
183 }
184
185 private static Log _log = LogFactoryUtil.getLog(
186 StringServletResponse.class);
187
188 private int _bufferSize;
189 private String _contentType;
190 private PrintWriter _printWriter;
191 private ServletOutputStream _servletOutputStream;
192 private int _status = SC_OK;
193 private String _string;
194 private UnsyncByteArrayOutputStream _unsyncByteArrayOutputStream;
195 private UnsyncStringWriter _unsyncStringWriter;
196
197 }