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

  ViewVC Help
Powered by ViewVC 1.1.20