--- android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/04/19 16:31:11 1311 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/RequestPlotter.java 2011/05/02 18:27:05 1427 @@ -17,7 +17,6 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.zip.GZIPInputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -27,7 +26,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import dk.thoerup.traininfoservice.banedk.TimeoutMap; +import dk.thoerup.genericjavautils.TimeoutMap; @WebServlet(urlPatterns={"/RequestPlotter"}) public class RequestPlotter extends HttpServlet { @@ -79,24 +78,16 @@ return empty; } - int from = logFiles.length - (count); + int from = (count>logFiles.length) ? 0 : logFiles.length - (count); int to = logFiles.length; return Arrays.copyOfRange(logFiles, from, to); } - protected PositionContainer getRequestsFromFileWorker(boolean multiple) throws IOException{ + protected PositionContainer getRequestsFromFileWorker(int count) throws IOException{ PositionContainer positions = new PositionContainer(); - try { - - int count; - - if (multiple == false) { - count = 1; - } else { - count = 4; - } + try { File files[] = getFiles(count); @@ -276,8 +267,9 @@ for(RequestPosition current : list) { String id = color + count++; sb.append( " \n" ); + sb.append( " ").append(current.time).append("\n" ); sb.append( " #").append(color).append("\n" ); - sb.append( " Time=").append(current.time).append("]]>\n" ); + sb.append( " ]]>\n" ); sb.append( " ").append(current.lng).append(",").append(current.lat).append(",0\n" ); sb.append( " \n" ); } @@ -345,20 +337,16 @@ return sb.toString(); } - protected String getRequestsFromFile(boolean multiple) throws IOException { + protected String getRequestsFromFile(int count) throws IOException { String kmlData = null; String key; - if (multiple == false) { - key = "kmldata"; - } else { - key = "kmldata-multi"; - } + key = "kmldata-" + count; - kmlData = null;// = cache.get(key); + kmlData = cache.get(key); if (kmlData == null) { - kmlData = formatXml( getRequestsFromFileWorker(multiple) ); + kmlData = formatXml( getRequestsFromFileWorker(count) ); cache.put(key, kmlData); kmlData += ""; } else { @@ -381,12 +369,27 @@ return (p != 0); } + int getCount(String param) { + if (param == null || param.equals("")) { + return 1; + } + + return Integer.parseInt(param); + } + + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - boolean multiple = enabled( req.getParameter("multi") ); - String kmlData = getRequestsFromFile(multiple); + int count = getCount( req.getParameter("count") ); + + if (count > 30) //limit to 30 days + count = 30; + if (count < 0) //negative count is not allowed + count = 0; + + String kmlData = getRequestsFromFile(count); if ( enabled(req.getParameter("zip")) ) { @@ -401,7 +404,8 @@ byte bytes[] = baos.toByteArray(); resp.setContentType(KMZ); - resp.setContentLength( bytes.length ); + resp.setHeader("Content-disposition", "attachment; filename=Traininfo-requestplotter.kmz"); + resp.setContentLength( bytes.length ); resp.getOutputStream().write(bytes); } else {