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