/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1311 - (show annotations) (download)
Tue Apr 19 16:31:11 2011 UTC (13 years, 1 month ago) by torben
File size: 12075 byte(s)
Now it should get the right gfv3 access log files
1 package dk.thoerup.traininfoservice;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FilenameFilter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Date;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20 import java.util.zip.GZIPInputStream;
21 import java.util.zip.ZipEntry;
22 import java.util.zip.ZipOutputStream;
23
24 import javax.servlet.ServletException;
25 import javax.servlet.annotation.WebServlet;
26 import javax.servlet.http.HttpServlet;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import dk.thoerup.traininfoservice.banedk.TimeoutMap;
31
32 @WebServlet(urlPatterns={"/RequestPlotter"})
33 public class RequestPlotter extends HttpServlet {
34 private static final long serialVersionUID = 1L;
35
36 static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
37
38 static final String KML = "application/vnd.google-earth.kml";
39 static final String KMZ = "application/vnd.google-earth.kmz";
40
41 Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000);
42
43 class RequestPosition {
44 public String ip;
45 public Date time;
46 public String lat;
47 public String lng;
48 }
49
50 class PositionContainer {
51 List<RequestPosition> blue = new ArrayList<RequestPosition>();
52 List<RequestPosition> green = new ArrayList<RequestPosition>();
53 List<RequestPosition> yellow = new ArrayList<RequestPosition>();
54 List<RequestPosition> red = new ArrayList<RequestPosition>();
55 }
56
57
58 boolean isGz(String fileStr) {
59 return fileStr.substring(fileStr.length() - 3).equals(".gz");
60 }
61
62 protected File[] getFiles(int count) {
63 File accessLogDir = new File("/home/app/domain1/logs/access/");
64 //File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/");
65
66 File logFiles[] = accessLogDir.listFiles( new FilenameFilter() {
67 @Override
68 public boolean accept(File dir, String name) {
69 //log.info("name:" + name);
70 return name.startsWith("server_access_log");
71 }
72 });
73
74 Arrays.sort(logFiles);
75
76 if (logFiles == null) {
77 File[] empty = {};
78 log.info("file array was empty");
79 return empty;
80 }
81
82 int from = logFiles.length - (count);
83 int to = logFiles.length;
84
85 return Arrays.copyOfRange(logFiles, from, to);
86 }
87
88 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
89 PositionContainer positions = new PositionContainer();
90
91 try {
92
93 int count;
94
95 if (multiple == false) {
96 count = 1;
97 } else {
98 count = 4;
99 }
100
101 File files[] = getFiles(count);
102
103 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
104
105 Date now = new Date();
106 for (File f : files ) {
107 log.info("Parsing file: " + f.getName() );
108
109 if ( !f.exists() ) {
110 continue;
111 }
112
113
114 InputStream input = new FileInputStream(f);
115
116
117 BufferedReader in = new BufferedReader( new InputStreamReader(input) );
118
119
120 String line;
121 while ( (line=in.readLine()) != null) {
122 if (line.indexOf("LocateStation") == -1 ){
123 continue;
124 }
125
126 if (line.indexOf("latitude=") == -1 ) {
127 continue;
128 }
129
130 if (line.indexOf("longitude=") == -1) {
131 continue;
132 }
133
134 RequestPosition pos = new RequestPosition();
135
136 String toks[] = line.split(" ");
137 pos.ip = toks[0].replaceAll("\"", "");
138
139 pos.time = df.parse( toks[2].replace("\"", "") );
140
141 String argpart = toks[5].split("\\?")[1];
142
143 String args[] = argpart.split("&");
144
145 pos.lat = args[0].split("=")[1];
146 pos.lng = args[1].split("=")[1];
147
148
149 long timediff = now.getTime() - pos.time.getTime();
150 if ( timediff < (3*60*60*1000) ) {
151 positions.red.add(pos); //RED
152 } else if ( timediff < (24*60*60*1000)) {
153 positions.yellow.add(pos); //YELLOW
154 } else if ( timediff < (7*24*60*60*1000)) {
155 positions.green.add(pos); //GREEN
156 } else {
157 positions.blue.add(pos); //BLUE
158 }
159
160 }
161 in.close();
162 input.close();
163
164 }
165 } catch (ParseException pe) {
166 log.log(Level.SEVERE, "parseException", pe);
167 throw new IOException(pe);
168 } catch (IOException e) {
169 log.log(Level.SEVERE, "getKml()", e);
170 throw e;
171 }
172
173 return positions;
174 }
175
176
177 /* old apache code
178 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
179 PositionContainer positions = new PositionContainer();
180
181 try {
182 String files_single[] = {"/var/log/apache2/app_access.log"};
183 String files_multi[] = {"/var/log/apache2/app_access.log.3.gz", "/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"};
184
185 String files[];
186
187 if (multiple == false) {
188 files = files_single;
189 } else {
190 files = files_multi;
191 }
192
193 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
194
195 Date now = new Date();
196 for (String fileStr : files ) {
197 File f = new File(fileStr);
198 if ( !f.exists() ) {
199 continue;
200 }
201
202 FileInputStream fis = new FileInputStream(fileStr);
203
204 InputStream input;
205 if ( isGz(fileStr)) {
206 input = new GZIPInputStream(fis);
207 } else {
208 input = fis;
209 }
210
211 BufferedReader in = new BufferedReader( new InputStreamReader(input) );
212
213
214 String line;
215 while ( (line=in.readLine()) != null) {
216 if (line.indexOf("LocateStation") == -1 ){
217 continue;
218 }
219
220 if (line.indexOf("latitude=") == -1 ) {
221 continue;
222 }
223
224 if (line.indexOf("longitude=") == -1) {
225 continue;
226 }
227
228 RequestPosition pos = new RequestPosition();
229
230 String toks[] = line.split(" ");
231 pos.ip = toks[0];
232
233 pos.time = df.parse( toks[3].replace("[", "") );
234
235 String argpart = toks[6].split("\\?")[1];
236
237 String args[] = argpart.split("&");
238
239 pos.lat = args[0].split("=")[1];
240 pos.lng = args[1].split("=")[1];
241
242
243 long timediff = now.getTime() - pos.time.getTime();
244 if ( timediff < (3*60*60*1000) ) {
245 positions.red.add(pos); //RED
246 } else if ( timediff < (24*60*60*1000)) {
247 positions.yellow.add(pos); //YELLOW
248 } else if ( timediff < (7*24*60*60*1000)) {
249 positions.green.add(pos); //GREEN
250 } else {
251 positions.blue.add(pos); //BLUE
252 }
253
254 }
255 in.close();
256 input.close();
257 fis.close();
258 }
259 } catch (ParseException pe) {
260 log.log(Level.SEVERE, "parseException", pe);
261 throw new IOException(pe);
262 } catch (IOException e) {
263 log.log(Level.SEVERE, "getKml()", e);
264 throw e;
265 }
266
267 return positions;
268 }*/
269
270 protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) {
271 sb.append( "<Folder>\n");
272 sb.append( " <name>" ).append(color).append("</name>\n");
273 sb.append( " <open>0</open>\n" );
274
275 int count=0;
276 for(RequestPosition current : list) {
277 String id = color + count++;
278 sb.append( " <Placemark id=\"" + id + "\">\n" );
279 sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" );
280 sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" );
281 sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
282 sb.append( " </Placemark>\n" );
283 }
284
285 sb.append("</Folder>\n");
286 }
287
288 protected String formatXml(PositionContainer positions) {
289 StringBuilder sb = new StringBuilder(1024*1024);
290
291 sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
292 sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
293 sb.append( "<Document>\n" );
294 sb.append( " <description><![CDATA[");
295 sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n");
296 sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n");
297 sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n");
298 sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n");
299 sb.append( " ]]></description>");
300
301
302 sb.append( " <Style id=\"red\">\n" );
303 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
304 sb.append( " </Style>\n");
305
306 sb.append( " <Style id=\"yellow\">\n" );
307 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
308 sb.append( " </Style>\n" );
309
310 sb.append( " <Style id=\"green\">\n" );
311 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
312 sb.append( " </Style>\n\n" );
313
314 sb.append( " <Style id=\"blue\">\n" );
315 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" );
316 sb.append( " </Style>\n\n" );
317 /*
318 String overlay =
319 " <ScreenOverlay>" +
320 " <name>Absolute Positioning: Top left</name>" +
321 " <description>Absolute Positioning: Top left</description>" +
322 " <visibility>1</visibility>" +
323 " <Icon>" +
324 " <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
325 " </Icon>" +
326 " <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
327 " <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
328 " <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
329 " <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
330 " </ScreenOverlay>" ;
331
332 sb.append(overlay);
333 */
334
335
336 formatPositions(sb, "blue", positions.blue);
337 formatPositions(sb, "green", positions.green);
338 formatPositions(sb, "yellow", positions.yellow);
339 formatPositions(sb, "red", positions.red);
340
341
342 sb.append( "</Document>\n" );
343 sb.append( "</kml>\n" );
344
345 return sb.toString();
346 }
347
348 protected String getRequestsFromFile(boolean multiple) throws IOException {
349 String kmlData = null;
350 String key;
351
352 if (multiple == false) {
353 key = "kmldata";
354 } else {
355 key = "kmldata-multi";
356 }
357
358 kmlData = null;// = cache.get(key);
359
360 if (kmlData == null) {
361 kmlData = formatXml( getRequestsFromFileWorker(multiple) );
362 cache.put(key, kmlData);
363 kmlData += "<!-- from source -->";
364 } else {
365 kmlData += "<!-- cached -->";
366 }
367
368 return kmlData;
369 }
370
371 boolean enabled(String param) {
372 if (param == null || param.equals("")) {
373 return false;
374 }
375
376 int p = 0;
377 try {
378 p = Integer.parseInt(param);
379 } catch (Exception e) {}
380
381 return (p != 0);
382 }
383
384 @Override
385 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
386
387 boolean multiple = enabled( req.getParameter("multi") );
388
389 String kmlData = getRequestsFromFile(multiple);
390
391 if ( enabled(req.getParameter("zip")) ) {
392
393 ByteArrayOutputStream baos = new ByteArrayOutputStream();
394
395 ZipOutputStream zip = new ZipOutputStream(baos);
396 zip.putNextEntry( new ZipEntry("trains.kml") );
397 zip.write( kmlData.getBytes() );
398 zip.closeEntry();
399 zip.close();
400
401 byte bytes[] = baos.toByteArray();
402
403 resp.setContentType(KMZ);
404 resp.setContentLength( bytes.length );
405 resp.getOutputStream().write(bytes);
406
407 } else {
408 resp.setContentType(KML);
409 resp.getWriter().print( kmlData );
410 }
411 }
412 }

  ViewVC Help
Powered by ViewVC 1.1.20