1 |
torben |
795 |
package dk.thoerup.traininfoservice;
|
2 |
|
|
|
3 |
|
|
import java.io.BufferedReader;
|
4 |
torben |
798 |
import java.io.ByteArrayOutputStream;
|
5 |
torben |
795 |
import java.io.FileInputStream;
|
6 |
|
|
import java.io.IOException;
|
7 |
|
|
import java.io.InputStreamReader;
|
8 |
torben |
800 |
import java.text.ParseException;
|
9 |
|
|
import java.text.SimpleDateFormat;
|
10 |
torben |
799 |
import java.util.ArrayList;
|
11 |
torben |
800 |
import java.util.Date;
|
12 |
torben |
799 |
import java.util.List;
|
13 |
torben |
796 |
import java.util.logging.Level;
|
14 |
|
|
import java.util.logging.Logger;
|
15 |
torben |
798 |
import java.util.zip.ZipEntry;
|
16 |
|
|
import java.util.zip.ZipOutputStream;
|
17 |
torben |
795 |
|
18 |
|
|
import javax.servlet.ServletException;
|
19 |
|
|
import javax.servlet.http.HttpServlet;
|
20 |
|
|
import javax.servlet.http.HttpServletRequest;
|
21 |
|
|
import javax.servlet.http.HttpServletResponse;
|
22 |
|
|
|
23 |
|
|
public class RequestPlotter extends HttpServlet {
|
24 |
|
|
private static final long serialVersionUID = 1L;
|
25 |
torben |
799 |
|
26 |
torben |
796 |
static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
|
27 |
torben |
799 |
|
28 |
torben |
798 |
static final String KML = "application/vnd.google-earth.kml";
|
29 |
|
|
static final String KMZ = "application/vnd.google-earth.kmz";
|
30 |
torben |
799 |
|
31 |
|
|
class RequestPosition {
|
32 |
|
|
public String ip;
|
33 |
torben |
800 |
public Date time;
|
34 |
torben |
799 |
public String lat;
|
35 |
|
|
public String lng;
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
protected List<RequestPosition> getRequestsFromFile() throws IOException{
|
41 |
|
|
List<RequestPosition> positions = new ArrayList<RequestPosition>();
|
42 |
|
|
|
43 |
torben |
795 |
try {
|
44 |
torben |
797 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
|
45 |
torben |
795 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
|
46 |
torben |
799 |
|
47 |
torben |
800 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
|
48 |
|
|
|
49 |
torben |
795 |
String line;
|
50 |
|
|
while ( (line=in.readLine()) != null) {
|
51 |
|
|
if (line.indexOf("LocateStation") == -1 ){
|
52 |
|
|
continue;
|
53 |
|
|
}
|
54 |
torben |
799 |
|
55 |
torben |
795 |
if (line.indexOf("latitude") == -1 ) {
|
56 |
|
|
continue;
|
57 |
|
|
}
|
58 |
torben |
799 |
RequestPosition pos = new RequestPosition();
|
59 |
|
|
|
60 |
torben |
795 |
String toks[] = line.split(" ");
|
61 |
torben |
799 |
pos.ip = toks[0];
|
62 |
torben |
800 |
|
63 |
|
|
pos.time = df.parse( toks[3].replace("[", "") );
|
64 |
|
|
|
65 |
torben |
798 |
String argpart = toks[6].split("\\?")[1];
|
66 |
|
|
|
67 |
torben |
795 |
String args[] = argpart.split("&");
|
68 |
torben |
799 |
|
69 |
|
|
pos.lat = args[0].split("=")[1];
|
70 |
|
|
pos.lng = args[1].split("=")[1];
|
71 |
torben |
800 |
|
72 |
torben |
799 |
positions.add(pos);
|
73 |
torben |
795 |
}
|
74 |
torben |
800 |
} catch (ParseException pe) {
|
75 |
|
|
log.log(Level.SEVERE, "parseException", pe);
|
76 |
|
|
throw new IOException(pe);
|
77 |
torben |
799 |
} catch (IOException e) {
|
78 |
torben |
796 |
log.log(Level.SEVERE, "getKml()", e);
|
79 |
torben |
799 |
throw e;
|
80 |
torben |
795 |
}
|
81 |
torben |
799 |
|
82 |
|
|
return positions;
|
83 |
torben |
795 |
}
|
84 |
|
|
|
85 |
torben |
799 |
protected String formatXml(List<RequestPosition> list) {
|
86 |
|
|
StringBuilder sb = new StringBuilder();
|
87 |
|
|
|
88 |
|
|
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
|
89 |
|
|
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
|
90 |
|
|
sb.append( "<Document>\n" );
|
91 |
torben |
800 |
|
92 |
|
|
|
93 |
|
|
sb.append( " <Style id=\"red\">\n" );
|
94 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
|
95 |
|
|
sb.append( " </Style>\n");
|
96 |
torben |
799 |
|
97 |
torben |
800 |
sb.append( " <Style id=\"yellow\">\n" );
|
98 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
|
99 |
|
|
sb.append( " </Style>\n" );
|
100 |
|
|
|
101 |
|
|
sb.append( " <Style id=\"green\">\n" );
|
102 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
|
103 |
|
|
sb.append( " </Style>\n\n" );
|
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
Date now = new Date();
|
109 |
torben |
799 |
for(RequestPosition current : list) {
|
110 |
torben |
800 |
|
111 |
|
|
String style;
|
112 |
|
|
long timediff = now.getTime() - current.time.getTime();
|
113 |
|
|
|
114 |
|
|
if ( timediff < (3*60*60*1000) ) {
|
115 |
|
|
style = "#red";
|
116 |
|
|
} else if ( timediff < (24*60*60*1000)) {
|
117 |
|
|
style = "#yellow";
|
118 |
|
|
} else {
|
119 |
|
|
style = "#green";
|
120 |
|
|
}
|
121 |
|
|
|
122 |
torben |
799 |
sb.append( " <Placemark>\n" );
|
123 |
torben |
800 |
sb.append( " <styleUrl>" + style + "</styleUrl>\n" );
|
124 |
torben |
799 |
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" );
|
125 |
|
|
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
|
126 |
|
|
sb.append( " </Placemark>\n" );
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
sb.append( "</Document>\n" );
|
130 |
|
|
sb.append( "</kml>\n" );
|
131 |
|
|
|
132 |
|
|
return sb.toString();
|
133 |
|
|
}
|
134 |
|
|
|
135 |
torben |
795 |
@Override
|
136 |
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
137 |
torben |
799 |
|
138 |
|
|
String data = formatXml( getRequestsFromFile() );
|
139 |
|
|
|
140 |
torben |
798 |
if (req.getParameter("zip") != null) {
|
141 |
torben |
799 |
|
142 |
torben |
798 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
143 |
torben |
799 |
|
144 |
torben |
798 |
ZipOutputStream zip = new ZipOutputStream(baos);
|
145 |
|
|
zip.putNextEntry( new ZipEntry("trains.kml") );
|
146 |
|
|
zip.write( data.getBytes() );
|
147 |
|
|
zip.closeEntry();
|
148 |
|
|
zip.close();
|
149 |
torben |
799 |
|
150 |
torben |
798 |
byte bytes[] = baos.toByteArray();
|
151 |
torben |
799 |
|
152 |
torben |
798 |
resp.setContentType(KMZ);
|
153 |
|
|
resp.setContentLength( bytes.length );
|
154 |
|
|
resp.getOutputStream().write(bytes);
|
155 |
torben |
799 |
|
156 |
torben |
798 |
} else {
|
157 |
|
|
resp.setContentType(KML);
|
158 |
torben |
799 |
resp.getWriter().print( data );
|
159 |
torben |
798 |
}
|
160 |
torben |
795 |
}
|
161 |
|
|
}
|