KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/XmlLogFilter.java,v 1.4 2004/08/05 14:43:34 dflorey Exp $
3  * $Revision: 1.4 $
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
31 import javax.servlet.Filter JavaDoc;
32 import javax.servlet.FilterChain JavaDoc;
33 import javax.servlet.FilterConfig JavaDoc;
34 import javax.servlet.ServletException JavaDoc;
35 import javax.servlet.ServletRequest JavaDoc;
36 import javax.servlet.ServletResponse JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
45  * A servlet filter for detailed XML logging.
46  *
47  * @version $Revision: 1.4 $
48  */

49 public class XmlLogFilter implements Filter JavaDoc {
50     
51     FilterConfig JavaDoc config;
52     boolean outputToFile = false;
53     String JavaDoc outputFilePath = null;
54     File JavaDoc outputFile = null;
55     BufferedOutputStream JavaDoc fout = null;
56     
57     /**
58      * Interface implementation
59      *
60      * @param config a FilterConfig
61      *
62      * @throws ServletException
63      *
64      */

65     public void init(FilterConfig JavaDoc config) throws ServletException JavaDoc {
66         this.config = config;
67         // get the init parms
68
String JavaDoc p;
69         p = config.getInitParameter( "outputToFile" );
70         if( p != null && !"".equals(p) ) {
71             outputFilePath = p;
72             outputFile = new File JavaDoc( outputFilePath );
73             try {
74                 if( outputFile.canWrite() || outputFile.createNewFile() ) {
75                     fout = new BufferedOutputStream JavaDoc( new FileOutputStream JavaDoc(outputFilePath, true) );
76                     outputToFile = true;
77                 }
78             }
79             catch (IOException JavaDoc e) {}
80         }
81     }
82     
83     /**
84      * Interface implementation
85      *
86      * @param req a ServletRequest
87      * @param resp a ServletResponse
88      * @param chain a FilterChain
89      *
90      * @throws IOException
91      * @throws ServletException
92      *
93      */

94     public void doFilter(ServletRequest JavaDoc req, ServletResponse JavaDoc resp, FilterChain JavaDoc chain) throws IOException JavaDoc, ServletException JavaDoc {
95         XHttpServletRequestFacade reqFac = new XHttpServletRequestFacade((HttpServletRequest JavaDoc)req);
96         XHttpServletResponseFacade respFac = new XHttpServletResponseFacade((HttpServletResponse JavaDoc)resp);
97         chain.doFilter( reqFac, respFac );
98         logXML( reqFac, respFac );
99     }
100     
101     /**
102      * Log one line.
103      *
104      * @param req a XHttpServletRequestFacade
105      * @param resp a XHttpServletResponseFacade
106      *
107      * @throws IOException
108      *
109      */

110     private void logXML( XHttpServletRequestFacade req, XHttpServletResponseFacade resp ) throws IOException JavaDoc {
111         String JavaDoc 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     /**
122      * Interface implementation.
123      *
124      */

125     public void destroy() {
126         try {
127             if( outputToFile )
128                 fout.close();
129         }
130         catch (IOException JavaDoc e) {}
131     }
132 }
133
134
Popular Tags