--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2009/10/02 15:06:08 387 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/06/16 10:03:13 862 @@ -1,9 +1,6 @@ package dk.thoerup.traininfoservice.banedk; import java.io.IOException; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.Statement; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -24,7 +21,9 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; -import dk.thoerup.traininfoservice.DBConnection; +import dk.thoerup.circuitbreaker.CircuitBreakerException; +import dk.thoerup.traininfoservice.StationDAO; +import dk.thoerup.traininfoservice.Statistics; /** * Servlet implementation class DepartureServlet @@ -34,27 +33,57 @@ Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); - DepartureFetcher fetcher = new DepartureFetcher(); + DepartureFetcher fetcher; TransformerFactory transformerFactory = TransformerFactory.newInstance(); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + @Override + public void init() throws ServletException { + super.init(); + + boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); + int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); + logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); + fetcher = new DepartureFetcher(useTempSite, cacheTimeout); + } + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + if (req.getParameter("station") == null) { + resp.sendError(400, "not enough parameters"); + return; + } + + boolean arrival = false; + try { + arrival = Integer.parseInt( req.getParameter("arrival")) != 0; + } catch (Exception e) {} + + Statistics.getInstance().incrementDepartureLookups(); + int station = Integer.parseInt( req.getParameter("station") ); String format = req.getParameter("format"); List beans; - String stationName = getStationName(station); + String stationName = StationDAO.getStationName(station); try { - beans = fetcher.cachedLookupDepartures(station); - } catch (java.net.SocketTimeoutException ste) { - logger.warning("Read timed out, station="+station); + beans = fetcher.cachedLookupDepartures(station, arrival); + } catch (java.io.IOException ioe) { + logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); + Statistics.getInstance().incrementDepartureErrors(); resp.sendError(500); return; - } catch (Exception e) { + } catch (CircuitBreakerException cbe) { + logger.warning("Circuitbreaker - failing fast"); + Statistics.getInstance().incrementDepartureErrors(); + resp.sendError(500); + return; + } catch (Exception e) { logger.log(Level.WARNING, "Unknown exception, station=" +station, e); + Statistics.getInstance().incrementDepartureErrors(); resp.sendError(500); return; } @@ -66,12 +95,17 @@ resp.setContentType("text/xml"); resp.getWriter().print( formatXml(beans, stationName) ); } else if (format.equalsIgnoreCase("html")) { + + String advStr = req.getParameter("advanced"); + boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; + + req.setAttribute("advanced", advanced); req.setAttribute("stationname", stationName ); req.setAttribute("departurebeans", beans); req.setAttribute("stationID", station ); getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); } else { - throw new ServletException("Unknown format"); + resp.sendError(400, "Unknown format"); } } @@ -121,6 +155,10 @@ note.setTextContent( departure.getNote() ); train.appendChild(note); + Element type = doc.createElement("type"); + type.setTextContent( departure.getType() ); + train.appendChild(type); + root.appendChild(train); } @@ -133,7 +171,7 @@ //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); - transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); java.io.StringWriter sw = new java.io.StringWriter(); StreamResult sr = new StreamResult(sw); @@ -147,27 +185,4 @@ return xml; } - protected String getStationName(int stationID) { - String station = ""; - - Connection conn = null; - try { - conn = DBConnection.getConnection(); - Statement stmt = conn.createStatement(); - ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); - if (rs.next()) { - station = rs.getString(1); - } - - } catch (Exception e) { - } finally { - try { - if (conn != null && !conn.isClosed()) - conn.close(); - } catch (Exception e) {} - } - - return station; - } - }