Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
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  130 public ServletWebResponse(HttpServletResponse response)
 51    {
 52  130 this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch"));
 53    }
 54   
 55    /**
 56    * Alternate constructor used by some tests.
 57    */
 58  132 ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch)
 59    {
 60  132 Defense.notNull(response, "response");
 61  132 Defense.notNull(log, "log");
 62   
 63  132 _servletResponse = response;
 64  132 _log = log;
 65  132 _tomcatPatch = tomcatPatch;
 66    }
 67   
 68  3 public OutputStream getOutputStream(ContentType contentType)
 69    {
 70  3 Defense.notNull(contentType, "contentType");
 71   
 72  3 _servletResponse.setContentType(contentType.getMimeType());
 73   
 74  3 try
 75    {
 76  3 return _servletResponse.getOutputStream();
 77    }
 78    catch (IOException ex)
 79    {
 80  1 throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
 81    null, ex);
 82    }
 83    }
 84   
 85  138 public PrintWriter getPrintWriter(ContentType contentType) throws IOException
 86    {
 87  138 Defense.notNull(contentType, "contentType");
 88   
 89  138 if (_needsReset)
 90  11 reset();
 91   
 92  138 _needsReset = true;
 93   
 94  138 if (_printWriterContentType == null || ! _tomcatPatch)
 95    {
 96  136 _servletResponse.setContentType(contentType.toString());
 97  136 _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  2 if (!_printWriterContentType.equals(contentType))
 105  1 _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType));
 106    }
 107   
 108  138 try
 109    {
 110  138 return _servletResponse.getWriter();
 111    }
 112    catch (IOException ex)
 113    {
 114  1 throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
 115    null, ex);
 116    }
 117    }
 118   
 119  109 public String encodeURL(String url)
 120    {
 121  109 return _servletResponse.encodeURL(url);
 122    }
 123   
 124  12 public void reset()
 125    {
 126  12 try
 127    {
 128  12 _servletResponse.reset();
 129    }
 130    catch (IllegalStateException ex)
 131    {
 132  0 _log.error(WebMessages.resetFailed(ex), ex);
 133    }
 134    }
 135   
 136  1 public void setContentLength(int length)
 137    {
 138  1 _servletResponse.setContentLength(length);
 139    }
 140   
 141  116 public String getNamespace()
 142    {
 143  116 return "";
 144    }
 145   
 146  3 public void setDateHeader(String name, long date)
 147    {
 148  3 _servletResponse.setDateHeader(name, date);
 149    }
 150   
 151  0 public void setStatus(int status)
 152    {
 153  0 _servletResponse.setStatus(status);
 154    }
 155   
 156  1 public void setHeader(String name, String value)
 157    {
 158  1 _servletResponse.setHeader(name, value);
 159    }
 160   
 161  1 public void setIntHeader(String name, int value)
 162    {
 163  1 _servletResponse.setIntHeader(name, value);
 164    }
 165   
 166  1 public void sendError(int statusCode, String message) throws IOException
 167    {
 168  1 _servletResponse.sendError(statusCode, message);
 169    }
 170   
 171    }