1 |
torben |
795 |
package dk.thoerup.traininfoservice;
|
2 |
|
|
|
3 |
|
|
import java.io.BufferedReader;
|
4 |
torben |
798 |
import java.io.ByteArrayOutputStream;
|
5 |
torben |
809 |
import java.io.File;
|
6 |
torben |
795 |
import java.io.FileInputStream;
|
7 |
|
|
import java.io.IOException;
|
8 |
torben |
809 |
import java.io.InputStream;
|
9 |
torben |
795 |
import java.io.InputStreamReader;
|
10 |
torben |
800 |
import java.text.ParseException;
|
11 |
|
|
import java.text.SimpleDateFormat;
|
12 |
torben |
799 |
import java.util.ArrayList;
|
13 |
torben |
800 |
import java.util.Date;
|
14 |
torben |
799 |
import java.util.List;
|
15 |
torben |
802 |
import java.util.Map;
|
16 |
torben |
796 |
import java.util.logging.Level;
|
17 |
|
|
import java.util.logging.Logger;
|
18 |
torben |
809 |
import java.util.zip.GZIPInputStream;
|
19 |
torben |
798 |
import java.util.zip.ZipEntry;
|
20 |
|
|
import java.util.zip.ZipOutputStream;
|
21 |
torben |
795 |
|
22 |
|
|
import javax.servlet.ServletException;
|
23 |
torben |
958 |
import javax.servlet.annotation.WebListener;
|
24 |
|
|
import javax.servlet.annotation.WebServlet;
|
25 |
torben |
795 |
import javax.servlet.http.HttpServlet;
|
26 |
|
|
import javax.servlet.http.HttpServletRequest;
|
27 |
|
|
import javax.servlet.http.HttpServletResponse;
|
28 |
|
|
|
29 |
torben |
802 |
import dk.thoerup.traininfoservice.banedk.TimeoutMap;
|
30 |
|
|
|
31 |
torben |
958 |
@WebServlet(urlPatterns={"/RequestPlotter"})
|
32 |
torben |
795 |
public class RequestPlotter extends HttpServlet {
|
33 |
|
|
private static final long serialVersionUID = 1L;
|
34 |
torben |
799 |
|
35 |
torben |
796 |
static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
|
36 |
torben |
799 |
|
37 |
torben |
798 |
static final String KML = "application/vnd.google-earth.kml";
|
38 |
|
|
static final String KMZ = "application/vnd.google-earth.kmz";
|
39 |
torben |
802 |
|
40 |
torben |
954 |
Map<String,String> cache = new TimeoutMap<String,String>(30*60*1000);
|
41 |
torben |
799 |
|
42 |
|
|
class RequestPosition {
|
43 |
|
|
public String ip;
|
44 |
torben |
800 |
public Date time;
|
45 |
torben |
799 |
public String lat;
|
46 |
|
|
public String lng;
|
47 |
|
|
}
|
48 |
torben |
892 |
|
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 |
torben |
799 |
|
55 |
|
|
|
56 |
torben |
809 |
boolean isGz(String fileStr) {
|
57 |
|
|
return fileStr.substring(fileStr.length() - 3).equals(".gz");
|
58 |
|
|
}
|
59 |
torben |
799 |
|
60 |
torben |
892 |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{
|
61 |
|
|
PositionContainer positions = new PositionContainer();
|
62 |
torben |
799 |
|
63 |
torben |
795 |
try {
|
64 |
torben |
809 |
String files_single[] = {"/var/log/apache2/app_access.log"};
|
65 |
torben |
817 |
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 |
torben |
809 |
|
67 |
|
|
String files[];
|
68 |
|
|
|
69 |
|
|
if (multiple == false) {
|
70 |
|
|
files = files_single;
|
71 |
|
|
} else {
|
72 |
|
|
files = files_multi;
|
73 |
|
|
}
|
74 |
torben |
799 |
|
75 |
torben |
800 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
|
76 |
torben |
892 |
|
77 |
|
|
Date now = new Date();
|
78 |
torben |
809 |
for (String fileStr : files ) {
|
79 |
|
|
File f = new File(fileStr);
|
80 |
|
|
if ( !f.exists() ) {
|
81 |
torben |
795 |
continue;
|
82 |
|
|
}
|
83 |
torben |
809 |
|
84 |
|
|
FileInputStream fis = new FileInputStream(fileStr);
|
85 |
|
|
|
86 |
|
|
InputStream input;
|
87 |
|
|
if ( isGz(fileStr)) {
|
88 |
|
|
input = new GZIPInputStream(fis);
|
89 |
|
|
} else {
|
90 |
|
|
input = fis;
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) );
|
94 |
torben |
799 |
|
95 |
torben |
809 |
|
96 |
|
|
String line;
|
97 |
|
|
while ( (line=in.readLine()) != null) {
|
98 |
|
|
if (line.indexOf("LocateStation") == -1 ){
|
99 |
|
|
continue;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
torben |
818 |
if (line.indexOf("latitude=") == -1 ) {
|
103 |
torben |
809 |
continue;
|
104 |
|
|
}
|
105 |
torben |
818 |
|
106 |
|
|
if (line.indexOf("longitude=") == -1) {
|
107 |
|
|
continue;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
torben |
809 |
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 |
torben |
892 |
|
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 |
torben |
809 |
|
134 |
torben |
795 |
}
|
135 |
torben |
809 |
in.close();
|
136 |
|
|
input.close();
|
137 |
|
|
fis.close();
|
138 |
torben |
795 |
}
|
139 |
torben |
800 |
} catch (ParseException pe) {
|
140 |
|
|
log.log(Level.SEVERE, "parseException", pe);
|
141 |
|
|
throw new IOException(pe);
|
142 |
torben |
799 |
} catch (IOException e) {
|
143 |
torben |
796 |
log.log(Level.SEVERE, "getKml()", e);
|
144 |
torben |
799 |
throw e;
|
145 |
torben |
795 |
}
|
146 |
torben |
799 |
|
147 |
|
|
return positions;
|
148 |
torben |
795 |
}
|
149 |
torben |
892 |
|
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 |
torben |
896 |
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 |
torben |
892 |
sb.append( " </Placemark>\n" );
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
sb.append("</Folder>\n");
|
164 |
|
|
}
|
165 |
torben |
795 |
|
166 |
torben |
892 |
protected String formatXml(PositionContainer positions) {
|
167 |
torben |
897 |
StringBuilder sb = new StringBuilder(1024*1024);
|
168 |
torben |
799 |
|
169 |
|
|
sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
|
170 |
|
|
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
|
171 |
|
|
sb.append( "<Document>\n" );
|
172 |
torben |
941 |
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 |
torben |
800 |
|
178 |
|
|
|
179 |
|
|
sb.append( " <Style id=\"red\">\n" );
|
180 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
|
181 |
|
|
sb.append( " </Style>\n");
|
182 |
torben |
799 |
|
183 |
torben |
800 |
sb.append( " <Style id=\"yellow\">\n" );
|
184 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
|
185 |
|
|
sb.append( " </Style>\n" );
|
186 |
|
|
|
187 |
|
|
sb.append( " <Style id=\"green\">\n" );
|
188 |
|
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
|
189 |
torben |
801 |
sb.append( " </Style>\n\n" );
|
190 |
|
|
|
191 |
|
|
/*
|
192 |
|
|
String overlay =
|
193 |
|
|
" <ScreenOverlay>" +
|
194 |
|
|
" <name>Absolute Positioning: Top left</name>" +
|
195 |
|
|
" <description>Absolute Positioning: Top left</description>" +
|
196 |
|
|
" <visibility>1</visibility>" +
|
197 |
|
|
" <Icon>" +
|
198 |
|
|
" <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
|
199 |
|
|
" </Icon>" +
|
200 |
|
|
" <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
|
201 |
|
|
" <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
|
202 |
|
|
" <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
|
203 |
|
|
" <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
|
204 |
|
|
" </ScreenOverlay>" ;
|
205 |
|
|
|
206 |
|
|
sb.append(overlay);
|
207 |
|
|
*/
|
208 |
|
|
|
209 |
torben |
800 |
|
210 |
torben |
892 |
formatPositions(sb, "green", positions.green);
|
211 |
torben |
893 |
formatPositions(sb, "yellow", positions.yellow);
|
212 |
torben |
892 |
formatPositions(sb, "red", positions.red);
|
213 |
|
|
|
214 |
torben |
800 |
|
215 |
torben |
805 |
sb.append( "</Document>\n" );
|
216 |
torben |
799 |
sb.append( "</kml>\n" );
|
217 |
|
|
|
218 |
|
|
return sb.toString();
|
219 |
|
|
}
|
220 |
torben |
809 |
|
221 |
|
|
protected String getRequestsFromFile(boolean multiple) throws IOException {
|
222 |
|
|
String kmlData = null;
|
223 |
|
|
String key;
|
224 |
torben |
802 |
|
225 |
torben |
809 |
if (multiple == false) {
|
226 |
|
|
key = "kmldata";
|
227 |
|
|
} else {
|
228 |
|
|
key = "kmldata-multi";
|
229 |
|
|
}
|
230 |
|
|
|
231 |
|
|
kmlData = cache.get(key);
|
232 |
|
|
|
233 |
torben |
803 |
if (kmlData == null) {
|
234 |
torben |
809 |
kmlData = formatXml( getRequestsFromFileWorker(multiple) );
|
235 |
|
|
cache.put(key, kmlData);
|
236 |
torben |
803 |
kmlData += "<!-- from source -->";
|
237 |
torben |
802 |
} else {
|
238 |
torben |
803 |
kmlData += "<!-- cached -->";
|
239 |
torben |
802 |
}
|
240 |
torben |
809 |
|
241 |
|
|
return kmlData;
|
242 |
|
|
}
|
243 |
torben |
810 |
|
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 |
torben |
799 |
|
257 |
torben |
809 |
@Override
|
258 |
|
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
259 |
torben |
802 |
|
260 |
torben |
810 |
boolean multiple = enabled( req.getParameter("multi") );
|
261 |
torben |
809 |
|
262 |
|
|
String kmlData = getRequestsFromFile(multiple);
|
263 |
|
|
|
264 |
torben |
810 |
if ( enabled(req.getParameter("zip")) ) {
|
265 |
torben |
799 |
|
266 |
torben |
798 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
267 |
torben |
799 |
|
268 |
torben |
798 |
ZipOutputStream zip = new ZipOutputStream(baos);
|
269 |
|
|
zip.putNextEntry( new ZipEntry("trains.kml") );
|
270 |
torben |
803 |
zip.write( kmlData.getBytes() );
|
271 |
torben |
798 |
zip.closeEntry();
|
272 |
|
|
zip.close();
|
273 |
torben |
799 |
|
274 |
torben |
798 |
byte bytes[] = baos.toByteArray();
|
275 |
torben |
799 |
|
276 |
torben |
798 |
resp.setContentType(KMZ);
|
277 |
|
|
resp.setContentLength( bytes.length );
|
278 |
|
|
resp.getOutputStream().write(bytes);
|
279 |
torben |
799 |
|
280 |
torben |
798 |
} else {
|
281 |
|
|
resp.setContentType(KML);
|
282 |
torben |
803 |
resp.getWriter().print( kmlData );
|
283 |
torben |
798 |
}
|
284 |
torben |
795 |
}
|
285 |
|
|
}
|