1 |
package dk.thoerup.traininfoservice;
|
2 |
|
3 |
import java.io.BufferedReader;
|
4 |
import java.io.ByteArrayOutputStream;
|
5 |
import java.io.FileInputStream;
|
6 |
import java.io.IOException;
|
7 |
import java.io.InputStreamReader;
|
8 |
import java.util.ArrayList;
|
9 |
import java.util.List;
|
10 |
import java.util.logging.Level;
|
11 |
import java.util.logging.Logger;
|
12 |
import java.util.zip.ZipEntry;
|
13 |
import java.util.zip.ZipOutputStream;
|
14 |
|
15 |
import javax.servlet.ServletException;
|
16 |
import javax.servlet.http.HttpServlet;
|
17 |
import javax.servlet.http.HttpServletRequest;
|
18 |
import javax.servlet.http.HttpServletResponse;
|
19 |
|
20 |
public class RequestPlotter extends HttpServlet {
|
21 |
private static final long serialVersionUID = 1L;
|
22 |
|
23 |
static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
|
24 |
|
25 |
static final String KML = "application/vnd.google-earth.kml";
|
26 |
static final String KMZ = "application/vnd.google-earth.kmz";
|
27 |
|
28 |
class RequestPosition {
|
29 |
public String ip;
|
30 |
public String time;
|
31 |
public String lat;
|
32 |
public String lng;
|
33 |
}
|
34 |
|
35 |
|
36 |
|
37 |
protected List<RequestPosition> getRequestsFromFile() throws IOException{
|
38 |
List<RequestPosition> positions = new ArrayList<RequestPosition>();
|
39 |
|
40 |
try {
|
41 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
|
42 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
|
43 |
|
44 |
String line;
|
45 |
while ( (line=in.readLine()) != null) {
|
46 |
if (line.indexOf("LocateStation") == -1 ){
|
47 |
continue;
|
48 |
}
|
49 |
|
50 |
if (line.indexOf("latitude") == -1 ) {
|
51 |
continue;
|
52 |
}
|
53 |
RequestPosition pos = new RequestPosition();
|
54 |
|
55 |
String toks[] = line.split(" ");
|
56 |
pos.ip = toks[0];
|
57 |
pos.time = toks[3].replace("[", "");
|
58 |
String argpart = toks[6].split("\\?")[1];
|
59 |
|
60 |
String args[] = argpart.split("&");
|
61 |
|
62 |
pos.lat = args[0].split("=")[1];
|
63 |
pos.lng = args[1].split("=")[1];
|
64 |
|
65 |
positions.add(pos);
|
66 |
}
|
67 |
} catch (IOException e) {
|
68 |
log.log(Level.SEVERE, "getKml()", e);
|
69 |
throw e;
|
70 |
}
|
71 |
|
72 |
return positions;
|
73 |
}
|
74 |
|
75 |
protected String formatXml(List<RequestPosition> list) {
|
76 |
StringBuilder sb = new StringBuilder();
|
77 |
|
78 |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
|
79 |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
|
80 |
sb.append( "<Document>\n" );
|
81 |
|
82 |
for(RequestPosition current : list) {
|
83 |
sb.append( " <Placemark>\n" );
|
84 |
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" );
|
85 |
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
|
86 |
sb.append( " </Placemark>\n" );
|
87 |
}
|
88 |
|
89 |
sb.append( "</Document>\n" );
|
90 |
sb.append( "</kml>\n" );
|
91 |
|
92 |
return sb.toString();
|
93 |
}
|
94 |
|
95 |
@Override
|
96 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
97 |
|
98 |
String data = formatXml( getRequestsFromFile() );
|
99 |
|
100 |
if (req.getParameter("zip") != null) {
|
101 |
|
102 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
103 |
|
104 |
ZipOutputStream zip = new ZipOutputStream(baos);
|
105 |
zip.putNextEntry( new ZipEntry("trains.kml") );
|
106 |
zip.write( data.getBytes() );
|
107 |
zip.closeEntry();
|
108 |
zip.close();
|
109 |
|
110 |
byte bytes[] = baos.toByteArray();
|
111 |
|
112 |
resp.setContentType(KMZ);
|
113 |
resp.setContentLength( bytes.length );
|
114 |
resp.getOutputStream().write(bytes);
|
115 |
|
116 |
} else {
|
117 |
resp.setContentType(KML);
|
118 |
resp.getWriter().print( data );
|
119 |
}
|
120 |
}
|
121 |
}
|