2 |
|
|
3 |
import java.io.BufferedReader; |
import java.io.BufferedReader; |
4 |
import java.io.ByteArrayOutputStream; |
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; |
import java.text.ParseException; |
11 |
import java.text.SimpleDateFormat; |
import java.text.SimpleDateFormat; |
12 |
import java.util.ArrayList; |
import java.util.ArrayList; |
13 |
import java.util.Date; |
import java.util.Date; |
14 |
import java.util.List; |
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; |
import java.util.zip.ZipEntry; |
20 |
import java.util.zip.ZipOutputStream; |
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 |
|
|
36 |
|
|
37 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
38 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
39 |
|
|
40 |
|
Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000); |
41 |
|
|
42 |
class RequestPosition { |
class RequestPosition { |
43 |
public String ip; |
public String ip; |
45 |
public String lat; |
public String lat; |
46 |
public String lng; |
public String lng; |
47 |
} |
} |
48 |
|
|
49 |
|
class PositionContainer { |
50 |
|
List<RequestPosition> green = new ArrayList<RequestPosition>(); |
51 |
|
List<RequestPosition> yellow = new ArrayList<RequestPosition>(); |
52 |
|
List<RequestPosition> red = new ArrayList<RequestPosition>(); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
boolean isGz(String fileStr) { |
57 |
|
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
58 |
|
} |
59 |
|
|
60 |
protected List<RequestPosition> getRequestsFromFile() throws IOException{ |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
61 |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
PositionContainer positions = new PositionContainer(); |
62 |
|
|
63 |
try { |
try { |
64 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); |
String files_single[] = {"/var/log/apache2/app_access.log"}; |
65 |
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"}; |
66 |
|
|
67 |
|
String files[]; |
68 |
|
|
69 |
|
if (multiple == false) { |
70 |
|
files = files_single; |
71 |
|
} else { |
72 |
|
files = files_multi; |
73 |
|
} |
74 |
|
|
75 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
76 |
|
|
77 |
String line; |
Date now = new Date(); |
78 |
while ( (line=in.readLine()) != null) { |
for (String fileStr : files ) { |
79 |
if (line.indexOf("LocateStation") == -1 ){ |
File f = new File(fileStr); |
80 |
|
if ( !f.exists() ) { |
81 |
continue; |
continue; |
82 |
} |
} |
83 |
|
|
84 |
if (line.indexOf("latitude") == -1 ) { |
FileInputStream fis = new FileInputStream(fileStr); |
85 |
continue; |
|
86 |
|
InputStream input; |
87 |
|
if ( isGz(fileStr)) { |
88 |
|
input = new GZIPInputStream(fis); |
89 |
|
} else { |
90 |
|
input = fis; |
91 |
} |
} |
92 |
RequestPosition pos = new RequestPosition(); |
|
93 |
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
|
String toks[] = line.split(" "); |
|
|
pos.ip = toks[0]; |
|
|
|
|
|
pos.time = df.parse( toks[3].replace("[", "") ); |
|
94 |
|
|
95 |
String argpart = toks[6].split("\\?")[1]; |
|
96 |
|
String line; |
97 |
String args[] = argpart.split("&"); |
while ( (line=in.readLine()) != null) { |
98 |
|
if (line.indexOf("LocateStation") == -1 ){ |
99 |
pos.lat = args[0].split("=")[1]; |
continue; |
100 |
pos.lng = args[1].split("=")[1]; |
} |
101 |
|
|
102 |
positions.add(pos); |
if (line.indexOf("latitude=") == -1 ) { |
103 |
|
continue; |
104 |
|
} |
105 |
|
|
106 |
|
if (line.indexOf("longitude=") == -1) { |
107 |
|
continue; |
108 |
|
} |
109 |
|
|
110 |
|
RequestPosition pos = new RequestPosition(); |
111 |
|
|
112 |
|
String toks[] = line.split(" "); |
113 |
|
pos.ip = toks[0]; |
114 |
|
|
115 |
|
pos.time = df.parse( toks[3].replace("[", "") ); |
116 |
|
|
117 |
|
String argpart = toks[6].split("\\?")[1]; |
118 |
|
|
119 |
|
String args[] = argpart.split("&"); |
120 |
|
|
121 |
|
pos.lat = args[0].split("=")[1]; |
122 |
|
pos.lng = args[1].split("=")[1]; |
123 |
|
|
124 |
|
|
125 |
|
long timediff = now.getTime() - pos.time.getTime(); |
126 |
|
if ( timediff < (3*60*60*1000) ) { |
127 |
|
positions.red.add(pos); //RED |
128 |
|
} else if ( timediff < (24*60*60*1000)) { |
129 |
|
positions.yellow.add(pos); //YELLOW |
130 |
|
} else { |
131 |
|
positions.green.add(pos); //GREEN |
132 |
|
} |
133 |
|
|
134 |
|
} |
135 |
|
in.close(); |
136 |
|
input.close(); |
137 |
|
fis.close(); |
138 |
} |
} |
139 |
} catch (ParseException pe) { |
} catch (ParseException pe) { |
140 |
log.log(Level.SEVERE, "parseException", pe); |
log.log(Level.SEVERE, "parseException", pe); |
146 |
|
|
147 |
return positions; |
return positions; |
148 |
} |
} |
149 |
|
|
150 |
|
protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) { |
151 |
|
sb.append( "<Folder>\n"); |
152 |
|
sb.append( " <name>" ).append(color).append("</name>\n"); |
153 |
|
sb.append( " <open>0</open>\n" ); |
154 |
|
|
155 |
|
for(RequestPosition current : list) { |
156 |
|
sb.append( " <Placemark>\n" ); |
157 |
|
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
158 |
|
sb.append( " <description>IP=").append(current.ip).append(" Time=").append(current.time).append("</description>\n" ); |
159 |
|
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
160 |
|
sb.append( " </Placemark>\n" ); |
161 |
|
} |
162 |
|
|
163 |
|
sb.append("</Folder>\n"); |
164 |
|
} |
165 |
|
|
166 |
protected String formatXml(List<RequestPosition> list) { |
protected String formatXml(PositionContainer positions) { |
167 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(1024*1024); |
168 |
|
|
169 |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
170 |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
171 |
sb.append( "<Document>\n" ); |
sb.append( "<Document>\n" ); |
172 |
|
sb.append( " <description><![CDATA["); |
173 |
|
sb.append( " Green:").append(positions.green.size()).append("<br/>\n"); |
174 |
|
sb.append( " Yellow:").append(positions.yellow.size()).append("<br/>\n"); |
175 |
|
sb.append( " Red:").append(positions.red.size()).append("\n"); |
176 |
|
sb.append( " ]]></description>"); |
177 |
|
|
178 |
|
|
179 |
sb.append( " <Style id=\"red\">\n" ); |
sb.append( " <Style id=\"red\">\n" ); |
207 |
*/ |
*/ |
208 |
|
|
209 |
|
|
210 |
|
formatPositions(sb, "green", positions.green); |
211 |
|
formatPositions(sb, "yellow", positions.yellow); |
212 |
|
formatPositions(sb, "red", positions.red); |
213 |
|
|
214 |
|
|
215 |
|
sb.append( "</Document>\n" ); |
|
|
|
|
|
|
|
Date now = new Date(); |
|
|
for(RequestPosition current : list) { |
|
|
|
|
|
String style; |
|
|
long timediff = now.getTime() - current.time.getTime(); |
|
|
|
|
|
if ( timediff < (3*60*60*1000) ) { |
|
|
style = "#red"; |
|
|
} else if ( timediff < (24*60*60*1000)) { |
|
|
style = "#yellow"; |
|
|
} else { |
|
|
style = "#green"; |
|
|
} |
|
|
|
|
|
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( "</Document>\n" ); |
|
216 |
sb.append( "</kml>\n" ); |
sb.append( "</kml>\n" ); |
217 |
|
|
218 |
return sb.toString(); |
return sb.toString(); |
219 |
} |
} |
220 |
|
|
221 |
|
protected String getRequestsFromFile(boolean multiple) throws IOException { |
222 |
|
String kmlData = null; |
223 |
|
String key; |
224 |
|
|
225 |
|
if (multiple == false) { |
226 |
|
key = "kmldata"; |
227 |
|
} else { |
228 |
|
key = "kmldata-multi"; |
229 |
|
} |
230 |
|
|
231 |
|
kmlData = cache.get(key); |
232 |
|
|
233 |
|
if (kmlData == null) { |
234 |
|
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
235 |
|
cache.put(key, kmlData); |
236 |
|
kmlData += "<!-- from source -->"; |
237 |
|
} else { |
238 |
|
kmlData += "<!-- cached -->"; |
239 |
|
} |
240 |
|
|
241 |
|
return kmlData; |
242 |
|
} |
243 |
|
|
244 |
|
boolean enabled(String param) { |
245 |
|
if (param == null || param.equals("")) { |
246 |
|
return false; |
247 |
|
} |
248 |
|
|
249 |
|
int p = 0; |
250 |
|
try { |
251 |
|
p = Integer.parseInt(param); |
252 |
|
} catch (Exception e) {} |
253 |
|
|
254 |
|
return (p != 0); |
255 |
|
} |
256 |
|
|
257 |
@Override |
@Override |
258 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
259 |
|
|
260 |
String data = formatXml( getRequestsFromFile() ); |
boolean multiple = enabled( req.getParameter("multi") ); |
261 |
|
|
262 |
if (req.getParameter("zip") != null) { |
String kmlData = getRequestsFromFile(multiple); |
263 |
|
|
264 |
|
if ( enabled(req.getParameter("zip")) ) { |
265 |
|
|
266 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
267 |
|
|
268 |
ZipOutputStream zip = new ZipOutputStream(baos); |
ZipOutputStream zip = new ZipOutputStream(baos); |
269 |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
270 |
zip.write( data.getBytes() ); |
zip.write( kmlData.getBytes() ); |
271 |
zip.closeEntry(); |
zip.closeEntry(); |
272 |
zip.close(); |
zip.close(); |
273 |
|
|
279 |
|
|
280 |
} else { |
} else { |
281 |
resp.setContentType(KML); |
resp.setContentType(KML); |
282 |
resp.getWriter().print( data ); |
resp.getWriter().print( kmlData ); |
283 |
} |
} |
284 |
} |
} |
285 |
} |
} |