KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > filters > CharArrayResponseWrapper


1 package jodd.servlet.filters;
2
3 import java.io.CharArrayWriter;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6
7 import javax.servlet.ServletOutputStream;
8 import javax.servlet.http.HttpServletResponse;
9 import javax.servlet.http.HttpServletResponseWrapper;
10
11 import jodd.util.CharUtil;
12
13 /**
14  * A response wrapper that takes everything the client would normally output
15  * and saves it in one big character array.
16  */

17 public class CharArrayResponseWrapper extends HttpServletResponseWrapper {
18     
19     private CharArrayWriter writer;
20
21     /**
22      * Initializes wrapper.
23      *
24      * First, this constructor calls the parent constructor. That call is crucial
25      * so that the response is stored and thus setHeader, setStatus, addCookie,
26      * and so forth work normally.
27      *
28      * Second, this constructor creates a CharArrayWriter that will be used to
29      * accumulate the response.
30      *
31      * @param response
32      */

33     public CharArrayResponseWrapper(HttpServletResponse response) {
34         super(response);
35         writer = new CharArrayWriter();
36     }
37
38     /**
39      * When servlets or JSP pages ask for the Writer, don't give them the real
40      * one. Instead, give them a version that writes into the character array.
41      * The filter needs to send the contents of the array to the client (perhaps
42      * after modifying it).
43      */

44     public PrintWriter getWriter() {
45         return new PrintWriter(writer);
46     }
47
48     /**
49      * Get a String representation of the entire buffer.
50      *
51      * Be sure <B>not</B> to call this method multiple times on the same wrapper.
52      * The API for CharArrayWriter does not guarantee that it "remembers" the
53      * previous value, so the call is likely to make a new String every time.
54      */

55     public String toString() {
56         return writer.toString();
57     }
58
59     /**
60      * Get the underlying character array.
61      *
62      * @return content as char array
63      */

64     public char[] toCharArray() {
65         return writer.toCharArray();
66     }
67
68     /**
69      * Get the underlying as byte array.
70      *
71      * @return content as byte array
72      */

73     public byte[] toByteArray() {
74         return CharUtil.toByteArray(writer.toCharArray());
75     }
76
77
78     /**
79      * This empty method <b>must</b> exist.
80      *
81      * @param len
82      */

83     public void setContentLength(int len) {
84     }
85
86     /**
87      * Returns the size (number of characters) of written data.
88      *
89      * @return size of written data
90      */

91     public int getSize() {
92         return writer.size();
93     }
94
95
96     private String contentType = "";
97
98     /**
99      * Sets the content type.
100      *
101      * @param type
102      */

103     public void setContentType(String type) {
104         super.setContentType(type);
105         contentType = type;
106     }
107
108     /**
109      * Returns content type.
110      *
111      * @return content type
112      */

113     public String getContentType() {
114         return contentType;
115     }
116
117     public void close() {
118         writer.close();
119     }
120
121     /**
122      * Returns output stream.
123      *
124      * @return output stream
125      * @exception java.io.IOException
126      */

127     public ServletOutputStream getOutputStream() throws java.io.IOException {
128
129         return new ServletOutputStream() {
130             public void write(int b) throws IOException {
131                 writer.write(b);
132             }
133
134             public void write(byte b[]) throws IOException {
135                 writer.write(CharUtil.toCharArray(b), 0, b.length);
136             }
137
138             public void write(byte b[], int off, int len) throws IOException {
139                 writer.write(CharUtil.toCharArray(b), off, len);
140             }
141         };
142     }
143
144 }
145
Popular Tags