001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.servlet;
022:
023: import com.liferay.portal.kernel.util.ByteArrayMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025:
026: import java.io.PrintWriter;
027: import java.io.StringWriter;
028: import java.io.UnsupportedEncodingException;
029:
030: import java.util.Locale;
031:
032: import javax.servlet.ServletOutputStream;
033: import javax.servlet.http.HttpServletResponse;
034: import javax.servlet.http.HttpServletResponseWrapper;
035:
036: /**
037: * <a href="StringServletResponse.java.html"><b><i>View Source</i></b></a>
038: *
039: * @author Brian Wing Shun Chan
040: *
041: */
042: public class StringServletResponse extends HttpServletResponseWrapper {
043:
044: public StringServletResponse(HttpServletResponse res) {
045: super (res);
046: }
047:
048: public String getContentType() {
049: return _contentType;
050: }
051:
052: public void setContentType(String contentType) {
053: _contentType = contentType;
054:
055: super .setContentType(contentType);
056: }
057:
058: public void setLocale(Locale locale) {
059: }
060:
061: public ServletOutputStream getOutputStream() {
062: /*if (_callGetWriter) {
063: throw new IllegalStateException();
064: }*/
065:
066: _callGetOutputStream = true;
067:
068: return _sos;
069: }
070:
071: public boolean isCalledGetOutputStream() {
072: return _callGetOutputStream;
073: }
074:
075: public int getStatus() {
076: return _status;
077: }
078:
079: public void setStatus(int status) {
080: _status = status;
081: }
082:
083: public String getString() throws UnsupportedEncodingException {
084: if (_string != null) {
085: return _string;
086: } else if (_callGetOutputStream) {
087: return _bam.toString();
088: } else if (_callGetWriter) {
089: return _sw.toString();
090: } else {
091: return StringPool.BLANK;
092: }
093: }
094:
095: public void setString(String string) {
096: _string = string;
097: }
098:
099: public PrintWriter getWriter() {
100: /*if (_callGetOutputStream) {
101: throw new IllegalStateException();
102: }*/
103:
104: _callGetWriter = true;
105:
106: return _pw;
107: }
108:
109: public ByteArrayMaker getByteArrayMaker() {
110: return _bam;
111: }
112:
113: public int getBufferSize() {
114: return _bufferSize;
115: }
116:
117: public void setBufferSize(int size) {
118: _bufferSize = size;
119: }
120:
121: public void resetBuffer() {
122: if (_callGetOutputStream) {
123: _bam.reset();
124: } else if (_callGetWriter) {
125: StringBuffer sb = _sw.getBuffer();
126:
127: sb.delete(0, sb.length());
128: }
129: }
130:
131: public void recycle() {
132: _bam.reset();
133: //_sos = new StringServletOutputStream(_bam);
134: _status = SC_OK;
135: _sw = new StringWriter();
136: _pw = new PrintWriter(_sw);
137: _callGetOutputStream = false;
138: _callGetWriter = false;
139: _string = null;
140: }
141:
142: private String _contentType;
143: private ByteArrayMaker _bam = new ByteArrayMaker();
144: private ServletOutputStream _sos = new StringServletOutputStream(
145: _bam);
146: private int _status = SC_OK;
147: private StringWriter _sw = new StringWriter();
148: private PrintWriter _pw = new PrintWriter(_sw);
149: private int _bufferSize;
150: private boolean _callGetOutputStream;
151: private boolean _callGetWriter;
152: private String _string = null;
153:
154: }
|