KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ajaxanywhere > BufferResponseWrapper


1 /*
2 Copyright 2005 Vitaliy Shevchuk (shevit@users.sourceforge.net)
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 */

17
18 package org.ajaxanywhere;
19
20 import javax.servlet.ServletOutputStream JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
23 import java.io.*;
24
25 /**
26  * Date: 23 juil. 2005
27  * Time: 21:19:14
28  */

29 public class BufferResponseWrapper extends HttpServletResponseWrapper JavaDoc {
30
31
32     PrintWriter pw;
33     ServletOutputStream JavaDoc sos;
34     private StringWriter writerBuffer;
35     private ByteArrayOutputStream streamBuffer;
36
37     HttpServletResponse JavaDoc originalResponse;
38
39     private String JavaDoc redirect;
40
41     public BufferResponseWrapper(HttpServletResponse JavaDoc httpServletResponse) {
42         super(httpServletResponse);
43         originalResponse = httpServletResponse;
44     }
45
46     public PrintWriter getWriter() throws IOException {
47         if (writerBuffer == null) {
48             writerBuffer = new StringWriter();
49             pw = new PrintWriter(writerBuffer);
50         }
51         return pw;
52     }
53
54     public ServletOutputStream JavaDoc getOutputStream() throws IOException {
55         if (streamBuffer == null) {
56             streamBuffer = new ByteArrayOutputStream();
57             sos = new ServletOutputStream JavaDoc() {
58                 public void write(int b) throws IOException {
59                     streamBuffer.write(b);
60                 }
61
62                 public void write(byte b[]) throws IOException {
63                     streamBuffer.write(b);
64                 }
65
66                 public void write(byte b[], int off, int len) throws IOException {
67                     streamBuffer.write(b, off, len);
68                 }
69             };
70         }
71         return sos;
72     }
73
74     /**
75      * Outputs the content to OutputStream if the application is using it or to the Writer otherwise.
76      */

77     public void output(String JavaDoc content) throws IOException{
78         if (streamBuffer!=null){
79             streamBuffer.write(content.getBytes(originalResponse.getCharacterEncoding()));
80         } else {
81             writerBuffer.write(content);
82         }
83     }
84
85     public String JavaDoc getBuffer() {
86
87             if (streamBuffer != null) {
88                 try {
89                    return streamBuffer.toString(originalResponse.getCharacterEncoding());
90                 } catch (UnsupportedEncodingException e) {
91                    return streamBuffer.toString();
92                 }
93             }
94             else if (writerBuffer != null)
95                 return writerBuffer.toString();
96             else
97                 return "";
98     }
99
100     public HttpServletResponse JavaDoc getOriginalResponse() {
101         return originalResponse;
102     }
103
104     public String JavaDoc getRedirect() {
105         return redirect;
106     }
107
108     public void sendRedirect(String JavaDoc redirect) throws IOException {
109         String JavaDoc key = "aaxmlrequest=true";
110         int pos = redirect.indexOf(key);
111         if (pos !=-1)
112             redirect = redirect.substring(0,pos)+redirect.substring(pos+key.length());
113         
114         this.redirect = redirect;
115     }
116
117     public String JavaDoc findSubstring(String JavaDoc firstDelimiter, String JavaDoc lastDelimiter){
118         String JavaDoc content;
119         if (streamBuffer!=null){
120             try {
121                 content = streamBuffer.toString("UTF-8");
122             } catch (UnsupportedEncodingException e) {
123                 content = streamBuffer.toString();
124             }
125         } else if (writerBuffer!=null) {
126             content = writerBuffer.toString();
127         } else {
128             return null;
129         }
130
131
132         int p1 = content.indexOf(firstDelimiter);
133         if (p1 != -1) {
134             p1+=firstDelimiter.length();
135             int p2 = content.indexOf(lastDelimiter, p1);
136             if (p2!=-1){
137                 return content.substring(p1, p2);
138             }
139         }
140         return null;
141     }
142
143     public void setContentType(String JavaDoc string) {
144         // do nothing
145
}
146
147     public void flushBuffer() throws IOException {
148         // do nothing
149
}
150
151     public void setCharacterEncoding(String JavaDoc string) {
152         // do nothing
153
}
154
155     public void setDateHeader(String JavaDoc string, long l) {
156         //do nothing
157
}
158
159     public void addDateHeader(String JavaDoc string, long l) {
160         //do nothing
161
}
162
163     public void setHeader(String JavaDoc string, String JavaDoc string1) {
164         //do nothing
165
}
166
167     public void addHeader(String JavaDoc string, String JavaDoc string1) {
168         //do nothing
169
}
170
171     public void setIntHeader(String JavaDoc string, int i) {
172         //do nothing
173
}
174
175     public void addIntHeader(String JavaDoc string, int i) {
176         //do nothing
177
}
178
179     public StringWriter getWriterBuffer() {
180         return writerBuffer;
181     }
182
183     public ByteArrayOutputStream getStreamBuffer() {
184         return streamBuffer;
185     }
186 }
187
Popular Tags