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

  ViewVC Help
Powered by ViewVC 1.1.20