--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/09/16 19:14:47 1075 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/11/12 18:33:38 1190 @@ -1,9 +1,9 @@ package dk.thoerup.traininfoservice.banedk; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; +import java.io.StringWriter; import java.util.logging.Level; import java.util.logging.Logger; @@ -14,6 +14,7 @@ import javax.servlet.http.HttpServletResponse; import javax.xml.transform.Source; +import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; @@ -24,6 +25,7 @@ import dk.thoerup.android.traininfo.common.DepartureBean; import dk.thoerup.circuitbreaker.CircuitBreakerException; +import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; /** @@ -39,7 +41,8 @@ - String xslPath; + TransformerFactory xslTransFact; + Templates xslTemplate; @Override @@ -52,15 +55,28 @@ logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); - xslPath = getServletContext().getRealPath("/departures.xsl"); + 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 { @@ -83,10 +99,15 @@ 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(); @@ -121,7 +142,7 @@ Serializer serializer = new Persister(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); + StringWriter out = new StringWriter(); try { serializer.write(beans, out); @@ -132,23 +153,18 @@ return out.toString(); } - protected String xmlToHtml(String input) { + protected String xmlToHtml(String input) throws ServletException { - try { - Source stylesheet = new StreamSource( new File(xslPath) ); - - - TransformerFactory transFact = TransformerFactory.newInstance(); - - Transformer trans = transFact.newTransformer(stylesheet); + try { + Transformer trans = xslTemplate.newTransformer(); Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); - ByteArrayOutputStream out = new ByteArrayOutputStream(); + StringWriter out = new StringWriter(); trans.transform(xml, new StreamResult(out)); return out.toString(); } catch (Exception e) { - return e.toString(); + throw new ServletException(e); } }