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; |
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; |
import javax.servlet.ServletException; |
23 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
24 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
25 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
26 |
|
|
27 |
|
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
28 |
|
|
29 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
30 |
private static final long serialVersionUID = 1L; |
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 |
protected String getKml() { |
Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000); |
38 |
StringBuilder sb = new StringBuilder(); |
|
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 { |
try { |
61 |
FileInputStream fis = new FileInputStream("/var/log/apache2/access.log"); |
String files_single[] = {"/var/log/apache2/app_access.log"}; |
62 |
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"}; |
63 |
|
|
64 |
|
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" ); |
|
65 |
|
|
66 |
String line; |
if (multiple == false) { |
67 |
while ( (line=in.readLine()) != null) { |
files = files_single; |
68 |
if (line.indexOf("LocateStation") == -1 ){ |
} else { |
69 |
continue; |
files = files_multi; |
70 |
} |
} |
71 |
|
|
72 |
if (line.indexOf("latitude") == -1 ) { |
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; |
continue; |
79 |
} |
} |
80 |
|
|
81 |
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("&"); |
|
82 |
|
|
83 |
String lat = args[0].split("=")[1]; |
InputStream input; |
84 |
String lng = args[0].split("=")[1]; |
if ( isGz(fileStr)) { |
85 |
|
input = new GZIPInputStream(fis); |
86 |
sb.append( " <Placemark>" ); |
} else { |
87 |
sb.append( " <description>IP=" + ip + " Time=" + time + "</description>" ); |
input = fis; |
88 |
sb.append( " <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>" ); |
} |
|
sb.append( " </Placemark>" ); |
|
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 |
sb.append( "</Document>\n" ); |
log.log(Level.SEVERE, "parseException", pe); |
138 |
sb.append( "</kml>\n" ); |
throw new IOException(pe); |
139 |
|
} catch (IOException e) { |
140 |
} catch (Exception e) { |
log.log(Level.SEVERE, "getKml()", e); |
141 |
sb.append("<!-- error -->"); |
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 |
|
sb.append( " <description>Count=").append(list.size()).append("</description>\n"); |
152 |
|
|
153 |
|
for(RequestPosition current : list) { |
154 |
|
sb.append( " <Placemark>\n" ); |
155 |
|
sb.append( " <styleUrl>#" + color + "</styleUrl>\n" ); |
156 |
|
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
157 |
|
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
158 |
|
sb.append( " </Placemark>\n" ); |
159 |
|
} |
160 |
|
|
161 |
return sb.toString(); |
sb.append("</Folder>\n"); |
162 |
} |
} |
163 |
|
|
164 |
@Override |
protected String formatXml(PositionContainer positions) { |
165 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
StringBuilder sb = new StringBuilder(); |
166 |
resp.setContentType("application/vnd.google-earth.kml"); |
|
167 |
resp.getWriter().print( getKml() ); |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
168 |
|
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
169 |
|
sb.append( "<Document>\n" ); |
170 |
|
|
171 |
|
|
172 |
|
sb.append( " <Style id=\"red\">\n" ); |
173 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" ); |
174 |
|
sb.append( " </Style>\n"); |
175 |
|
|
176 |
|
sb.append( " <Style id=\"yellow\">\n" ); |
177 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" ); |
178 |
|
sb.append( " </Style>\n" ); |
179 |
|
|
180 |
|
sb.append( " <Style id=\"green\">\n" ); |
181 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" ); |
182 |
|
sb.append( " </Style>\n\n" ); |
183 |
|
|
184 |
|
/* |
185 |
|
String overlay = |
186 |
|
" <ScreenOverlay>" + |
187 |
|
" <name>Absolute Positioning: Top left</name>" + |
188 |
|
" <description>Absolute Positioning: Top left</description>" + |
189 |
|
" <visibility>1</visibility>" + |
190 |
|
" <Icon>" + |
191 |
|
" <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" + |
192 |
|
" </Icon>" + |
193 |
|
" <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" + |
194 |
|
" <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" + |
195 |
|
" <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" + |
196 |
|
" <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" + |
197 |
|
" </ScreenOverlay>" ; |
198 |
|
|
199 |
|
sb.append(overlay); |
200 |
|
*/ |
201 |
|
|
202 |
|
|
203 |
|
formatPositions(sb, "green", positions.green); |
204 |
|
formatPositions(sb, "yello", positions.yellow); |
205 |
|
formatPositions(sb, "red", positions.red); |
206 |
|
|
207 |
|
|
208 |
|
sb.append( "</Document>\n" ); |
209 |
|
sb.append( "</kml>\n" ); |
210 |
|
|
211 |
|
return sb.toString(); |
212 |
} |
} |
213 |
|
|
214 |
|
protected String getRequestsFromFile(boolean multiple) throws IOException { |
215 |
|
String kmlData = null; |
216 |
|
String key; |
217 |
|
|
218 |
|
if (multiple == false) { |
219 |
|
key = "kmldata"; |
220 |
|
} else { |
221 |
|
key = "kmldata-multi"; |
222 |
|
} |
223 |
|
|
224 |
|
kmlData = cache.get(key); |
225 |
|
|
226 |
|
if (kmlData == null) { |
227 |
|
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
228 |
|
cache.put(key, kmlData); |
229 |
|
kmlData += "<!-- from source -->"; |
230 |
|
} else { |
231 |
|
kmlData += "<!-- cached -->"; |
232 |
|
} |
233 |
|
|
234 |
|
return kmlData; |
235 |
|
} |
236 |
|
|
237 |
|
boolean enabled(String param) { |
238 |
|
if (param == null || param.equals("")) { |
239 |
|
return false; |
240 |
|
} |
241 |
|
|
242 |
|
int p = 0; |
243 |
|
try { |
244 |
|
p = Integer.parseInt(param); |
245 |
|
} catch (Exception e) {} |
246 |
|
|
247 |
|
return (p != 0); |
248 |
|
} |
249 |
|
|
250 |
|
@Override |
251 |
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
252 |
|
|
253 |
|
boolean multiple = enabled( req.getParameter("multi") ); |
254 |
|
|
255 |
|
String kmlData = getRequestsFromFile(multiple); |
256 |
|
|
257 |
|
if ( enabled(req.getParameter("zip")) ) { |
258 |
|
|
259 |
|
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
260 |
|
|
261 |
|
ZipOutputStream zip = new ZipOutputStream(baos); |
262 |
|
zip.putNextEntry( new ZipEntry("trains.kml") ); |
263 |
|
zip.write( kmlData.getBytes() ); |
264 |
|
zip.closeEntry(); |
265 |
|
zip.close(); |
266 |
|
|
267 |
|
byte bytes[] = baos.toByteArray(); |
268 |
|
|
269 |
|
resp.setContentType(KMZ); |
270 |
|
resp.setContentLength( bytes.length ); |
271 |
|
resp.getOutputStream().write(bytes); |
272 |
|
|
273 |
|
} else { |
274 |
|
resp.setContentType(KML); |
275 |
|
resp.getWriter().print( kmlData ); |
276 |
|
} |
277 |
|
} |
278 |
} |
} |