KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > sample > servlet > util > GenericResponseWrapper


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

20 package org.apache.cactus.sample.servlet.util;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 import javax.servlet.ServletOutputStream JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
28
29 /**
30  * Wrapper around a <code>HttpServletResponse</code> that we use to easily
31  * write filters that manipulate the output stream. Indeed, we cannot pass
32  * the output stream of our filter direectly to the next filter in the chain
33  * because then we won't be able to write to it (the response will have been
34  * committed). Instead, we pass this wrapper class and then copy its data
35  * to our filter output stream.
36  *
37  * Note: This code was adapted from the Filter tutorial found
38  * {@link <a HREF="http://www.orionserver.com/tutorials/filters/lesson3/">
39  * here</a>}
40  *
41  * @version $Id: GenericResponseWrapper.java,v 1.3 2004/02/29 16:36:45 vmassol Exp $
42  *
43  * @see FilterServletOutputStream
44  */

45 public class GenericResponseWrapper extends HttpServletResponseWrapper JavaDoc
46 {
47     /**
48      * Holder for the output data
49      */

50     private ByteArrayOutputStream JavaDoc output;
51
52     /**
53      * Save the content length so that we can query it at a later time
54      * (otherwise it would not be possible as
55      * <code>HttpServletResponseWrapper</code> does not have a method to get
56      * the content length).
57      */

58     private int contentLength;
59
60     /**
61      * Save the content type so that we can query it at a later time
62      * (otherwise it would not be possible as
63      * <code>HttpServletResponseWrapper</code> does not have a method to get
64      * the content type).
65      */

66     private String JavaDoc contentType;
67
68     // Constructors ----------------------------------------------------------
69

70     /**
71      * @param theResponse the wrapped response object
72      */

73     public GenericResponseWrapper(HttpServletResponse JavaDoc theResponse)
74     {
75         super(theResponse);
76         this.output = new ByteArrayOutputStream JavaDoc();
77     }
78
79     // New methods -----------------------------------------------------------
80

81     /**
82      * @return the data sent to the output stream
83      */

84     public byte[] getData()
85     {
86         return output.toByteArray();
87     }
88
89     // Overridden methods ----------------------------------------------------
90

91     /**
92      * @see HttpServletResponseWrapper#getOutputStream()
93      */

94     public ServletOutputStream JavaDoc getOutputStream()
95     {
96         return new FilterServletOutputStream(this.output);
97     }
98
99     /**
100      * @see HttpServletResponseWrapper#setContentLength(int)
101      */

102     public void setContentLength(int theLength)
103     {
104         this.contentLength = theLength;
105         super.setContentLength(theLength);
106     }
107
108     /**
109      * @see HttpServletResponseWrapper#getContentLength()
110      */

111     public int getContentLength()
112     {
113         return this.contentLength;
114     }
115
116     /**
117      * @see HttpServletResponseWrapper#setContentType(String)
118      */

119     public void setContentType(String JavaDoc theType)
120     {
121         this.contentType = theType;
122         super.setContentType(theType);
123     }
124
125     /**
126      * @see HttpServletResponseWrapper#getContentType()
127      */

128     public String JavaDoc getContentType()
129     {
130         return this.contentType;
131     }
132
133     /**
134      * @see HttpServletResponseWrapper#getWriter()
135      */

136     public PrintWriter JavaDoc getWriter()
137     {
138         return new PrintWriter JavaDoc(getOutputStream(), true);
139     }
140 }
141
Popular Tags