/[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 805 - (hide annotations) (download)
Mon Jun 7 18:26:09 2010 UTC (13 years, 11 months ago) by torben
File size: 6265 byte(s)
Sort placemarks into folders
1 torben 795 package dk.thoerup.traininfoservice;
2    
3     import java.io.BufferedReader;
4 torben 798 import java.io.ByteArrayOutputStream;
5 torben 795 import java.io.FileInputStream;
6     import java.io.IOException;
7     import java.io.InputStreamReader;
8 torben 800 import java.text.ParseException;
9     import java.text.SimpleDateFormat;
10 torben 799 import java.util.ArrayList;
11 torben 800 import java.util.Date;
12 torben 799 import java.util.List;
13 torben 802 import java.util.Map;
14 torben 796 import java.util.logging.Level;
15     import java.util.logging.Logger;
16 torben 798 import java.util.zip.ZipEntry;
17     import java.util.zip.ZipOutputStream;
18 torben 795
19     import javax.servlet.ServletException;
20     import javax.servlet.http.HttpServlet;
21     import javax.servlet.http.HttpServletRequest;
22     import javax.servlet.http.HttpServletResponse;
23    
24 torben 802 import dk.thoerup.traininfoservice.banedk.TimeoutMap;
25    
26 torben 795 public class RequestPlotter extends HttpServlet {
27     private static final long serialVersionUID = 1L;
28 torben 799
29 torben 796 static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
30 torben 799
31 torben 798 static final String KML = "application/vnd.google-earth.kml";
32     static final String KMZ = "application/vnd.google-earth.kmz";
33 torben 802
34     Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000);
35 torben 799
36     class RequestPosition {
37     public String ip;
38 torben 800 public Date time;
39 torben 799 public String lat;
40     public String lng;
41     }
42    
43    
44    
45     protected List<RequestPosition> getRequestsFromFile() throws IOException{
46     List<RequestPosition> positions = new ArrayList<RequestPosition>();
47    
48 torben 795 try {
49 torben 797 FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
50 torben 795 BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
51 torben 799
52 torben 800 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
53    
54 torben 795 String line;
55     while ( (line=in.readLine()) != null) {
56     if (line.indexOf("LocateStation") == -1 ){
57     continue;
58     }
59 torben 799
60 torben 795 if (line.indexOf("latitude") == -1 ) {
61     continue;
62     }
63 torben 799 RequestPosition pos = new RequestPosition();
64    
65 torben 795 String toks[] = line.split(" ");
66 torben 799 pos.ip = toks[0];
67 torben 800
68     pos.time = df.parse( toks[3].replace("[", "") );
69    
70 torben 798 String argpart = toks[6].split("\\?")[1];
71    
72 torben 795 String args[] = argpart.split("&");
73 torben 799
74     pos.lat = args[0].split("=")[1];
75     pos.lng = args[1].split("=")[1];
76 torben 800
77 torben 799 positions.add(pos);
78 torben 795 }
79 torben 800 } catch (ParseException pe) {
80     log.log(Level.SEVERE, "parseException", pe);
81     throw new IOException(pe);
82 torben 799 } catch (IOException e) {
83 torben 796 log.log(Level.SEVERE, "getKml()", e);
84 torben 799 throw e;
85 torben 795 }
86 torben 799
87     return positions;
88 torben 795 }
89    
90 torben 799 protected String formatXml(List<RequestPosition> list) {
91     StringBuilder sb = new StringBuilder();
92    
93     sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
94     sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
95     sb.append( "<Document>\n" );
96 torben 800
97    
98     sb.append( " <Style id=\"red\">\n" );
99     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
100     sb.append( " </Style>\n");
101 torben 799
102 torben 800 sb.append( " <Style id=\"yellow\">\n" );
103     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
104     sb.append( " </Style>\n" );
105    
106     sb.append( " <Style id=\"green\">\n" );
107     sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
108 torben 801 sb.append( " </Style>\n\n" );
109    
110     /*
111     String overlay =
112     " <ScreenOverlay>" +
113     " <name>Absolute Positioning: Top left</name>" +
114     " <description>Absolute Positioning: Top left</description>" +
115     " <visibility>1</visibility>" +
116     " <Icon>" +
117     " <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
118     " </Icon>" +
119     " <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
120     " <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
121     " <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
122     " <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
123     " </ScreenOverlay>" ;
124    
125     sb.append(overlay);
126     */
127    
128 torben 800
129 torben 805 final String STYLE_GREEN = "green";
130     final String STYLE_YELLOW = "yellow";
131     final String STYLE_RED = "red";
132 torben 800
133 torben 805 String oldstyle = null;
134 torben 800 Date now = new Date();
135 torben 799 for(RequestPosition current : list) {
136 torben 800
137     String style;
138     long timediff = now.getTime() - current.time.getTime();
139    
140     if ( timediff < (3*60*60*1000) ) {
141 torben 805 style = STYLE_RED;
142 torben 800 } else if ( timediff < (24*60*60*1000)) {
143 torben 805 style = STYLE_YELLOW;
144 torben 800 } else {
145 torben 805 style = STYLE_GREEN;
146 torben 800 }
147    
148 torben 805 if ( !style.equals(oldstyle) ) {
149     if (oldstyle != null)
150     sb.append( "</Folder>\n");
151     sb.append( "<Folder>\n");
152     sb.append( " <name>" ).append(style).append("</name>\n");
153    
154     oldstyle = style;
155     }
156    
157 torben 799 sb.append( " <Placemark>\n" );
158 torben 805 sb.append( " <styleUrl>#" + style + "</styleUrl>\n" );
159 torben 799 sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" );
160     sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
161     sb.append( " </Placemark>\n" );
162     }
163 torben 805 sb.append( "</Folder>\n" );
164     sb.append( "</Document>\n" );
165 torben 799 sb.append( "</kml>\n" );
166    
167     return sb.toString();
168     }
169    
170 torben 795 @Override
171     protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
172 torben 803 final String KEY = "kmldata";
173 torben 802
174 torben 803 String kmlData = cache.get(KEY);
175 torben 802
176 torben 803 if (kmlData == null) {
177     kmlData = formatXml( getRequestsFromFile() );
178     cache.put(KEY, kmlData);
179     kmlData += "<!-- from source -->";
180 torben 802 } else {
181 torben 803 kmlData += "<!-- cached -->";
182 torben 802 }
183 torben 799
184 torben 802
185 torben 799
186 torben 798 if (req.getParameter("zip") != null) {
187 torben 799
188 torben 798 ByteArrayOutputStream baos = new ByteArrayOutputStream();
189 torben 799
190 torben 798 ZipOutputStream zip = new ZipOutputStream(baos);
191     zip.putNextEntry( new ZipEntry("trains.kml") );
192 torben 803 zip.write( kmlData.getBytes() );
193 torben 798 zip.closeEntry();
194     zip.close();
195 torben 799
196 torben 798 byte bytes[] = baos.toByteArray();
197 torben 799
198 torben 798 resp.setContentType(KMZ);
199     resp.setContentLength( bytes.length );
200     resp.getOutputStream().write(bytes);
201 torben 799
202 torben 798 } else {
203     resp.setContentType(KML);
204 torben 803 resp.getWriter().print( kmlData );
205 torben 798 }
206 torben 795 }
207     }

  ViewVC Help
Powered by ViewVC 1.1.20