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