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; |
15 |
import java.util.Map; |
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 |
|
|
44 |
} |
} |
45 |
|
|
46 |
|
|
47 |
|
boolean isGz(String fileStr) { |
48 |
|
return fileStr.substring(fileStr.length() - 3).equals(".gz"); |
49 |
|
} |
50 |
|
|
51 |
protected List<RequestPosition> getRequestsFromFile() throws IOException{ |
protected List<RequestPosition> getRequestsFromFileWorker(boolean multiple) throws IOException{ |
52 |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
List<RequestPosition> positions = new ArrayList<RequestPosition>(); |
53 |
|
|
54 |
try { |
try { |
55 |
FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); |
String files_single[] = {"/var/log/apache2/app_access.log"}; |
56 |
BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); |
String files_multi[] = {"/var/log/apache2/app_access.log.2.gz", "/var/log/apache2/app_access.log.1", "/var/log/apache2/app_access.log"}; |
57 |
|
|
58 |
|
String files[]; |
59 |
|
|
60 |
|
if (multiple == false) { |
61 |
|
files = files_single; |
62 |
|
} else { |
63 |
|
files = files_multi; |
64 |
|
} |
65 |
|
|
66 |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss"); |
67 |
|
|
68 |
String line; |
for (String fileStr : files ) { |
69 |
while ( (line=in.readLine()) != null) { |
File f = new File(fileStr); |
70 |
if (line.indexOf("LocateStation") == -1 ){ |
if ( !f.exists() ) { |
71 |
continue; |
continue; |
72 |
} |
} |
73 |
|
|
74 |
if (line.indexOf("latitude") == -1 ) { |
FileInputStream fis = new FileInputStream(fileStr); |
75 |
continue; |
|
76 |
|
InputStream input; |
77 |
|
if ( isGz(fileStr)) { |
78 |
|
input = new GZIPInputStream(fis); |
79 |
|
} else { |
80 |
|
input = fis; |
81 |
} |
} |
82 |
RequestPosition pos = new RequestPosition(); |
|
83 |
|
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("&"); |
|
|
|
|
|
pos.lat = args[0].split("=")[1]; |
|
|
pos.lng = args[1].split("=")[1]; |
|
84 |
|
|
85 |
positions.add(pos); |
|
86 |
|
String line; |
87 |
|
while ( (line=in.readLine()) != null) { |
88 |
|
if (line.indexOf("LocateStation") == -1 ){ |
89 |
|
continue; |
90 |
|
} |
91 |
|
|
92 |
|
if (line.indexOf("latitude") == -1 ) { |
93 |
|
continue; |
94 |
|
} |
95 |
|
RequestPosition pos = new RequestPosition(); |
96 |
|
|
97 |
|
String toks[] = line.split(" "); |
98 |
|
pos.ip = toks[0]; |
99 |
|
|
100 |
|
pos.time = df.parse( toks[3].replace("[", "") ); |
101 |
|
|
102 |
|
String argpart = toks[6].split("\\?")[1]; |
103 |
|
|
104 |
|
String args[] = argpart.split("&"); |
105 |
|
|
106 |
|
pos.lat = args[0].split("=")[1]; |
107 |
|
pos.lng = args[1].split("=")[1]; |
108 |
|
|
109 |
|
positions.add(pos); |
110 |
|
} |
111 |
|
in.close(); |
112 |
|
input.close(); |
113 |
|
fis.close(); |
114 |
} |
} |
115 |
} catch (ParseException pe) { |
} catch (ParseException pe) { |
116 |
log.log(Level.SEVERE, "parseException", pe); |
log.log(Level.SEVERE, "parseException", pe); |
203 |
|
|
204 |
return sb.toString(); |
return sb.toString(); |
205 |
} |
} |
206 |
|
|
207 |
@Override |
protected String getRequestsFromFile(boolean multiple) throws IOException { |
208 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
String kmlData = null; |
209 |
final String KEY = "kmldata"; |
String key; |
|
|
|
|
String kmlData = cache.get(KEY); |
|
210 |
|
|
211 |
|
if (multiple == false) { |
212 |
|
key = "kmldata"; |
213 |
|
} else { |
214 |
|
key = "kmldata-multi"; |
215 |
|
} |
216 |
|
|
217 |
|
kmlData = cache.get(key); |
218 |
|
|
219 |
if (kmlData == null) { |
if (kmlData == null) { |
220 |
kmlData = formatXml( getRequestsFromFile() ); |
kmlData = formatXml( getRequestsFromFileWorker(multiple) ); |
221 |
cache.put(KEY, kmlData); |
cache.put(key, kmlData); |
222 |
kmlData += "<!-- from source -->"; |
kmlData += "<!-- from source -->"; |
223 |
} else { |
} else { |
224 |
kmlData += "<!-- cached -->"; |
kmlData += "<!-- cached -->"; |
225 |
} |
} |
226 |
|
|
227 |
|
return kmlData; |
228 |
|
} |
229 |
|
|
230 |
|
@Override |
231 |
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
232 |
|
|
233 |
|
boolean multiple = req.getParameter("multi") != null; |
234 |
|
|
235 |
|
String kmlData = getRequestsFromFile(multiple); |
236 |
|
|
|
|
|
237 |
if (req.getParameter("zip") != null) { |
if (req.getParameter("zip") != null) { |
238 |
|
|
239 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |