1 23 24 package org.apache.slide.webdav.filter; 25 26 import java.io.BufferedOutputStream ; 27 import java.io.File ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 31 import javax.servlet.Filter ; 32 import javax.servlet.FilterChain ; 33 import javax.servlet.FilterConfig ; 34 import javax.servlet.ServletException ; 35 import javax.servlet.ServletRequest ; 36 import javax.servlet.ServletResponse ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import org.apache.slide.webdav.logger.XHttpServletRequestFacade; 41 import org.apache.slide.webdav.logger.XHttpServletResponseFacade; 42 import org.apache.slide.webdav.logger.XMLTestCaseGenerator; 43 44 49 public class XmlLogFilter implements Filter { 50 51 FilterConfig config; 52 boolean outputToFile = false; 53 String outputFilePath = null; 54 File outputFile = null; 55 BufferedOutputStream fout = null; 56 57 65 public void init(FilterConfig config) throws ServletException { 66 this.config = config; 67 String p; 69 p = config.getInitParameter( "outputToFile" ); 70 if( p != null && !"".equals(p) ) { 71 outputFilePath = p; 72 outputFile = new File ( outputFilePath ); 73 try { 74 if( outputFile.canWrite() || outputFile.createNewFile() ) { 75 fout = new BufferedOutputStream ( new FileOutputStream (outputFilePath, true) ); 76 outputToFile = true; 77 } 78 } 79 catch (IOException e) {} 80 } 81 } 82 83 94 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException , ServletException { 95 XHttpServletRequestFacade reqFac = new XHttpServletRequestFacade((HttpServletRequest )req); 96 XHttpServletResponseFacade respFac = new XHttpServletResponseFacade((HttpServletResponse )resp); 97 chain.doFilter( reqFac, respFac ); 98 logXML( reqFac, respFac ); 99 } 100 101 110 private void logXML( XHttpServletRequestFacade req, XHttpServletResponseFacade resp ) throws IOException { 111 String thread = Thread.currentThread().getName(); 112 XMLTestCaseGenerator xmlGen = new XMLTestCaseGenerator( req, resp ); 113 xmlGen.setThreadName( thread ); 114 115 if( outputToFile ) { 116 fout.write( xmlGen.toString().getBytes("UTF-8") ); 117 fout.flush(); 118 } 119 } 120 121 125 public void destroy() { 126 try { 127 if( outputToFile ) 128 fout.close(); 129 } 130 catch (IOException e) {} 131 } 132 } 133 134 | Popular Tags |