/[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 1311 - (hide 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 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 torben 1311 //File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/");
65 torben 1310
66     File logFiles[] = accessLogDir.listFiles( new FilenameFilter() {
67     @Override
68     public boolean accept(File dir, String name) {
69 torben 1311 //log.info("name:" + name);
70 torben 1310 return name.startsWith("server_access_log");
71     }
72     });
73    
74 torben 1311 Arrays.sort(logFiles);
75    
76 torben 1310 if (logFiles == null) {
77     File[] empty = {};
78 torben 1311 log.info("file array was empty");
79 torben 1310 return empty;
80     }
81    
82 torben 1311 int from = logFiles.length - (count);
83     int to = logFiles.length;
84 torben 1310
85     return Arrays.copyOfRange(logFiles, from, to);
86     }
87    
88     protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
89     PositionContainer positions = new PositionContainer();
90 torben 799
91 torben 1310 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 torben 892 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
179     PositionContainer positions = new PositionContainer();
180 torben 799
181 torben 795 try {
182 torben 809 String files_single[] = {"/var/log/apache2/app_access.log"};
183 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"};
184 torben 809
185     String files[];
186    
187     if (multiple == false) {
188     files = files_single;
189     } else {
190     files = files_multi;
191     }
192 torben 799
193 torben 800 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
194 torben 892
195     Date now = new Date();
196 torben 809 for (String fileStr : files ) {
197     File f = new File(fileStr);
198     if ( !f.exists() ) {
199 torben 795 continue;
200     }
201 torben 809
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 torben 799
213 torben 809
214     String line;
215     while ( (line=in.readLine()) != null) {
216     if (line.indexOf("LocateStation") == -1 ){
217     continue;
218     }
219    
220 torben 818 if (line.indexOf("latitude=") == -1 ) {
221 torben 809 continue;
222     }
223 torben 818
224     if (line.indexOf("longitude=") == -1) {
225     continue;
226     }
227    
228 torben 809 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 torben 892
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 torben 987 } else if ( timediff < (7*24*60*60*1000)) {
249     positions.green.add(pos); //GREEN
250 torben 892 } else {
251 torben 987 positions.blue.add(pos); //BLUE
252 torben 892 }
253 torben 809
254 torben 795 }
255 torben 809 in.close();
256     input.close();
257     fis.close();
258 torben 795 }
259 torben 800 } catch (ParseException pe) {
260     log.log(Level.SEVERE, "parseException", pe);
261     throw new IOException(pe);
262 torben 799 } catch (IOException e) {
263 torben 796 log.log(Level.SEVERE, "getKml()", e);
264 torben 799 throw e;
265 torben 795 }
266 torben 799
267     return positions;
268 torben 1310 }*/
269 torben 892
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 torben 1000 int count=0;
276     for(RequestPosition current : list) {
277     String id = color + count++;
278     sb.append( " <Placemark id=\"" + id + "\">\n" );
279 torben 896 sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" );
280 torben 998 sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" );
281 torben 896 sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
282 torben 892 sb.append( " </Placemark>\n" );
283     }
284    
285     sb.append("</Folder>\n");
286     }
287 torben 795
288 torben 892 protected String formatXml(PositionContainer positions) {
289 torben 897 StringBuilder sb = new StringBuilder(1024*1024);
290 torben 799
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 torben 941 sb.append( " <description><![CDATA[");
295 torben 987 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 torben 941 sb.append( " ]]></description>");
300 torben 800
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 torben 799
306 torben 800 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 torben 801 sb.append( " </Style>\n\n" );
313    
314 torben 987 sb.append( " <Style id=\"blue\">\n" );
315 torben 988 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" );
316 torben 987 sb.append( " </Style>\n\n" );
317 torben 801 /*
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 torben 800
336 torben 987 formatPositions(sb, "blue", positions.blue);
337 torben 892 formatPositions(sb, "green", positions.green);
338 torben 893 formatPositions(sb, "yellow", positions.yellow);
339 torben 892 formatPositions(sb, "red", positions.red);
340    
341 torben 800
342 torben 805 sb.append( "</Document>\n" );
343 torben 799 sb.append( "</kml>\n" );
344    
345     return sb.toString();
346     }
347 torben 809
348     protected String getRequestsFromFile(boolean multiple) throws IOException {
349     String kmlData = null;
350     String key;
351 torben 802
352 torben 809 if (multiple == false) {
353     key = "kmldata";
354     } else {
355     key = "kmldata-multi";
356     }
357    
358 torben 1311 kmlData = null;// = cache.get(key);
359 torben 809
360 torben 803 if (kmlData == null) {
361 torben 809 kmlData = formatXml( getRequestsFromFileWorker(multiple) );
362     cache.put(key, kmlData);
363 torben 803 kmlData += "<!-- from source -->";
364 torben 802 } else {
365 torben 803 kmlData += "<!-- cached -->";
366 torben 802 }
367 torben 809
368     return kmlData;
369     }
370 torben 810
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 torben 799
384 torben 809 @Override
385     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
386 torben 802
387 torben 810 boolean multiple = enabled( req.getParameter("multi") );
388 torben 809
389     String kmlData = getRequestsFromFile(multiple);
390    
391 torben 810 if ( enabled(req.getParameter("zip")) ) {
392 torben 799
393 torben 798 ByteArrayOutputStream baos = new ByteArrayOutputStream();
394 torben 799
395 torben 798 ZipOutputStream zip = new ZipOutputStream(baos);
396     zip.putNextEntry( new ZipEntry("trains.kml") );
397 torben 803 zip.write( kmlData.getBytes() );
398 torben 798 zip.closeEntry();
399     zip.close();
400 torben 799
401 torben 798 byte bytes[] = baos.toByteArray();
402 torben 799
403 torben 798 resp.setContentType(KMZ);
404     resp.setContentLength( bytes.length );
405     resp.getOutputStream().write(bytes);
406 torben 799
407 torben 798 } else {
408     resp.setContentType(KML);
409 torben 803 resp.getWriter().print( kmlData );
410 torben 798 }
411 torben 795 }
412     }

  ViewVC Help
Powered by ViewVC 1.1.20