KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > ssi > ResponseIncludeWrapper


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

17
18 package org.apache.catalina.ssi;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23
24 import javax.servlet.ServletContext JavaDoc;
25 import javax.servlet.ServletOutputStream JavaDoc;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
29
30 import org.apache.catalina.util.DateTool;
31 /**
32  * A HttpServletResponseWrapper, used from
33  * <code>SSIServletExternalResolver</code>
34  *
35  * @author Bip Thelin
36  * @author David Becker
37  * @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
38  */

39 public class ResponseIncludeWrapper extends HttpServletResponseWrapper JavaDoc {
40     /**
41      * The names of some headers we want to capture.
42      */

43     private static final String JavaDoc CONTENT_TYPE = "content-type";
44     private static final String JavaDoc LAST_MODIFIED = "last-modified";
45     protected long lastModified = -1;
46     private String JavaDoc contentType = null;
47
48     /**
49      * Our ServletOutputStream
50      */

51     protected ServletOutputStream JavaDoc captureServletOutputStream;
52     protected ServletOutputStream JavaDoc servletOutputStream;
53     protected PrintWriter JavaDoc printWriter;
54     
55     private ServletContext JavaDoc context;
56     private HttpServletRequest JavaDoc request;
57
58
59     /**
60      * Initialize our wrapper with the current HttpServletResponse and
61      * ServletOutputStream.
62      *
63      * @param context The servlet context
64      * @param request The HttpServletResponse to use
65      * @param response The response to use
66      * @param captureServletOutputStream The ServletOutputStream to use
67      */

68     public ResponseIncludeWrapper(ServletContext JavaDoc context,
69             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
70            ServletOutputStream JavaDoc captureServletOutputStream) {
71         super(response);
72         this.context = context;
73         this.request = request;
74         this.captureServletOutputStream = captureServletOutputStream;
75     }
76
77
78     /**
79      * Flush the servletOutputStream or printWriter ( only one will be non-null )
80      * This must be called after a requestDispatcher.include, since we can't
81      * assume that the included servlet flushed its stream.
82      */

83     public void flushOutputStreamOrWriter() throws IOException JavaDoc {
84         if (servletOutputStream != null) {
85             servletOutputStream.flush();
86         }
87         if (printWriter != null) {
88             printWriter.flush();
89         }
90     }
91
92
93     /**
94      * Return a printwriter, throws and exception if a OutputStream already
95      * been returned.
96      *
97      * @return a PrintWriter object
98      * @exception java.io.IOException
99      * if the outputstream already been called
100      */

101     public PrintWriter JavaDoc getWriter() throws java.io.IOException JavaDoc {
102         if (servletOutputStream == null) {
103             if (printWriter == null) {
104                 setCharacterEncoding(getCharacterEncoding());
105                 printWriter = new PrintWriter JavaDoc(
106                         new OutputStreamWriter JavaDoc(captureServletOutputStream,
107                                                getCharacterEncoding()));
108             }
109             return printWriter;
110         }
111         throw new IllegalStateException JavaDoc();
112     }
113
114
115     /**
116      * Return a OutputStream, throws and exception if a printwriter already
117      * been returned.
118      *
119      * @return a OutputStream object
120      * @exception java.io.IOException
121      * if the printwriter already been called
122      */

123     public ServletOutputStream JavaDoc getOutputStream() throws java.io.IOException JavaDoc {
124         if (printWriter == null) {
125             if (servletOutputStream == null) {
126                 servletOutputStream = captureServletOutputStream;
127             }
128             return servletOutputStream;
129         }
130         throw new IllegalStateException JavaDoc();
131     }
132     
133     
134     /**
135      * Returns the value of the <code>last-modified</code> header field. The
136      * result is the number of milliseconds since January 1, 1970 GMT.
137      *
138      * @return the date the resource referenced by this
139      * <code>ResponseIncludeWrapper</code> was last modified, or -1 if not
140      * known.
141      */

142     public long getLastModified() {
143         if (lastModified == -1) {
144             // javadocs say to return -1 if date not known, if you want another
145
// default, put it here
146
return -1;
147         }
148         return lastModified;
149     }
150
151     /**
152      * Sets the value of the <code>last-modified</code> header field.
153      *
154      * @param lastModified The number of milliseconds since January 1, 1970 GMT.
155      */

156     public void setLastModified(long lastModified) {
157         this.lastModified = lastModified;
158         ((HttpServletResponse JavaDoc) getResponse()).setDateHeader(LAST_MODIFIED,
159                 lastModified);
160     }
161
162     /**
163      * Returns the value of the <code>content-type</code> header field.
164      *
165      * @return the content type of the resource referenced by this
166      * <code>ResponseIncludeWrapper</code>, or <code>null</code> if not known.
167      */

168     public String JavaDoc getContentType() {
169         if (contentType == null) {
170             String JavaDoc url = request.getRequestURI();
171             String JavaDoc mime = context.getMimeType(url);
172             if (mime != null)
173             {
174                 setContentType(mime);
175             }
176             else
177             {
178                 // return a safe value
179
setContentType("application/x-octet-stream");
180             }
181         }
182         return contentType;
183     }
184     
185     /**
186      * Sets the value of the <code>content-type</code> header field.
187      *
188      * @param mime a mime type
189      */

190     public void setContentType(String JavaDoc mime) {
191         contentType = mime;
192         if (contentType != null) {
193             getResponse().setContentType(contentType);
194         }
195     }
196
197
198     public void addDateHeader(String JavaDoc name, long value) {
199         super.addDateHeader(name, value);
200         String JavaDoc lname = name.toLowerCase();
201         if (lname.equals(LAST_MODIFIED)) {
202             lastModified = value;
203         }
204     }
205
206     public void addHeader(String JavaDoc name, String JavaDoc value) {
207         super.addHeader(name, value);
208         String JavaDoc lname = name.toLowerCase();
209         if (lname.equals(LAST_MODIFIED)) {
210             try {
211                 lastModified = DateTool.rfc1123Format.parse(value).getTime();
212             } catch (Throwable JavaDoc ignore) { }
213         } else if (lname.equals(CONTENT_TYPE)) {
214             contentType = value;
215         }
216     }
217
218     public void setDateHeader(String JavaDoc name, long value) {
219         super.setDateHeader(name, value);
220         String JavaDoc lname = name.toLowerCase();
221         if (lname.equals(LAST_MODIFIED)) {
222             lastModified = value;
223         }
224     }
225
226     public void setHeader(String JavaDoc name, String JavaDoc value) {
227         super.setHeader(name, value);
228         String JavaDoc lname = name.toLowerCase();
229         if (lname.equals(LAST_MODIFIED)) {
230             try {
231                 lastModified = DateTool.rfc1123Format.parse(value).getTime();
232             } catch (Throwable JavaDoc ignore) { }
233         }
234         else if (lname.equals(CONTENT_TYPE))
235         {
236             contentType = value;
237         }
238     }
239 }
240
Popular Tags