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