001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.webapp;
021    
022    import org.apache.commons.logging.Log;
023    import org.apache.commons.logging.LogFactory;
024    
025    import javax.servlet.ServletOutputStream;
026    import javax.servlet.http.HttpServletResponse;
027    import javax.servlet.http.HttpServletResponseWrapper;
028    import java.io.IOException;
029    import java.io.PrintWriter;
030    import java.io.StringWriter;
031    
032    /*
033     * Created: Nov 29, 2004 4:48:29 PM
034     * User: bommel
035     * $Id: TobagoResponse.java 1368577 2012-08-02 16:20:31Z lofwyr $
036     */
037    public class TobagoResponse extends HttpServletResponseWrapper {
038    
039      private static final Log LOG = LogFactory.getLog(TobagoResponse.class);
040    
041      private PrintWriter printWriter = null;
042      private StringWriter bufferedWriter = null;
043    
044    
045      public TobagoResponse(HttpServletResponse base) {
046        super(base);
047      }
048    
049      public void setBuffering() {
050        if (bufferedWriter == null) {
051          bufferedWriter = new StringWriter();
052          printWriter = new PrintWriter(bufferedWriter);
053        }
054      }
055    
056      public String getBufferedString() {
057        if (bufferedWriter != null) {
058          printWriter.flush();
059          return bufferedWriter.toString();
060        } else {
061          return "";
062        }
063      }
064    
065      public ServletOutputStream getOutputStream() throws IOException {
066        LOG.debug("***** getOutputStream() from " + new Exception().getStackTrace()[1]);
067        return getResponse().getOutputStream();
068      }
069    
070      public PrintWriter getWriter() throws IOException {
071        LOG.debug("***** getWriter() from " + new Exception().getStackTrace()[1]);
072        if (printWriter != null) {
073          return printWriter;
074        }
075        return getResponse().getWriter();
076      }
077    
078    
079      public void setContentType(String s) {
080        LOG.debug("***** setContentType(" + s + ") from " + new Exception().getStackTrace()[1]);
081        getResponse().setContentType(s);
082      }
083    
084    
085    }