1 |
torben |
1041 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
import java.io.IOException; |
4 |
torben |
1042 |
import java.util.logging.Level; |
5 |
torben |
1041 |
import java.util.logging.Logger; |
6 |
|
|
|
7 |
|
|
import javax.servlet.ServletException; |
8 |
|
|
import javax.servlet.annotation.WebServlet; |
9 |
|
|
import javax.servlet.http.HttpServlet; |
10 |
|
|
import javax.servlet.http.HttpServletRequest; |
11 |
|
|
import javax.servlet.http.HttpServletResponse; |
12 |
|
|
|
13 |
torben |
1042 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
14 |
|
|
import dk.thoerup.traininfoservice.Statistics; |
15 |
|
|
import dk.thoerup.traininfoservice.banedk.MetroFetcher.MetroBean; |
16 |
|
|
import dk.thoerup.traininfoservice.banedk.MetroFetcher.MetroEntry; |
17 |
torben |
1041 |
|
18 |
|
|
|
19 |
torben |
1042 |
|
20 |
torben |
1041 |
@WebServlet(urlPatterns={"/MetroServlet"}) |
21 |
|
|
public class MetroServlet extends HttpServlet { |
22 |
torben |
1042 |
|
23 |
|
|
private static final long serialVersionUID = 1L; |
24 |
|
|
|
25 |
|
|
Logger logger = Logger.getLogger(MetroServlet.class.getName()); |
26 |
torben |
1041 |
|
27 |
torben |
1042 |
MetroFetcher fetcher = new MetroFetcher(); |
28 |
torben |
1041 |
|
29 |
torben |
1042 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
30 |
torben |
1041 |
|
31 |
torben |
1042 |
if (req.getParameter("station") == null) { |
32 |
|
|
resp.sendError(400, "not enough parameters"); |
33 |
|
|
return; |
34 |
|
|
} |
35 |
torben |
1041 |
|
36 |
torben |
1042 |
//TODO: How should statistics be handled for the metro |
37 |
|
|
//Statistics.getInstance().incrementDepartureLookups(); |
38 |
torben |
1041 |
|
39 |
torben |
1042 |
int station = Integer.parseInt( req.getParameter("station") ); |
40 |
torben |
1041 |
|
41 |
|
|
|
42 |
torben |
1042 |
MetroBean bean; |
43 |
torben |
1041 |
|
44 |
torben |
1042 |
try { |
45 |
torben |
1041 |
|
46 |
torben |
1042 |
bean = fetcher.cachedLookupMetroDepartures(station); |
47 |
|
|
String xml = formatXml(bean); |
48 |
torben |
1041 |
|
49 |
torben |
1042 |
resp.setDateHeader("Expires", 0); |
50 |
|
|
resp.setHeader("Cache-Control", "no-cache, must-revalidate"); |
51 |
|
|
resp.getWriter().print(xml); |
52 |
torben |
1041 |
|
53 |
torben |
1042 |
} catch (java.io.IOException ioe) { |
54 |
|
|
logger.warning("Metro read failed, station="+station + ". " + ioe.getMessage() ); |
55 |
|
|
//Statistics.getInstance().incrementDepartureErrors(); |
56 |
|
|
resp.sendError(500); |
57 |
|
|
return; |
58 |
|
|
} catch (CircuitBreakerException cbe) { |
59 |
|
|
logger.warning("Circuitbreaker - failing fast"); |
60 |
|
|
//Statistics.getInstance().incrementDepartureErrors(); |
61 |
|
|
resp.sendError(500); |
62 |
|
|
return; |
63 |
|
|
} catch (Exception e) { |
64 |
|
|
logger.log(Level.WARNING, "Unknown exception, metro-station=" +station, e); |
65 |
|
|
Statistics.getInstance().incrementDepartureErrors(); |
66 |
|
|
resp.sendError(500); |
67 |
|
|
return; |
68 |
torben |
1041 |
} |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
} |
72 |
|
|
|
73 |
torben |
1042 |
String formatXml(MetroBean metro) { |
74 |
|
|
StringBuilder sb = new StringBuilder(); |
75 |
|
|
|
76 |
|
|
sb.append("<metrodepatures>\n"); |
77 |
|
|
sb.append(" <head>" + metro.head + "</head>\n"); |
78 |
|
|
sb.append(" <operations>" + metro.operationInfo + "</operations>\n"); |
79 |
|
|
|
80 |
|
|
sb.append(" <entries>\n"); |
81 |
|
|
for (MetroEntry entry : metro.entries) { |
82 |
|
|
sb.append(" <entry>\n"); |
83 |
|
|
sb.append(" <metro>" + entry.metro + "</metro>\n"); |
84 |
|
|
sb.append(" <destination>" + entry.destination + "</destination>\n"); |
85 |
torben |
1056 |
sb.append(" <minutes>" + entry.minutes + "</minutes>\n"); |
86 |
torben |
1042 |
sb.append(" </entry>\n"); |
87 |
|
|
} |
88 |
|
|
sb.append(" </entries>\n"); |
89 |
|
|
|
90 |
|
|
sb.append(" <plan>" + metro.plan + "</plan>\n"); |
91 |
|
|
|
92 |
|
|
sb.append("</metrodepatures>\n"); |
93 |
|
|
|
94 |
|
|
|
95 |
|
|
return sb.toString(); |
96 |
|
|
} |
97 |
|
|
|
98 |
torben |
1041 |
} |