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

  ViewVC Help
Powered by ViewVC 1.1.20