View Javadoc

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  package org.apache.portals.gems.util;
18  
19  import java.io.IOException;
20  import java.io.PrintWriter;
21  import java.io.UnsupportedEncodingException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletResponse;
27  
28  public class HttpBufferedResponse extends javax.servlet.http.HttpServletResponseWrapper
29  {
30      private boolean usingWriter;
31      private boolean usingStream;
32  
33      /*** Commons logging */
34      protected final static Log log = LogFactory.getLog(HttpBufferedResponse.class);
35  
36      private ServletOutputStream wrappedStream;
37      private PrintWriter writer;
38  
39      public HttpBufferedResponse(HttpServletResponse servletResponse,
40                                  PrintWriter writer)
41      {
42          super(servletResponse);
43          this.writer = writer;
44      }
45  
46      public ServletOutputStream getOutputStream() throws IllegalStateException, IOException
47      {
48          if (usingWriter)
49          {
50              throw new IllegalStateException("getOutputStream can't be used after getWriter was invoked");
51          }
52  
53          if (wrappedStream == null)
54          {            
55              wrappedStream = new PrintWriterServletOutputStream(writer, getResponse().getCharacterEncoding());                                                               
56          }
57  
58          usingStream = true;
59  
60          return wrappedStream;
61      }
62  
63      public PrintWriter getWriter() throws UnsupportedEncodingException, IllegalStateException, IOException {
64  
65          if (usingStream)
66          {
67              throw new IllegalStateException("getWriter can't be used after getOutputStream was invoked");
68          }
69  
70          usingWriter = true;
71  
72          return writer;
73      }
74  
75  
76      public void setBufferSize(int size)
77      {
78          // ignore
79      }
80  
81      public int getBufferSize()
82      {
83          return 0;
84      }
85  
86      public void flushBuffer() throws IOException
87      {
88          writer.flush();
89      }
90  
91      public boolean isCommitted()
92      {
93          return false;
94      }
95  
96      public void reset()
97      {
98          // ignore right now
99      }
100 }