KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > servlet > ServletResponseImpl


1 /*
2  * Copyright 2004,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto.servlet;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import javax.servlet.ServletOutputStream JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
26
27 /**
28  * Our response wrapper
29  *
30  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
31  * @version CVS $Id: ServletResponseImpl.java 278950 2005-09-06 09:15:36Z cziegeler $
32  */

33 public class ServletResponseImpl extends HttpServletResponseWrapper JavaDoc {
34
35     protected MyOutputStream stream;
36     protected PrintWriter JavaDoc writer;
37
38     protected boolean committed = false;
39     protected int bufferSize = 1024;
40
41     protected String JavaDoc redirectURL;
42
43     public ServletResponseImpl(HttpServletResponse JavaDoc response) {
44         super(response);
45         this.stream = new MyOutputStream();
46         this.writer = new PrintWriter JavaDoc(this.stream);
47     }
48
49     /**
50      * Return the content of the portlet
51      */

52     public String JavaDoc getContent() {
53         this.writer.flush();
54         try {
55             this.stream.flush();
56         } catch (IOException JavaDoc ignore) {
57             // just ignore it
58
}
59         final String JavaDoc value = new String JavaDoc(this.stream.stream.toByteArray());
60         this.stream = new MyOutputStream();
61         this.writer = new PrintWriter JavaDoc(this.stream);
62         return value;
63     }
64
65     /**
66      * Get redirect url
67      */

68     public String JavaDoc getRedirectURL() {
69         return this.redirectURL;
70     }
71
72     /* (non-Javadoc)
73      * @see java.lang.Object#toString()
74      */

75     public String JavaDoc toString() {
76         return this.getContent();
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see javax.servlet.http.HttpServletResponse#sendError(int,
83      * java.lang.String)
84      */

85     public void sendError(int arg0, String JavaDoc arg1) throws IOException JavaDoc {
86         //this.response.sendError(arg0, arg1);
87
}
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see javax.servlet.http.HttpServletResponse#sendError(int)
93      */

94     public void sendError(int arg0) throws IOException JavaDoc {
95         //this.response.sendError(arg0);
96
}
97
98     /*
99      * (non-Javadoc)
100      *
101      * @see javax.servlet.http.HttpServletResponse#redirect(java.lang.String)
102      */

103     public void sendRedirect(String JavaDoc arg0) throws IOException JavaDoc {
104         this.redirectURL = arg0;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see javax.servlet.http.HttpServletResponse#setStatus(int,
111      * java.lang.String)
112      */

113     public void setStatus(int arg0, String JavaDoc arg1) {
114         //this.response.setStatus(arg0, arg1);
115
}
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see javax.servlet.http.HttpServletResponse#setStatus(int)
121      */

122     public void setStatus(int arg0) {
123         //this.response.setStatus(arg0);
124
}
125
126     /*
127      * (non-Javadoc)
128      *
129      * @see javax.servlet.ServletResponse#flushBuffer()
130      */

131     public void flushBuffer() throws IOException JavaDoc {
132         this.committed = true;
133     }
134
135     /*
136      * (non-Javadoc)
137      *
138      * @see javax.servlet.ServletResponse#getBufferSize()
139      */

140     public int getBufferSize() {
141         return this.bufferSize = 1024;
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see javax.servlet.ServletResponse#getOutputStream()
148      */

149     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
150         return this.stream;
151     }
152
153     /*
154      * (non-Javadoc)
155      *
156      * @see javax.servlet.ServletResponse#getWriter()
157      */

158     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
159         return this.writer;
160     }
161
162     /*
163      * (non-Javadoc)
164      *
165      * @see javax.servlet.ServletResponse#isCommitted()
166      */

167     public boolean isCommitted() {
168         return this.committed;
169     }
170
171     /*
172      * (non-Javadoc)
173      *
174      * @see javax.servlet.ServletResponse#reset()
175      */

176     public void reset() {
177         if (!this.committed) {
178             this.stream = new MyOutputStream();
179         }
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see javax.servlet.ServletResponse#setBufferSize(int)
186      */

187     public void setBufferSize(int arg0) {
188         this.bufferSize = arg0;
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see javax.servlet.ServletResponse#setContentLength(int)
195      */

196     public void setContentLength(int arg0) {
197         // nothing to do
198
}
199
200     /*
201      * (non-Javadoc)
202      *
203      * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
204      */

205     public void setContentType(String JavaDoc arg0) {
206         // nothing to do
207
}
208
209     /*
210      * (non-Javadoc)
211      *
212      * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
213      */

214     public void setLocale(Locale JavaDoc arg0) {
215         // nothing to do
216
}
217
218     /*
219      * (non-Javadoc)
220      *
221      * @see javax.servlet.ServletResponse#resetBuffer()
222      */

223     public void resetBuffer() {
224         // nothing to do
225
}
226
227 }
228
229 class MyOutputStream extends ServletOutputStream JavaDoc {
230
231     ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
232
233     public MyOutputStream() {
234         // nothing to do
235
}
236
237     /*
238      * (non-Javadoc)
239      *
240      * @see java.io.OutputStream#write(int)
241      */

242     public void write(int b) throws IOException JavaDoc {
243         this.stream.write(b);
244     }
245
246     /*
247      * (non-Javadoc)
248      *
249      * @see java.io.OutputStream#flush()
250      */

251     public void flush() throws IOException JavaDoc {
252         super.flush();
253         this.stream.flush();
254     }
255
256 }
257
Popular Tags