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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20