View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
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  package org.apache.jetspeed.util.servlet;
18  
19  import java.io.IOException;
20  import java.io.OutputStream;
21  import java.io.PrintWriter;
22  import javax.servlet.*;
23  import javax.servlet.http.*;
24  import java.util.Locale;
25  
26  /***
27   *
28   * EcsServletResponse is an HttpServletResponse implementation that 
29   * wraps an existing HttpServletResponse and redirects its output to
30   * an alternate OutputStream.
31   *
32   * @author <a href="raphael@apache.org">Raphaël Luta</a>
33   */
34  public class EcsServletResponse implements HttpServletResponse
35  {
36      /*** The wrapped ServletResponse */
37      private HttpServletResponse rsp;
38  
39      /*** The new OutputStream to use */
40      private EcsServletOutputStream sout; 
41  
42      /*** The PrintWriter to use if defined */
43      private PrintWriter writer; 
44      
45      /*** state flag: has getOutputStream() already been called ? */
46      private boolean outUsed = false;
47  
48      /*** state flag: has getWriter() already been called ? */
49      private boolean writerUsed = false;
50     
51      /*** Build a Response
52       *
53       * @param rsp the base HttpServletResponse to wrap
54       * @param out the OutputStream to be used for writing data
55       */
56      public EcsServletResponse(HttpServletResponse rsp, OutputStream out) {
57          this.rsp = rsp;
58          this.sout = new EcsServletOutputStream(out);
59      }        
60  
61      /***
62       * @see javax.servlet.ServletResponse#getCharacterEncoding
63       */
64      public String getCharacterEncoding()
65      {
66          return rsp.getCharacterEncoding();
67      }
68      
69      /***
70       * @see javax.servlet.ServletResponse#getOutputStream
71       */
72      public ServletOutputStream getOutputStream() throws IOException 
73      {
74          if (writerUsed)
75          {
76              throw new IllegalStateException("Writer already used");
77          }
78  
79          this.outUsed = true;
80  
81          return this.sout;
82      }
83      
84      /***
85       * @see javax.servlet.ServletResponse#getWriter
86       */
87      public PrintWriter getWriter() throws IOException
88      {
89          if (outUsed) 
90          {
91              throw new IllegalStateException("OutputStream already in use");
92          }
93          
94          if (writer==null) 
95          {
96              this.writer = new PrintWriter(this.sout);
97          }
98      
99          this.writerUsed = true;
100         
101         return writer;
102     }
103     
104     /***
105      * @see javax.servlet.ServletResponse#setContentLength
106      */
107     public void setContentLength(int len)
108     {
109         // silently fail
110     }
111     
112     /***
113      * @see javax.servlet.ServletResponse#setContentType
114      */
115     public void setContentType(String type) 
116     {
117         // silently fail
118     }
119     
120     /***
121      * @see javax.servlet.ServletResponse#setBufferSize
122      */
123     public void setBufferSize(int size)
124     {
125         // silently fail
126     }
127     
128     /***
129      * @see javax.servlet.ServletResponse#getBufferSize
130      */
131     public int getBufferSize() 
132     {
133         return rsp.getBufferSize();
134     }
135     
136     /***
137      * @see javax.servlet.ServletResponse#flushBuffer
138      */
139     public void flushBuffer() throws IOException
140     {
141         this.sout.flush();
142     }
143     
144     /***
145      * @see javax.servlet.ServletResponse#isCommitted
146      */
147     public boolean isCommitted()
148     {
149         // we don't implement reset() so always claim 
150         // the data is committed
151         return true;
152     }
153     
154     /***
155      * @see javax.servlet.ServletResponse#reset
156      */
157     public void reset()
158     {
159         // silently fail
160     }
161 
162     /***
163      * @see javax.servlet.ServletResponse#setLocale
164      */
165     public void setLocale(Locale loc)
166     {
167         // we can safely ignore this
168     }
169     
170     /***
171      * @see javax.servlet.ServletResponse#getLocale
172      */
173     public Locale getLocale()
174     {
175         return rsp.getLocale();
176     }
177 
178     /***
179      * @see javax.servlet.http.HttpServletResponse#addCookie
180      */
181     public void addCookie(Cookie cookie)
182     {
183         rsp.addCookie(cookie);
184     }
185 
186     /***
187      * @see javax.servlet.http.HttpServletResponse#containsHeader
188      */
189     public boolean containsHeader(String name)
190     {
191         return rsp.containsHeader(name);
192     }
193 
194     /***
195      * @see javax.servlet.http.HttpServletResponse#encodeURL
196      */
197     public String encodeURL(String url)
198     {
199         return rsp.encodeURL(url);
200     }
201 
202     /***
203      * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL
204      */
205     public String encodeRedirectURL(String url)
206     {
207         return rsp.encodeRedirectURL(url);
208     }
209 
210     /***
211      * @see javax.servlet.http.HttpServletResponse#encodeUrl
212      */
213     public String encodeUrl(String url)
214     {
215         return rsp.encodeUrl(url);
216     }
217     
218     /***
219      * @see javax.servlet.http.HttpServletResponse#encodeRedirectUrl
220      */
221     public String encodeRedirectUrl(String url)
222     {
223         return rsp.encodeRedirectUrl(url);
224     }
225 
226     /***
227      * @see javax.servlet.http.HttpServletResponse#sendError
228      */
229     public void sendError(int sc, String msg) throws IOException
230     {
231         // silently fail
232     }
233 
234     /***
235      * @see javax.servlet.http.HttpServletResponse#sendError
236      */
237     public void sendError(int sc) throws IOException
238     {
239         // silently fail
240     }
241 
242     /***
243      * @see javax.servlet.http.HttpServletResponse#sendRedirect
244      */
245     public void sendRedirect(String location) throws IOException
246     {
247         // silently fail
248     }
249     
250     /***
251      * @see javax.servlet.http.HttpServletResponse#setDateHeader
252      */
253     public void setDateHeader(String name, long date)
254     {
255         // silently fail
256     }
257     
258     /***
259      * @see javax.servlet.http.HttpServletResponse#addDateHeader
260      */
261     public void addDateHeader(String name, long date)
262     {
263         // silently fail
264     }
265     
266     /***
267      * @see javax.servlet.http.HttpServletResponse#setHeader
268      */
269     public void setHeader(String name, String value)
270     {
271         // silently fail
272     }
273     
274     /***
275      * @see javax.servlet.http.HttpServletResponse#addHeader
276      */
277     public void addHeader(String name, String value)
278     {
279         // silently fail
280     }
281 
282     /***
283      * @see javax.servlet.http.HttpServletResponse#setIntHeader
284      */
285     public void setIntHeader(String name, int value)
286     {
287         // silently fail
288     }
289 
290     /***
291      * @see javax.servlet.http.HttpServletResponse#addIntHeader
292      */
293     public void addIntHeader(String name, int value)
294     {
295         // silently fail
296     }
297 
298     /***
299      * @see javax.servlet.http.HttpServletResponse#setStatus
300      */
301     public void setStatus(int sc)
302     {
303         // silently fail
304     }
305   
306     /***
307      * @see javax.servlet.http.HttpServletResponse#setStatus
308      */
309     public void setStatus(int sc, String sm)
310     {
311         // silently fail
312     }
313 
314     public void resetBuffer()
315     {
316         // silently fail
317         //added by Ben Woodward
318     }
319 
320 }