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