/[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 800 by torben, Mon Jun 7 10:02:26 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.text.ParseException;
9    import java.text.SimpleDateFormat;
10    import java.util.ArrayList;
11    import java.util.Date;
12    import java.util.List;
13    import java.util.logging.Level;
14    import java.util.logging.Logger;
15    import java.util.zip.ZipEntry;
16    import java.util.zip.ZipOutputStream;
17    
18  import javax.servlet.ServletException;  import javax.servlet.ServletException;
19  import javax.servlet.http.HttpServlet;  import javax.servlet.http.HttpServlet;
# Line 13  import javax.servlet.http.HttpServletRes Line 22  import javax.servlet.http.HttpServletRes
22    
23  public class RequestPlotter extends HttpServlet {  public class RequestPlotter extends HttpServlet {
24          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
25            
26          protected String getKml() {          static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
27                  StringBuilder sb = new StringBuilder();  
28            static final String KML = "application/vnd.google-earth.kml";
29            static final String KMZ = "application/vnd.google-earth.kmz";
30    
31            class RequestPosition {
32                    public String ip;
33                    public Date time;
34                    public String lat;
35                    public String lng;
36            }
37    
38    
39    
40            protected List<RequestPosition> getRequestsFromFile() throws IOException{
41                    List<RequestPosition> positions = new ArrayList<RequestPosition>();
42    
43                  try {                  try {
44                          FileInputStream fis = new FileInputStream("/var/log/apache2/access.log");                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
45                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
46                            
47                                            SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
48                          sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );  
                         sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );  
                         sb.append( "<Document>\n" );  
                           
49                          String line;                          String line;
50                          while ( (line=in.readLine()) != null) {                          while ( (line=in.readLine()) != null) {
51                                  if (line.indexOf("LocateStation") == -1 ){                                  if (line.indexOf("LocateStation") == -1 ){
52                                          continue;                                          continue;
53                                  }                                  }
54                                    
55                                  if (line.indexOf("latitude") == -1 ) {                                  if (line.indexOf("latitude") == -1 ) {
56                                          continue;                                          continue;
57                                  }                                  }
58                                                                    RequestPosition pos = new RequestPosition();
59    
60                                  String toks[] = line.split(" ");                                  String toks[] = line.split(" ");
61                                  String ip = toks[0];                                  pos.ip = toks[0];
62                                  String time = toks[3].replace("[", "");  
63                                  String argpart = toks[6].split("?")[1];                                  pos.time = df.parse( toks[3].replace("[", "") );
64                                    
65                                    String argpart = toks[6].split("\\?")[1];
66    
67                                  String args[] = argpart.split("&");                                  String args[] = argpart.split("&");
68                                    
69                                  String lat = args[0].split("=")[1];                                  pos.lat = args[0].split("=")[1];
70                                  String lng = args[0].split("=")[1];                                  pos.lng = args[1].split("=")[1];
71                                    
72                          sb.append( " <Placemark>" );                                  positions.add(pos);
                         sb.append( "  <description>IP=" + ip + "  Time=" + time + "</description>" );  
                         sb.append( "  <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>" );  
                         sb.append( " </Placemark>" );  
                                   
73                          }                          }
74                    } catch (ParseException pe) {
75                            log.log(Level.SEVERE, "parseException", pe);
76                            throw new IOException(pe);
77                    } catch (IOException e) {
78                            log.log(Level.SEVERE, "getKml()", e);
79                            throw e;
80                    }
81    
82                    return positions;
83            }
84    
85            protected String formatXml(List<RequestPosition> list) {
86                    StringBuilder sb = new StringBuilder();
87    
88                    sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
89                    sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
90                    sb.append( "<Document>\n" );            
91                    
92                    
93                    sb.append( " <Style id=\"red\">\n" );
94                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
95                    sb.append( " </Style>\n");
96    
97                    sb.append( " <Style id=\"yellow\">\n" );
98                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
99                    sb.append( " </Style>\n" );
100    
101                    sb.append( " <Style id=\"green\">\n" );
102                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
103                    sb.append( " </Style>\n\n" );          
104    
105    
106    
107    
108                    Date now = new Date();
109                    for(RequestPosition current : list) {
110                            
111                            String style;
112                            long timediff = now.getTime() - current.time.getTime();
113                                                    
114                          sb.append(  "</Document>\n" );                          if ( timediff < (3*60*60*1000) ) {
115                          sb.append( "</kml>\n" );                                  style = "#red";
116                            } else if ( timediff < (24*60*60*1000)) {
117                                    style = "#yellow";
118                            } else {
119                                    style = "#green";
120                            }
121                                                    
122                  } catch (Exception e) {                          sb.append( " <Placemark>\n" );
123                          sb.append("<!-- error -->");                          sb.append( "  <styleUrl>" + style + "</styleUrl>\n" );
124                            sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
125                            sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
126                            sb.append( " </Placemark>\n" );                
127                  }                  }
128                    
129                  return sb.toString();                  sb.append(  "</Document>\n" );
130                    sb.append( "</kml>\n" );
131    
132                    return sb.toString();
133          }          }
134    
135          @Override          @Override
136          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                  resp.setContentType("application/vnd.google-earth.kml");  
                  resp.getWriter().print( getKml() );  
         }  
           
           
137    
138                    String data = formatXml( getRequestsFromFile() );
139    
140                    if (req.getParameter("zip") != null) {
141    
142                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
143    
144                            ZipOutputStream zip = new ZipOutputStream(baos);
145                            zip.putNextEntry( new ZipEntry("trains.kml") );
146                            zip.write( data.getBytes() );
147                            zip.closeEntry();
148                            zip.close();
149    
150                            byte bytes[] = baos.toByteArray();
151    
152                            resp.setContentType(KMZ);
153                            resp.setContentLength( bytes.length );
154                            resp.getOutputStream().write(bytes);
155    
156                    } else {
157                            resp.setContentType(KML);
158                            resp.getWriter().print( data );
159                    }
160            }
161  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20