-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.java
More file actions
296 lines (237 loc) · 9.86 KB
/
LogFile.java
File metadata and controls
296 lines (237 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package javaxt.express;
import java.util.*;
//******************************************************************************
//** LogFile
//******************************************************************************
/**
* Used to parse parse files generated by the Logger
*
******************************************************************************/
public class LogFile {
private javaxt.io.File log;
private Integer id;
//**************************************************************************
//** Constructor
//**************************************************************************
public LogFile(javaxt.io.File log) {
this.log = log;
try{
id = Integer.parseInt(log.getName(false));
}
catch(Exception e){
}
}
//**************************************************************************
//** getID
//**************************************************************************
public Integer getID(){
return id;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the contents of the log file as a string.
*/
public String toString(){
return log.toString();
}
//**************************************************************************
//** Entry Class
//**************************************************************************
/** Used to represent and individual entry in a log file.
*/
public class Entry {
private String ip;
private String host;
private String method;
private String url;
private String path;
private String protocol;
private javaxt.utils.Date date;
private String[] header;
private String tld;
private String domainName;
private boolean skip = false;
public Entry(javaxt.utils.Date date, String ip, String method, String url, String header){
this.ip = ip;
this.url = url;
this.date = date;
this.method = method;
this.header = header.split("\r\n");
//Parse host and extract domain name
try{
String str = url.substring(url.indexOf("://")+3);
int idx = str.indexOf("/");
if (idx>0){
host = str.substring(0, idx);
path = str.substring(idx+1);
}
else{
host = str;
}
host = host.toLowerCase();
if (host.contains(".")){
String[] arr = host.split("\\.");
tld = arr[arr.length-1]; //top level domain
domainName = arr[arr.length-2];
}
}
catch(Exception e){
e.printStackTrace();
}
}
public String[] getValues(String name){
ArrayList<String> values = new ArrayList<String>();
for (String row : header){
if (row.contains(":")){
String key = row.substring(0, row.indexOf(":")).trim();
String value = row.substring(row.indexOf(":")+1).trim();
if (key.equalsIgnoreCase(name)){
values.add(value);
}
}
}
return values.toArray(new String[values.size()]);
}
public String getValue(String name){
String[] values = this.getValues(name);
return (values.length>0) ? values[0] : "";
}
/** Returns true is the entry can/should be ignored. This will return
* true for duplicate requests (same request made within 5 minutes by
* the same IP address) and also "Range" requests made before 5/18/2013
*/
public boolean ignore(){
return skip;
}
public String getMethod(){
return method;
}
public String getURL(){
return url;
}
public String getPath(){
return path;
}
public String getProtocol(){
return protocol;
}
public javaxt.utils.Date getDate(){
return date.clone();
}
public String getDomainName(){
return domainName;
}
public String getIP(){
return ip;
}
}
//**************************************************************************
//** getEntries
//**************************************************************************
public javaxt.utils.Generator<Entry> getEntries(){
return new javaxt.utils.Generator<Entry>() {
@Override
public void run() {
HashMap<String, javaxt.utils.Date> map = new HashMap<>();
String[] requests = log.getText().split("\r\n\r\n");
for (int i=0; i<requests.length; i++){
try{
if (requests[i].startsWith("New Request From")){
//Flag used to indicate whether the user should consider
//skipping over the entry when procesing entries
boolean skip = false;
//Parse request metadata
String ip = null;
String url = null;
String op = null; //GET, POST, etc
javaxt.utils.Date date = null;
for (String row : requests[i].split("\r\n")){
if (row.contains(":")){
String key = row.substring(0, row.indexOf(":")).trim();
String value = row.substring(row.indexOf(":")+1).trim();
if (key.equals("New Request From")){
ip = value;
}
else if (key.equalsIgnoreCase("Timestamp")){
try{
date = new javaxt.utils.Date(value);
date.setTimeZone("UTC", true);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
op = key;
url = value;
}
}
}
//Check the last time the client requested this url. If
//it was less then 5 minutes ago, mark the entry as skipable
try {
String key = ip + "-" + url.toLowerCase();
if (map.containsKey(key)){
javaxt.utils.Date d = map.get(key);
long seconds = date.compareTo(d, "seconds");
if (seconds<(60*5)) skip = true;
else map.put(key, date);
}
else{
map.put(key, date);
}
}
catch(Exception e){
}
//Parse request header
i=i+1;
String requestHeader = requests[i];
if (op!=null) op = op.toUpperCase();
Entry entry = new Entry(date, ip, op, url, requestHeader);
//Check if this is a range request. If so mark it as skipable
String range = entry.getValue("Range");
if (range.contains("bytes=")){ //Range: bytes=658282-
//The JavaXT Server did not support range requests
//before 5/18/2013
if (id!=null && id<20130518) skip = true;
}
//Update skip flag
entry.skip = skip;
//Yield entry to caller
yield(entry);
}
}
catch(Exception e){
e.printStackTrace();
//System.out.println(log.getName());
//System.out.println(requests[i]);
}
}
}
};
}
//**************************************************************************
//** cleanLogFile
//**************************************************************************
/** Used to update the log file by removing any records for a given IP
* address.
*/
public void cleanLogFile(String ipAddress){
Date date = log.getDate();
String[] requests = log.getText().split("\r\n\r\n");
StringBuilder str = new StringBuilder();
for (int i=0; i<requests.length; i++){
if (requests[i].startsWith("New Request From")){
String request = requests[i] + "\r\n\r\n" + requests[i+1] + "\r\n\r\n";
i++;
if (!request.startsWith("New Request From: " + ipAddress)){
//System.out.println(request);
str.append(request);
}
}
}
log.write(str.toString());
log.setDate(date);
}
}