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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20