1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.logging.Level; |
5 |
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 |
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 |
|
18 |
|
19 |
|
20 |
@WebServlet(urlPatterns={"/MetroServlet"}) |
21 |
public class MetroServlet extends HttpServlet { |
22 |
|
23 |
private static final long serialVersionUID = 1L; |
24 |
|
25 |
Logger logger = Logger.getLogger(MetroServlet.class.getName()); |
26 |
|
27 |
MetroFetcher fetcher = new MetroFetcher(); |
28 |
|
29 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
30 |
|
31 |
if (req.getParameter("station") == null) { |
32 |
resp.sendError(400, "not enough parameters"); |
33 |
return; |
34 |
} |
35 |
|
36 |
//TODO: How should statistics be handled for the metro |
37 |
//Statistics.getInstance().incrementDepartureLookups(); |
38 |
|
39 |
int station = Integer.parseInt( req.getParameter("station") ); |
40 |
|
41 |
|
42 |
MetroBean bean; |
43 |
|
44 |
try { |
45 |
|
46 |
bean = fetcher.cachedLookupMetroDepartures(station); |
47 |
String xml = formatXml(bean); |
48 |
|
49 |
resp.setDateHeader("Expires", 0); |
50 |
resp.setHeader("Cache-Control", "no-cache, must-revalidate"); |
51 |
resp.getWriter().print(xml); |
52 |
|
53 |
} 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 |
} |
69 |
|
70 |
|
71 |
} |
72 |
|
73 |
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 |
sb.append(" <minutes>" + entry.minutes + "</minutes>\n"); |
86 |
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 |
} |