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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1080 - (show annotations) (download)
Mon Sep 20 20:11:55 2010 UTC (13 years, 7 months ago) by torben
File size: 2456 byte(s)
Add a copy with partial support for google app engine
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.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.simpleframework.xml.Serializer;
14 import org.simpleframework.xml.core.Persister;
15
16 import dk.thoerup.android.traininfo.common.MetroBean;
17 import dk.thoerup.circuitbreaker.CircuitBreakerException;
18 import dk.thoerup.traininfoservice.Statistics;
19
20
21
22
23
24 public class MetroServlet extends HttpServlet {
25
26 private static final long serialVersionUID = 1L;
27
28 Logger logger = Logger.getLogger(MetroServlet.class.getName());
29
30 MetroFetcher fetcher = new MetroFetcher();
31
32 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
33
34 if (req.getParameter("station") == null) {
35 resp.sendError(400, "not enough parameters");
36 return;
37 }
38
39 //TODO: How should statistics be handled for the metro
40 //Statistics.getInstance().incrementDepartureLookups();
41
42 int station = Integer.parseInt( req.getParameter("station") );
43
44
45 MetroBean bean;
46
47 try {
48
49 bean = fetcher.cachedLookupMetroDepartures(station);
50 String xml = formatXml(bean);
51
52 resp.setDateHeader("Expires", 0);
53 resp.setHeader("Cache-Control", "no-cache, must-revalidate");
54 resp.setContentType("text/xml");
55 resp.getWriter().print(xml);
56
57 } 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 }
73
74
75 }
76
77 String formatXml(MetroBean metro) throws ServletException {
78
79 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 }
88
89 return out.toString();
90 }
91
92 }

  ViewVC Help
Powered by ViewVC 1.1.20