Clover coverage report - Maven Clover report
Coverage timestamp: Sun Jan 3 2010 19:08:46 CET
file stats: LOC: 261   Methods: 14
NCLOC: 142   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BufferedResponseWrapper13Impl.java 23.1% 34% 57.1% 34.5%
coverage coverage
 1    /**
 2    * Licensed under the Artistic License; you may not use this file
 3    * except in compliance with the License.
 4    * You may obtain a copy of the License at
 5    *
 6    * http://displaytag.sourceforge.net/license.html
 7    *
 8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 10    * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 11    */
 12    package org.displaytag.filter;
 13   
 14    import java.io.CharArrayWriter;
 15    import java.io.IOException;
 16    import java.io.PrintWriter;
 17   
 18    import javax.servlet.ServletOutputStream;
 19    import javax.servlet.http.HttpServletResponse;
 20    import javax.servlet.http.HttpServletResponseWrapper;
 21   
 22    import org.apache.commons.lang.ArrayUtils;
 23    import org.apache.commons.lang.StringUtils;
 24    import org.apache.commons.logging.Log;
 25    import org.apache.commons.logging.LogFactory;
 26    import org.displaytag.tags.TableTagParameters;
 27   
 28   
 29    /**
 30    * J2ee 1.3 implementation of BufferedResponseWrapper. Need to extend HttpServletResponseWrapper for Weblogic
 31    * compatibility.
 32    * @author rapruitt
 33    * @author Fabrizio Giustina
 34    * @version $Revision: 1081 $ ($Author: fgiust $)
 35    */
 36    public class BufferedResponseWrapper13Impl extends HttpServletResponseWrapper implements BufferedResponseWrapper
 37    {
 38   
 39    /**
 40    * logger.
 41    */
 42    private static Log log = LogFactory.getLog(BufferedResponseWrapper13Impl.class);
 43   
 44    /**
 45    * The buffered response.
 46    */
 47    private CharArrayWriter outputWriter;
 48   
 49    /**
 50    * The outputWriter stream.
 51    */
 52    private SimpleServletOutputStream servletOutputStream;
 53   
 54    /**
 55    * The contentType.
 56    */
 57    private String contentType;
 58   
 59    /**
 60    * If state is set, allow getOutputStream() to return the "real" output stream, elsewhere returns a internal buffer.
 61    */
 62    private boolean state;
 63   
 64    /**
 65    * Writer has been requested.
 66    */
 67    private boolean outRequested;
 68   
 69    /**
 70    * @param httpServletResponse the response to wrap
 71    */
 72  14 public BufferedResponseWrapper13Impl(HttpServletResponse httpServletResponse)
 73    {
 74  14 super(httpServletResponse);
 75  14 this.outputWriter = new CharArrayWriter();
 76  14 this.servletOutputStream = new SimpleServletOutputStream();
 77    }
 78   
 79    /**
 80    * @see org.displaytag.filter.BufferedResponseWrapper#getContentType()
 81    */
 82  14 public String getContentType()
 83    {
 84  14 return this.contentType;
 85    }
 86   
 87    /**
 88    * The content type is NOT set on the wrapped response. You must set it manually. Overrides any previously set
 89    * value.
 90    * @param theContentType the content type.
 91    */
 92  16 public void setContentType(String theContentType)
 93    {
 94  16 if (state)
 95    {
 96  0 log.debug("Allowing content type");
 97   
 98  0 if (this.contentType != null && // content type has been set before
 99    this.contentType.indexOf("charset") > -1) // and it specified charset
 100    {
 101    // so copy the charset
 102  0 String charset = this.contentType.substring(this.contentType.indexOf("charset"));
 103  0 if (log.isDebugEnabled())
 104    {
 105  0 log.debug("Adding charset: [" + charset + "]");
 106    }
 107   
 108  0 getResponse().setContentType(StringUtils.substringBefore(theContentType, "charset") + '=' + charset);
 109    }
 110    else
 111    {
 112  0 getResponse().setContentType(theContentType);
 113    }
 114   
 115    }
 116  16 this.contentType = theContentType;
 117    }
 118   
 119    /**
 120    * @see javax.servlet.ServletResponse#getWriter()
 121    */
 122  14 public PrintWriter getWriter() throws IOException
 123    {
 124   
 125  14 if (state && !outRequested)
 126    {
 127  0 log.debug("getWriter() returned");
 128   
 129    // ok, exporting in progress, discard old data and go on streaming
 130  0 this.servletOutputStream.reset();
 131  0 this.outputWriter.reset();
 132  0 this.outRequested = true;
 133  0 return ((HttpServletResponse) getResponse()).getWriter();
 134    }
 135   
 136  14 return new PrintWriter(this.outputWriter);
 137    }
 138   
 139    /**
 140    * Flush the buffer, not the response.
 141    * @throws IOException if encountered when flushing
 142    */
 143  2 public void flushBuffer() throws IOException
 144    {
 145  2 if (outputWriter != null)
 146    {
 147  2 this.outputWriter.flush();
 148  2 this.servletOutputStream.outputStream.reset();
 149    }
 150    }
 151   
 152    /**
 153    * @see javax.servlet.ServletResponse#getOutputStream()
 154    */
 155  0 public ServletOutputStream getOutputStream() throws IOException
 156    {
 157  0 if (state && !outRequested)
 158    {
 159  0 log.debug("getOutputStream() returned");
 160   
 161    // ok, exporting in progress, discard old data and go on streaming
 162  0 this.servletOutputStream.reset();
 163  0 this.outputWriter.reset();
 164  0 this.outRequested = true;
 165  0 return ((HttpServletResponse) getResponse()).getOutputStream();
 166    }
 167  0 return this.servletOutputStream;
 168    }
 169   
 170    /**
 171    * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String)
 172    */
 173  7 public void addHeader(String name, String value)
 174    {
 175    // if the "magic parameter" is set, a table tag is going to call getOutputStream()
 176  7 if (TableTagParameters.PARAMETER_EXPORTING.equals(name))
 177    {
 178  0 log.debug("Magic header received, real response is now accessible");
 179  0 state = true;
 180    }
 181    else
 182    {
 183  7 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 184    {
 185  1 ((HttpServletResponse) getResponse()).addHeader(name, value);
 186    }
 187    }
 188    }
 189   
 190    /**
 191    * @see org.displaytag.filter.BufferedResponseWrapper#isOutRequested()
 192    */
 193  14 public boolean isOutRequested()
 194    {
 195  14 return this.outRequested;
 196    }
 197   
 198    /**
 199    * @see org.displaytag.filter.BufferedResponseWrapper#getContentAsString()
 200    */
 201  2 public String getContentAsString()
 202    {
 203  2 return this.outputWriter.toString() + this.servletOutputStream.toString();
 204    }
 205   
 206    /**
 207    * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long)
 208    */
 209  0 public void setDateHeader(String name, long date)
 210    {
 211  0 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 212    {
 213  0 ((HttpServletResponse) getResponse()).setDateHeader(name, date);
 214    }
 215    }
 216   
 217    /**
 218    * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long)
 219    */
 220  0 public void addDateHeader(String name, long date)
 221    {
 222  0 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 223    {
 224  0 ((HttpServletResponse) getResponse()).addDateHeader(name, date);
 225    }
 226    }
 227   
 228    /**
 229    * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String)
 230    */
 231  0 public void setHeader(String name, String value)
 232    {
 233  0 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 234    {
 235  0 ((HttpServletResponse) getResponse()).setHeader(name, value);
 236    }
 237    }
 238   
 239    /**
 240    * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int)
 241    */
 242  0 public void setIntHeader(String name, int value)
 243    {
 244  0 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 245    {
 246  0 ((HttpServletResponse) getResponse()).setIntHeader(name, value);
 247    }
 248    }
 249   
 250    /**
 251    * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int)
 252    */
 253  0 public void addIntHeader(String name, int value)
 254    {
 255  0 if (!ArrayUtils.contains(FILTERED_HEADERS, StringUtils.lowerCase(name)))
 256    {
 257  0 ((HttpServletResponse) getResponse()).addIntHeader(name, value);
 258    }
 259    }
 260   
 261    }