KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > filter > LogFilter


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/LogFilter.java,v 1.13 2004/08/05 14:43:34 dflorey Exp $
3  * $Revision: 1.13 $
4  * $Date: 2004/08/05 14:43:34 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav.filter;
25
26 import java.io.BufferedOutputStream JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.security.Principal JavaDoc;
31 import java.text.DateFormat JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34
35 import javax.servlet.Filter JavaDoc;
36 import javax.servlet.FilterChain JavaDoc;
37 import javax.servlet.FilterConfig JavaDoc;
38 import javax.servlet.ServletContext JavaDoc;
39 import javax.servlet.ServletException JavaDoc;
40 import javax.servlet.ServletRequest JavaDoc;
41 import javax.servlet.ServletResponse JavaDoc;
42 import javax.servlet.http.HttpServletRequest JavaDoc;
43 import javax.servlet.http.HttpServletResponse JavaDoc;
44
45 import org.apache.slide.webdav.logger.StatusHttpServletResponseWrapper;
46 import org.apache.slide.webdav.util.WebdavStatus;
47
48 /**
49  * A servlet filter for one-line-per-request logging. Log format and where to
50  * output can be configured in the deployment descriptors.
51  *
52  * @version $Revision: 1.13 $
53  */

54 public class LogFilter implements Filter JavaDoc {
55
56     FilterConfig JavaDoc config;
57     ServletContext JavaDoc context;
58     String JavaDoc logFormat = "%T, %t, %P, %m, %s \"%l\", %i, %p";
59     boolean outputToConsole = true;
60     boolean outputToServletLog = false;
61     boolean outputToFile = false;
62     String JavaDoc outputFilePath = null;
63     File JavaDoc outputFile = null;
64     BufferedOutputStream JavaDoc fout = null;
65     DateFormat JavaDoc df;
66     String JavaDoc dateTimePattern = "dd-MMM-yyyy HH:mm:ss";
67
68     // log elements
69

70     /**
71      * Interface implementation
72      *
73      * @param config a FilterConfig
74      *
75      * @throws ServletException
76      *
77      */

78     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
79         this.config = config;
80         this.context = config.getServletContext();
81         this.df = new SimpleDateFormat JavaDoc( dateTimePattern );
82
83         // get the init parms
84
String JavaDoc 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 JavaDoc( outputFilePath );
98             try {
99                 if( outputFile.canWrite() || outputFile.createNewFile() ) {
100                     fout = new BufferedOutputStream JavaDoc( new FileOutputStream JavaDoc(outputFilePath, true) );
101                     outputToFile = true;
102                 }
103             }
104             catch (IOException JavaDoc e) {}
105         }
106     }
107
108     /**
109      * Interface implementation
110      *
111      * @param req a ServletRequest
112      * @param resp a ServletResponse
113      * @param chain a FilterChain
114      *
115      * @throws IOException
116      * @throws ServletException
117      *
118      */

119     public void doFilter(ServletRequest JavaDoc request, ServletResponse JavaDoc response, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
120         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc)request;
121         StatusHttpServletResponseWrapper resp = new StatusHttpServletResponseWrapper((HttpServletResponse JavaDoc)response);
122         long start = System.currentTimeMillis();
123
124         // incomming
125
String JavaDoc thread = Thread.currentThread().getName();
126         String JavaDoc useragent = req.getHeader("User-Agent") != null
127             ? req.getHeader("User-Agent")
128             : "<user-agent-unknown>";
129         String JavaDoc datetime = df.format( new Date JavaDoc() );
130         String JavaDoc method = req.getMethod();
131         String JavaDoc uri = req.getRequestURI();
132         Principal JavaDoc p = req.getUserPrincipal();
133         String JavaDoc principal = (p != null ? p.getName() : null);
134         // with tomcat p.getName() may be null too
135
if (principal == null) principal = "unauthenticated";
136         String JavaDoc contentlength = req.getHeader( "Content-Length" );
137         if( contentlength == null )
138             contentlength = "-";
139
140         // next please!
141
chain.doFilter( req, resp );
142
143         // way back
144
int status = resp.getStatus();
145         String JavaDoc message = WebdavStatus.getStatusText(status);
146         String JavaDoc detail = resp.getStatusText();
147         if( detail == null || "".equals(detail) )
148             detail = message;
149         String JavaDoc path = (String JavaDoc)req.getAttribute("slide_uri"); // set by
150

151         long end = System.currentTimeMillis();
152         logLine( (end-start), status, thread, method, datetime, uri, path, contentlength, principal,
153                message, detail, useragent);
154     }
155
156     /**
157      * Log one line.
158      *
159      * @param req a XHttpServletRequestFacade
160      * @param resp a XHttpServletResponseFacade
161      * @param elapsed a long
162      *
163      * @throws IOException
164      *
165      */

166     private void logLine(long elapsed, int status,
167                         String JavaDoc thread, String JavaDoc method, String JavaDoc datetime, String JavaDoc uri,
168                          String JavaDoc path, String JavaDoc contentlength, String JavaDoc principal,
169                         String JavaDoc message, String JavaDoc detail, String JavaDoc useragent)
170         throws IOException JavaDoc
171     {
172
173         StringBuffer JavaDoc b = new StringBuffer JavaDoc( 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     /**
212      * Interface implementation.
213      *
214      */

215     public void destroy() {
216         try {
217             if( outputToFile )
218                 fout.close();
219         }
220         catch (IOException JavaDoc e) {}
221     }
222 }
223
224
Popular Tags