/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java
ViewVC logotype

Diff of /android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 795 by torben, Thu Jun 3 14:12:56 2010 UTC revision 798 by torben, Mon Jun 7 08:37:29 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice;  package dk.thoerup.traininfoservice;
2    
3  import java.io.BufferedReader;  import java.io.BufferedReader;
4    import java.io.ByteArrayOutputStream;
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.logging.Level;
9    import java.util.logging.Logger;
10    import java.util.zip.ZipEntry;
11    import java.util.zip.ZipOutputStream;
12    
13  import javax.servlet.ServletException;  import javax.servlet.ServletException;
14  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
# Line 14  import javax.servlet.http.HttpServletRes Line 18  import javax.servlet.http.HttpServletRes
18  public class RequestPlotter extends HttpServlet {  public class RequestPlotter extends HttpServlet {
19          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
20                    
21            static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
22            
23            static final String KML = "application/vnd.google-earth.kml";
24            static final String KMZ = "application/vnd.google-earth.kmz";
25            
26          protected String getKml() {          protected String getKml() {
27                  StringBuilder sb = new StringBuilder();                  StringBuilder sb = new StringBuilder();
28                  try {                  try {
29                          FileInputStream fis = new FileInputStream("/var/log/apache2/access.log");                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
30                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
31                                                    
32                                    
# Line 38  public class RequestPlotter extends Http Line 47  public class RequestPlotter extends Http
47                                  String toks[] = line.split(" ");                                  String toks[] = line.split(" ");
48                                  String ip = toks[0];                                  String ip = toks[0];
49                                  String time = toks[3].replace("[", "");                                  String time = toks[3].replace("[", "");
50                                  String argpart = toks[6].split("?")[1];                                  String argpart = toks[6].split("\\?")[1];
51    
52                                                                    
53                                  String args[] = argpart.split("&");                                  String args[] = argpart.split("&");
54                                                                    
55                                  String lat = args[0].split("=")[1];                                  String lat = args[0].split("=")[1];
56                                  String lng = args[0].split("=")[1];                                  String lng = args[1].split("=")[1];
57                                                                    
58                          sb.append( " <Placemark>" );                          sb.append( " <Placemark>\n" );
59                          sb.append( "  <description>IP=" + ip + "  Time=" + time + "</description>" );                          sb.append( "  <description>IP=" + ip + "  Time=" + time + "</description>\n" );
60                          sb.append( "  <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>" );                          sb.append( "  <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>\n" );
61                          sb.append( " </Placemark>" );                          sb.append( " </Placemark>\n" );
62                                                                    
63                          }                          }
64                                                    
# Line 57  public class RequestPlotter extends Http Line 67  public class RequestPlotter extends Http
67                                                    
68                  } catch (Exception e) {                  } catch (Exception e) {
69                          sb.append("<!-- error -->");                          sb.append("<!-- error -->");
70                            log.log(Level.SEVERE, "getKml()", e);
71                  }                  }
72                                    
73                  return sb.toString();                  return sb.toString();
# Line 64  public class RequestPlotter extends Http Line 75  public class RequestPlotter extends Http
75    
76          @Override          @Override
77          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
78                   resp.setContentType("application/vnd.google-earth.kml");                  String data = getKml();
79                   resp.getWriter().print( getKml() );                  
80                    if (req.getParameter("zip") != null) {
81                            
82                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
83                            
84                            ZipOutputStream zip = new ZipOutputStream(baos);
85                            zip.putNextEntry( new ZipEntry("trains.kml") );
86                            zip.write( data.getBytes() );
87                            zip.closeEntry();
88                            zip.close();
89                                                    
90                            byte bytes[] = baos.toByteArray();
91                            
92                            resp.setContentType(KMZ);
93                            resp.setContentLength( bytes.length );
94                            resp.getOutputStream().write(bytes);
95                            
96                    } else {
97                            resp.setContentType(KML);
98                            resp.getWriter().print( getKml() );
99                    }
100          }          }
101                    
102                    

Legend:
Removed from v.795  
changed lines
  Added in v.798

  ViewVC Help
Powered by ViewVC 1.1.20