package org.w3c.jigsaw.filters;
import java.io.*;
import java.net.*;
import java.util.*;
import org.w3c.tools.resources.*;
import org.w3c.tools.resources.ProtocolException;
import org.w3c.www.http.*;
import org.w3c.jigsaw.http.*;
public class LogFilter extends ResourceFilter {
public static final
String DONT_LOG = "org.w3c.jigsaw.filters.LogFilter.nolog";
protected static int ATTR_REQUEST_HEADERS = -1;
protected static int ATTR_REPLY_HEADERS = -1;
protected static int ATTR_LOGFILE = -1;
static {
Class c = null;
Attribute a = null;
try {
c = Class.forName("org.w3c.jigsaw.filters.LogFilter");
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
a = new StringArrayAttribute("request-headers"
, null
, Attribute.EDITABLE);
ATTR_REQUEST_HEADERS = AttributeRegistry.registerAttribute(c, a);
a = new StringArrayAttribute("reply-headers"
, null
, Attribute.EDITABLE);
ATTR_REPLY_HEADERS = AttributeRegistry.registerAttribute(c, a);
a = new FileAttribute("logfile"
, null
, Attribute.EDITABLE);
ATTR_LOGFILE = AttributeRegistry.registerAttribute(c, a);
}
protected HeaderDescription reqheaders[] = null;
protected HeaderDescription repheaders[] = null;
protected RandomAccessFile log = null;
@param kind@param headers@return
protected HeaderDescription[] compileHeaders(HttpMessage kind
, String headers[]) {
Enumeration e = kind.enumerateHeaderDescriptions(true);
HeaderDescription r[] = new HeaderDescription[headers.length];
while ( e.hasMoreElements() ) {
HeaderDescription d = (HeaderDescription) e.nextElement();
if ( d == null )
continue;
for (int i = 0 ; i < headers.length ; i++) {
if (headers[i].equals(d.getName())) {
r[i] = d;
break;
}
}
}
return r;
}
@param record
protected synchronized void writelog(String record) {
try {
if ( log != null )
log.writeBytes(record);
} catch (IOException ex) {
FramedResource target = (FramedResource) getTargetResource();
if (target != null) {
String msg = ("IO error while writing to log file \""+
ex.getMessage() + "\".");
target.getServer().errlog(this, msg);
}
}
}
protected synchronized void openLog() {
if ( log != null ) {
try {
log.close();
} catch (IOException ex) {
}
log = null;
}
File logfile = getLogfile();
try {
if (logfile != null) {
log = new RandomAccessFile(logfile, "rw");
log.seek(log.length());
}
} catch (Exception ex) {
}
if ( log == null ) {
FramedResource target = (FramedResource) getTargetResource();
if (target != null) {
String msg = ("unable to open log file \""+
logfile
+ "\".");
target.getServer().errlog(this, msg);
}
}
}
@return
public File getLogfile() {
return (File) getValue(ATTR_LOGFILE, null);
}
@return
public String[] getRequestHeaders() {
return (String[]) getValue(ATTR_REQUEST_HEADERS, null);
}
@return
public String[] getReplyHeaders() {
return (String[]) getValue(ATTR_REPLY_HEADERS, null);
}
public void setValue(int idx, Object value) {
super.setValue(idx, value);
if ( idx == ATTR_REQUEST_HEADERS ) {
synchronized(this) {
reqheaders = compileHeaders(new HttpRequestMessage()
, (String[]) value);
}
} else if (idx == ATTR_REPLY_HEADERS ) {
synchronized(this) {
repheaders = compileHeaders(new HttpReplyMessage()
, (String[]) value);
}
} else if (idx == ATTR_LOGFILE ) {
openLog();
}
}
@param request@param reply
protected void log(Request request, Reply reply) {
StringBuffer sb = new StringBuffer();
sb.append("url=");
sb.append(request.getURL().toString());
sb.append('\n');
if ( reqheaders != null ) {
for (int i = 0 ; i < reqheaders.length ; i++) {
HeaderDescription d = reqheaders[i];
if ( d == null )
continue;
HeaderValue v = request.getHeaderValue(d);
if ( v != null ) {
sb.append("request.");
sb.append(d.getName());
sb.append('=');
sb.append(v.toString());
sb.append('\n');
}
}
}
if ( repheaders != null ) {
for (int i = 0 ; i < repheaders.length ; i++) {
HeaderDescription d = repheaders[i];
if ( d == null )
continue;
HeaderValue v = reply.getHeaderValue(d);
if ( v != null ) {
sb.append("reply.");
sb.append(d.getName());
sb.append('=');
sb.append(v.toString());
sb.append('\n');
}
}
}
writelog(sb.toString());
}
@param request@param reply
public ReplyInterface outgoingFilter(RequestInterface req,
ReplyInterface rep)
throws ProtocolException
{
Reply reply = (Reply) rep;
Request request = (Request) req;
if ( ! reply.hasState(DONT_LOG) )
log(request, reply);
return null;
}
public void initialize(Object values[]) {
super.initialize(values);
if ( log == null )
openLog();
if ( reqheaders == null ) {
String headers[] = getRequestHeaders();
if ( headers != null )
reqheaders = compileHeaders(new HttpRequestMessage(), headers);
}
if ( repheaders == null ) {
String headers[] = getReplyHeaders();
if ( headers != null )
repheaders = compileHeaders(new HttpReplyMessage(), headers);
}
}
}