Show CommonLogParser.java syntax highlighted
/*
* This file is distributed under the GPL v2 as part of teastats site statistics package
* http://teastats.sourceforge.net
*/
package net.time4tea.webstats.parser;
import net.time4tea.webstats.record.Page;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import java.net.URISyntaxException;
public class CommonLogParser implements LogParser {
private DateTimeFormatter dtf = DateTimeFormat.forPattern(FORMAT);
private static final String FORMAT = "[dd/MMM/yyyy:HH:mm:ss Z]";
public CommonLogParser() {
super();
}
public Page parseLine(String input) throws ParseException {
String[] tokens = input.split("\"");
if (tokens.length < 3) {
System.out.println(input);
}
String[] firstStuff = tokens[0].split(" ");
String[] methodUrl = tokens[1].split(" ");
String[] bytesStatus = tokens[2].split(" ");
Page record = new Page();
record.setClientAddress(firstStuff[0]);
record.setVirtualHost(firstStuff[1].equals("-") ? null : firstStuff[1]);
record.setUsername(firstStuff[2].equals("-") ? null : firstStuff[2]);
record.setMethod(methodUrl[0]);
try {
record.setUriString(methodUrl[1]);
}
catch (URISyntaxException e) {
try {
record.setUriString("invalid");
}
catch (URISyntaxException e2) {
}
}
record.setBytes(bytesStatus[2].equals("-") ? 0 : Integer.parseInt(bytesStatus[2]));
record.setStatusCode(Integer.parseInt(bytesStatus[1]));
record.setDate(dtf.parseDateTime(firstStuff[3] + " " + firstStuff[4]).toInstant());
return record;
}
}
See more files for this project here