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.HttpUtil; |
30 |
|
import dk.thoerup.genericjavautils.TimeoutMap; |
31 |
|
|
32 |
@WebServlet(urlPatterns={"/RequestPlotter"}) |
@WebServlet(urlPatterns={"/RequestPlotter"}) |
33 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
59 |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
60 |
} |
} |
61 |
|
|
62 |
protected File[] getFiles(int count) { |
public static File[] getFiles(int count) { |
63 |
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
64 |
//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/"); |
65 |
|
|
79 |
return empty; |
return empty; |
80 |
} |
} |
81 |
|
|
82 |
int from = logFiles.length - (count); |
int from = (count>logFiles.length) ? 0 : logFiles.length - (count); |
83 |
int to = logFiles.length; |
int to = logFiles.length; |
84 |
|
|
85 |
return Arrays.copyOfRange(logFiles, from, to); |
return Arrays.copyOfRange(logFiles, from, to); |
86 |
} |
} |
87 |
|
|
88 |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
protected PositionContainer getRequestsFromFileWorker(int count) throws IOException{ |
89 |
PositionContainer positions = new PositionContainer(); |
PositionContainer positions = new PositionContainer(); |
90 |
|
|
91 |
try { |
try { |
|
|
|
|
int count; |
|
|
|
|
|
if (multiple == false) { |
|
|
count = 1; |
|
|
} else { |
|
|
count = 4; |
|
|
} |
|
92 |
|
|
93 |
File files[] = getFiles(count); |
File files[] = getFiles(count); |
94 |
|
|
123 |
continue; |
continue; |
124 |
} |
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
RequestPosition pos = new RequestPosition(); |
RequestPosition pos = new RequestPosition(); |
129 |
|
|
130 |
String toks[] = line.split(" "); |
String toks[] = line.split(" "); |
132 |
|
|
133 |
pos.time = df.parse( toks[2].replace("\"", "") ); |
pos.time = df.parse( toks[2].replace("\"", "") ); |
134 |
|
|
135 |
String argpart = toks[5].split("\\?")[1]; |
Map<String,String> params = HttpUtil.decodeUri( toks[5] ); |
136 |
|
|
137 |
String args[] = argpart.split("&"); |
pos.lat = params.get("latitude"); |
138 |
|
pos.lng = params.get("longitude"); |
|
pos.lat = args[0].split("=")[1]; |
|
|
pos.lng = args[1].split("=")[1]; |
|
139 |
|
|
140 |
|
|
141 |
long timediff = now.getTime() - pos.time.getTime(); |
long timediff = now.getTime() - pos.time.getTime(); |
268 |
for(RequestPosition current : list) { |
for(RequestPosition current : list) { |
269 |
String id = color + count++; |
String id = color + count++; |
270 |
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
271 |
|
sb.append( " <name>").append(current.time).append("</name>\n" ); |
272 |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
273 |
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" ); |
274 |
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" ); |
275 |
sb.append( " </Placemark>\n" ); |
sb.append( " </Placemark>\n" ); |
276 |
} |
} |
326 |
*/ |
*/ |
327 |
|
|
328 |
|
|
|
formatPositions(sb, "blue", positions.blue); |
|
|
formatPositions(sb, "green", positions.green); |
|
|
formatPositions(sb, "yellow", positions.yellow); |
|
329 |
formatPositions(sb, "red", positions.red); |
formatPositions(sb, "red", positions.red); |
330 |
|
formatPositions(sb, "yellow", positions.yellow); |
331 |
|
formatPositions(sb, "green", positions.green); |
332 |
|
formatPositions(sb, "blue", positions.blue); |
333 |
|
|
334 |
|
|
335 |
sb.append( "</Document>\n" ); |
sb.append( "</Document>\n" ); |
338 |
return sb.toString(); |
return sb.toString(); |
339 |
} |
} |
340 |
|
|
341 |
protected String getRequestsFromFile(boolean multiple) throws IOException { |
protected String getRequestsFromFile(int count) throws IOException { |
342 |
String kmlData = null; |
String kmlData = null; |
343 |
String key; |
String key; |
344 |
|
|
345 |
if (multiple == false) { |
key = "kmldata-" + count; |
|
key = "kmldata"; |
|
|
} else { |
|
|
key = "kmldata-multi"; |
|
|
} |
|
346 |
|
|
347 |
kmlData = null;// = cache.get(key); |
kmlData = cache.get(key); |
348 |
|
|
349 |
if (kmlData == null) { |
if (kmlData == null) { |
350 |
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
kmlData = formatXml( getRequestsFromFileWorker(count) ); |
351 |
cache.put(key, kmlData); |
cache.put(key, kmlData); |
352 |
kmlData += "<!-- from source -->"; |
kmlData += "<!-- from source -->"; |
353 |
} else { |
} else { |
370 |
return (p != 0); |
return (p != 0); |
371 |
} |
} |
372 |
|
|
373 |
|
int getCount(String param) { |
374 |
|
if (param == null || param.equals("")) { |
375 |
|
return 1; |
376 |
|
} |
377 |
|
|
378 |
|
return Integer.parseInt(param); |
379 |
|
} |
380 |
|
|
381 |
|
|
382 |
@Override |
@Override |
383 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
384 |
|
|
|
boolean multiple = enabled( req.getParameter("multi") ); |
|
385 |
|
|
386 |
String kmlData = getRequestsFromFile(multiple); |
int count = getCount( req.getParameter("count") ); |
387 |
|
|
388 |
|
if (count > 30) //limit to 30 days |
389 |
|
count = 30; |
390 |
|
if (count < 0) //negative count is not allowed |
391 |
|
count = 0; |
392 |
|
|
393 |
|
String kmlData = getRequestsFromFile(count); |
394 |
|
|
395 |
if ( enabled(req.getParameter("zip")) ) { |
if ( enabled(req.getParameter("zip")) ) { |
396 |
|
|
405 |
byte bytes[] = baos.toByteArray(); |
byte bytes[] = baos.toByteArray(); |
406 |
|
|
407 |
resp.setContentType(KMZ); |
resp.setContentType(KMZ); |
408 |
resp.setContentLength( bytes.length ); |
resp.setHeader("Content-Disposition", "attachment; filename=Traininfo-requestplotter.kmz"); |
409 |
|
resp.setContentLength( bytes.length ); |
410 |
resp.getOutputStream().write(bytes); |
resp.getOutputStream().write(bytes); |
411 |
|
|
412 |
} else { |
} else { |
413 |
resp.setContentType(KML); |
byte bytes[] = kmlData.getBytes(); |
414 |
resp.getWriter().print( kmlData ); |
|
415 |
|
resp.setContentType(KML); |
416 |
|
resp.setContentLength( bytes.length ); |
417 |
|
resp.getOutputStream().write(bytes); |
418 |
} |
} |
419 |
} |
} |
420 |
} |
} |