| 71 | | import java.text.ParseException; |
| | 74 | {{{ |
| | 75 | public LogParser(String line) throws ParseException, Exception{ |
| | 76 | Matcher matcher = p.matcher(line); |
| | 77 | if(matcher.matches()){ |
| | 78 | this.ip = matcher.group(1); |
| | 79 | // IP address of the client requesting the web page. |
| | 80 | if(isIpAddress(ip)){ |
| | 81 | SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",Locale.US); |
| | 82 | this.timestamp = sdf.parse(matcher.group(4)).getTime(); |
| | 83 | String[] http = matcher.group(5).split(" "); |
| | 84 | this.method = http[0]; |
| | 85 | this.url = http[1]; |
| | 86 | this.protocol = http[2]; |
| | 87 | this.code = matcher.group(6); |
| | 88 | this.byteSize = matcher.group(7); |
| | 89 | this.referrer = matcher.group(8); |
| | 90 | this.agent = matcher.group(9); |
| | 91 | } |
| | 92 | } |
| | 93 | } |
| | 94 | }}} |
| 73 | | import java.text.SimpleDateFormat; |
| 74 | | |
| 75 | | import java.util.Locale; |
| 76 | | |
| 77 | | import java.util.StringTokenizer; |
| 78 | | |
| 79 | | import java.util.regex.Matcher; |
| 80 | | |
| 81 | | import java.util.regex.Pattern; |
| 82 | | |
| 83 | | public class LogParser { |
| 84 | | |
| 85 | | private String ip; |
| 86 | | |
| 87 | | private String protocol; |
| 88 | | |
| 89 | | private String method; |
| 90 | | |
| 91 | | private String url; |
| 92 | | |
| 93 | | private String code; |
| 94 | | |
| 95 | | private String byteSize; |
| 96 | | |
| 97 | | private String referrer; |
| 98 | | |
| 99 | | private String agent; |
| 100 | | |
| 101 | | private long timestamp; |
| 102 | | |
| 103 | | private static Pattern p = Pattern |
| 104 | | |
| 105 | | .compile("([^ ]*) ([^ ]*) ([^ ]*) \\[([^]]*)\\] \"([^\"]*)\"" + |
| 106 | | |
| 107 | | " ([^ ]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\".*"); |
| 108 | | |
| 109 | | public LogParser(String line) throws ParseException, Exception{ |
| 110 | | |
| 111 | | Matcher matcher = p.matcher(line); |
| 112 | | |
| 113 | | if(matcher.matches()){ |
| 114 | | |
| 115 | | this.ip = matcher.group(1); |
| 116 | | |
| 117 | | // IP address of the client requesting the web page. |
| 118 | | |
| 119 | | if(isIpAddress(ip)){ |
| 120 | | |
| 121 | | SimpleDateFormat sdf = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z",Locale.US); |
| 122 | | |
| 123 | | this.timestamp = sdf.parse(matcher.group(4)).getTime(); |
| 124 | | |
| 125 | | String[] http = matcher.group(5).split(" "); |
| 126 | | |
| 127 | | this.method = http[0]; |
| 128 | | |
| 129 | | this.url = http[1]; |
| 130 | | |
| 131 | | this.protocol = http[2]; |
| 132 | | |
| 133 | | this.code = matcher.group(6); |
| 134 | | |
| 135 | | this.byteSize = matcher.group(7); |
| 136 | | |
| 137 | | this.referrer = matcher.group(8); |
| 138 | | |
| 139 | | this.agent = matcher.group(9); |
| 140 | | |
| 141 | | } |
| 142 | | |
| 143 | | } |
| 144 | | |
| 145 | | } |
| 146 | | |
| | 96 | {{{ |