--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 08:37:29 798 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2010/06/07 08:55:34 799 @@ -5,6 +5,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.ZipEntry; @@ -17,88 +19,103 @@ public class RequestPlotter extends HttpServlet { private static final long serialVersionUID = 1L; - + static final Logger log = Logger.getLogger(RequestPlotter.class.getName()); - + static final String KML = "application/vnd.google-earth.kml"; static final String KMZ = "application/vnd.google-earth.kmz"; - - protected String getKml() { - StringBuilder sb = new StringBuilder(); + + class RequestPosition { + public String ip; + public String time; + public String lat; + public String lng; + } + + + + protected List getRequestsFromFile() throws IOException{ + List positions = new ArrayList(); + try { FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log"); BufferedReader in = new BufferedReader( new InputStreamReader(fis) ); - - - sb.append( "\n" ); - sb.append( "\n" ); - sb.append( "\n" ); - + String line; while ( (line=in.readLine()) != null) { if (line.indexOf("LocateStation") == -1 ){ continue; } - + if (line.indexOf("latitude") == -1 ) { continue; } - + RequestPosition pos = new RequestPosition(); + String toks[] = line.split(" "); - String ip = toks[0]; - String time = toks[3].replace("[", ""); + pos.ip = toks[0]; + pos.time = 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]; - String lat = args[0].split("=")[1]; - String lng = args[1].split("=")[1]; - - sb.append( " \n" ); - sb.append( " IP=" + ip + " Time=" + time + "\n" ); - sb.append( " " + lng + "," + lat + ",0\n" ); - sb.append( " \n" ); - + positions.add(pos); } - - sb.append( "\n" ); - sb.append( "\n" ); - - } catch (Exception e) { - sb.append(""); + } catch (IOException e) { log.log(Level.SEVERE, "getKml()", e); + throw e; } - - return sb.toString(); + + return positions; + } + + protected String formatXml(List list) { + StringBuilder sb = new StringBuilder(); + + sb.append( "\n" ); + sb.append( "\n" ); + sb.append( "\n" ); + + for(RequestPosition current : list) { + sb.append( " \n" ); + sb.append( " IP=" + current.ip + " Time=" + current.time + "\n" ); + sb.append( " " + current.lng + "," + current.lat + ",0\n" ); + sb.append( " \n" ); + } + + sb.append( "\n" ); + sb.append( "\n" ); + + return sb.toString(); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - String data = getKml(); - + + String data = formatXml( getRequestsFromFile() ); + if (req.getParameter("zip") != null) { - + ByteArrayOutputStream baos = new ByteArrayOutputStream(); - + ZipOutputStream zip = new ZipOutputStream(baos); zip.putNextEntry( new ZipEntry("trains.kml") ); zip.write( data.getBytes() ); zip.closeEntry(); zip.close(); - + byte bytes[] = baos.toByteArray(); - + resp.setContentType(KMZ); resp.setContentLength( bytes.length ); resp.getOutputStream().write(bytes); - + } else { resp.setContentType(KML); - resp.getWriter().print( getKml() ); + resp.getWriter().print( data ); } } - - - }