1
22
23 package com.liferay.portal.kernel.servlet;
24
25 import com.liferay.portal.kernel.util.ByteArrayMaker;
26 import com.liferay.portal.kernel.util.StringPool;
27
28 import java.io.PrintWriter;
29 import java.io.StringWriter;
30
31 import java.util.Locale;
32
33 import javax.servlet.ServletOutputStream;
34 import javax.servlet.http.HttpServletResponse;
35 import javax.servlet.http.HttpServletResponseWrapper;
36
37
43 public class StringServletResponse extends HttpServletResponseWrapper {
44
45 public StringServletResponse(HttpServletResponse response) {
46 super(response);
47 }
48
49 public int getBufferSize() {
50 return _bufferSize;
51 }
52
53 public ByteArrayMaker getByteArrayMaker() {
54 return _bam;
55 }
56
57 public String getContentType() {
58 return _contentType;
59 }
60
61 public ServletOutputStream getOutputStream() {
62
65
66 _callGetOutputStream = true;
67
68 return _sos;
69 }
70
71 public int getStatus() {
72 return _status;
73 }
74
75 public String getString() {
76 if (_string != null) {
77 return _string;
78 }
79 else if (_callGetOutputStream) {
80 return _bam.toString();
81 }
82 else if (_callGetWriter) {
83 return _sw.toString();
84 }
85 else {
86 return StringPool.BLANK;
87 }
88 }
89
90 public PrintWriter getWriter() {
91
94
95 _callGetWriter = true;
96
97 return _pw;
98 }
99
100 public boolean isCalledGetOutputStream() {
101 return _callGetOutputStream;
102 }
103
104 public void recycle() {
105 _bam.reset();
106 _status = SC_OK;
108 _sw = new StringWriter();
109 _pw = new PrintWriter(_sw);
110 _callGetOutputStream = false;
111 _callGetWriter = false;
112 _string = null;
113 }
114
115 public void resetBuffer() {
116 if (_callGetOutputStream) {
117 _bam.reset();
118 }
119 else if (_callGetWriter) {
120 StringBuffer sb = _sw.getBuffer();
121
122 sb.delete(0, sb.length());
123 }
124 }
125
126 public void setBufferSize(int size) {
127 _bufferSize = size;
128 }
129
130 public void setContentType(String contentType) {
131 _contentType = contentType;
132
133 super.setContentType(contentType);
134 }
135
136 public void setLocale(Locale locale) {
137 }
138
139 public void setStatus(int status) {
140 _status = status;
141 }
142
143 public void setString(String string) {
144 _string = string;
145 }
146
147 private String _contentType;
148 private int _status = SC_OK;
149 private ByteArrayMaker _bam = new ByteArrayMaker();
150 private ServletOutputStream _sos = new StringServletOutputStream(_bam);
151 private StringWriter _sw = new StringWriter();
152 private PrintWriter _pw = new PrintWriter(_sw);
153 private int _bufferSize;
154 private boolean _callGetOutputStream;
155 private boolean _callGetWriter;
156 private String _string = null;
157
158 }