--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2011/04/04 10:56:44 1255 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureServlet.java 2011/04/28 10:13:36 1396 @@ -17,8 +17,10 @@ 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; @@ -43,11 +45,9 @@ 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(); @@ -90,19 +90,19 @@ type = FetchTrainType.valueOf( req.getParameter("type") ); } + try { 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(); @@ -135,7 +135,13 @@ } 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{