--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/04/19 19:04:34 650 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/09/02 18:37:49 1026 @@ -1,11 +1,11 @@ package dk.thoerup.traininfoservice.banedk; import java.io.IOException; -import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -22,11 +22,12 @@ import org.w3c.dom.Element; import dk.thoerup.circuitbreaker.CircuitBreakerException; -import dk.thoerup.traininfoservice.StationDAO; +import dk.thoerup.traininfoservice.Statistics; /** * Servlet implementation class DepartureServlet */ +@WebServlet(urlPatterns={"/DepartureServlet"}) public class DepartureServlet extends HttpServlet { private static final long serialVersionUID = 1L; @@ -43,36 +44,45 @@ boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); + int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); - fetcher = new DepartureFetcher(useTempSite, cacheTimeout); + fetcher = new DepartureFetcher(useTempSite, cacheTimeout, replyTimeout); } @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + 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 = StationDAO.getStationName(station); + DepartureBean beans; try { - beans = fetcher.cachedLookupDepartures(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 (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; } @@ -82,24 +92,23 @@ if (format.equalsIgnoreCase("xml")) { resp.setContentType("text/xml"); - resp.getWriter().print( formatXml(beans, stationName) ); + resp.getWriter().print( formatXml(beans, arrival) ); } 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"); } } - protected String formatXml(List beans, String stationName) throws ServletException{ + protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ String xml = ""; try { DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); @@ -108,8 +117,16 @@ Document doc = impl.createDocument(null,null,null); Element root = doc.createElement("departureinfo"); - root.setAttribute("station", stationName); - for (DepartureBean departure : beans) { + root.setAttribute("station", beans.getStationName()); + root.setAttribute("arrival", Boolean.toString(arrival) ); + + for (String notif : beans.notifications) { + Element notElem = doc.createElement("notification"); + notElem.setTextContent(notif); + root.appendChild(notElem); + } + + for (DepartureEntry departure : beans.departureEntries) { Element train = doc.createElement("train"); Element time = doc.createElement("time"); @@ -144,6 +161,10 @@ note.setTextContent( departure.getNote() ); train.appendChild(note); + Element type = doc.createElement("type"); + type.setTextContent( departure.getType() ); + train.appendChild(type); + root.appendChild(train); } @@ -156,7 +177,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);