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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 421 - (show annotations) (download)
Thu Oct 8 12:19:42 2009 UTC (14 years, 7 months ago) by torben
File size: 2673 byte(s)
Enabled the usage of Circuit breaker to guard agains bane.dk failures
1 package dk.thoerup.traininfoservice.banedk;
2
3 import java.io.IOException;
4 import java.util.List;
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 dk.thoerup.curcuitbreaker.CircuitBreakerException;
14
15 /**
16 * Servlet implementation class TimetableServlet
17 */
18 public class TimetableServlet extends HttpServlet {
19 private static final long serialVersionUID = 1L;
20
21
22 Logger logger = Logger.getLogger( TimetableServlet.class.getName() );
23
24 TimetableFetcher fetcher = new TimetableFetcher();
25
26 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27 String train = request.getParameter("train").trim();
28
29 String sections[] = train.split(" ");
30 if (sections.length != 2) {
31 response.sendError(400, "Invalid parameter");
32 return;
33 }
34
35 String type = lookupType(sections[0]);
36 String trainID = sections[1];
37 try
38 {
39 List<TimetableBean> timetable = fetcher.cachedLookupTimetable(trainID, type);
40
41 String responseXML = formatTimetable(timetable);
42
43 response.setContentType("text/xml");
44 response.getWriter().print(responseXML);
45 } catch (java.net.SocketTimeoutException ste) {
46 logger.warning("(timetable) Read timed out, train="+train);
47 response.sendError(500, "Backend timed out");
48 } catch (CircuitBreakerException cbe) {
49 logger.warning("Circuitbreaker - failing fast");
50 response.sendError(500);
51 }catch (Throwable e) {
52 logger.log(Level.WARNING, "failure looking up timetable: train=" +train, e);
53 response.sendError(500);
54 }
55
56 }
57
58 String lookupType(String trainCode) {
59 trainCode = trainCode.toLowerCase();
60
61 if (trainCode.equals("a") || trainCode.equals("b") || trainCode.equals("bx") || trainCode.equals("c") ||
62 trainCode.equals("e") || trainCode.equals("f") || trainCode.equals("h")) {
63 return "S2";
64 } else {
65 return "FJRN";
66 }
67 }
68
69 String formatTimetable(List<TimetableBean> timetable) {
70 StringBuffer sb = new StringBuffer();
71
72 sb.append("<timetable>");
73
74 for (TimetableBean bean : timetable) {
75 sb.append("<entry");
76 if (bean.isCurrent())
77 sb.append(" current=\"true\"");
78 sb.append(">");
79
80 sb.append("<station>").append( bean.getStation() ).append("</station>");
81 sb.append("<arrival>").append( bean.getArrival() ).append("</arrival>");
82 sb.append("<departure>").append( bean.getDeparture() ).append("</departure>");
83
84 sb.append("</entry>");
85 }
86
87 sb.append("</timetable>");
88
89 return sb.toString();
90 }
91
92 }

  ViewVC Help
Powered by ViewVC 1.1.20