Clover coverage report - Code Coverage for tapestry release 4.0.1
Coverage timestamp: Fri Mar 31 2006 09:12:14 EST
file stats: LOC: 171   Methods: 13
NCLOC: 113   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ServletWebResponse.java 100% 94.1% 92.3% 94.3%
coverage coverage
 1    // Copyright 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.web;
 16   
 17    import java.io.IOException;
 18    import java.io.OutputStream;
 19    import java.io.PrintWriter;
 20   
 21    import javax.servlet.http.HttpServletResponse;
 22   
 23    import org.apache.commons.logging.Log;
 24    import org.apache.commons.logging.LogFactory;
 25    import org.apache.hivemind.ApplicationRuntimeException;
 26    import org.apache.hivemind.util.Defense;
 27    import org.apache.tapestry.util.ContentType;
 28   
 29    /**
 30    * Adapts {@link javax.servlet.http.HttpServletResponse} as
 31    * {@link org.apache.tapestry.web.WebResponse}.
 32    *
 33    * @author Howard M. Lewis Ship
 34    * @since 4.0
 35    */
 36    public class ServletWebResponse implements WebResponse
 37    {
 38    private static final Log DEFAULT_LOG = LogFactory.getLog(ServletWebResponse.class);
 39   
 40    private final Log _log;
 41   
 42    private final boolean _tomcatPatch;
 43   
 44    private final HttpServletResponse _servletResponse;
 45   
 46    private boolean _needsReset;
 47   
 48    private ContentType _printWriterContentType;
 49   
 50  260 public ServletWebResponse(HttpServletResponse response)
 51    {
 52  260 this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch"));
 53    }
 54   
 55    /**
 56    * Alternate constructor used by some tests.
 57    */
 58  264 ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch)
 59    {
 60  264 Defense.notNull(response, "response");
 61  264 Defense.notNull(log, "log");
 62   
 63  264 _servletResponse = response;
 64  264 _log = log;
 65  264 _tomcatPatch = tomcatPatch;
 66    }
 67   
 68  6 public OutputStream getOutputStream(ContentType contentType)
 69    {
 70  6 Defense.notNull(contentType, "contentType");
 71   
 72  6 _servletResponse.setContentType(contentType.getMimeType());
 73   
 74  6 try
 75    {
 76  6 return _servletResponse.getOutputStream();
 77    }
 78    catch (IOException ex)
 79    {
 80  2 throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
 81    null, ex);
 82    }
 83    }
 84   
 85  276 public PrintWriter getPrintWriter(ContentType contentType) throws IOException
 86    {
 87  276 Defense.notNull(contentType, "contentType");
 88   
 89  276 if (_needsReset)
 90  22 reset();
 91   
 92  276 _needsReset = true;
 93   
 94  276 if (_printWriterContentType == null || ! _tomcatPatch)
 95    {
 96  272 _servletResponse.setContentType(contentType.toString());
 97  272 _printWriterContentType = contentType;
 98    }
 99    else
 100    {
 101    // This is a workaround for a tomcat bug; it takes effect when a page is reset so that
 102    // the exception page (typically) can be rendered. See TAPESTRY-607 for details.
 103   
 104  4 if (!_printWriterContentType.equals(contentType))
 105  2 _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType));
 106    }
 107   
 108  276 try
 109    {
 110  276 return _servletResponse.getWriter();
 111    }
 112    catch (IOException ex)
 113    {
 114  2 throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
 115    null, ex);
 116    }
 117    }
 118   
 119  372 public String encodeURL(String url)
 120    {
 121  372 return _servletResponse.encodeURL(url);
 122    }
 123   
 124  24 public void reset()
 125    {
 126  24 try
 127    {
 128  24 _servletResponse.reset();
 129    }
 130    catch (IllegalStateException ex)
 131    {
 132  0 _log.error(WebMessages.resetFailed(ex), ex);
 133    }
 134    }
 135   
 136  2 public void setContentLength(int length)
 137    {
 138  2 _servletResponse.setContentLength(length);
 139    }
 140   
 141  232 public String getNamespace()
 142    {
 143  232 return "";
 144    }
 145   
 146  6 public void setDateHeader(String name, long date)
 147    {
 148  6 _servletResponse.setDateHeader(name, date);
 149    }
 150   
 151  0 public void setStatus(int status)
 152    {
 153  0 _servletResponse.setStatus(status);
 154    }
 155   
 156  2 public void setHeader(String name, String value)
 157    {
 158  2 _servletResponse.setHeader(name, value);
 159    }
 160   
 161  2 public void setIntHeader(String name, int value)
 162    {
 163  2 _servletResponse.setIntHeader(name, value);
 164    }
 165   
 166  2 public void sendError(int statusCode, String message) throws IOException
 167    {
 168  2 _servletResponse.sendError(statusCode, message);
 169    }
 170   
 171    }