/[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 1000 - (hide annotations) (download)
Sat Jul 17 06:29:04 2010 UTC (13 years, 10 months ago) by torben
File size: 9232 byte(s)
Set an id to please KML validator http://feedvalidator.org/
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     import java.io.IOException;
8 torben 809 import java.io.InputStream;
9 torben 795 import java.io.InputStreamReader;
10 torben 800 import java.text.ParseException;
11     import java.text.SimpleDateFormat;
12 torben 799 import java.util.ArrayList;
13 torben 800 import java.util.Date;
14 torben 799 import java.util.List;
15 torben 802 import java.util.Map;
16 torben 796 import java.util.logging.Level;
17     import java.util.logging.Logger;
18 torben 809 import java.util.zip.GZIPInputStream;
19 torben 798 import java.util.zip.ZipEntry;
20     import java.util.zip.ZipOutputStream;
21 torben 795
22     import javax.servlet.ServletException;
23 torben 958 import javax.servlet.annotation.WebListener;
24     import javax.servlet.annotation.WebServlet;
25 torben 795 import javax.servlet.http.HttpServlet;
26     import javax.servlet.http.HttpServletRequest;
27     import javax.servlet.http.HttpServletResponse;
28    
29 torben 802 import dk.thoerup.traininfoservice.banedk.TimeoutMap;
30    
31 torben 958 @WebServlet(urlPatterns={"/RequestPlotter"})
32 torben 795 public class RequestPlotter extends HttpServlet {
33     private static final long serialVersionUID = 1L;
34 torben 799
35 torben 796 static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
36 torben 799
37 torben 798 static final String KML = "application/vnd.google-earth.kml";
38     static final String KMZ = "application/vnd.google-earth.kmz";
39 torben 802
40 torben 954 Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000);
41 torben 799
42     class RequestPosition {
43     public String ip;
44 torben 800 public Date time;
45 torben 799 public String lat;
46     public String lng;
47     }
48 torben 892
49     class PositionContainer {
50 torben 987 List<RequestPosition> blue = new ArrayList<RequestPosition>();
51 torben 892 List<RequestPosition> green = new ArrayList<RequestPosition>();
52     List<RequestPosition> yellow = new ArrayList<RequestPosition>();
53     List<RequestPosition> red = new ArrayList<RequestPosition>();
54     }
55 torben 799
56    
57 torben 809 boolean isGz(String fileStr) {
58     return fileStr.substring(fileStr.length() - 3).equals(".gz");
59     }
60 torben 799
61 torben 892 protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
62     PositionContainer positions = new PositionContainer();
63 torben 799
64 torben 795 try {
65 torben 809 String files_single[] = {"/var/log/apache2/app_access.log"};
66 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"};
67 torben 809
68     String files[];
69    
70     if (multiple == false) {
71     files = files_single;
72     } else {
73     files = files_multi;
74     }
75 torben 799
76 torben 800 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
77 torben 892
78     Date now = new Date();
79 torben 809 for (String fileStr : files ) {
80     File f = new File(fileStr);
81     if ( !f.exists() ) {
82 torben 795 continue;
83     }
84 torben 809
85     FileInputStream fis = new FileInputStream(fileStr);
86    
87     InputStream input;
88     if ( isGz(fileStr)) {
89     input = new GZIPInputStream(fis);
90     } else {
91     input = fis;
92     }
93    
94     BufferedReader in = new BufferedReader( new InputStreamReader(input) );
95 torben 799
96 torben 809
97     String line;
98     while ( (line=in.readLine()) != null) {
99     if (line.indexOf("LocateStation") == -1 ){
100     continue;
101     }
102    
103 torben 818 if (line.indexOf("latitude=") == -1 ) {
104 torben 809 continue;
105     }
106 torben 818
107     if (line.indexOf("longitude=") == -1) {
108     continue;
109     }
110    
111 torben 809 RequestPosition pos = new RequestPosition();
112    
113     String toks[] = line.split(" ");
114     pos.ip = toks[0];
115    
116     pos.time = df.parse( toks[3].replace("[", "") );
117    
118     String argpart = toks[6].split("\\?")[1];
119    
120     String args[] = argpart.split("&");
121    
122     pos.lat = args[0].split("=")[1];
123     pos.lng = args[1].split("=")[1];
124 torben 892
125    
126     long timediff = now.getTime() - pos.time.getTime();
127     if ( timediff < (3*60*60*1000) ) {
128     positions.red.add(pos); //RED
129     } else if ( timediff < (24*60*60*1000)) {
130     positions.yellow.add(pos); //YELLOW
131 torben 987 } else if ( timediff < (7*24*60*60*1000)) {
132     positions.green.add(pos); //GREEN
133 torben 892 } else {
134 torben 987 positions.blue.add(pos); //BLUE
135 torben 892 }
136 torben 809
137 torben 795 }
138 torben 809 in.close();
139     input.close();
140     fis.close();
141 torben 795 }
142 torben 800 } catch (ParseException pe) {
143     log.log(Level.SEVERE, "parseException", pe);
144     throw new IOException(pe);
145 torben 799 } catch (IOException e) {
146 torben 796 log.log(Level.SEVERE, "getKml()", e);
147 torben 799 throw e;
148 torben 795 }
149 torben 799
150     return positions;
151 torben 795 }
152 torben 892
153     protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) {
154     sb.append( "<Folder>\n");
155     sb.append( " <name>" ).append(color).append("</name>\n");
156     sb.append( " <open>0</open>\n" );
157    
158 torben 1000 int count=0;
159     for(RequestPosition current : list) {
160     String id = color + count++;
161     sb.append( " <Placemark id=\"" + id + "\">\n" );
162 torben 896 sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" );
163 torben 998 sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" );
164 torben 896 sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" );
165 torben 892 sb.append( " </Placemark>\n" );
166     }
167    
168     sb.append("</Folder>\n");
169     }
170 torben 795
171 torben 892 protected String formatXml(PositionContainer positions) {
172 torben 897 StringBuilder sb = new StringBuilder(1024*1024);
173 torben 799
174     sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
175     sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
176     sb.append( "<Document>\n" );
177 torben 941 sb.append( " <description><![CDATA[");
178 torben 987 sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n");
179     sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n");
180     sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n");
181     sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n");
182 torben 941 sb.append( " ]]></description>");
183 torben 800
184    
185     sb.append( " <Style id=\"red\">\n" );
186     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
187     sb.append( " </Style>\n");
188 torben 799
189 torben 800 sb.append( " <Style id=\"yellow\">\n" );
190     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
191     sb.append( " </Style>\n" );
192    
193     sb.append( " <Style id=\"green\">\n" );
194     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
195 torben 801 sb.append( " </Style>\n\n" );
196    
197 torben 987 sb.append( " <Style id=\"blue\">\n" );
198 torben 988 sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" );
199 torben 987 sb.append( " </Style>\n\n" );
200 torben 801 /*
201     String overlay =
202     " <ScreenOverlay>" +
203     " <name>Absolute Positioning: Top left</name>" +
204     " <description>Absolute Positioning: Top left</description>" +
205     " <visibility>1</visibility>" +
206     " <Icon>" +
207     " <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
208     " </Icon>" +
209     " <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
210     " <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
211     " <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
212     " <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
213     " </ScreenOverlay>" ;
214    
215     sb.append(overlay);
216     */
217    
218 torben 800
219 torben 987 formatPositions(sb, "blue", positions.blue);
220 torben 892 formatPositions(sb, "green", positions.green);
221 torben 893 formatPositions(sb, "yellow", positions.yellow);
222 torben 892 formatPositions(sb, "red", positions.red);
223    
224 torben 800
225 torben 805 sb.append( "</Document>\n" );
226 torben 799 sb.append( "</kml>\n" );
227    
228     return sb.toString();
229     }
230 torben 809
231     protected String getRequestsFromFile(boolean multiple) throws IOException {
232     String kmlData = null;
233     String key;
234 torben 802
235 torben 809 if (multiple == false) {
236     key = "kmldata";
237     } else {
238     key = "kmldata-multi";
239     }
240    
241     kmlData = cache.get(key);
242    
243 torben 803 if (kmlData == null) {
244 torben 809 kmlData = formatXml( getRequestsFromFileWorker(multiple) );
245     cache.put(key, kmlData);
246 torben 803 kmlData += "<!-- from source -->";
247 torben 802 } else {
248 torben 803 kmlData += "<!-- cached -->";
249 torben 802 }
250 torben 809
251     return kmlData;
252     }
253 torben 810
254     boolean enabled(String param) {
255     if (param == null || param.equals("")) {
256     return false;
257     }
258    
259     int p = 0;
260     try {
261     p = Integer.parseInt(param);
262     } catch (Exception e) {}
263    
264     return (p != 0);
265     }
266 torben 799
267 torben 809 @Override
268     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
269 torben 802
270 torben 810 boolean multiple = enabled( req.getParameter("multi") );
271 torben 809
272     String kmlData = getRequestsFromFile(multiple);
273    
274 torben 810 if ( enabled(req.getParameter("zip")) ) {
275 torben 799
276 torben 798 ByteArrayOutputStream baos = new ByteArrayOutputStream();
277 torben 799
278 torben 798 ZipOutputStream zip = new ZipOutputStream(baos);
279     zip.putNextEntry( new ZipEntry("trains.kml") );
280 torben 803 zip.write( kmlData.getBytes() );
281 torben 798 zip.closeEntry();
282     zip.close();
283 torben 799
284 torben 798 byte bytes[] = baos.toByteArray();
285 torben 799
286 torben 798 resp.setContentType(KMZ);
287     resp.setContentLength( bytes.length );
288     resp.getOutputStream().write(bytes);
289 torben 799
290 torben 798 } else {
291     resp.setContentType(KML);
292 torben 803 resp.getWriter().print( kmlData );
293 torben 798 }
294 torben 795 }
295     }

  ViewVC Help
Powered by ViewVC 1.1.20