KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > CofaxJspResponse


1 /*
2  * CofaxJspResponse is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/CofaxJspResponse.java,v 1.2.2.1 2006/12/11 16:28:44 fxrobin Exp $
21  */

22
23 package org.cofax;
24
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
27 import javax.servlet.http.Cookie JavaDoc;
28 import javax.servlet.ServletOutputStream JavaDoc;
29 import java.io.ByteArrayOutputStream JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.PrintWriter JavaDoc;
33 import java.io.UnsupportedEncodingException JavaDoc;
34 import java.text.SimpleDateFormat JavaDoc;
35 import java.util.Date JavaDoc;
36 import java.util.Locale JavaDoc;
37 import org.cofax.CofaxPage;
38
39 /**
40  * The Cofax project's web serving program.
41  * <p>
42  *
43  * <p>
44  * <code>CofaxJspResponse</code> get the jsp execution and returns it in a
45  * CofaxPage It is used to allow cache in Cofax CDSServlet for JSP templates.
46  * </p>
47  *
48  * @author Jacques Archimede
49  * @created May 10, 2004
50  */

51
52 public class CofaxJspResponse extends HttpServletResponseWrapper JavaDoc implements HttpServletResponse JavaDoc {
53
54     private final SimpleDateFormat JavaDoc format = new SimpleDateFormat JavaDoc("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
55
56     private String JavaDoc encoding = null;
57
58     private CofaxJspResponseStream stream = null;
59
60     private PrintWriter JavaDoc printWriter = null;
61
62     private StringWriter JavaDoc stringWriter = null;
63
64     private CofaxPage page = null;
65
66     private String JavaDoc redirect = null;
67
68     public CofaxJspResponse(HttpServletResponse JavaDoc response, CofaxPage page) {
69         super(response);
70         this.page = page;
71     }
72
73     public String JavaDoc getRedirect() {
74         return this.redirect;
75     }
76
77     public void addCookie(Cookie JavaDoc cookie) {
78         super.addCookie(cookie);
79     }
80
81     public boolean containsHeader(String JavaDoc name) {
82         return super.containsHeader(name) || this.page.getHeaders().containsKey(name);
83     }
84
85     /*
86      * FX : only in servlet 2.4 :-(
87      *
88      * public String getContentType(String name) { return this.getContentType(); }
89      */

90
91     public String JavaDoc encodeURL(String JavaDoc url) {
92         return super.encodeURL(url);
93     }
94
95     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
96         return super.encodeRedirectURL(url);
97     }
98
99     public String JavaDoc encodeUrl(String JavaDoc url) {
100         return super.encodeUrl(url);
101     }
102
103     public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
104         return super.encodeRedirectUrl(url);
105     }
106
107     public void sendError(int sc, String JavaDoc msg) throws IOException JavaDoc {
108         this.page.setStatus(sc);
109         this.page.setErrorMsg(msg);
110     }
111
112     public void sendError(int sc) throws IOException JavaDoc {
113         this.page.setStatus(sc);
114     }
115
116     public void sendRedirect(String JavaDoc location) throws java.io.IOException JavaDoc {
117         this.page.setStatus(SC_MOVED_TEMPORARILY);
118         this.redirect = location;
119     }
120
121     public void setDateHeader(String JavaDoc name, long date) {
122         this.page.putHeader(name, format.format(new Date JavaDoc(date)));
123     }
124
125     public void addDateHeader(String JavaDoc name, long date) {
126         this.page.putHeader(name, format.format(new Date JavaDoc(date)));
127     }
128
129     public void setHeader(String JavaDoc name, String JavaDoc value) {
130         this.page.putHeader(name, value);
131     }
132
133     public void addHeader(String JavaDoc name, String JavaDoc value) {
134         this.page.putHeader(name, value);
135     }
136
137     public void setIntHeader(String JavaDoc name, int value) {
138         this.page.putHeader(name, String.valueOf(value));
139     }
140
141     public void addIntHeader(String JavaDoc name, int value) {
142         this.page.putHeader(name, String.valueOf(value));
143     }
144
145     public void setStatus(int sc) {
146         this.page.setStatus(sc);
147     }
148
149     public void setStatus(int sc, String JavaDoc sm) {
150         this.page.setStatus(sc);
151         this.page.setErrorMsg(sm);
152     }
153
154     public String JavaDoc getCharacterEncoding() {
155         return super.getCharacterEncoding();
156     }
157
158     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
159         if (this.printWriter != null) {
160             throw new IOException JavaDoc("getWriter() is already called");
161         }
162         if (this.stream == null) {
163             this.stream = new CofaxJspResponseStream(encoding);
164         }
165         return this.stream;
166     }
167
168     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
169         if (this.stream != null) {
170             throw new IOException JavaDoc("getOutputStream() is already called");
171         }
172         if (this.printWriter == null) {
173             this.stringWriter = new StringWriter JavaDoc(CofaxUtil.COMMON_DATA_SIZE);
174             this.printWriter = new PrintWriter JavaDoc(this.stringWriter);
175         }
176         return this.printWriter;
177     }
178
179     public void setContentLength(int len) {
180     }
181
182     public void setContentType(String JavaDoc type) {
183         int semicolon = type.lastIndexOf(';');
184         if (semicolon >= 0 && semicolon != (type.length() - 1)) {
185             String JavaDoc charset = type.substring(semicolon + 1);
186             charset = charset.trim();
187             if (charset.toLowerCase().startsWith("charset")) {
188                 charset = charset.substring(charset.indexOf("=") + 1).trim();
189                 if (charset.length() > 0) {
190                     this.encoding = charset;
191                 }
192             }
193         }
194         super.setContentType(type);
195     }
196
197     public void setBufferSize(int size) {
198     }
199
200     public int getBufferSize() {
201         return this.stringWriter.getBuffer().length();
202     }
203
204     public void flushBuffer() throws IOException JavaDoc {
205         this.stringWriter.flush();
206     }
207
208     public void resetBuffer() {
209         this.stringWriter.getBuffer().setLength(0);
210     }
211
212     public boolean isCommitted() {
213         return super.isCommitted();
214     }
215
216     public void reset() {
217         super.reset();
218         this.resetBuffer();
219     }
220
221     public void setLocale(java.util.Locale JavaDoc loc) {
222         super.setLocale(loc);
223     }
224
225     public Locale JavaDoc getLocale() {
226         return super.getLocale();
227     }
228
229     public String JavaDoc toString() {
230         if (this.stringWriter != null) {
231             return this.stringWriter.getBuffer().toString();
232         } else if (this.stream != null) {
233             return this.stream.toString();
234         } else {
235             return "";
236         }
237     }
238
239     class CofaxJspResponseStream extends ServletOutputStream JavaDoc {
240         protected String JavaDoc encoding = null;
241
242         protected ByteArrayOutputStream JavaDoc stream = new ByteArrayOutputStream JavaDoc();
243
244         public CofaxJspResponseStream(String JavaDoc encoding) {
245             this.encoding = encoding;
246         }
247
248         public void close() throws IOException JavaDoc {
249             this.stream.close();
250         }
251
252         public void flush() throws IOException JavaDoc {
253             this.stream.flush();
254         }
255
256         public void reset() {
257             this.stream.reset();
258         }
259
260         public int size() {
261             return this.stream.size();
262         }
263
264         public void write(int b) throws IOException JavaDoc {
265             this.stream.write(b);
266         }
267
268         public void write(byte[] b) throws IOException JavaDoc {
269             this.stream.write(b);
270         }
271
272         public void write(byte[] b, int off, int len) throws IOException JavaDoc {
273             this.stream.write(b, off, len);
274         }
275
276         public String JavaDoc toString() {
277             try {
278                 return encoding == null ? stream.toString() : stream.toString(encoding);
279             } catch (UnsupportedEncodingException JavaDoc e) {
280                 return stream.toString();
281             }
282         }
283     }
284
285 }
Popular Tags