--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/09/16 13:32:10 1060 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2011/04/04 08:59:46 1253 @@ -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,14 +10,17 @@ 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.circuitbreaker.CircuitBreakerException; +import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType; /** * Servlet implementation class DepartureServlet @@ -29,8 +32,11 @@ Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); DepartureFetcher fetcher; - TransformerFactory transformerFactory = TransformerFactory.newInstance(); - DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); + + + + TransformerFactory xslTransFact; + Templates xslTemplate; @Override @@ -42,14 +48,30 @@ int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); + + /* + 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 { @@ -61,20 +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); return; } catch (CircuitBreakerException cbe) { - logger.warning("Circuitbreaker - failing fast"); + logger.warning("Circuitbreaker - failing fast, station=" +station); Statistics.getInstance().incrementDepartureErrors(); resp.sendError(500); return; + } 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(); @@ -87,7 +120,7 @@ if (format.equalsIgnoreCase("xml")) { resp.setContentType("text/xml"); - resp.getWriter().print( formatXml(beans, arrival) ); + resp.getWriter().print( formatXml(beans) ); } else if (format.equalsIgnoreCase("html")) { String advStr = req.getParameter("advanced"); @@ -97,17 +130,19 @@ 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 String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ + protected String formatXml(DepartureBean beans) throws ServletException{ Serializer serializer = new Persister(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); + StringWriter out = new StringWriter(); try { serializer.write(beans, out); @@ -117,5 +152,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); + } + }*/ }