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