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; |
|
import java.util.zip.GZIPInputStream; |
|
20 |
import java.util.zip.ZipEntry; |
import java.util.zip.ZipEntry; |
21 |
import java.util.zip.ZipOutputStream; |
import java.util.zip.ZipOutputStream; |
22 |
|
|
26 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
27 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
28 |
|
|
29 |
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
import dk.thoerup.genericjavautils.TimeoutMap; |
30 |
|
|
31 |
@WebServlet(urlPatterns={"/RequestPlotter"}) |
@WebServlet(urlPatterns={"/RequestPlotter"}) |
32 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
58 |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
59 |
} |
} |
60 |
|
|
61 |
protected File[] getFiles(int count) { |
public static File[] getFiles(int count) { |
62 |
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
63 |
//File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/"); |
//File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/"); |
64 |
|
|
78 |
return empty; |
return empty; |
79 |
} |
} |
80 |
|
|
81 |
int from = logFiles.length - (count); |
int from = (count>logFiles.length) ? 0 : logFiles.length - (count); |
82 |
int to = logFiles.length; |
int to = logFiles.length; |
83 |
|
|
84 |
return Arrays.copyOfRange(logFiles, from, to); |
return Arrays.copyOfRange(logFiles, from, to); |
85 |
} |
} |
86 |
|
|
87 |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
protected PositionContainer getRequestsFromFileWorker(int count) throws IOException{ |
88 |
PositionContainer positions = new PositionContainer(); |
PositionContainer positions = new PositionContainer(); |
89 |
|
|
90 |
try { |
try { |
|
|
|
|
int count; |
|
|
|
|
|
if (multiple == false) { |
|
|
count = 1; |
|
|
} else { |
|
|
count = 4; |
|
|
} |
|
91 |
|
|
92 |
File files[] = getFiles(count); |
File files[] = getFiles(count); |
93 |
|
|
267 |
for(RequestPosition current : list) { |
for(RequestPosition current : list) { |
268 |
String id = color + count++; |
String id = color + count++; |
269 |
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
270 |
|
sb.append( " <name>").append(current.time).append("</name>\n" ); |
271 |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
272 |
sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" ); |
sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/> ]]></description>\n" ); |
273 |
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
274 |
sb.append( " </Placemark>\n" ); |
sb.append( " </Placemark>\n" ); |
275 |
} |
} |
337 |
return sb.toString(); |
return sb.toString(); |
338 |
} |
} |
339 |
|
|
340 |
protected String getRequestsFromFile(boolean multiple) throws IOException { |
protected String getRequestsFromFile(int count) throws IOException { |
341 |
String kmlData = null; |
String kmlData = null; |
342 |
String key; |
String key; |
343 |
|
|
344 |
if (multiple == false) { |
key = "kmldata-" + count; |
|
key = "kmldata"; |
|
|
} else { |
|
|
key = "kmldata-multi"; |
|
|
} |
|
345 |
|
|
346 |
kmlData = null;// = cache.get(key); |
kmlData = cache.get(key); |
347 |
|
|
348 |
if (kmlData == null) { |
if (kmlData == null) { |
349 |
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
kmlData = formatXml( getRequestsFromFileWorker(count) ); |
350 |
cache.put(key, kmlData); |
cache.put(key, kmlData); |
351 |
kmlData += "<!-- from source -->"; |
kmlData += "<!-- from source -->"; |
352 |
} else { |
} else { |
369 |
return (p != 0); |
return (p != 0); |
370 |
} |
} |
371 |
|
|
372 |
|
int getCount(String param) { |
373 |
|
if (param == null || param.equals("")) { |
374 |
|
return 1; |
375 |
|
} |
376 |
|
|
377 |
|
return Integer.parseInt(param); |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
@Override |
@Override |
382 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
383 |
|
|
|
boolean multiple = enabled( req.getParameter("multi") ); |
|
384 |
|
|
385 |
String kmlData = getRequestsFromFile(multiple); |
int count = getCount( req.getParameter("count") ); |
386 |
|
|
387 |
|
if (count > 30) //limit to 30 days |
388 |
|
count = 30; |
389 |
|
if (count < 0) //negative count is not allowed |
390 |
|
count = 0; |
391 |
|
|
392 |
|
String kmlData = getRequestsFromFile(count); |
393 |
|
|
394 |
if ( enabled(req.getParameter("zip")) ) { |
if ( enabled(req.getParameter("zip")) ) { |
395 |
|
|
404 |
byte bytes[] = baos.toByteArray(); |
byte bytes[] = baos.toByteArray(); |
405 |
|
|
406 |
resp.setContentType(KMZ); |
resp.setContentType(KMZ); |
407 |
resp.setContentLength( bytes.length ); |
resp.setHeader("Content-Disposition", "attachment; filename=Traininfo-requestplotter.kmz"); |
408 |
|
resp.setContentLength( bytes.length ); |
409 |
resp.getOutputStream().write(bytes); |
resp.getOutputStream().write(bytes); |
410 |
|
|
411 |
} else { |
} else { |
412 |
resp.setContentType(KML); |
byte bytes[] = kmlData.getBytes(); |
413 |
resp.getWriter().print( kmlData ); |
|
414 |
|
resp.setContentType(KML); |
415 |
|
resp.setContentLength( bytes.length ); |
416 |
|
resp.getOutputStream().write(bytes); |
417 |
} |
} |
418 |
} |
} |
419 |
} |
} |