pnever 2002/12/18 08:04:37
Added: src/webdav/server/org/apache/slide/webdav/filter
LogFilter.java XmlLogFilter.java
Log:
Initial. Servlet filters replacing request logging removed from the server.
Revision Changes Path
1.1
jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/LogFilter.java
Index: LogFilter.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/LogFilter.java,v
1.1 2002/12/18 16:04:37 pnever Exp $
* $Revision: 1.1 $
* $Date: 2002/12/18 16:04:37 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Slide", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.webdav.filter;
import java.io.File;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.security.Principal;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.slide.webdav.logger.XHttpServletRequestFacade;
import org.apache.slide.webdav.logger.XHttpServletResponseFacade;
import org.apache.util.WebdavStatus;
/**
* A servlet filter for one-line-per-request logging. Log format and where to
* output can be configured in the deployment descriptors.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Nevermann</a>
* @version $Revision: 1.1 $
*/
public class LogFilter implements Filter {
FilterConfig config;
ServletContext context;
String logFormat = "%t, %p, %m, %s \"%l\", %i, %u";
boolean outputToConsole = true;
boolean outputToServletLog = false;
boolean outputToFile = false;
String outputFilePath = null;
File outputFile = null;
BufferedOutputStream fout = null;
/**
* Interface implementation
*
* @param config a FilterConfig
*
* @throws ServletException
*
*/
public void init(FilterConfig config) throws ServletException {
this.config = config;
this.context = config.getServletContext();
// get the init parms
String p;
p = config.getInitParameter( "logFormat" );
if( p != null && !"".equals(p) )
logFormat = p;
p = config.getInitParameter( "outputToConsole" );
if( "false".equalsIgnoreCase(p) )
outputToConsole = false;
p = config.getInitParameter( "outputToServletLog" );
if( "true".equalsIgnoreCase(p) )
outputToServletLog = true;
p = config.getInitParameter( "outputToFile" );
if( p != null && !"".equals(p) ) {
outputFilePath = p;
outputFile = new File( outputFilePath );
try {
if( outputFile.canWrite() || outputFile.createNewFile() ) {
fout = new BufferedOutputStream( new
FileOutputStream(outputFilePath, true) );
outputToFile = true;
}
}
catch (IOException e) {}
}
}
/**
* Interface implementation
*
* @param req a ServletRequest
* @param resp a ServletResponse
* @param chain a FilterChain
*
* @throws IOException
* @throws ServletException
*
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain
chain) throws IOException, ServletException {
XHttpServletRequestFacade reqFac = new
XHttpServletRequestFacade((HttpServletRequest)req);
XHttpServletResponseFacade respFac = new
XHttpServletResponseFacade((HttpServletResponse)resp);
long b = System.currentTimeMillis();
chain.doFilter( reqFac, respFac );
long a = System.currentTimeMillis();
logLine( reqFac, respFac, (a-b) );
}
/**
* Log one line.
*
* @param req a XHttpServletRequestFacade
* @param resp a XHttpServletResponseFacade
* @param elapsed a long
*
* @throws IOException
*
*/
private void logLine( XHttpServletRequestFacade req, XHttpServletResponseFacade
resp, long elapsed ) throws IOException {
String thread = Thread.currentThread().getName();
String method = req.getMethod();
String uri = req.getRequestURI();
String path = req.getServletPath();
if( "".equals(path) )
path = "/";
int status = resp.getStatus();
String message = WebdavStatus.getStatusText(status);
Principal p = req.getUserPrincipal();
String principal = (p != null ? p.getName() : "");
String detail = resp.getStatusText();
if( detail == null || "".equals(detail) )
detail = message;
StringBuffer b = new StringBuffer( logFormat );
int i;
i = b.toString().indexOf("%t");
if( i >= 0 ) b.replace( i, i+2, thread );
i = b.toString().indexOf("%p");
if( i >= 0 ) b.replace( i, i+2, principal );
i = b.toString().indexOf("%m");
if( i >= 0 ) b.replace( i, i+2, method );
i = b.toString().indexOf("%s");
if( i >= 0 ) b.replace( i, i+2, String.valueOf(status) );
i = b.toString().indexOf("%l");
if( i >= 0 ) b.replace( i, i+2, message );
i = b.toString().indexOf("%k");
if( i >= 0 ) b.replace( i, i+2, detail );
i = b.toString().indexOf("%i");
if( i >= 0 ) b.replace( i, i+2, String.valueOf(elapsed)+" ms" );
i = b.toString().indexOf("%u");
if( i >= 0 ) b.replace( i, i+2, path );
i = b.toString().indexOf("%v");
if( i >= 0 ) b.replace( i, i+2, uri );
if( outputToConsole )
System.out.println( b.toString() );
if( outputToServletLog )
context.log( b.toString() );
if( outputToFile ) {
b.append("\n");
fout.write( b.toString().getBytes("UTF-8") );
fout.flush();
}
}
/**
* Interface implementation.
*
*/
public void destroy() {
try {
if( outputToFile )
fout.close();
}
catch (IOException e) {}
}
}
1.1
jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/XmlLogFilter.java
Index: XmlLogFilter.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/filter/XmlLogFilter.java,v
1.1 2002/12/18 16:04:37 pnever Exp $
* $Revision: 1.1 $
* $Date: 2002/12/18 16:04:37 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Slide", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
* [Additional notices, if required by prior licensing conditions]
*
*/
package org.apache.slide.webdav.filter;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.slide.webdav.logger.XHttpServletRequestFacade;
import org.apache.slide.webdav.logger.XHttpServletResponseFacade;
import org.apache.slide.webdav.logger.XMLTestCaseGenerator;
/**
* A servlet filter for detailed XML logging.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Peter Nevermann</a>
* @version $Revision: 1.1 $
*/
public class XmlLogFilter implements Filter {
FilterConfig config;
boolean outputToFile = false;
String outputFilePath = null;
File outputFile = null;
BufferedOutputStream fout = null;
/**
* Interface implementation
*
* @param config a FilterConfig
*
* @throws ServletException
*
*/
public void init(FilterConfig config) throws ServletException {
this.config = config;
// get the init parms
String p;
p = config.getInitParameter( "outputToFile" );
if( p != null && !"".equals(p) ) {
outputFilePath = p;
outputFile = new File( outputFilePath );
try {
if( outputFile.canWrite() || outputFile.createNewFile() ) {
fout = new BufferedOutputStream( new
FileOutputStream(outputFilePath, true) );
outputToFile = true;
}
}
catch (IOException e) {}
}
}
/**
* Interface implementation
*
* @param req a ServletRequest
* @param resp a ServletResponse
* @param chain a FilterChain
*
* @throws IOException
* @throws ServletException
*
*/
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain
chain) throws IOException, ServletException {
XHttpServletRequestFacade reqFac = new
XHttpServletRequestFacade((HttpServletRequest)req);
XHttpServletResponseFacade respFac = new
XHttpServletResponseFacade((HttpServletResponse)resp);
chain.doFilter( reqFac, respFac );
logXML( reqFac, respFac );
}
/**
* Log one line.
*
* @param req a XHttpServletRequestFacade
* @param resp a XHttpServletResponseFacade
*
* @throws IOException
*
*/
private void logXML( XHttpServletRequestFacade req, XHttpServletResponseFacade
resp ) throws IOException {
String thread = Thread.currentThread().getName();
XMLTestCaseGenerator xmlGen = new XMLTestCaseGenerator( req, resp );
xmlGen.setThreadName( thread );
if( outputToFile ) {
fout.write( xmlGen.toString().getBytes("UTF-8") );
fout.flush();
}
}
/**
* Interface implementation.
*
*/
public void destroy() {
try {
if( outputToFile )
fout.close();
}
catch (IOException e) {}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>