--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/09/16 17:48:17 1074 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2011/04/28 10:13:36 1396 @@ -1,7 +1,7 @@ package dk.thoerup.traininfoservice.banedk; -import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.StringWriter; import java.util.logging.Level; import java.util.logging.Logger; @@ -10,15 +10,19 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Templates; import javax.xml.transform.TransformerFactory; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import dk.thoerup.android.traininfo.common.DepartureBean; +import dk.thoerup.android.traininfo.common.StationBean; import dk.thoerup.circuitbreaker.CircuitBreakerException; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.TraininfoSettings; +import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType; +import dk.thoerup.traininfoservice.db.StationDAO; /** * Servlet implementation class DepartureServlet @@ -30,27 +34,44 @@ Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); DepartureFetcher fetcher; - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + + + + TransformerFactory xslTransFact; + Templates xslTemplate; @Override public void init() throws ServletException { super.init(); - boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); - int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); - int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); - logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); - fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); + + TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings"); + fetcher = new DepartureFetcher(settings); + + /* + xslTransFact = TransformerFactory.newInstance(); + + String xslPath = getServletContext().getRealPath("/departures.xsl"); + Source stylesheet = new StreamSource( new File(xslPath) ); + + try { + xslTemplate = xslTransFact.newTemplates(stylesheet); + } catch (Exception e) { + throw new ServletException(e); + }*/ } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getParameter("station") == null) { - resp.sendError(400, "not enough parameters"); + resp.sendError(400, "not enough parameters (station)"); return; } + if (req.getParameter("format") == null) { + resp.sendError(400, "not enough parameters (format)"); + return; + } boolean arrival = false; try { @@ -62,21 +83,31 @@ int station = Integer.parseInt( req.getParameter("station") ); String format = req.getParameter("format"); - DepartureBean beans; + DepartureBean beans; + + DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH; + if ( req.getParameter("type") != null) { + type = FetchTrainType.valueOf( req.getParameter("type") ); + } + try { - beans = fetcher.cachedLookupDepartures(station, arrival); + beans = fetcher.cachedLookupDepartures(station, arrival, type); beans.arrival = arrival; } catch (java.io.IOException ioe) { logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); Statistics.getInstance().incrementDepartureErrors(); - resp.sendError(500); + resp.sendError(500, "backend didnt answer"); return; } catch (CircuitBreakerException cbe) { - logger.warning("Circuitbreaker - failing fast"); + logger.warning("Circuitbreaker - failing fast, station=" +station); Statistics.getInstance().incrementDepartureErrors(); - resp.sendError(500); - return; + beans = generateErrorBean(1); + } catch (StationDAO.NostationException nse) { + logger.log(Level.WARNING, "Station not in Database, station=" +station); + Statistics.getInstance().incrementDepartureErrors(); + resp.sendError(400, "invalid station ID"); + return; } catch (Exception e) { logger.log(Level.WARNING, "Unknown exception, station=" +station, e); Statistics.getInstance().incrementDepartureErrors(); @@ -99,17 +130,25 @@ req.setAttribute("departurebeans", beans); req.setAttribute("stationID", station ); getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); + /*resp.setContentType("text/html"); + resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/ } else { resp.sendError(400, "Unknown format"); } - + } + + protected DepartureBean generateErrorBean(int code) { + DepartureBean bean = new DepartureBean(); + bean.stationName=""; + bean.errorCode = code; + return bean; } protected String formatXml(DepartureBean beans) throws ServletException{ Serializer serializer = new Persister(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); + StringWriter out = new StringWriter(); try { serializer.write(beans, out); @@ -119,5 +158,21 @@ return out.toString(); } + /* + protected String xmlToHtml(String input) throws ServletException { + + try { + Transformer trans = xslTemplate.newTransformer(); + + + Source xml = new StreamSource( new StringReader(input)); + StringWriter out = new StringWriter(); + trans.transform(xml, new StreamResult(out)); + + return out.toString(); + } catch (Exception e) { + throw new ServletException(e); + } + }*/ }