1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
import java.util.List; |
import java.io.StringWriter; |
5 |
|
|
6 |
import java.util.logging.Level; |
import java.util.logging.Level; |
7 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
8 |
|
|
12 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
13 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
14 |
|
|
15 |
|
import org.simpleframework.xml.Serializer; |
16 |
|
import org.simpleframework.xml.core.Persister; |
17 |
|
|
18 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
19 |
|
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; |
22 |
|
import dk.thoerup.traininfoservice.TraininfoSettings;; |
23 |
|
|
24 |
/** |
/** |
25 |
* Servlet implementation class TimetableServlet |
* Servlet implementation class TimetableServlet |
33 |
|
|
34 |
TimetableFetcher fetcher; |
TimetableFetcher fetcher; |
35 |
|
|
36 |
|
|
37 |
@Override |
@Override |
38 |
public void init() throws ServletException { |
public void init() throws ServletException { |
39 |
super.init(); |
super.init(); |
40 |
|
|
41 |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
|
42 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings"); |
43 |
logger.info( "TimetableServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
fetcher = new TimetableFetcher(settings); |
|
fetcher = new TimetableFetcher(useTempSite,cacheTimeout); |
|
44 |
} |
} |
45 |
|
|
46 |
@Override |
@Override |
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 |
List<TimetableBean> timetable = fetcher.cachedLookupTimetable(train, type); |
timetable = fetcher.cachedLookupTimetable(train, type); |
66 |
|
|
|
String responseXML = formatTimetable(timetable); |
|
|
|
|
|
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 |
|
response.sendError( 500, "Backend didn't answer" ); |
72 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
73 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast, type=" + type + ", train="+train); |
74 |
Statistics.getInstance().incrementTimetableErrors(); |
Statistics.getInstance().incrementTimetableErrors(); |
75 |
response.sendError(500); |
timetable = generateErrorBean(1); |
76 |
}catch (Exception e) { |
}catch (Exception e) { |
77 |
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); |
78 |
Statistics.getInstance().incrementTimetableErrors(); |
Statistics.getInstance().incrementTimetableErrors(); |
79 |
response.sendError(500); |
response.sendError(500); |
80 |
} |
} |
81 |
|
|
82 |
|
if (timetable != null) { |
83 |
|
String responseXML = formatTimetable(timetable); |
84 |
|
|
85 |
|
response.setContentType("text/xml"); |
86 |
|
response.getWriter().print(responseXML); |
87 |
|
} |
88 |
|
|
89 |
} |
} |
90 |
|
|
91 |
String formatTimetable(List<TimetableBean> timetable) { |
protected TimetableBean generateErrorBean(int code) { |
92 |
StringBuffer sb = new StringBuffer(); |
TimetableBean bean = new TimetableBean(); |
93 |
//sb.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"); |
bean.errorCode = code; |
94 |
|
return bean; |
95 |
sb.append("<timetable>"); |
} |
96 |
|
|
97 |
for (TimetableBean bean : timetable) { |
String formatTimetable(TimetableBean timetable) throws ServletException { |
98 |
sb.append("<entry"); |
Serializer serializer = new Persister(); |
99 |
if (bean.isCurrent()) |
|
100 |
sb.append(" current=\"true\""); |
StringWriter out = new StringWriter(); |
101 |
sb.append(">"); |
|
102 |
|
try { |
103 |
sb.append("<station>").append( bean.getStation() ).append("</station>"); |
serializer.write(timetable, out); |
104 |
sb.append("<arrival>").append( bean.getArrival() ).append("</arrival>"); |
} catch (Exception e) { |
105 |
sb.append("<departure>").append( bean.getDeparture() ).append("</departure>"); |
throw new ServletException(e); |
|
sb.append("<stationid>").append( bean.getStationId() ).append("</stationid>"); |
|
|
|
|
|
sb.append("</entry>"); |
|
106 |
} |
} |
107 |
|
|
108 |
sb.append("</timetable>"); |
return out.toString(); |
|
|
|
|
return sb.toString(); |
|
109 |
} |
} |
110 |
|
|
111 |
} |
} |