5 |
import java.io.FileInputStream; |
import java.io.FileInputStream; |
6 |
import java.io.IOException; |
import java.io.IOException; |
7 |
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
8 |
|
import java.util.ArrayList; |
9 |
|
import java.util.List; |
10 |
import java.util.logging.Level; |
import java.util.logging.Level; |
11 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
12 |
import java.util.zip.ZipEntry; |
import java.util.zip.ZipEntry; |
19 |
|
|
20 |
public class RequestPlotter extends HttpServlet { |
public class RequestPlotter extends HttpServlet { |
21 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
22 |
|
|
23 |
static final Logger log = Logger.getLogger(RequestPlotter.class.getName()); |
static final Logger log = Logger.getLogger(RequestPlotter.class.getName()); |
24 |
|
|
25 |
static final String KML = "application/vnd.google-earth.kml"; |
static final String KML = "application/vnd.google-earth.kml"; |
26 |
static final String KMZ = "application/vnd.google-earth.kmz"; |
static final String KMZ = "application/vnd.google-earth.kmz"; |
27 |
|
|
28 |
protected String getKml() { |
class RequestPosition { |
29 |
StringBuilder sb = new StringBuilder(); |
public String ip; |
30 |
|
public String time; |
31 |
|
public String lat; |
32 |
|
public String lng; |
33 |
|
} |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
protected List<RequestPosition> getRequestsFromFile() throws IOException{ |
38 |
|
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
39 |
|
|
40 |
try { |
try { |
41 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); |
42 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
43 |
|
|
|
|
|
|
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" ); |
|
|
|
|
44 |
String line; |
String line; |
45 |
while ( (line=in.readLine()) != null) { |
while ( (line=in.readLine()) != null) { |
46 |
if (line.indexOf("LocateStation") == -1 ){ |
if (line.indexOf("LocateStation") == -1 ){ |
47 |
continue; |
continue; |
48 |
} |
} |
49 |
|
|
50 |
if (line.indexOf("latitude") == -1 ) { |
if (line.indexOf("latitude") == -1 ) { |
51 |
continue; |
continue; |
52 |
} |
} |
53 |
|
RequestPosition pos = new RequestPosition(); |
54 |
|
|
55 |
String toks[] = line.split(" "); |
String toks[] = line.split(" "); |
56 |
String ip = toks[0]; |
pos.ip = toks[0]; |
57 |
String time = toks[3].replace("[", ""); |
pos.time = toks[3].replace("[", ""); |
58 |
String argpart = toks[6].split("\\?")[1]; |
String argpart = toks[6].split("\\?")[1]; |
59 |
|
|
|
|
|
60 |
String args[] = argpart.split("&"); |
String args[] = argpart.split("&"); |
61 |
|
|
62 |
|
pos.lat = args[0].split("=")[1]; |
63 |
|
pos.lng = args[1].split("=")[1]; |
64 |
|
|
65 |
String lat = args[0].split("=")[1]; |
positions.add(pos); |
|
String lng = args[1].split("=")[1]; |
|
|
|
|
|
sb.append( " <Placemark>\n" ); |
|
|
sb.append( " <description>IP=" + ip + " Time=" + time + "</description>\n" ); |
|
|
sb.append( " <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>\n" ); |
|
|
sb.append( " </Placemark>\n" ); |
|
|
|
|
66 |
} |
} |
67 |
|
} catch (IOException e) { |
|
sb.append( "</Document>\n" ); |
|
|
sb.append( "</kml>\n" ); |
|
|
|
|
|
} catch (Exception e) { |
|
|
sb.append("<!-- error -->"); |
|
68 |
log.log(Level.SEVERE, "getKml()", e); |
log.log(Level.SEVERE, "getKml()", e); |
69 |
|
throw e; |
70 |
} |
} |
71 |
|
|
72 |
return sb.toString(); |
return positions; |
73 |
|
} |
74 |
|
|
75 |
|
protected String formatXml(List<RequestPosition> list) { |
76 |
|
StringBuilder sb = new StringBuilder(); |
77 |
|
|
78 |
|
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ); |
79 |
|
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
80 |
|
sb.append( "<Document>\n" ); |
81 |
|
|
82 |
|
for(RequestPosition current : list) { |
83 |
|
sb.append( " <Placemark>\n" ); |
84 |
|
sb.append( " <description>IP=" + current.ip + " Time=" + current.time + "</description>\n" ); |
85 |
|
sb.append( " <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" ); |
86 |
|
sb.append( " </Placemark>\n" ); |
87 |
|
} |
88 |
|
|
89 |
|
sb.append( "</Document>\n" ); |
90 |
|
sb.append( "</kml>\n" ); |
91 |
|
|
92 |
|
return sb.toString(); |
93 |
} |
} |
94 |
|
|
95 |
@Override |
@Override |
96 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
97 |
String data = getKml(); |
|
98 |
|
String data = formatXml( getRequestsFromFile() ); |
99 |
|
|
100 |
if (req.getParameter("zip") != null) { |
if (req.getParameter("zip") != null) { |
101 |
|
|
102 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
103 |
|
|
104 |
ZipOutputStream zip = new ZipOutputStream(baos); |
ZipOutputStream zip = new ZipOutputStream(baos); |
105 |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
zip.putNextEntry( new ZipEntry("trains.kml") ); |
106 |
zip.write( data.getBytes() ); |
zip.write( data.getBytes() ); |
107 |
zip.closeEntry(); |
zip.closeEntry(); |
108 |
zip.close(); |
zip.close(); |
109 |
|
|
110 |
byte bytes[] = baos.toByteArray(); |
byte bytes[] = baos.toByteArray(); |
111 |
|
|
112 |
resp.setContentType(KMZ); |
resp.setContentType(KMZ); |
113 |
resp.setContentLength( bytes.length ); |
resp.setContentLength( bytes.length ); |
114 |
resp.getOutputStream().write(bytes); |
resp.getOutputStream().write(bytes); |
115 |
|
|
116 |
} else { |
} else { |
117 |
resp.setContentType(KML); |
resp.setContentType(KML); |
118 |
resp.getWriter().print( getKml() ); |
resp.getWriter().print( data ); |
119 |
} |
} |
120 |
} |
} |
|
|
|
|
|
|
|
|
|
121 |
} |
} |