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

  ViewVC Help
Powered by ViewVC 1.1.20