KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > logger > XHttpServletResponseFacade


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/logger/XHttpServletResponseFacade.java,v 1.6 2004/07/28 09:32:35 ib Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/07/28 09:32:35 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24
25 package org.apache.slide.webdav.logger;
26
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.Writer JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 import javax.servlet.ServletOutputStream JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
38
39 import org.apache.slide.common.Domain;
40
41 /**
42  * This class supports additional set-methods and a re-readable
43  * inputstream to the interface javax.servlet.http.HttpServletResponse.
44  *
45  * Christopher Lenz (cmlenz at apache.org)
46  *
47  * @version 0.1
48  *
49  * @invariant (inputStream != null)
50  *
51  * @see javax.servlet.http.HttpServletResponse
52  *
53  */

54 public class XHttpServletResponseFacade extends HttpServletResponseWrapper JavaDoc
55 {
56
57     
58     public static final String JavaDoc DEFAULT_CHAR_ENCODING = "8859_1";
59
60     
61     // default implementation will just append everything here
62
StringBuffer JavaDoc body=null;
63
64     protected HttpServletRequest JavaDoc request;
65
66     private int statusCode = -1;
67     private String JavaDoc statusText = "";
68     
69     public void setRequest(HttpServletRequest JavaDoc request) {
70         this.request = request;
71     }
72
73     public HttpServletRequest JavaDoc getRequest() {
74         return request;
75     }
76     
77     /**
78      * true - if outputstream can write using a stream; false otherwise.
79      */

80     private boolean usingStream = false;
81     
82     /**
83      * true - if outputstream can write using a writer; false otherwise.
84      */

85     private boolean usingWriter = false;
86
87     /**
88      * output stream used for building the writer.
89      *
90      * @see javax.servlet.ServletOutputStream
91      */

92     XServletOutputStreamFacade osFacade = null;
93     
94     /**
95      * writer which uses the output stream.
96      *
97      * @see java.io.PrintWriter
98      */

99     PrintWriter JavaDoc writer = null;
100
101     /**
102      * response headers
103      */

104     Vector JavaDoc responseHeaders = new Vector JavaDoc();
105     public Enumeration JavaDoc getResponseHeaders() {
106         return responseHeaders.elements();
107     }
108     
109     
110     /**
111      * This constructor creates an re-writable HttpServletResüpmse.
112      *
113      * @pre (response != null)
114      * @post
115      *
116      * @param response HttpServletResponse
117      *
118      * @time
119      * @space
120      */

121     public XHttpServletResponseFacade(HttpServletResponse JavaDoc response) {
122         super(response);
123         Domain.debug("Create XHttpServletResponseFacade");
124     }
125
126
127     // -------------------- Public methods --------------------
128

129     /**
130      * Adds the specified cookie to the response.
131      *
132      * @param cookie - the Cookie to return to the client
133      *
134      * @return none
135      */

136     public void addCookie(javax.servlet.http.Cookie JavaDoc cookie) {
137         responseHeaders.add(
138             new XResponseHeader(
139                 "Cookie",
140                 cookie.getName() + " " +
141                 cookie.getValue() + " " +
142                 cookie.getDomain()
143                 )
144             );
145         super.addCookie(cookie);
146     }
147     
148     /**
149      * Returns a ServletOutputStream suitable for writing binary data in the response.
150      *
151      * @return a ServletOutputStream for writing binary data
152      */

153     public ServletOutputStream JavaDoc getOutputStream()
154         throws IOException JavaDoc {
155         Domain.debug("ENTER: XHttpServletResponseFacade:getOutputStream()");
156         if ( usingWriter ) {
157             throw new IllegalStateException JavaDoc("Writer is already being used for this response");
158         }
159         usingStream = true;
160
161         if( osFacade == null ) {
162             osFacade = new XServletOutputStreamFacade(
163                 super.getOutputStream(),
164                 super.getCharacterEncoding());
165         }
166             
167         Domain.debug("LEAVE: XHttpServletResponseFacade:getOutputStream()");
168         return osFacade;
169     }
170
171     /**
172      * Returns a PrintWriter object that can send character text to the client.
173      *
174      * @return a PrintWriter object that can return character data to the client
175      */

176     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
177         Domain.debug("ENTER: XHttpServletResponseFacade:getWriter()");
178         if (usingStream) {
179             throw new IllegalStateException JavaDoc("OutputStream is already being used for this response");
180         }
181         usingWriter= true ;
182
183         if( writer == null ) {
184             writer = new XServletWriterFacade( super.getWriter() );
185         }
186
187         Domain.debug("LEAVE: XHttpServletResponseFacade:getWriter()");
188         return writer;
189     }
190
191     public PrintWriter JavaDoc getWriter(ServletOutputStream JavaDoc outs) throws IOException JavaDoc {
192         Domain.debug("ENTER: XHttpServletResponseFacade:getWriter(ServletOutputStream)");
193         if(writer!=null) return writer;
194         // it already did all the checkings
195

196         //hak started = true;
197
usingWriter = true;
198     
199         // writer = new XServletWriterFacade( getConverter(outs), this);
200
Domain.debug("LEAVE: XHttpServletResponseFacade:getWriter(ServletOutputStream)");
201         return writer;
202     }
203
204     public Writer JavaDoc getConverter( ServletOutputStream JavaDoc outs ) throws IOException JavaDoc {
205         String JavaDoc encoding = getCharacterEncoding();
206         OutputStreamWriter JavaDoc osWriter;
207         if (encoding == null) {
208             // use default platform encoding - is this correct ?
209
osWriter = new OutputStreamWriter JavaDoc(outs);
210         } else {
211             try {
212                 osWriter = new OutputStreamWriter JavaDoc(outs, encoding);
213             } catch (java.io.UnsupportedEncodingException JavaDoc ex) {
214                 // XXX log it
215
System.out.println("Unsuported encoding: " + encoding );
216                 return new OutputStreamWriter JavaDoc(outs);
217             }
218         }
219         return osWriter;
220     }
221
222     /**
223      * Sets a response header with the given name and date-value.
224      *
225      * @param name - the name of the header to set
226      * value - the assigned date value
227      */

