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

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 805 - (show 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 package dk.thoerup.traininfoservice;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayOutputStream;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.text.ParseException;
9 import java.text.SimpleDateFormat;
10 import java.util.ArrayList;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16 import java.util.zip.ZipEntry;
17 import java.util.zip.ZipOutputStream;
18
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 import dk.thoerup.traininfoservice.banedk.TimeoutMap;
25
26 public class RequestPlotter extends HttpServlet {
27 private static final long serialVersionUID = 1L;
28
29 static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
30
31 static final String KML = "application/vnd.google-earth.kml";
32 static final String KMZ = "application/vnd.google-earth.kmz";
33
34 Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000);
35
36 class RequestPosition {
37 public String ip;
38 public Date time;
39 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 try {
49 FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
50 BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
51
52 SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
53
54 String line;
55 while ( (line=in.readLine()) != null) {
56 if (line.indexOf("LocateStation") == -1 ){
57 continue;
58 }
59
60 if (line.indexOf("latitude") == -1 ) {
61 continue;
62 }
63 RequestPosition pos = new RequestPosition();
64
65 String toks[] = line.split(" ");
66 pos.ip = toks[0];
67
68 pos.time = df.parse( toks[3].replace("[", "") );
69
70 String argpart = toks[6].split("\\?")[1];
71
72 String args[] = argpart.split("&");
73
74 pos.lat = args[0].split("=")[1];
75 pos.lng = args[1].split("=")[1];
76
77 positions.add(pos);
78 }
79 } catch (ParseException pe) {
80 log.log(Level.SEVERE, "parseException", pe);
81 throw new IOException(pe);
82 } catch (IOException e) {
83 log.log(Level.SEVERE, "getKml()", e);
84 throw e;
85 }
86
87 return positions;
88 }
89
90 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
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
102 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 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
129 final String STYLE_GREEN = "green";
130 final String STYLE_YELLOW = "yellow";
131 final String STYLE_RED = "red";
132
133 String oldstyle = null;
134 Date now = new Date();
135 for(RequestPosition current : list) {
136
137 String style;
138 long timediff = now.getTime() - current.time.getTime();
139
140 if ( timediff < (3*60*60*1000) ) {
141 style = STYLE_RED;
142 } else if ( timediff < (24*60*60*1000)) {
143 style = STYLE_YELLOW;
144 } else {
145 style = STYLE_GREEN;
146 }
147
148 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 sb.append( " <Placemark>\n" );
158 sb.append( " <styleUrl>#" + style + "</styleUrl>\n" );
159 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 sb.append( "</Folder>\n" );
164 sb.append( "</Document>\n" );
165 sb.append( "</kml>\n" );
166
167 return sb.toString();
168 }
169
170 @Override
171 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
172 final String KEY = "kmldata";
173
174 String kmlData = cache.get(KEY);
175
176 if (kmlData == null) {
177 kmlData = formatXml( getRequestsFromFile() );
178 cache.put(KEY, kmlData);
179 kmlData += "<!-- from source -->";
180 } else {
181 kmlData += "<!-- cached -->";
182 }
183
184
185
186 if (req.getParameter("zip") != null) {
187
188 ByteArrayOutputStream baos = new ByteArrayOutputStream();
189
190 ZipOutputStream zip = new ZipOutputStream(baos);
191 zip.putNextEntry( new ZipEntry("trains.kml") );
192 zip.write( kmlData.getBytes() );
193 zip.closeEntry();
194 zip.close();
195
196 byte bytes[] = baos.toByteArray();
197
198 resp.setContentType(KMZ);
199 resp.setContentLength( bytes.length );
200 resp.getOutputStream().write(bytes);
201
202 } else {
203 resp.setContentType(KML);
204 resp.getWriter().print( kmlData );
205 }
206 }
207 }

  ViewVC Help
Powered by ViewVC 1.1.20