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; |
48 |
} |
} |
49 |
|
|
50 |
class PositionContainer { |
class PositionContainer { |
51 |
|
List<RequestPosition> blue = new ArrayList<RequestPosition>(); |
52 |
List<RequestPosition> green = new ArrayList<RequestPosition>(); |
List<RequestPosition> green = new ArrayList<RequestPosition>(); |
53 |
List<RequestPosition> yellow = new ArrayList<RequestPosition>(); |
List<RequestPosition> yellow = new ArrayList<RequestPosition>(); |
54 |
List<RequestPosition> red = new ArrayList<RequestPosition>(); |
List<RequestPosition> red = new ArrayList<RequestPosition>(); |
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 |
|
|
65 |
|
File logFiles[] = accessLogDir.listFiles( new FilenameFilter() { |
66 |
|
@Override |
67 |
|
public boolean accept(File dir, String name) { |
68 |
|
return name.startsWith("server_access_log"); |
69 |
|
} |
70 |
|
}); |
71 |
|
|
72 |
|
if (logFiles == null) { |
73 |
|
File[] empty = {}; |
74 |
|
return empty; |
75 |
|
} |
76 |
|
|
77 |
|
int from = logFiles.length - count; |
78 |
|
int to = logFiles.length-1; |
79 |
|
|
80 |
|
return Arrays.copyOfRange(logFiles, from, to); |
81 |
|
} |
82 |
|
|
83 |
|
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
84 |
|
PositionContainer positions = new PositionContainer(); |
85 |
|
|
86 |
|
try { |
87 |
|
|
88 |
|
int count; |
89 |
|
|
90 |
|
if (multiple == false) { |
91 |
|
count = 1; |
92 |
|
} else { |
93 |
|
count = 4; |
94 |
|
} |
95 |
|
|
96 |
|
File files[] = getFiles(count); |
97 |
|
|
98 |
|
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
99 |
|
|
100 |
|
Date now = new Date(); |
101 |
|
for (File f : files ) { |
102 |
|
log.info("Parsing file: " + f.getName() ); |
103 |
|
|
104 |
|
if ( !f.exists() ) { |
105 |
|
continue; |
106 |
|
} |
107 |
|
|
108 |
|
|
109 |
|
InputStream input = new FileInputStream(f); |
110 |
|
|
111 |
|
|
112 |
|
BufferedReader in = new BufferedReader( new InputStreamReader(input) ); |
113 |
|
|
114 |
|
|
115 |
|
String line; |
116 |
|
while ( (line=in.readLine()) != null) { |
117 |
|
if (line.indexOf("LocateStation") == -1 ){ |
118 |
|
continue; |
119 |
|
} |
120 |
|
|
121 |
|
if (line.indexOf("latitude=") == -1 ) { |
122 |
|
continue; |
123 |
|
} |
124 |
|
|
125 |
|
if (line.indexOf("longitude=") == -1) { |
126 |
|
continue; |
127 |
|
} |
128 |
|
|
129 |
|
RequestPosition pos = new RequestPosition(); |
130 |
|
|
131 |
|
String toks[] = line.split(" "); |
132 |
|
pos.ip = toks[0].replaceAll("\"", ""); |
133 |
|
|
134 |
|
pos.time = df.parse( toks[2].replace("\"", "") ); |
135 |
|
|
136 |
|
String argpart = toks[5].split("\\?")[1]; |
137 |
|
|
138 |
|
String args[] = argpart.split("&"); |
139 |
|
|
140 |
|
pos.lat = args[0].split("=")[1]; |
141 |
|
pos.lng = args[1].split("=")[1]; |
142 |
|
|
143 |
|
|
144 |
|
long timediff = now.getTime() - pos.time.getTime(); |
145 |
|
if ( timediff < (3*60*60*1000) ) { |
146 |
|
positions.red.add(pos); //RED |
147 |
|
} else if ( timediff < (24*60*60*1000)) { |
148 |
|
positions.yellow.add(pos); //YELLOW |
149 |
|
} else if ( timediff < (7*24*60*60*1000)) { |
150 |
|
positions.green.add(pos); //GREEN |
151 |
|
} else { |
152 |
|
positions.blue.add(pos); //BLUE |
153 |
|
} |
154 |
|
|
155 |
|
} |
156 |
|
in.close(); |
157 |
|
input.close(); |
158 |
|
|
159 |
|
} |
160 |
|
} catch (ParseException pe) { |
161 |
|
log.log(Level.SEVERE, "parseException", pe); |
162 |
|
throw new IOException(pe); |
163 |
|
} catch (IOException e) { |
164 |
|
log.log(Level.SEVERE, "getKml()", e); |
165 |
|
throw e; |
166 |
|
} |
167 |
|
|
168 |
|
return positions; |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
|
/* old apache code |
173 |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ |
174 |
PositionContainer positions = new PositionContainer(); |
PositionContainer positions = new PositionContainer(); |
175 |
|
|
240 |
positions.red.add(pos); //RED |
positions.red.add(pos); //RED |
241 |
} else if ( timediff < (24*60*60*1000)) { |
} else if ( timediff < (24*60*60*1000)) { |
242 |
positions.yellow.add(pos); //YELLOW |
positions.yellow.add(pos); //YELLOW |
243 |
} else { |
} else if ( timediff < (7*24*60*60*1000)) { |
244 |
positions.green.add(pos); //GREEN |
positions.green.add(pos); //GREEN |
245 |
|
} else { |
246 |
|
positions.blue.add(pos); //BLUE |
247 |
} |
} |
248 |
|
|
249 |
} |
} |
260 |
} |
} |
261 |
|
|
262 |
return positions; |
return positions; |
263 |
} |
}*/ |
264 |
|
|
265 |
protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) { |
protected void formatPositions(StringBuilder sb, String color, List<RequestPosition> list) { |
266 |
sb.append( "<Folder>\n"); |
sb.append( "<Folder>\n"); |
267 |
sb.append( " <name>" ).append(color).append("</name>\n"); |
sb.append( " <name>" ).append(color).append("</name>\n"); |
268 |
sb.append( " <open>0</open>\n" ); |
sb.append( " <open>0</open>\n" ); |
269 |
|
|
270 |
for(RequestPosition current : list) { |
int count=0; |
271 |
sb.append( " <Placemark>\n" ); |
for(RequestPosition current : list) { |
272 |
|
String id = color + count++; |
273 |
|
sb.append( " <Placemark id=\"" + id + "\">\n" ); |
274 |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
sb.append( " <styleUrl>#").append(color).append("</styleUrl>\n" ); |
275 |
sb.append( " <description>IP=").append(current.ip).append(" Time=").append(current.time).append("</description>\n" ); |
sb.append( " <description><![CDATA[IP=").append(current.ip).append("<br/>Time=").append(current.time).append("]]></description>\n" ); |
276 |
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
sb.append( " <Point><coordinates>").append(current.lng).append(",").append(current.lat).append(",0</coordinates></Point>\n" ); |
277 |
sb.append( " </Placemark>\n" ); |
sb.append( " </Placemark>\n" ); |
278 |
} |
} |
287 |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" ); |
288 |
sb.append( "<Document>\n" ); |
sb.append( "<Document>\n" ); |
289 |
sb.append( " <description><![CDATA["); |
sb.append( " <description><![CDATA["); |
290 |
sb.append( " Green:").append(positions.green.size()).append("<br/>\n"); |
sb.append( " Red:").append(positions.red.size()).append(" (whithin 3 hours)<br/>\n"); |
291 |
sb.append( " Yellow:").append(positions.yellow.size()).append("<br/>\n"); |
sb.append( " Yellow:").append(positions.yellow.size()).append(" (within 24 hours)<br/>\n"); |
292 |
sb.append( " Red:").append(positions.red.size()).append("\n"); |
sb.append( " Green:").append(positions.green.size()).append(" (within one week)<br/>\n"); |
293 |
|
sb.append( " Blue:").append(positions.blue.size()).append(" (older)<br/>\n"); |
294 |
sb.append( " ]]></description>"); |
sb.append( " ]]></description>"); |
295 |
|
|
296 |
|
|
306 |
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" ); |
307 |
sb.append( " </Style>\n\n" ); |
sb.append( " </Style>\n\n" ); |
308 |
|
|
309 |
|
sb.append( " <Style id=\"blue\">\n" ); |
310 |
|
sb.append( " <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/blu-circle.png</href></Icon></IconStyle>\n" ); |
311 |
|
sb.append( " </Style>\n\n" ); |
312 |
/* |
/* |
313 |
String overlay = |
String overlay = |
314 |
" <ScreenOverlay>" + |
" <ScreenOverlay>" + |
328 |
*/ |
*/ |
329 |
|
|
330 |
|
|
331 |
|
formatPositions(sb, "blue", positions.blue); |
332 |
formatPositions(sb, "green", positions.green); |
formatPositions(sb, "green", positions.green); |
333 |
formatPositions(sb, "yellow", positions.yellow); |
formatPositions(sb, "yellow", positions.yellow); |
334 |
formatPositions(sb, "red", positions.red); |
formatPositions(sb, "red", positions.red); |