Clover coverage report - Maven Clover report
Coverage timestamp: Wed Aug 15 2007 21:58:57 GMT+01:00
file stats: LOC: 154   Methods: 2
NCLOC: 90   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExportDelegate.java 60% 90.2% 50% 79.4%
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.IOException;
 15    import java.io.OutputStream;
 16    import java.io.PrintWriter;
 17    import java.util.Map;
 18   
 19    import javax.servlet.ServletRequest;
 20    import javax.servlet.http.HttpServletResponse;
 21   
 22    import org.apache.commons.lang.StringUtils;
 23    import org.apache.commons.logging.Log;
 24    import org.apache.commons.logging.LogFactory;
 25    import org.displaytag.tags.TableTag;
 26    import org.displaytag.tags.TableTagParameters;
 27   
 28   
 29    /**
 30    * Actually writes out the content of the wrapped response. Used by the j2ee filter and the Spring interceptor
 31    * implementations.
 32    * @author Fabrizio Giustina
 33    * @version $Revision: 1081 $ ($Author: fgiust $)
 34    */
 35    public final class ExportDelegate
 36    {
 37   
 38    /**
 39    * logger.
 40    */
 41    private static Log log = LogFactory.getLog(ExportDelegate.class);
 42   
 43    /**
 44    * Don�t instantiate.
 45    */
 46  0 private ExportDelegate()
 47    {
 48    // unused
 49    }
 50   
 51    /**
 52    * Actually writes exported data. Extracts content from the Map stored in request with the
 53    * <code>TableTag.FILTER_CONTENT_OVERRIDE_BODY</code> key.
 54    * @param wrapper BufferedResponseWrapper implementation
 55    * @param response HttpServletResponse
 56    * @param request ServletRequest
 57    * @throws IOException exception thrown by response writer/outputStream
 58    */
 59  14 protected static void writeExport(HttpServletResponse response, ServletRequest request,
 60    BufferedResponseWrapper wrapper) throws IOException
 61    {
 62   
 63  14 if (wrapper.isOutRequested())
 64    {
 65    // data already written
 66  0 log.debug("Filter operating in unbuffered mode. Everything done, exiting");
 67  0 return;
 68    }
 69   
 70    // if you reach this point the PARAMETER_EXPORTING has been found, but the special header has never been set in
 71    // response (this is the signal from table tag that it is going to write exported data)
 72  14 log.debug("Filter operating in buffered mode. ");
 73   
 74  14 Map bean = (Map) request.getAttribute(TableTag.FILTER_CONTENT_OVERRIDE_BODY);
 75   
 76  14 if (log.isDebugEnabled())
 77    {
 78  14 log.debug(bean);
 79    }
 80   
 81  14 Object pageContent = bean.get(TableTagParameters.BEAN_BODY);
 82   
 83  14 if (pageContent == null)
 84    {
 85  2 if (log.isDebugEnabled())
 86    {
 87  2 log.debug("Filter is enabled but exported content has not been found. Maybe an error occurred?");
 88    }
 89   
 90  2 response.setContentType(wrapper.getContentType());
 91  2 PrintWriter out = response.getWriter();
 92   
 93  2 out.write(wrapper.getContentAsString());
 94  2 out.flush();
 95  2 return;
 96    }
 97   
 98    // clear headers
 99  12 if (!response.isCommitted())
 100    {
 101  12 response.reset();
 102    }
 103   
 104  12 String filename = (String) bean.get(TableTagParameters.BEAN_FILENAME);
 105  12 String contentType = (String) bean.get(TableTagParameters.BEAN_CONTENTTYPE);
 106   
 107  12 if (StringUtils.isNotBlank(filename))
 108    {
 109  0 response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
 110    }
 111   
 112  12 String characterEncoding = wrapper.getCharacterEncoding();
 113  12 String wrappedContentType = wrapper.getContentType();
 114   
 115  12 if (wrappedContentType != null && wrappedContentType.indexOf("charset") > -1)
 116    {
 117    // charset is already specified (see #921811)
 118  12 characterEncoding = StringUtils.substringAfter(wrappedContentType, "charset=");
 119    }
 120   
 121  12 if (characterEncoding != null && contentType.indexOf("charset") == -1) //$NON-NLS-1$
 122    {
 123  12 contentType += "; charset=" + characterEncoding; //$NON-NLS-1$
 124    }
 125   
 126  12 response.setContentType(contentType);
 127   
 128  12 if (pageContent instanceof String)
 129    {
 130    // text content
 131  10 if (characterEncoding != null)
 132    {
 133  10 response.setContentLength(((String) pageContent).getBytes(characterEncoding).length);
 134    }
 135    else
 136    {
 137  0 response.setContentLength(((String) pageContent).getBytes().length);
 138    }
 139   
 140  10 PrintWriter out = response.getWriter();
 141  10 out.write((String) pageContent);
 142  10 out.flush();
 143    }
 144    else
 145    {
 146    // dealing with binary content
 147  2 byte[] content = (byte[]) pageContent;
 148  2 response.setContentLength(content.length);
 149  2 OutputStream out = response.getOutputStream();
 150  2 out.write(content);
 151  2 out.flush();
 152    }
 153    }
 154    }