15 |
import org.simpleframework.xml.Serializer; |
import org.simpleframework.xml.Serializer; |
16 |
import org.simpleframework.xml.core.Persister; |
import org.simpleframework.xml.core.Persister; |
17 |
|
|
18 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
19 |
import dk.thoerup.android.traininfo.common.TimetableBean; |
import dk.thoerup.android.traininfo.common.TimetableBean; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
21 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
57 |
|
|
58 |
String train = request.getParameter("train").trim(); |
String train = request.getParameter("train").trim(); |
59 |
String type = request.getParameter("type").trim(); |
String type = request.getParameter("type").trim(); |
60 |
|
|
61 |
|
|
62 |
|
TimetableBean timetable = null; |
63 |
try |
try |
64 |
{ |
{ |
65 |
TimetableBean timetable = fetcher.cachedLookupTimetable(train, type); |
timetable = fetcher.cachedLookupTimetable(train, type); |
|
|
|
|
String responseXML = formatTimetable(timetable); |
|
66 |
|
|
|
response.setContentType("text/xml"); |
|
|
response.getWriter().print(responseXML); |
|
67 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
68 |
logger.warning("(timetable) Read failed, type=" + type + ", train="+train + ". " + ioe.getMessage()); |
logger.warning("(timetable) Read failed, type=" + type + ", train="+train + ". " + ioe.getMessage()); |
69 |
Statistics.getInstance().incrementTimetableErrors(); |
Statistics.getInstance().incrementTimetableErrors(); |
70 |
response.sendError(500, "Backend timed out"); |
timetable = generateErrorBean(1); |
71 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
72 |
logger.warning("Circuitbreaker - failing fast, type=" + type + ", train="+train); |
logger.warning("Circuitbreaker - failing fast, type=" + type + ", train="+train); |
73 |
Statistics.getInstance().incrementTimetableErrors(); |
Statistics.getInstance().incrementTimetableErrors(); |
74 |
response.sendError(500); |
timetable = generateErrorBean(1); |
75 |
}catch (Exception e) { |
}catch (Exception e) { |
76 |
logger.log(Level.WARNING, "failure looking up timetable: type=" + type + ",train=" +train, e); |
logger.log(Level.WARNING, "failure looking up timetable: type=" + type + ",train=" +train, e); |
77 |
Statistics.getInstance().incrementTimetableErrors(); |
Statistics.getInstance().incrementTimetableErrors(); |
78 |
response.sendError(500); |
response.sendError(500); |
79 |
} |
} |
80 |
|
|
81 |
|
if (timetable != null) { |
82 |
|
String responseXML = formatTimetable(timetable); |
83 |
|
|
84 |
|
response.setContentType("text/xml"); |
85 |
|
response.getWriter().print(responseXML); |
86 |
|
} |
87 |
|
|
88 |
|
} |
89 |
|
|
90 |
|
protected TimetableBean generateErrorBean(int code) { |
91 |
|
TimetableBean bean = new TimetableBean(); |
92 |
|
bean.errorCode = code; |
93 |
|
return bean; |
94 |
} |
} |
95 |
|
|
96 |
String formatTimetable(TimetableBean timetable) throws ServletException { |
String formatTimetable(TimetableBean timetable) throws ServletException { |