Coverage Report - org.apache.tapestry.web.ServletWebResponse
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletWebResponse
0%
0/46
0%
0/8
1.846
 
 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 org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.apache.hivemind.ApplicationRuntimeException;
 20  
 import org.apache.hivemind.util.Defense;
 21  
 import org.apache.tapestry.util.ContentType;
 22  
 
 23  
 import javax.servlet.http.HttpServletResponse;
 24  
 import java.io.IOException;
 25  
 import java.io.OutputStream;
 26  
 import java.io.PrintWriter;
 27  
 
 28  
 /**
 29  
  * Adapts {@link javax.servlet.http.HttpServletResponse} as
 30  
  * {@link org.apache.tapestry.web.WebResponse}.
 31  
  * 
 32  
  * @author Howard M. Lewis Ship
 33  
  * @since 4.0
 34  
  */
 35  
 public class ServletWebResponse implements WebResponse
 36  
 {
 37  0
     private static final Log DEFAULT_LOG = LogFactory.getLog(ServletWebResponse.class);
 38  
 
 39  
     private final Log _log;
 40  
 
 41  
     private final boolean _tomcatPatch;
 42  
 
 43  
     private final HttpServletResponse _servletResponse;
 44  
 
 45  
     private boolean _needsReset;
 46  
 
 47  
     private ContentType _printWriterContentType;
 48  
 
 49  
     public ServletWebResponse(HttpServletResponse response)
 50  
     {
 51  0
         this(response, DEFAULT_LOG, Boolean.getBoolean("org.apache.tapestry.607-patch"));
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Alternate constructor used by some tests.
 56  
      *
 57  
      * @param response
 58  
      *          The wrapped response.
 59  
      * @param log
 60  
      *          Logger.
 61  
      * @param tomcatPatch
 62  
      *          Whether or not to apply tomcat workaround.
 63  
      */
 64  
     ServletWebResponse(HttpServletResponse response, Log log, boolean tomcatPatch)
 65  0
     {
 66  0
         Defense.notNull(response, "response");
 67  0
         Defense.notNull(log, "log");
 68  
 
 69  0
         _servletResponse = response;
 70  0
         _log = log;
 71  0
         _tomcatPatch = tomcatPatch;
 72  0
     }
 73  
 
 74  
     public OutputStream getOutputStream(ContentType contentType)
 75  
     {
 76  0
         Defense.notNull(contentType, "contentType");
 77  
 
 78  0
         _servletResponse.setContentType(contentType.getMimeType());
 79  
 
 80  
         try
 81  
         {
 82  0
             return _servletResponse.getOutputStream();
 83  
         }
 84  0
         catch (IOException ex)
 85  
         {
 86  0
             throw new ApplicationRuntimeException(WebMessages.streamOpenError(contentType, ex),
 87  
                     null, ex);
 88  
         }
 89  
     }
 90  
 
 91  
     public PrintWriter getPrintWriter(ContentType contentType) throws IOException
 92  
     {
 93  0
         Defense.notNull(contentType, "contentType");
 94  
 
 95  0
         if (_needsReset)
 96  0
             reset();
 97  
 
 98  0
         _needsReset = true;
 99  
         
 100  0
         if (_printWriterContentType == null || ! _tomcatPatch)
 101  
         {
 102  0
             _servletResponse.setContentType(contentType.toString());
 103  0
             _printWriterContentType = contentType;
 104  
         }
 105  
         else
 106  
         {
 107  
             // This is a workaround for a tomcat bug; it takes effect when a page is reset so that
 108  
             // the exception page (typically) can be rendered. See TAPESTRY-607 for details.
 109  
 
 110  0
             if (!_printWriterContentType.equals(contentType))
 111  0
                 _log.warn(WebMessages.contentTypeUnchanged(_printWriterContentType, contentType));
 112  
         }
 113  
 
 114  
         try
 115  
         {
 116  0
             return _servletResponse.getWriter();
 117  
         }
 118  0
         catch (IOException ex)
 119  
         {
 120  0
             throw new ApplicationRuntimeException(WebMessages.writerOpenError(contentType, ex),
 121  
                     null, ex);
 122  
         }
 123  
     }
 124  
 
 125  
     public String encodeURL(String url)
 126  
     {
 127  0
         return _servletResponse.encodeURL(url);
 128  
     }
 129  
 
 130  
     public void reset()
 131  
     {
 132  
         try
 133  
         {
 134  0
             _servletResponse.reset();
 135  
         }
 136  0
         catch (IllegalStateException ex)
 137  
         {
 138  0
             _log.error(WebMessages.resetFailed(ex), ex);
 139  0
         }
 140  0
     }
 141  
 
 142  
     public void setContentLength(int length)
 143  
     {
 144  0
         _servletResponse.setContentLength(length);
 145  0
     }
 146  
 
 147  
     public String getNamespace()
 148  
     {
 149  0
         return "";
 150  
     }
 151  
 
 152  
     public void setDateHeader(String name, long date)
 153  
     {
 154  0
         _servletResponse.setDateHeader(name, date);
 155  0
     }
 156  
 
 157  
     public void setStatus(int status)
 158  
     {
 159  0
         _servletResponse.setStatus(status);
 160  0
     }
 161  
 
 162  
     public void setHeader(String name, String value)
 163  
     {
 164  0
         _servletResponse.setHeader(name, value);
 165  0
     }
 166  
 
 167  
     public void setIntHeader(String name, int value)
 168  
     {
 169  0
         _servletResponse.setIntHeader(name, value);
 170  0
     }
 171  
 
 172  
     public void sendError(int statusCode, String message) throws IOException
 173  
     {
 174  0
         _servletResponse.sendError(statusCode, message);
 175  0
     }
 176  
 
 177  
 }