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