4 |
import java.io.ByteArrayOutputStream; |
import java.io.ByteArrayOutputStream; |
5 |
import java.io.File; |
import java.io.File; |
6 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
7 |
|
import java.io.FilenameFilter; |
8 |
import java.io.IOException; |
import java.io.IOException; |
9 |
import java.io.InputStream; |
import java.io.InputStream; |
10 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
11 |
import java.text.ParseException; |
import java.text.ParseException; |
12 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
13 |
import java.util.ArrayList; |
import java.util.ArrayList; |
14 |
|
import java.util.Arrays; |
15 |
import java.util.Date; |
import java.util.Date; |
16 |
import java.util.List; |
import java.util.List; |
17 |
import java.util.Map; |
import java.util.Map; |
22 |
import java.util.zip.ZipOutputStream; |
import java.util.zip.ZipOutputStream; |
23 |
|
|
24 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
25 |
|
import javax.servlet.annotation.WebServlet; |
26 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
27 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
28 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
29 |
|
|
30 |
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
import dk.thoerup.traininfoservice.banedk.TimeoutMap; |
31 |
|
|
32 |
|
@WebServlet(urlPatterns={"/RequestPlotter"}) |
33 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
34 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
35 |
|
|
38 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
39 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
40 |
|
|
41 |
Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000); |
Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000); |
42 |
|
|
43 |
class RequestPosition { |
class RequestPosition { |
44 |
public String ip; |
public String ip; |
46 |
public String lat; |
public String lat; |
47 |
public String lng; |
public String lng; |
48 |
} |
} |
49 |
|
|
50 |
|
class PositionContainer { |
51 |
|
List<RequestPosition> blue = new ArrayList<RequestPosition>(); |
52 |
|
List<RequestPosition> green = new ArrayList<RequestPosition>(); |
53 |
|
List<RequestPosition> yellow = new ArrayList<RequestPosition>(); |
54 |
|
List<RequestPosition> red = new ArrayList<RequestPosition>(); |
55 |
|
} |
56 |
|
|
57 |
|
|
58 |
boolean isGz(String fileStr) { |
boolean isGz(String fileStr) { |
59 |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
60 |
} |
} |
61 |
|
|
62 |
|
protected File[] getFiles(int count) { |
63 |
|
File accessLogDir = new File("/home/app/domain1/logs/access/"); |
64 |
|
//File accessLogDir = new File("/home/torben/inst/glassfishv3/glassfish/domains/domain1/logs/access/"); |
65 |
|
|
66 |
|
File logFiles[] = accessLogDir.listFiles( new FilenameFilter() { |
67 |
|
@Override |
68 |
|
public boolean accept(File dir, String name) { |
69 |
|
//log.info("name:" + name); |
70 |
|
return name.startsWith("server_access_log"); |
71 |
|
} |
72 |
|
}); |
73 |
|
|
74 |
|
Arrays.sort(logFiles); |
75 |
|
|
76 |
|
if (logFiles == null) { |
77 |
|
File[] empty = {}; |
78 |
|
log.info("file array was empty"); |
79 |
|
return empty; |
80 |
|
} |
81 |
|
|
82 |
|
int from = logFiles.length - (count); |
83 |
|
int to = logFiles.length; |
84 |
|
|
85 |
|
return Arrays.copyOfRange(logFiles, from, to); |
86 |
|
} |
87 |
|
|
88 |
|
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
89 |
|
PositionContainer positions = new PositionContainer(); |
90 |
|
|
91 |
|
try { |
92 |
|
|
93 |
|
int count; |
94 |
|
|
95 |
|
if (multiple == false) { |
96 |
|
count = 1; |
97 |
|
} else { |
98 |
|
count = 4; |
99 |
|
} |
100 |
|
|
101 |
|
File files[] = getFiles(count); |
102 |
|
|
103 |
|
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
104 |
|
|
105 |
|
Date now = new Date(); |
106 |
|
for (File f : files ) { |
107 |
|
log.info("Parsing file: " + f.getName() ); |
108 |
|
|
109 |
|
if ( !f.exists() ) { |
110 |
|
continue; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
InputStream input = new FileInputStream(f); |
115 |
|
|
116 |
|
|
117 |
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
118 |
|
|
119 |
|
|
120 |
|
String line; |
121 |
|
while ( (line=in.readLine()) != null) { |
122 |
|
if (line.indexOf("LocateStation") == -1 ){ |
123 |
|
continue; |
124 |
|
} |
125 |
|
|
126 |
|
if (line.indexOf("latitude=") == -1 ) { |
127 |
|
continue; |
128 |
|
} |
129 |
|
|
130 |
|
if (line.indexOf("longitude=") == -1) { |
131 |
|
continue; |
132 |
|
} |
133 |
|
|
134 |
|
RequestPosition pos = new RequestPosition(); |
135 |
|
|
136 |
|
String toks[] = line.split(" "); |
137 |
|
pos.ip = toks[0].replaceAll("\"", ""); |
138 |
|
|
139 |
|
pos.time = df.parse( toks[2].replace("\"", "") ); |
140 |
|
|
141 |
|
String argpart = toks[5].split("\\?")[1]; |
142 |
|
|
143 |
|
String args[] = argpart.split("&"); |
144 |
|
|
145 |
|
pos.lat = args[0].split("=")[1]; |
146 |
|
pos.lng = args[1].split("=")[1]; |
147 |
|
|
148 |
|
|
149 |
|
long timediff = now.getTime() - pos.time.getTime(); |
150 |
|
if ( timediff < (3*60*60*1000) ) { |
151 |
|
positions.red.add(pos); //RED |
152 |
|
} else if ( timediff < (24*60*60*1000)) { |
153 |
|
positions.yellow.add(pos); //YELLOW |
154 |
|
} else if ( timediff < (7*24*60*60*1000)) { |
155 |
|
positions.green.add(pos); //GREEN |
156 |
|
} else { |
157 |
|
positions.blue.add(pos); //BLUE |
158 |
|
} |
159 |
|
|
160 |
|
} |
161 |
|
in.close(); |
162 |
|
input.close(); |
163 |
|
|
164 |
|
} |
165 |
|
} catch (ParseException pe) { |
166 |
|
log.log(Level.SEVERE, "parseException", pe); |
167 |
|
throw new IOException(pe); |
168 |
|
} catch (IOException e) { |
169 |
|
log.log(Level.SEVERE, "getKml()", e); |
170 |
|
throw e; |
171 |
|
} |
172 |
|
|
173 |
|
return positions; |
174 |
|
} |
175 |
|
|
176 |
protected List<RequestPosition> getRequestsFromFileWorker(boolean multiple) throws IOException{ |
|
177 |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
/* old apache code |
178 |
|
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
179 |
|
PositionContainer positions = new PositionContainer(); |
180 |
|
|
181 |
try { |
try { |
182 |
String files_single[] = {"/var/log/apache2/app_access.log"}; |
String files_single[] = {"/var/log/apache2/app_access.log"}; |
183 |
String files_multi[] = {"/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"}; |
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"}; |
184 |
|
|
185 |
String files[]; |
String files[]; |
186 |
|
|
191 |
} |
} |
192 |
|
|
193 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
194 |
|
|
195 |
|
Date now = new Date(); |
196 |
for (String fileStr : files ) { |
for (String fileStr : files ) { |
197 |
File f = new File(fileStr); |
File f = new File(fileStr); |
198 |
if ( !f.exists() ) { |
if ( !f.exists() ) { |
217 |
continue; |
continue; |
218 |
} |
} |
219 |
|
|
220 |
if (line.indexOf("latitude") == -1 ) { |
if (line.indexOf("latitude=") == -1 ) { |
221 |
|
continue; |
222 |
|
} |
223 |
|
|
224 |
|
if (line.indexOf("longitude=") == -1) { |
225 |
continue; |
continue; |
226 |
} |
} |
227 |
|
|
228 |
RequestPosition pos = new RequestPosition(); |
RequestPosition pos = new RequestPosition(); |
229 |
|
|
230 |
String toks[] = line.split(" "); |
String toks[] = line.split(" "); |
238 |
|
|
239 |
pos.lat = args[0].split("=")[1]; |
pos.lat = args[0].split("=")[1]; |
240 |
pos.lng = args[1].split("=")[1]; |
pos.lng = args[1].split("=")[1]; |
241 |
|
|
242 |
|
|
243 |
|
long timediff = now.getTime() - pos.time.getTime(); |
244 |
|
if ( timediff < (3*60*60*1000) ) { |
245 |
|
positions.red.add(pos); //RED |
246 |
|
} else if ( timediff < (24*60*60*1000)) { |
247 |
|
positions.yellow.add(pos); //YELLOW |
248 |
|
} else if ( timediff < (7*24*60*60*1000)) { |
249 |
|
positions.green.add(pos); //GREEN |
250 |
|
} else { |
251 |
|
positions.blue.add(pos); //BLUE |
252 |
|
} |
253 |
|
|
|
positions.add(pos); |
|
254 |
} |
} |
255 |
in.close(); |
in.close(); |
256 |
input.close(); |
input.close(); |
265 |
} |
} |
266 |
|
|
267 |
return positions; |
return positions; |
268 |
|
}*/ |
269 |
|
|
270 |
|
protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) { |
271 |
|
sb.append( "<Folder>\n"); |
272 |
|
sb.append( " <name>" ).append(color).append("</name>\n"); |
273 |
|
sb.append( " <open>0</open>\n" ); |
274 |
|
|
275 |
|
int count=0; |
276 |
|
for(RequestPosition current : list) { |
277 |
|
String id = color + count++; |
278 |
|
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
279 |
|
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
280 |
|
sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" ); |
281 |
|
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
282 |
|
sb.append( " </Placemark>\n" ); |
283 |
|
} |
284 |
|
|
285 |
|
sb.append("</Folder>\n"); |
286 |
} |
} |
287 |
|
|
288 |
protected String formatXml(List<RequestPosition> list) { |
protected String formatXml(PositionContainer positions) { |
289 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(1024*1024); |
290 |
|
|
291 |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
292 |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
293 |
sb.append( "<Document>\n" ); |
sb.append( "<Document>\n" ); |
294 |
|
sb.append( " <description><![CDATA["); |
295 |
|
sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n"); |
296 |
|
sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n"); |
297 |
|
sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n"); |
298 |
|
sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n"); |
299 |
|
sb.append( " ]]></description>"); |
300 |
|
|
301 |
|
|
302 |
sb.append( " <Style id=\"red\">\n" ); |
sb.append( " <Style id=\"red\">\n" ); |
311 |
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" ); |
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" ); |
312 |
sb.append( " </Style>\n\n" ); |
sb.append( " </Style>\n\n" ); |
313 |
|
|
314 |
|
sb.append( " <Style id=\"blue\">\n" ); |
315 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" ); |
316 |
|
sb.append( " </Style>\n\n" ); |
317 |
/* |
/* |
318 |
String overlay = |
String overlay = |
319 |
" <ScreenOverlay>" + |
" <ScreenOverlay>" + |
333 |
*/ |
*/ |
334 |
|
|
335 |
|
|
336 |
final String STYLE_GREEN = "green"; |
formatPositions(sb, "blue", positions.blue); |
337 |
final String STYLE_YELLOW = "yellow"; |
formatPositions(sb, "green", positions.green); |
338 |
final String STYLE_RED = "red"; |
formatPositions(sb, "yellow", positions.yellow); |
339 |
|
formatPositions(sb, "red", positions.red); |
340 |
|
|
341 |
|
|
|
String oldstyle = null; |
|
|
Date now = new Date(); |
|
|
for(RequestPosition current : list) { |
|
|
|
|
|
String style; |
|
|
long timediff = now.getTime() - current.time.getTime(); |
|
|
|
|
|
if ( timediff < (3*60*60*1000) ) { |
|
|
style = STYLE_RED; |
|
|
} else if ( timediff < (24*60*60*1000)) { |
|
|
style = STYLE_YELLOW; |
|
|
} else { |
|
|
style = STYLE_GREEN; |
|
|
} |
|
|
|
|
|
if ( !style.equals(oldstyle) ) { |
|
|
if (oldstyle != null) |
|
|
sb.append( "</Folder>\n"); |
|
|
sb.append( "<Folder>\n"); |
|
|
sb.append( " <name>" ).append(style).append("</name>\n"); |
|
|
sb.append( " <open>0</open>\n" ); |
|
|
|
|
|
oldstyle = style; |
|
|
} |
|
|
|
|
|
sb.append( " <Placemark>\n" ); |
|
|
sb.append( " <styleUrl>#" + style + "</styleUrl>\n" ); |
|
|
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
|
|
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
|
|
sb.append( " </Placemark>\n" ); |
|
|
} |
|
|
sb.append( "</Folder>\n" ); |
|
342 |
sb.append( "</Document>\n" ); |
sb.append( "</Document>\n" ); |
343 |
sb.append( "</kml>\n" ); |
sb.append( "</kml>\n" ); |
344 |
|
|
355 |
key = "kmldata-multi"; |
key = "kmldata-multi"; |
356 |
} |
} |
357 |
|
|
358 |
kmlData = cache.get(key); |
kmlData = null;// = cache.get(key); |
359 |
|
|
360 |
if (kmlData == null) { |
if (kmlData == null) { |
361 |
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
367 |
|
|
368 |
return kmlData; |
return kmlData; |
369 |
} |
} |
370 |
|
|
371 |
|
boolean enabled(String param) { |
372 |
|
if (param == null || param.equals("")) { |
373 |
|
return false; |
374 |
|
} |
375 |
|
|
376 |
|
int p = 0; |
377 |
|
try { |
378 |
|
p = Integer.parseInt(param); |
379 |
|
} catch (Exception e) {} |
380 |
|
|
381 |
|
return (p != 0); |
382 |
|
} |
383 |
|
|
384 |
@Override |
@Override |
385 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
386 |
|
|
387 |
boolean multiple = req.getParameter("multi") != null; |
boolean multiple = enabled( req.getParameter("multi") ); |
388 |
|
|
389 |
String kmlData = getRequestsFromFile(multiple); |
String kmlData = getRequestsFromFile(multiple); |
390 |
|
|
391 |
if (req.getParameter("zip") != null) { |
if ( enabled(req.getParameter("zip")) ) { |
392 |
|
|
393 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
394 |
|
|