Coverage Report - net.admin4j.ui.filters.PerformanceTimeStampingFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
PerformanceTimeStampingFilter
80%
29/36
85%
12/14
2.571
PerformanceTimeStampingFilter$ContentTrappingResponseWrapper
60%
12/20
25%
1/4
2.571
PerformanceTimeStampingFilter$ServletOutputStreamWrapper
33%
6/18
N/A
2.571
 
 1  
 /*
 2  
  * This software is licensed under the Apache License, Version 2.0
 3  
  * (the "License") agreement; you may not use this file except in compliance with
 4  
  * the License.  You may obtain a copy of the License at
 5  
  * 
 6  
  *      http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS,
 10  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 11  
  * See the License for the specific language governing permissions and
 12  
  * limitations under the License.
 13  
  */
 14  
 package net.admin4j.ui.filters;
 15  
 
 16  
 import java.io.ByteArrayOutputStream;
 17  
 import java.io.CharArrayWriter;
 18  
 import java.io.IOException;
 19  
 import java.io.OutputStream;
 20  
 import java.io.PrintWriter;
 21  
 import java.io.UnsupportedEncodingException;
 22  
 
 23  
 import javax.servlet.Filter;
 24  
 import javax.servlet.FilterChain;
 25  
 import javax.servlet.FilterConfig;
 26  
 import javax.servlet.ServletException;
 27  
 import javax.servlet.ServletOutputStream;
 28  
 import javax.servlet.ServletRequest;
 29  
 import javax.servlet.ServletResponse;
 30  
 import javax.servlet.http.HttpServletResponse;
 31  
 import javax.servlet.http.HttpServletResponseWrapper;
 32  
 
 33  
 import net.admin4j.config.Admin4JConfiguration;
 34  
 import net.admin4j.deps.commons.io.IOUtils;
 35  
 import net.admin4j.deps.commons.lang3.StringUtils;
 36  
 import net.admin4j.util.Admin4jRuntimeException;
 37  
 
 38  
 /**
 39  
  *
 40  
  * This filter will record the server-side time for this request as a comment at the bottom of any generated HTML.
 41  
  * 
 42  
  * @author D. Ashmore
 43  
  * @since 1.0
 44  
  */
 45  9
 public class PerformanceTimeStampingFilter extends BaseFilter implements Filter {
 46  
     
 47  
     /* (non-Javadoc)
 48  
      * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
 49  
      */
 50  
     public void init(FilterConfig arg0) throws ServletException {
 51  9
         new Admin4JConfiguration();
 52  
         
 53  9
     }
 54  
 
 55  
         public void doFilter(ServletRequest request, ServletResponse response,
 56  
                         FilterChain filterChain) throws IOException, ServletException {
 57  
 
 58  9
                 long beginTime = System.currentTimeMillis();
 59  
                 
 60  
                 try {
 61  
                         
 62  9
                         ContentTrappingResponseWrapper wrapper = new ContentTrappingResponseWrapper((HttpServletResponse)response);
 63  9
                         filterChain.doFilter(request, wrapper);
 64  
                         
 65  
                         try {
 66  9
                             String responseStr = wrapper.toString();
 67  
                             
 68  9
                             double durationInSeconds = (System.currentTimeMillis() - beginTime) / 1000.000;
 69  9
                             if (response.getContentType() != null && response.getContentType().toLowerCase().indexOf("html") >= 0 && this.isOuterExecution()) {
 70  
                                     responseStr = StringUtils.replaceOnce(responseStr, ">", "> <!-- server side time: " + durationInSeconds + " seconds -->");
 71  
                                     
 72  3
                                     PrintWriter out = response.getWriter();
 73  
                     try {
 74  3
                         CharArrayWriter caw = new CharArrayWriter();
 75  3
                         caw.write(responseStr);
 76  
                         
 77  3
                         response.setContentLength(caw.toString().length());
 78  3
                         out.write(caw.toString());
 79  
                     }
 80  
                     finally {
 81  
                         IOUtils.closeQuietly(out);
 82  3
                     }
 83  3
                             }
 84  
                             else {
 85  6
                                 response.getOutputStream().write(wrapper.toByteArray());
 86  6
                                 response.getOutputStream().flush();
 87  6
                                 response.getOutputStream().close();
 88  
                             }
 89  
                             
 90  
                         }
 91  0
                         catch (Throwable fubar) {
 92  0
                 logger.error("Error providing performance timestamp.", fubar);
 93  9
             }
 94  0
         } catch (ServletException se) {
 95  0
                 throw se;
 96  0
         } catch (Throwable ex) {
 97  0
             throw new ServletException(ex);
 98  9
         }
 99  9
         }
 100  
         
 101  
         private boolean isOuterExecution() {
 102  3
         StackTraceElement[] stack = Thread.currentThread().getStackTrace();
 103  3
         int nbrPresent = 0;
 104  
         
 105  84
         for (int i = 0; i < stack.length; i++) {
 106  81
             if (stack[i].getClassName().equals(PerformanceTimeStampingFilter.class.getName()) && stack[i].getMethodName().equals("doFilter")) {
 107  3
                 nbrPresent++;
 108  
             }
 109  
         }
 110  
         
 111  3
         return nbrPresent == 1;
 112  
     }
 113  
         
 114  0
         public void destroy() {        }        
 115  
         
 116  
         private static class ContentTrappingResponseWrapper extends HttpServletResponseWrapper {
 117  9
                 private ByteArrayOutputStream stream = null;
 118  9
                 protected PrintWriter writer = null;
 119  9
                 private HttpServletResponse origResponse = null;
 120  
                 
 121  
                 public ContentTrappingResponseWrapper(HttpServletResponse response) {
 122  9
                         super(response);
 123  9
                 origResponse = response;
 124  9
                 stream = new ByteArrayOutputStream(3000000);
 125  9
             }
 126  
                 
 127  
                 public byte[] toByteArray() {
 128  6
                     return stream.toByteArray();
 129  
                 }
 130  
 
 131  
                 @Override
 132  
                 public ServletOutputStream getOutputStream() throws IOException {
 133  33
                         return new ServletOutputStreamWrapper(stream);
 134  
                 }
 135  
 
 136  
                 @Override
 137  
                 public PrintWriter getWriter() throws IOException {
 138  0
                         if (this.writer == null) {
 139  0
                                 this.writer = new PrintWriter(stream);
 140  
                         }
 141  0
                         return this.writer;
 142  
                 }
 143  
                 
 144  
                 @Override
 145  
                 public String toString(){
 146  
                     try{
 147  9
                             if (this.writer != null) {
 148  0
                                     this.writer.flush();
 149  
                             }
 150  9
                             String responseStr = stream.toString(this.origResponse.getCharacterEncoding());
 151  9
                             return responseStr;
 152  0
                     }catch(UnsupportedEncodingException e){
 153  
                             // an attempt to set an invalid character encoding would have caused this exception before
 154  0
                             throw new RuntimeException("PageTokenResponseWrapper response accepted invalid character encoding " + getCharacterEncoding());
 155  
                     }
 156  
             }
 157  
 
 158  
                 @Override
 159  
                 public void flushBuffer() throws IOException {
 160  0
                         stream.flush();
 161  0
                 }
 162  
                 
 163  
                 
 164  
 
 165  
 
 166  
         }
 167  
         
 168  9
         private static class ServletOutputStreamWrapper extends ServletOutputStream {
 169  
                 private OutputStream outputStream;
 170  
 
 171  33
                 public ServletOutputStreamWrapper(OutputStream outputStream){
 172  33
                         this.outputStream = outputStream;
 173  33
                 }
 174  
 
 175  
                 public void write(int b){
 176  
                         try {
 177  0
                                 this.outputStream.write( b );
 178  0
                         } catch (IOException e) {
 179  0
                                 throw new Admin4jRuntimeException(e);
 180  0
                         }
 181  0
                 }
 182  
 
 183  
                 public void write(byte[] bytes) throws IOException{
 184  
                         try {
 185  6
                                 this.outputStream.write( bytes );
 186  0
                         } catch (IOException e) {
 187  0
                                 throw new Admin4jRuntimeException(e);
 188  6
                         }
 189  6
                 }
 190  
 
 191  
                 public void write(byte[] bytes, int off, int len){
 192  
                         try {
 193  0
                                 this.outputStream.write( bytes, off, len );
 194  0
                         } catch (IOException e) {
 195  0
                                 throw new Admin4jRuntimeException(e);
 196  0
                         }
 197  0
                 }
 198  
 
 199  
     }
 200  
 }
 201