/[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 797 by torben, Thu Jun 3 14:28:13 2010 UTC revision 801 by torben, Mon Jun 7 11:29:55 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;  import java.util.logging.Level;
14  import java.util.logging.Logger;  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 15  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          static final Logger log = Logger.getLogger(RequestPlotter.class.getName());          static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
27            
28          protected String getKml() {          static final String KML = "application/vnd.google-earth.kml";
29                  StringBuilder sb = new StringBuilder();          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/app_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                          sb.append(  "</Document>\n" );                          log.log(Level.SEVERE, "parseException", pe);
76                          sb.append( "</kml>\n" );                          throw new IOException(pe);
77                                            } catch (IOException e) {
                 } catch (Exception e) {  
                         sb.append("<!-- error -->");  
78                          log.log(Level.SEVERE, "getKml()", e);                          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                  return sb.toString();  /*
106                    String overlay =
107                  " <ScreenOverlay>" +
108                    " <name>Absolute Positioning: Top left</name>" +
109                    " <description>Absolute Positioning: Top left</description>" +
110                    " <visibility>1</visibility>" +
111                    "  <Icon>" +
112                    "   <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
113                    "  </Icon>" +
114                    "  <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
115                    "  <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
116                    "  <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
117                    "  <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
118                  " </ScreenOverlay>" ;
119                    
120                    sb.append(overlay);
121            */      
122                    
123    
124    
125    
126    
127    
128                    Date now = new Date();
129                    for(RequestPosition current : list) {
130                            
131                            String style;
132                            long timediff = now.getTime() - current.time.getTime();
133                            
134                            if ( timediff < (3*60*60*1000) ) {
135                                    style = "#red";
136                            } else if ( timediff < (24*60*60*1000)) {
137                                    style = "#yellow";
138                            } else {
139                                    style = "#green";
140                            }
141                            
142                            sb.append( " <Placemark>\n" );
143                            sb.append( "  <styleUrl>" + style + "</styleUrl>\n" );
144                            sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
145                            sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
146                            sb.append( " </Placemark>\n" );                
147                    }
148    
149                    sb.append(  "</Document>\n" );
150                    sb.append( "</kml>\n" );
151    
152                    return sb.toString();
153          }          }
154    
155          @Override          @Override
156          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() );  
         }  
           
           
157    
158                    String data = formatXml( getRequestsFromFile() );
159    
160                    if (req.getParameter("zip") != null) {
161    
162                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
163    
164                            ZipOutputStream zip = new ZipOutputStream(baos);
165                            zip.putNextEntry( new ZipEntry("trains.kml") );
166                            zip.write( data.getBytes() );
167                            zip.closeEntry();
168                            zip.close();
169    
170                            byte bytes[] = baos.toByteArray();
171    
172                            resp.setContentType(KMZ);
173                            resp.setContentLength( bytes.length );
174                            resp.getOutputStream().write(bytes);
175    
176                    } else {
177                            resp.setContentType(KML);
178                            resp.getWriter().print( data );
179                    }
180            }
181  }  }

Legend:
Removed from v.797  
changed lines
  Added in v.801

  ViewVC Help
Powered by ViewVC 1.1.20