View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.webapp;
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  
25  import javax.servlet.ServletOutputStream;
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.http.HttpServletResponseWrapper;
28  import java.io.IOException;
29  import java.io.PrintWriter;
30  import java.io.StringWriter;
31  
32  public class TobagoResponse extends HttpServletResponseWrapper {
33  
34    private static final Logger LOG = LoggerFactory.getLogger(TobagoResponse.class);
35  
36    private PrintWriter printWriter = null;
37    private StringWriter bufferedWriter = null;
38  
39  
40    public TobagoResponse(final HttpServletResponse base) {
41      super(base);
42    }
43  
44    public void setBuffering() {
45      if (bufferedWriter == null) {
46        bufferedWriter = new StringWriter();
47        printWriter = new PrintWriter(bufferedWriter);
48      }
49    }
50  
51    public String getBufferedString() {
52      if (bufferedWriter != null) {
53        printWriter.flush();
54        return bufferedWriter.toString();
55      } else {
56        return "";
57      }
58    }
59  
60    @Override
61    public ServletOutputStream getOutputStream() throws IOException {
62      LOG.debug("***** getOutputStream() from " + new Exception().getStackTrace()[1]);
63      return getResponse().getOutputStream();
64    }
65  
66    @Override
67    public PrintWriter getWriter() throws IOException {
68      LOG.debug("***** getWriter() from " + new Exception().getStackTrace()[1]);
69      if (printWriter != null) {
70        return printWriter;
71      }
72      return getResponse().getWriter();
73    }
74  
75  
76    @Override
77    public void setContentType(final String s) {
78      LOG.debug("***** setContentType(" + s + ") from " + new Exception().getStackTrace()[1]);
79      getResponse().setContentType(s);
80    }
81  
82  }