/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroServlet.java
ViewVC logotype

Contents of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/MetroServlet.java

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20