001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.servlet;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.io.OutputStreamWriter;
022: import java.io.PrintWriter;
023: import java.io.UnsupportedEncodingException;
024: import java.util.Locale;
025:
026: import javax.servlet.ServletOutputStream;
027: import javax.servlet.http.HttpServletResponse;
028: import javax.servlet.http.HttpServletResponseWrapper;
029:
030: /**
031: * Our response wrapper
032: *
033: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
034: * @version CVS $Id: ServletResponseImpl.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class ServletResponseImpl extends HttpServletResponseWrapper {
037:
038: protected MyOutputStream stream;
039: protected PrintWriter writer;
040:
041: protected boolean committed = false;
042: protected int bufferSize = 1024;
043:
044: protected String redirectURL;
045:
046: protected String encoding = "ISO-8859-1";
047:
048: public ServletResponseImpl(HttpServletResponse response) {
049: super (response);
050: this .stream = new MyOutputStream();
051: }
052:
053: /**
054: * Return the content of the portlet
055: */
056: public String getContent() {
057: if (this .writer != null) {
058: this .writer.flush();
059: }
060: try {
061: this .stream.flush();
062: } catch (IOException ignore) {
063: // just ignore it
064: }
065: String value;
066: try {
067: value = this .stream.stream.toString(this .encoding);
068: } catch (UnsupportedEncodingException uee) {
069: value = new String(this .stream.stream.toByteArray());
070: }
071: this .stream = new MyOutputStream();
072: this .writer = null;
073: return value;
074: }
075:
076: /**
077: * Get redirect url
078: */
079: public String getRedirectURL() {
080: return this .redirectURL;
081: }
082:
083: /**
084: * @see java.lang.Object#toString()
085: */
086: public String toString() {
087: return this .getContent();
088: }
089:
090: /**
091: * @see javax.servlet.http.HttpServletResponse#sendError(int, String)
092: */
093: public void sendError(int arg0, String arg1) throws IOException {
094: //this.response.sendError(arg0, arg1);
095: }
096:
097: /**
098: * @see javax.servlet.http.HttpServletResponse#sendError(int)
099: */
100: public void sendError(int arg0) throws IOException {
101: //this.response.sendError(arg0);
102: }
103:
104: /**
105: * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)
106: */
107: public void sendRedirect(String arg0) throws IOException {
108: this .redirectURL = arg0;
109: }
110:
111: /**
112: * @see javax.servlet.http.HttpServletResponse#setStatus(int, String)
113: */
114: public void setStatus(int arg0, String arg1) {
115: //this.response.setStatus(arg0, arg1);
116: }
117:
118: /**
119: * @see javax.servlet.http.HttpServletResponse#setStatus(int)
120: */
121: public void setStatus(int arg0) {
122: //this.response.setStatus(arg0);
123: }
124:
125: /**
126: * @see javax.servlet.ServletResponse#flushBuffer()
127: */
128: public void flushBuffer() throws IOException {
129: this .committed = true;
130: }
131:
132: /**
133: * @see javax.servlet.ServletResponse#getBufferSize()
134: */
135: public int getBufferSize() {
136: return this .bufferSize = 1024;
137: }
138:
139: /**
140: * @see javax.servlet.ServletResponse#getOutputStream()
141: */
142: public ServletOutputStream getOutputStream() throws IOException {
143: return this .stream;
144: }
145:
146: /**
147: * @see javax.servlet.ServletResponse#getWriter()
148: */
149: public PrintWriter getWriter() throws IOException {
150: if (this .writer == null) {
151: this .writer = new PrintWriter(new OutputStreamWriter(
152: this .stream, this .encoding));
153: }
154: return this .writer;
155: }
156:
157: /**
158: * @see javax.servlet.ServletResponse#isCommitted()
159: */
160: public boolean isCommitted() {
161: return this .committed;
162: }
163:
164: /**
165: * @see javax.servlet.ServletResponse#reset()
166: */
167: public void reset() {
168: if (!this .committed) {
169: this .stream = new MyOutputStream();
170: }
171: }
172:
173: /**
174: * @see javax.servlet.ServletResponse#setBufferSize(int)
175: */
176: public void setBufferSize(int arg0) {
177: this .bufferSize = arg0;
178: }
179:
180: /**
181: * @see javax.servlet.ServletResponse#setContentLength(int)
182: */
183: public void setContentLength(int arg0) {
184: // nothing to do
185: }
186:
187: /**
188: * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
189: */
190: public void setContentType(String typeInfo) {
191: if (typeInfo != null) {
192: int pos = typeInfo.indexOf("charset=");
193: if (pos != -1) {
194: this .encoding = typeInfo.substring(pos + 8);
195: }
196: }
197: }
198:
199: public void setCharacterEncoding(String enc) {
200: this .encoding = enc;
201: }
202:
203: /**
204: * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
205: */
206: public void setLocale(Locale locale) {
207: // nothing to do
208: }
209:
210: /**
211: * @see javax.servlet.ServletResponse#resetBuffer()
212: */
213: public void resetBuffer() {
214: // nothing to do
215: }
216:
217: protected final static class MyOutputStream extends
218: ServletOutputStream {
219:
220: ByteArrayOutputStream stream = new ByteArrayOutputStream();
221:
222: public MyOutputStream() {
223: // nothing to do
224: }
225:
226: /**
227: * @see java.io.OutputStream#write(int)
228: */
229: public void write(int b) throws IOException {
230: this .stream.write(b);
231: }
232:
233: /**
234: * @see java.io.OutputStream#flush()
235: */
236: public void flush() throws IOException {
237: super.flush();
238: this.stream.flush();
239: }
240: }
241: }
|