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 |
|
|
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 |
|
|
33 |
|
|
34 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
35 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
36 |
|
|
37 |
|
Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000); |
38 |
|
|
39 |
class RequestPosition { |
class RequestPosition { |
40 |
public String ip; |
public String ip; |
42 |
public String lat; |
public String lat; |
43 |
public String lng; |
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 List<RequestPosition> getRequestsFromFile() throws IOException{ |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
58 |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
PositionContainer positions = new PositionContainer(); |
59 |
|
|
60 |
try { |
try { |
61 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_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[]; |
65 |
|
|
66 |
|
if (multiple == false) { |
67 |
|
files = files_single; |
68 |
|
} else { |
69 |
|
files = files_multi; |
70 |
|
} |
71 |
|
|
72 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
73 |
|
|
74 |
String line; |
Date now = new Date(); |
75 |
while ( (line=in.readLine()) != null) { |
for (String fileStr : files ) { |
76 |
if (line.indexOf("LocateStation") == -1 ){ |
File f = new File(fileStr); |
77 |
|
if ( !f.exists() ) { |
78 |
continue; |
continue; |
79 |
} |
} |
80 |
|
|
81 |
if (line.indexOf("latitude") == -1 ) { |
FileInputStream fis = new FileInputStream(fileStr); |
82 |
continue; |
|
83 |
|
InputStream input; |
84 |
|
if ( isGz(fileStr)) { |
85 |
|
input = new GZIPInputStream(fis); |
86 |
|
} else { |
87 |
|
input = fis; |
88 |
} |
} |
89 |
RequestPosition pos = new RequestPosition(); |
|
90 |
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
|
String toks[] = line.split(" "); |
|
|
pos.ip = toks[0]; |
|
|
|
|
|
pos.time = df.parse( toks[3].replace("[", "") ); |
|
|
|
|
|
String argpart = toks[6].split("\\?")[1]; |
|
|
|
|
|
String args[] = argpart.split("&"); |
|
91 |
|
|
92 |
pos.lat = args[0].split("=")[1]; |
|
93 |
pos.lng = args[1].split("=")[1]; |
String line; |
94 |
|
while ( (line=in.readLine()) != null) { |
95 |
positions.add(pos); |
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) { |
} catch (ParseException pe) { |
137 |
log.log(Level.SEVERE, "parseException", pe); |
log.log(Level.SEVERE, "parseException", pe); |
143 |
|
|
144 |
return positions; |
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 |
|
sb.append("</Folder>\n"); |
162 |
|
} |
163 |
|
|
164 |
protected String formatXml(List<RequestPosition> list) { |
protected String formatXml(PositionContainer positions) { |
165 |
StringBuilder sb = new StringBuilder(); |
StringBuilder sb = new StringBuilder(); |
166 |
|
|
167 |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
179 |
|
|
180 |
sb.append( " <Style id=\"green\">\n" ); |
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" ); |
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" ); |
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 |
Date now = new Date(); |
formatPositions(sb, "green", positions.green); |
204 |
for(RequestPosition current : list) { |
formatPositions(sb, "yellow", positions.yellow); |
205 |
|
formatPositions(sb, "red", positions.red); |
206 |
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" ); |
|
|
} |
|
207 |
|
|
208 |
sb.append( "</Document>\n" ); |
sb.append( "</Document>\n" ); |
209 |
sb.append( "</kml>\n" ); |
sb.append( "</kml>\n" ); |
210 |
|
|
211 |
return sb.toString(); |
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 |
@Override |
251 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
252 |
|
|
253 |
String data = formatXml( getRequestsFromFile() ); |
boolean multiple = enabled( req.getParameter("multi") ); |
254 |
|
|
255 |
if (req.getParameter("zip") != null) { |
String kmlData = getRequestsFromFile(multiple); |
256 |
|
|
257 |
|
if ( enabled(req.getParameter("zip")) ) { |
258 |
|
|
259 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
260 |
|
|
261 |
ZipOutputStream zip = new ZipOutputStream(baos); |
ZipOutputStream zip = new ZipOutputStream(baos); |
262 |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
263 |
zip.write( data.getBytes() ); |
zip.write( kmlData.getBytes() ); |
264 |
zip.closeEntry(); |
zip.closeEntry(); |
265 |
zip.close(); |
zip.close(); |
266 |
|
|
272 |
|
|
273 |
} else { |
} else { |
274 |
resp.setContentType(KML); |
resp.setContentType(KML); |
275 |
resp.getWriter().print( data ); |
resp.getWriter().print( kmlData ); |
276 |
} |
} |
277 |
} |
} |
278 |
} |
} |