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