/[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 798 by torben, Mon Jun 7 08:37:29 2010 UTC revision 806 by torben, Mon Jun 7 18:35:40 2010 UTC
# Line 5  import java.io.ByteArrayOutputStream; Line 5  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.Map;
14  import java.util.logging.Level;  import java.util.logging.Level;
15  import java.util.logging.Logger;  import java.util.logging.Logger;
16  import java.util.zip.ZipEntry;  import java.util.zip.ZipEntry;
# Line 15  import javax.servlet.http.HttpServlet; Line 21  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;  import javax.servlet.http.HttpServletResponse;
23    
24    import dk.thoerup.traininfoservice.banedk.TimeoutMap;
25    
26  public class RequestPlotter extends HttpServlet {  public class RequestPlotter extends HttpServlet {
27          private static final long serialVersionUID = 1L;          private static final long serialVersionUID = 1L;
28            
29          static final Logger log = Logger.getLogger(RequestPlotter.class.getName());          static final Logger log = Logger.getLogger(RequestPlotter.class.getName());
30            
31          static final String KML = "application/vnd.google-earth.kml";          static final String KML = "application/vnd.google-earth.kml";
32          static final String KMZ = "application/vnd.google-earth.kmz";          static final String KMZ = "application/vnd.google-earth.kmz";
33                    
34          protected String getKml() {          Map<String,String> cache = new TimeoutMap<String,String>(2*60*1000);
35                  StringBuilder sb = new StringBuilder();  
36            class RequestPosition {
37                    public String ip;
38                    public Date time;
39                    public String lat;
40                    public String lng;
41            }
42    
43    
44    
45            protected List<RequestPosition> getRequestsFromFile() throws IOException{
46                    List<RequestPosition> positions = new ArrayList<RequestPosition>();
47    
48                  try {                  try {
49                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");                          FileInputStream fis = new FileInputStream("/var/log/apache2/app_access.log");
50                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );                          BufferedReader in = new BufferedReader( new InputStreamReader(fis) );
51                            
52                                            SimpleDateFormat df = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss");
53                          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" );  
                           
54                          String line;                          String line;
55                          while ( (line=in.readLine()) != null) {                          while ( (line=in.readLine()) != null) {
56                                  if (line.indexOf("LocateStation") == -1 ){                                  if (line.indexOf("LocateStation") == -1 ){
57                                          continue;                                          continue;
58                                  }                                  }
59                                    
60                                  if (line.indexOf("latitude") == -1 ) {                                  if (line.indexOf("latitude") == -1 ) {
61                                          continue;                                          continue;
62                                  }                                  }
63                                                                    RequestPosition pos = new RequestPosition();
64    
65                                  String toks[] = line.split(" ");                                  String toks[] = line.split(" ");
66                                  String ip = toks[0];                                  pos.ip = toks[0];
67                                  String time = toks[3].replace("[", "");  
68                                    pos.time = df.parse( toks[3].replace("[", "") );
69    
70                                  String argpart = toks[6].split("\\?")[1];                                  String argpart = toks[6].split("\\?")[1];
71    
                                   
72                                  String args[] = argpart.split("&");                                  String args[] = argpart.split("&");
73                                    
74                                  String lat = args[0].split("=")[1];                                  pos.lat = args[0].split("=")[1];
75                                  String lng = args[1].split("=")[1];                                  pos.lng = args[1].split("=")[1];
76                                    
77                          sb.append( " <Placemark>\n" );                                  positions.add(pos);
                         sb.append( "  <description>IP=" + ip + "  Time=" + time + "</description>\n" );  
                         sb.append( "  <Point><coordinates>" + lng + "," + lat + ",0</coordinates></Point>\n" );  
                         sb.append( " </Placemark>\n" );  
                                   
78                          }                          }
79                                            } catch (ParseException pe) {
80                          sb.append(  "</Document>\n" );                          log.log(Level.SEVERE, "parseException", pe);
81                          sb.append( "</kml>\n" );                          throw new IOException(pe);
82                                            } catch (IOException e) {
                 } catch (Exception e) {  
                         sb.append("<!-- error -->");  
83                          log.log(Level.SEVERE, "getKml()", e);                          log.log(Level.SEVERE, "getKml()", e);
84                            throw e;
85                  }                  }
86    
87                    return positions;
88            }
89    
90            protected String formatXml(List<RequestPosition> list) {
91                    StringBuilder sb = new StringBuilder();
92    
93                    sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
94                    sb.append( "<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" );
95                    sb.append( "<Document>\n" );            
96                    
97                    
98                    sb.append( " <Style id=\"red\">\n" );
99                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/red-circle.png</href></Icon></IconStyle>\n" );
100                    sb.append( " </Style>\n");
101    
102                    sb.append( " <Style id=\"yellow\">\n" );
103                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/ylw-circle.png</href></Icon></IconStyle>\n" );
104                    sb.append( " </Style>\n" );
105    
106                    sb.append( " <Style id=\"green\">\n" );
107                    sb.append( "  <IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/paddle/grn-circle.png</href></Icon></IconStyle>\n" );
108                    sb.append( " </Style>\n\n" );
109                                    
110                  return sb.toString();  /*
111                    String overlay =
112                  " <ScreenOverlay>" +
113                    " <name>Absolute Positioning: Top left</name>" +
114                    " <description>Absolute Positioning: Top left</description>" +
115                    " <visibility>1</visibility>" +
116                    "  <Icon>" +
117                    "   <href>http://code.google.com/apis/kml/documentation/top_left.jpg</href>" +
118                    "  </Icon>" +
119                    "  <overlayXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
120                    "  <screenXY x=\"0\" y=\"1\" xunits=\"fraction\" yunits=\"fraction\"/>" +
121                    "  <rotationXY x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
122                    "  <size x=\"0\" y=\"0\" xunits=\"fraction\" yunits=\"fraction\"/>" +
123                  " </ScreenOverlay>" ;
124                    
125                    sb.append(overlay);
126            */      
127                    
128    
129                    final String STYLE_GREEN = "green";
130                    final String STYLE_YELLOW = "yellow";
131                    final String STYLE_RED = "red";
132    
133                    String oldstyle = null;
134                    Date now = new Date();
135                    for(RequestPosition current : list) {
136                            
137                            String style;
138                            long timediff = now.getTime() - current.time.getTime();
139                            
140                            if ( timediff < (3*60*60*1000) ) {
141                                    style = STYLE_RED;
142                            } else if ( timediff < (24*60*60*1000)) {
143                                    style = STYLE_YELLOW;
144                            } else {
145                                    style = STYLE_GREEN;
146                            }
147                            
148                            if (  !style.equals(oldstyle) ) {
149                                    if (oldstyle != null)                          
150                                            sb.append( "</Folder>\n");
151                                    sb.append( "<Folder>\n");
152                                    sb.append( " <name>" ).append(style).append("</name>\n");
153                                    sb.append( " <open>0</open>\n" );
154                                    
155                                    oldstyle = style;
156                            }
157                            
158                            sb.append( " <Placemark>\n" );
159                            sb.append( "  <styleUrl>#" + style + "</styleUrl>\n" );
160                            sb.append( "  <description>IP=" + current.ip + "  Time=" + current.time + "</description>\n" );
161                            sb.append( "  <Point><coordinates>" + current.lng + "," + current.lat + ",0</coordinates></Point>\n" );
162                            sb.append( " </Placemark>\n" );                
163                    }
164                    sb.append( "</Folder>\n" );
165                    sb.append( "</Document>\n" );
166                    sb.append( "</kml>\n" );
167    
168                    return sb.toString();
169          }          }
170    
171          @Override          @Override
172          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
173                  String data = getKml();                  final String KEY = "kmldata";
174                    
175                    String kmlData = cache.get(KEY);
176                                    
177                    if (kmlData == null) {
178                            kmlData = formatXml( getRequestsFromFile() );
179                            cache.put(KEY, kmlData);
180                            kmlData += "<!-- from source -->";                      
181                    } else {
182                            kmlData += "<!-- cached -->";
183                    }
184    
185                    
186    
187                  if (req.getParameter("zip") != null) {                  if (req.getParameter("zip") != null) {
188                            
189                          ByteArrayOutputStream baos = new ByteArrayOutputStream();                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
190                            
191                          ZipOutputStream zip = new ZipOutputStream(baos);                          ZipOutputStream zip = new ZipOutputStream(baos);
192                          zip.putNextEntry( new ZipEntry("trains.kml") );                          zip.putNextEntry( new ZipEntry("trains.kml") );
193                          zip.write( data.getBytes() );                          zip.write( kmlData.getBytes() );
194                          zip.closeEntry();                          zip.closeEntry();
195                          zip.close();                          zip.close();
196                                                    
197                          byte bytes[] = baos.toByteArray();                          byte bytes[] = baos.toByteArray();
198                            
199                          resp.setContentType(KMZ);                          resp.setContentType(KMZ);
200                          resp.setContentLength( bytes.length );                          resp.setContentLength( bytes.length );
201                          resp.getOutputStream().write(bytes);                          resp.getOutputStream().write(bytes);
202                            
203                  } else {                  } else {
204                          resp.setContentType(KML);                          resp.setContentType(KML);
205                          resp.getWriter().print( getKml() );                          resp.getWriter().print( kmlData );
206                  }                  }
207          }          }
           
           
   
208  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20