source: sample/hadoop-0.16/tw/org/nchc/code/LogParser.java @ 31

Last change on this file since 31 was 31, checked in by waue, 16 years ago

update some new ..

File size: 2.5 KB
Line 
1/**
2 * Program: LogParser.java
3 * Editor: Waue Chen
4 * From :  NCHC. Taiwn
5 * Last Update Date: 07/02/2008
6 */
7
8package tw.org.nchc.code;
9
10import java.text.ParseException;
11import java.text.SimpleDateFormat;
12import java.util.Locale;
13import java.util.StringTokenizer;
14import java.util.regex.Matcher;
15import java.util.regex.Pattern;
16
17
18
19public class LogParser {
20  private String ip;
21  private String protocol;
22  private String method;
23  private String url;
24  private String code;
25  private String byteSize;
26  private String referrer;
27  private String agent;
28  private long timestamp;
29
30  private static Pattern p = Pattern
31  .compile("([^ ]*) ([^ ]*) ([^ ]*) \\[([^]]*)\\] \"([^\"]*)\"" +
32                  " ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\".*");
33 
34 
35 
36  public LogParser(String line) throws ParseException, Exception{
37   
38   Matcher matcher = p.matcher(line);
39   if(matcher.matches()){
40     this.ip = matcher.group(1);
41     // IP address of the client requesting the web page.
42     if(isIpAddress(ip)){
43       SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",Locale.US);
44       this.timestamp = sdf.parse(matcher.group(4)).getTime();
45       String[] http = matcher.group(5).split(" ");
46       this.method = http[0];
47       this.url = http[1];
48       this.protocol = http[2];
49       this.code = matcher.group(6);
50       this.byteSize = matcher.group(7);
51       this.referrer = matcher.group(8);
52       this.agent = matcher.group(9);
53     }
54   }
55
56
57  }
58
59  public static boolean isIpAddress(String inputString) {
60    StringTokenizer tokenizer = new StringTokenizer(inputString, ".");
61    if (tokenizer.countTokens() != 4) {
62      return false;
63    }
64    try {
65      for (int i = 0; i < 4; i++) {
66        String t = tokenizer.nextToken();
67        int chunk = Integer.parseInt(t);
68        if ((chunk & 255) != chunk) {
69          return false;
70        }
71      }
72    } catch (NumberFormatException e) {
73      return false;
74    }
75    if (inputString.indexOf("..") >= 0) {
76      return false;
77    }
78    return true;
79  }
80
81  public String getIp() {
82    return ip;
83  }
84
85  public String getProtocol() {
86    return protocol;
87  }
88
89  public String getMethod() {
90    return method;
91  }
92
93  public String getUrl() {
94    return url;
95  }
96
97  public String getCode() {
98    return code;
99  }
100
101  public String getByteSize() {
102    return byteSize;
103  }
104
105  public String getReferrer() {
106    return referrer;
107  }
108
109  public String getAgent() {
110    return agent;
111  }
112
113  public long getTimestamp() {
114    return timestamp;
115  }
116 
117}
Note: See TracBrowser for help on using the repository browser.