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 import java.security.Principal ; 31 import java.text.DateFormat ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 35 import javax.servlet.Filter ; 36 import javax.servlet.FilterChain ; 37 import javax.servlet.FilterConfig ; 38 import javax.servlet.ServletContext ; 39 import javax.servlet.ServletException ; 40 import javax.servlet.ServletRequest ; 41 import javax.servlet.ServletResponse ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 45 import org.apache.slide.webdav.logger.StatusHttpServletResponseWrapper; 46 import org.apache.slide.webdav.util.WebdavStatus; 47 48 54 public class LogFilter implements Filter { 55 56 FilterConfig config; 57 ServletContext context; 58 String logFormat = "%T, %t, %P, %m, %s \"%l\", %i, %p"; 59 boolean outputToConsole = true; 60 boolean outputToServletLog = false; 61 boolean outputToFile = false; 62 String outputFilePath = null; 63 File outputFile = null; 64 BufferedOutputStream fout = null; 65 DateFormat df; 66 String dateTimePattern = "dd-MMM-yyyy HH:mm:ss"; 67 68 70 78 public void init(FilterConfig config) throws ServletException { 79 this.config = config; 80 this.context = config.getServletContext(); 81 this.df = new SimpleDateFormat ( dateTimePattern ); 82 83 String p; 85 p = config.getInitParameter( "logFormat" ); 86 if( p != null && !"".equals(p) ) 87 logFormat = p; 88 p = config.getInitParameter( "outputToConsole" ); 89 if( "false".equalsIgnoreCase(p) ) 90 outputToConsole = false; 91 p = config.getInitParameter( "outputToServletLog" ); 92 if( "true".equalsIgnoreCase(p) ) 93 outputToServletLog = true; 94 p = config.getInitParameter( "outputToFile" ); 95 if( p != null && !"".equals(p) ) { 96 outputFilePath = p; 97 outputFile = new File ( outputFilePath ); 98 try { 99 if( outputFile.canWrite() || outputFile.createNewFile() ) { 100 fout = new BufferedOutputStream ( new FileOutputStream (outputFilePath, true) ); 101 outputToFile = true; 102 } 103 } 104 catch (IOException e) {} 105 } 106 } 107 108 119 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException , ServletException { 120 HttpServletRequest req = (HttpServletRequest )request; 121 StatusHttpServletResponseWrapper resp = new StatusHttpServletResponseWrapper((HttpServletResponse )response); 122 long start = System.currentTimeMillis(); 123 124 String thread = Thread.currentThread().getName(); 126 String useragent = req.getHeader("User-Agent") != null 127 ? req.getHeader("User-Agent") 128 : "<user-agent-unknown>"; 129 String datetime = df.format( new Date () ); 130 String method = req.getMethod(); 131 String uri = req.getRequestURI(); 132 Principal p = req.getUserPrincipal(); 133 String principal = (p != null ? p.getName() : null); 134 if (principal == null) principal = "unauthenticated"; 136 String contentlength = req.getHeader( "Content-Length" ); 137 if( contentlength == null ) 138 contentlength = "-"; 139 140 chain.doFilter( req, resp ); 142 143 int status = resp.getStatus(); 145 String message = WebdavStatus.getStatusText(status); 146 String detail = resp.getStatusText(); 147 if( detail == null || "".equals(detail) ) 148 detail = message; 149 String path = (String )req.getAttribute("slide_uri"); 151 long end = System.currentTimeMillis(); 152 logLine( (end-start), status, thread, method, datetime, uri, path, contentlength, principal, 153 message, detail, useragent); 154 } 155 156 166 private void logLine(long elapsed, int status, 167 String thread, String method, String datetime, String uri, 168 String path, String contentlength, String principal, 169 String message, String detail, String useragent) 170 throws IOException 171 { 172 173 StringBuffer b = new StringBuffer ( logFormat ); 174 int i; 175 i = b.toString().indexOf("%T"); 176 if( i >= 0 ) b.replace( i, i+2, thread ); 177 i = b.toString().indexOf("%t"); 178 if( i >= 0 ) b.replace( i, i+2, datetime ); 179 i = b.toString().indexOf("%P"); 180 if( i >= 0 ) b.replace( i, i+2, principal ); 181 i = b.toString().indexOf("%m"); 182 if( i >= 0 ) b.replace( i, i+2, method ); 183 i = b.toString().indexOf("%s"); 184 if( i >= 0 ) b.replace( i, i+2, String.valueOf(status) ); 185 i = b.toString().indexOf("%l"); 186 if( i >= 0 ) b.replace( i, i+2, message ); 187 i = b.toString().indexOf("%L"); 188 if( i >= 0 ) b.replace( i, i+2, detail ); 189 i = b.toString().indexOf("%i"); 190 if( i >= 0 ) b.replace( i, i+2, String.valueOf(elapsed)+" ms" ); 191 i = b.toString().indexOf("%p"); 192 if( i >= 0 ) b.replace( i, i+2, path ); 193 i = b.toString().indexOf("%u"); 194 if( i >= 0 ) b.replace( i, i+2, uri ); 195 i = b.toString().indexOf("%x"); 196 if( i >= 0 ) b.replace( i, i+2, contentlength ); 197 i = b.toString().indexOf("%A"); 198 if( i >= 0 ) b.replace( i, i+2, useragent ); 199 200 if( outputToConsole ) 201 System.out.println( b.toString() ); 202 if( outputToServletLog ) 203 context.log( b.toString() ); 204 if( outputToFile ) { 205 b.append("\n"); 206 fout.write( b.toString().getBytes("UTF-8") ); 207 fout.flush(); 208 } 209 } 210 211 215 public void destroy() { 216 try { 217 if( outputToFile ) 218 fout.close(); 219 } 220 catch (IOException e) {} 221 } 222 } 223 224 | Popular Tags |