--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2010/11/12 18:38:30 1191 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2012/05/09 17:23:50 1801 @@ -1,8 +1,6 @@ package dk.thoerup.traininfoservice.banedk; -import java.io.File; import java.io.IOException; -import java.io.StringReader; import java.io.StringWriter; import java.util.logging.Level; import java.util.logging.Logger; @@ -12,20 +10,19 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; 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; -import javax.xml.transform.stream.StreamSource; 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.StationDAO; 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 @@ -48,12 +45,11 @@ 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"); @@ -63,16 +59,16 @@ 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) { + if (req.getParameter("station") == null || req.getParameter("station").equals("") ) { resp.sendError(400, "not enough parameters (station)"); return; } - if (req.getParameter("format") == null) { + if (req.getParameter("format") == null || req.getParameter("format").equals("")) { resp.sendError(400, "not enough parameters (format)"); return; } @@ -87,21 +83,26 @@ 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, 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(); @@ -121,20 +122,26 @@ resp.setContentType("text/xml"); resp.getWriter().print( formatXml(beans) ); } else if (format.equalsIgnoreCase("html")) { - /* + String advStr = req.getParameter("advanced"); boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; req.setAttribute("advanced", advanced); 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)) ); + 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{ @@ -151,7 +158,7 @@ return out.toString(); } - + /* protected String xmlToHtml(String input) throws ServletException { try { @@ -166,6 +173,6 @@ } catch (Exception e) { throw new ServletException(e); } - } + }*/ }