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

  ViewVC Help
Powered by ViewVC 1.1.20