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

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

  ViewVC Help
Powered by ViewVC 1.1.20