2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
import java.io.ByteArrayOutputStream; |
import java.io.ByteArrayOutputStream; |
5 |
|
import java.io.File; |
6 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
7 |
|
import java.io.FilenameFilter; |
8 |
import java.io.IOException; |
import java.io.IOException; |
9 |
|
import java.io.InputStream; |
10 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
11 |
import java.text.ParseException; |
import java.text.ParseException; |
12 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
13 |
import java.util.ArrayList; |
import java.util.ArrayList; |
14 |
|
import java.util.Arrays; |
15 |
import java.util.Date; |
import java.util.Date; |
16 |
import java.util.List; |
import java.util.List; |
17 |
import java.util.Map; |
import java.util.Map; |
18 |
import java.util.logging.Level; |
import java.util.logging.Level; |
19 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
20 |
|
import java.util.zip.GZIPInputStream; |
21 |
import java.util.zip.ZipEntry; |
import java.util.zip.ZipEntry; |
22 |
import java.util.zip.ZipOutputStream; |
import java.util.zip.ZipOutputStream; |
23 |
|
|
24 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
25 |
|
import javax.servlet.annotation.WebServlet; |
26 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
27 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
28 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
29 |
|
|
30 |
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
31 |
|
|
32 |
|
@WebServlet(urlPatterns={"/RequestPlotter"}) |
33 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
34 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
35 |
|
|
38 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
39 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
40 |
|
|
41 |
Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000); |
Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000); |
42 |
|
|
43 |
class RequestPosition { |
class RequestPosition { |
44 |
public String ip; |
public String ip; |
46 |
public String lat; |
public String lat; |
47 |
public String lng; |
public String lng; |
48 |
} |
} |
49 |
|
|
50 |
|
class PositionContainer { |
51 |
|
List<RequestPosition> blue = new ArrayList<RequestPosition>(); |
52 |
|
List<RequestPosition> green = new ArrayList<RequestPosition>(); |
53 |
|
List<RequestPosition> yellow = new ArrayList<RequestPosition>(); |
54 |
|
List<RequestPosition> red = new ArrayList<RequestPosition>(); |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
|
boolean isGz(String fileStr) { |
59 |
protected List<RequestPosition> getRequestsFromFile() throws IOException{ |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
60 |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
} |
61 |
|
|
62 |
|
protected File[] getFiles(int count) { |
63 |
|
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
64 |
|
//File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/"); |
65 |
|
|
66 |
|
File logFiles[] = accessLogDir.listFiles( new FilenameFilter() { |
67 |
|
@Override |
68 |
|
public boolean accept(File dir, String name) { |
69 |
|
//log.info("name:" + name); |
70 |
|
return name.startsWith("server_access_log"); |
71 |
|
} |
72 |
|
}); |
73 |
|
|
74 |
|
Arrays.sort(logFiles); |
75 |
|
|
76 |
|
if (logFiles == null) { |
77 |
|
File[] empty = {}; |
78 |
|
log.info("file array was empty"); |
79 |
|
return empty; |
80 |
|
} |
81 |
|
|
82 |
|
int from = logFiles.length - (count); |
83 |
|
int to = logFiles.length; |
84 |
|
|
85 |
|
return Arrays.copyOfRange(logFiles, from, to); |
86 |
|
} |
87 |
|
|
88 |
|
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
89 |
|
PositionContainer positions = new PositionContainer(); |
90 |
|
|
91 |
try { |
try { |
92 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); |
|
93 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
int count; |
94 |
|
|
95 |
|
if (multiple == false) { |
96 |
|
count = 1; |
97 |
|
} else { |
98 |
|
count = 4; |
99 |
|
} |
100 |
|
|
101 |
|
File files[] = getFiles(count); |
102 |
|
|
103 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
104 |
|
|
105 |
String line; |
Date now = new Date(); |
106 |
while ( (line=in.readLine()) != null) { |
for (File f : files ) { |
107 |
if (line.indexOf("LocateStation") == -1 ){ |
log.info("Parsing file: " + f.getName() ); |
108 |
|
|
109 |
|
if ( !f.exists() ) { |
110 |
continue; |
continue; |
111 |
} |
} |
112 |
|
|
113 |
|
|
114 |
|
InputStream input = new FileInputStream(f); |
115 |
|
|
116 |
if (line.indexOf("latitude") == -1 ) { |
|
117 |
continue; |
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
|
} |
|
|
RequestPosition pos = new RequestPosition(); |
|
118 |
|
|
119 |
String toks[] = line.split(" "); |
|
120 |
pos.ip = toks[0]; |
String line; |
121 |
|
while ( (line=in.readLine()) != null) { |
122 |
|
if (line.indexOf("LocateStation") == -1 ){ |
123 |
|
continue; |
124 |
|
} |
125 |
|
|
126 |
|
if (line.indexOf("latitude=") == -1 ) { |
127 |
|
continue; |
128 |
|
} |
129 |
|
|
130 |
|
if (line.indexOf("longitude=") == -1) { |
131 |
|
continue; |
132 |
|
} |
133 |
|
|
134 |
|
RequestPosition pos = new RequestPosition(); |
135 |
|
|
136 |
|
String toks[] = line.split(" "); |
137 |
|
pos.ip = toks[0].replaceAll("\"", ""); |
138 |
|
|
139 |
|
pos.time = df.parse( toks[2].replace("\"", "") ); |
140 |
|
|
141 |
|
String argpart = toks[5].split("\\?")[1]; |
142 |
|
|
143 |
|
String args[] = argpart.split("&"); |
144 |
|
|
145 |
|
pos.lat = args[0].split("=")[1]; |
146 |
|
pos.lng = args[1].split("=")[1]; |
147 |
|
|
148 |
|
|
149 |
|
long timediff = now.getTime() - pos.time.getTime(); |
150 |
|
if ( timediff < (3*60*60*1000) ) { |
151 |
|
positions.red.add(pos); //RED |
152 |
|
} else if ( timediff < (24*60*60*1000)) { |
153 |
|
positions.yellow.add(pos); //YELLOW |
154 |
|
} else if ( timediff < (7*24*60*60*1000)) { |
155 |
|
positions.green.add(pos); //GREEN |
156 |
|
} else { |
157 |
|
positions.blue.add(pos); //BLUE |
158 |
|
} |
159 |
|
|
160 |
|
} |
161 |
|
in.close(); |
162 |
|
input.close(); |
163 |
|
|
164 |
|
} |
165 |
|
} catch (ParseException pe) { |
166 |
|
log.log(Level.SEVERE, "parseException", pe); |
167 |
|
throw new IOException(pe); |
168 |
|
} catch (IOException e) { |
169 |
|
log.log(Level.SEVERE, "getKml()", e); |
170 |
|
throw e; |
171 |
|
} |
172 |
|
|
173 |
pos.time = df.parse( toks[3].replace("[", "") ); |
return positions; |
174 |
|
} |
175 |
|
|
176 |
String argpart = toks[6].split("\\?")[1]; |
|
177 |
|
/* old apache code |
178 |
|
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
179 |
|
PositionContainer positions = new PositionContainer(); |
180 |
|
|
181 |
String args[] = argpart.split("&"); |
try { |
182 |
|
String files_single[] = {"/var/log/apache2/app_access.log"}; |
183 |
|
String files_multi[] = {"/var/log/apache2/app_access.log.3.gz", "/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"}; |
184 |
|
|
185 |
|
String files[]; |
186 |
|
|
187 |
|
if (multiple == false) { |
188 |
|
files = files_single; |
189 |
|
} else { |
190 |
|
files = files_multi; |
191 |
|
} |
192 |
|
|
193 |
pos.lat = args[0].split("=")[1]; |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
194 |
pos.lng = args[1].split("=")[1]; |
|
195 |
|
Date now = new Date(); |
196 |
|
for (String fileStr : files ) { |
197 |
|
File f = new File(fileStr); |
198 |
|
if ( !f.exists() ) { |
199 |
|
continue; |
200 |
|
} |
201 |
|
|
202 |
|
FileInputStream fis = new FileInputStream(fileStr); |
203 |
|
|
204 |
|
InputStream input; |
205 |
|
if ( isGz(fileStr)) { |
206 |
|
input = new GZIPInputStream(fis); |
207 |
|
} else { |
208 |
|
input = fis; |
209 |
|
} |
210 |
|
|
211 |
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
212 |
|
|
213 |
positions.add(pos); |
|
214 |
|
String line; |
215 |
|
while ( (line=in.readLine()) != null) { |
216 |
|
if (line.indexOf("LocateStation") == -1 ){ |
217 |
|
continue; |
218 |
|
} |
219 |
|
|
220 |
|
if (line.indexOf("latitude=") == -1 ) { |
221 |
|
continue; |
222 |
|
} |
223 |
|
|
224 |
|
if (line.indexOf("longitude=") == -1) { |
225 |
|
continue; |
226 |
|
} |
227 |
|
|
228 |
|
RequestPosition pos = new RequestPosition(); |
229 |
|
|
230 |
|
String toks[] = line.split(" "); |
231 |
|
pos.ip = toks[0]; |
232 |
|
|
233 |
|
pos.time = df.parse( toks[3].replace("[", "") ); |
234 |
|
|
235 |
|
String argpart = toks[6].split("\\?")[1]; |
236 |
|
|
237 |
|
String args[] = argpart.split("&"); |
238 |
|
|
239 |
|
pos.lat = args[0].split("=")[1]; |
240 |
|
pos.lng = args[1].split("=")[1]; |
241 |
|
|
242 |
|
|
243 |
|
long timediff = now.getTime() - pos.time.getTime(); |
244 |
|
if ( timediff < (3*60*60*1000) ) { |
245 |
|
positions.red.add(pos); //RED |
246 |
|
} else if ( timediff < (24*60*60*1000)) { |
247 |
|
positions.yellow.add(pos); //YELLOW |
248 |
|
} else if ( timediff < (7*24*60*60*1000)) { |
249 |
|
positions.green.add(pos); //GREEN |
250 |
|
} else { |
251 |
|
positions.blue.add(pos); //BLUE |
252 |
|
} |
253 |
|
|
254 |
|
} |
255 |
|
in.close(); |
256 |
|
input.close(); |
257 |
|
fis.close(); |
258 |
} |
} |
259 |
} catch (ParseException pe) { |
} catch (ParseException pe) { |
260 |
log.log(Level.SEVERE, "parseException", pe); |
log.log(Level.SEVERE, "parseException", pe); |
265 |
} |
} |
266 |
|
|
267 |
return positions; |
return positions; |
268 |
|
}*/ |
269 |
|
|
270 |
|
protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) { |
271 |
|
sb.append( "<Folder>\n"); |
272 |
|
sb.append( " <name>" ).append(color).append("</name>\n"); |
273 |
|
sb.append( " <open>0</open>\n" ); |
274 |
|
|
275 |
|
int count=0; |
276 |
|
for(RequestPosition current : list) { |
277 |
|
String id = color + count++; |
278 |
|
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
279 |
|
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
280 |
|
sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" ); |
281 |
|
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
282 |
|
sb.append( " </Placemark>\n" ); |
283 |
|
} |
284 |
|
|
285 |
|
sb.append("</Folder>\n"); |
286 |
} |
} |
287 |
|
|
288 |
protected String formatXml(List<RequestPosition> list) { |
protected String formatXml(PositionContainer positions) { |
289 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(1024*1024); |
290 |
|
|
291 |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
292 |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
293 |
sb.append( "<Document>\n" ); |
sb.append( "<Document>\n" ); |
294 |
|
sb.append( " <description><![CDATA["); |
295 |
|
sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n"); |
296 |
|
sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n"); |
297 |
|
sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n"); |
298 |
|
sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n"); |
299 |
|
sb.append( " ]]></description>"); |
300 |
|
|
301 |
|
|
302 |
sb.append( " <Style id=\"red\">\n" ); |
sb.append( " <Style id=\"red\">\n" ); |
311 |
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" ); |
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" ); |
312 |
sb.append( " </Style>\n\n" ); |
sb.append( " </Style>\n\n" ); |
313 |
|
|
314 |
|
sb.append( " <Style id=\"blue\">\n" ); |
315 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" ); |
316 |
|
sb.append( " </Style>\n\n" ); |
317 |
/* |
/* |
318 |
String overlay = |
String overlay = |
319 |
" <ScreenOverlay>" + |
" <ScreenOverlay>" + |
333 |
*/ |
*/ |
334 |
|
|
335 |
|
|
336 |
|
formatPositions(sb, "blue", positions.blue); |
337 |
|
formatPositions(sb, "green", positions.green); |
338 |
|
formatPositions(sb, "yellow", positions.yellow); |
339 |
|
formatPositions(sb, "red", positions.red); |
340 |
|
|
341 |
|
|
342 |
|
sb.append( "</Document>\n" ); |
|
|
|
|
|
|
|
Date now = new Date(); |
|
|
for(RequestPosition current : list) { |
|
|
|
|
|
String style; |
|
|
long timediff = now.getTime() - current.time.getTime(); |
|
|
|
|
|
if ( timediff < (3*60*60*1000) ) { |
|
|
style = "#red"; |
|
|
} else if ( timediff < (24*60*60*1000)) { |
|
|
style = "#yellow"; |
|
|
} else { |
|
|
style = "#green"; |
|
|
} |
|
|
|
|
|
sb.append( " <Placemark>\n" ); |
|
|
sb.append( " <styleUrl>" + style + "</styleUrl>\n" ); |
|
|
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
|
|
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
|
|
sb.append( " </Placemark>\n" ); |
|
|
} |
|
|
|
|
|
sb.append( "</Document>\n" ); |
|
343 |
sb.append( "</kml>\n" ); |
sb.append( "</kml>\n" ); |
344 |
|
|
345 |
return sb.toString(); |
return sb.toString(); |
346 |
} |
} |
347 |
|
|
348 |
|
protected String getRequestsFromFile(boolean multiple) throws IOException { |
349 |
|
String kmlData = null; |
350 |
|
String key; |
351 |
|
|
352 |
|
if (multiple == false) { |
353 |
|
key = "kmldata"; |
354 |
|
} else { |
355 |
|
key = "kmldata-multi"; |
356 |
|
} |
357 |
|
|
358 |
|
kmlData = null;// = cache.get(key); |
359 |
|
|
360 |
|
if (kmlData == null) { |
361 |
|
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
362 |
|
cache.put(key, kmlData); |
363 |
|
kmlData += "<!-- from source -->"; |
364 |
|
} else { |
365 |
|
kmlData += "<!-- cached -->"; |
366 |
|
} |
367 |
|
|
368 |
|
return kmlData; |
369 |
|
} |
370 |
|
|
371 |
|
boolean enabled(String param) { |
372 |
|
if (param == null || param.equals("")) { |
373 |
|
return false; |
374 |
|
} |
375 |
|
|
376 |
|
int p = 0; |
377 |
|
try { |
378 |
|
p = Integer.parseInt(param); |
379 |
|
} catch (Exception e) {} |
380 |
|
|
381 |
|
return (p != 0); |
382 |
|
} |
383 |
|
|
384 |
@Override |
@Override |
385 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
386 |
|
|
387 |
String data = cache.get("data"); |
boolean multiple = enabled( req.getParameter("multi") ); |
388 |
|
|
389 |
if (data == null) { |
String kmlData = getRequestsFromFile(multiple); |
|
data = formatXml( getRequestsFromFile() ); |
|
|
cache.put("data", data); |
|
|
data += "<!-- from source -->"; |
|
|
} else { |
|
|
data += "<!-- cached -->"; |
|
|
} |
|
|
|
|
390 |
|
|
391 |
|
if ( enabled(req.getParameter("zip")) ) { |
|
if (req.getParameter("zip") != null) { |
|
392 |
|
|
393 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
394 |
|
|
395 |
ZipOutputStream zip = new ZipOutputStream(baos); |
ZipOutputStream zip = new ZipOutputStream(baos); |
396 |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
397 |
zip.write( data.getBytes() ); |
zip.write( kmlData.getBytes() ); |
398 |
zip.closeEntry(); |
zip.closeEntry(); |
399 |
zip.close(); |
zip.close(); |
400 |
|
|
406 |
|
|
407 |
} else { |
} else { |
408 |
resp.setContentType(KML); |
resp.setContentType(KML); |
409 |
resp.getWriter().print( data ); |
resp.getWriter().print( kmlData ); |
410 |
} |
} |
411 |
} |
} |
412 |
} |
} |