228     public void setDateHeader(String JavaDoc name, long date) {
229         responseHeaders.add(new XResponseHeader(name, String.valueOf(date)));
230         super.setDateHeader(name, date);
231     }
232
233     /**
234      * Adds a response header with the given name and date-value.
235      *
236      * @param name - the name of the header to set
237      * value - the additional date value
238      */

239     public void addDateHeader(String JavaDoc name, long date) {
240         responseHeaders.add(new XResponseHeader(name, String.valueOf(date)));
241         super.addDateHeader(name, date);
242     }
243
244     /**
245      * Sets a response header with the given name and value.
246      *
247      * @param name - the name of the header
248      * value - the header value
249      */

250     public void setHeader(String JavaDoc name, String JavaDoc value) {
251         responseHeaders.add(new XResponseHeader(name, value));
252         super.setHeader(name, value);
253     }
254
255     /**
256      * Adds a response header with the given name and value.
257      *
258      * @param name - the name of the header
259      * value - the header value
260      */

261     public void addHeader(String JavaDoc name, String JavaDoc value) {
262         responseHeaders.add(new XResponseHeader(name, value));
263         super.addHeader(name, value);
264     }
265
266     /**
267      * Sets a response header with the given name and integer value.
268      *
269      * @param name - the name of the header
270      * value - the assigned integer value
271      */

272     public void setIntHeader(String JavaDoc name, int value) {
273         responseHeaders.add(new XResponseHeader(name, String.valueOf(value)));
274         super.setHeader(name, Integer.toString(value));
275     }
276
277     /**
278      * Adds a response header with the given name and integer value.
279      *
280      * @param name - the name of the header
281      * value - the assigned integer value
282      */

283     public void addIntHeader(String JavaDoc name, int value) {
284         responseHeaders.add(new XResponseHeader(name, String.valueOf(value)));
285         super.addHeader(name, Integer.toString(value));
286     }
287
288     public int getStatus() {
289         return statusCode;
290     }
291     public String JavaDoc getStatusText() {
292         return statusText;
293     }
294     public void setStatus(int sc) {
295         statusCode = sc;
296         super.setStatus(sc);
297     }
298     public void setStatus(int sc, String JavaDoc msg) {
299         statusCode = sc;
300         statusText = msg;
301         super.setStatus(sc, msg);
302     }
303     public void sendError( int sc ) throws IOException JavaDoc {
304         statusCode = sc;
305         super.sendError(sc);
306     }
307     public void sendError( int sc, String JavaDoc msg ) throws IOException JavaDoc {
308         statusCode = sc;
309         statusText = msg;
310         super.sendError(sc, msg);
311     }
312
313     /**
314      * Returns the content of the buffered output stream.
315      *
316      * @return copy of outputstream as string.
317      */

318     public String JavaDoc getResponseBody() throws IOException JavaDoc {
319         if ( usingStream ) {
320             Domain.debug("XHttpServletResponseFacade:getResponseBody() - usingStream");
321             return osFacade.getOutputBuffer();
322         } else if ( usingWriter ) {
323             Domain.debug("XHttpServletResponseFacade:getResponseBody() - usingWriter");
324             return (( XServletWriterFacade )writer).getOutputBuffer();
325         } else {
326             Domain.debug("XHttpServletResponseFacade:getResponseBody() - nor writer nor stream - osFacade = " + osFacade + " writer = " + writer);
327             return "";
328         }
329     }
330
331
332 }
333
Popular Tags