1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringWriter; |
5 |
|
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.TimetableBean; |
19 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
20 |
import dk.thoerup.traininfoservice.Statistics; |
21 |
import dk.thoerup.traininfoservice.TraininfoSettings;; |
22 |
|
23 |
/** |
24 |
* Servlet implementation class TimetableServlet |
25 |
*/ |
26 |
@WebServlet(urlPatterns={"/TimetableServlet"}) |
27 |
public class TimetableServlet extends HttpServlet { |
28 |
private static final long serialVersionUID = 1L; |
29 |
|
30 |
|
31 |
Logger logger = Logger.getLogger( TimetableServlet.class.getName() ); |
32 |
|
33 |
TimetableFetcher fetcher; |
34 |
|
35 |
|
36 |
@Override |
37 |
public void init() throws ServletException { |
38 |
super.init(); |
39 |
|
40 |
|
41 |
TraininfoSettings settings = (TraininfoSettings) getServletContext().getAttribute("settings"); |
42 |
fetcher = new TimetableFetcher(settings); |
43 |
} |
44 |
|
45 |
@Override |
46 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
47 |
if (request.getParameter("train") == null) { |
48 |
response.sendError(400, "not enough parameters (train)"); |
49 |
return; |
50 |
} |
51 |
if (request.getParameter("type") == null) { |
52 |
response.sendError(400, "not enough parameters (type)"); |
53 |
return; |
54 |
} |
55 |
Statistics.getInstance().incrementTimetableLookups(); |
56 |
|
57 |
String train = request.getParameter("train").trim(); |
58 |
String type = request.getParameter("type").trim(); |
59 |
|
60 |
try |
61 |
{ |
62 |
TimetableBean timetable = fetcher.cachedLookupTimetable(train, type); |
63 |
|
64 |
String responseXML = formatTimetable(timetable); |
65 |
|
66 |
response.setContentType("text/xml"); |
67 |
response.getWriter().print(responseXML); |
68 |
} catch (java.io.IOException ioe) { |
69 |
logger.warning("(timetable) Read failed, type=" + type + ", train="+train + ". " + ioe.getMessage()); |
70 |
Statistics.getInstance().incrementTimetableErrors(); |
71 |
response.sendError(500, "Backend timed out"); |
72 |
} catch (CircuitBreakerException cbe) { |
73 |
logger.warning("Circuitbreaker - failing fast, type=" + type + ", train="+train); |
74 |
Statistics.getInstance().incrementTimetableErrors(); |
75 |
response.sendError(500); |
76 |
}catch (Exception e) { |
77 |
logger.log(Level.WARNING, "failure looking up timetable: type=" + type + ",train=" +train, e); |
78 |
Statistics.getInstance().incrementTimetableErrors(); |
79 |
response.sendError(500); |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
String formatTimetable(TimetableBean timetable) throws ServletException { |
85 |
Serializer serializer = new Persister(); |
86 |
|
87 |
StringWriter out = new StringWriter(); |
88 |
|
89 |
try { |
90 |
serializer.write(timetable, out); |
91 |
} catch (Exception e) { |
92 |
throw new ServletException(e); |
93 |
} |
94 |
|
95 |
return out.toString(); |
96 |
} |
97 |
|
98 |
} |