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 |
import javax.xml.parsers.DocumentBuilderFactory;
|
14 |
import javax.xml.transform.TransformerFactory;
|
15 |
|
16 |
import org.simpleframework.xml.Serializer;
|
17 |
import org.simpleframework.xml.core.Persister;
|
18 |
|
19 |
import dk.thoerup.android.traininfo.common.DepartureBean;
|
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerException;
|
21 |
import dk.thoerup.traininfoservice.Statistics;
|
22 |
|
23 |
/**
|
24 |
* Servlet implementation class DepartureServlet
|
25 |
*/
|
26 |
@WebServlet(urlPatterns={"/DepartureServlet"})
|
27 |
public class DepartureServlet extends HttpServlet {
|
28 |
private static final long serialVersionUID = 1L;
|
29 |
|
30 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
|
31 |
|
32 |
DepartureFetcher fetcher;
|
33 |
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
34 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
35 |
|
36 |
|
37 |
@Override
|
38 |
public void init() throws ServletException {
|
39 |
super.init();
|
40 |
|
41 |
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") );
|
42 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") );
|
43 |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") );
|
44 |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout);
|
45 |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout);
|
46 |
}
|
47 |
|
48 |
@Override
|
49 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
50 |
if (req.getParameter("station") == null) {
|
51 |
resp.sendError(400, "not enough parameters");
|
52 |
return;
|
53 |
}
|
54 |
|
55 |
boolean arrival = false;
|
56 |
try {
|
57 |
arrival = Integer.parseInt( req.getParameter("arrival")) != 0;
|
58 |
} catch (Exception e) {}
|
59 |
|
60 |
Statistics.getInstance().incrementDepartureLookups();
|
61 |
|
62 |
int station = Integer.parseInt( req.getParameter("station") );
|
63 |
String format = req.getParameter("format");
|
64 |
|
65 |
DepartureBean beans;
|
66 |
|
67 |
try {
|
68 |
beans = fetcher.cachedLookupDepartures(station, arrival);
|
69 |
} catch (java.io.IOException ioe) {
|
70 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
|
71 |
Statistics.getInstance().incrementDepartureErrors();
|
72 |
resp.sendError(500);
|
73 |
return;
|
74 |
} catch (CircuitBreakerException cbe) {
|
75 |
logger.warning("Circuitbreaker - failing fast");
|
76 |
Statistics.getInstance().incrementDepartureErrors();
|
77 |
resp.sendError(500);
|
78 |
return;
|
79 |
} catch (Exception e) {
|
80 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e);
|
81 |
Statistics.getInstance().incrementDepartureErrors();
|
82 |
resp.sendError(500);
|
83 |
return;
|
84 |
}
|
85 |
|
86 |
resp.setDateHeader("Expires", 0);
|
87 |
resp.setHeader("Cache-Control", "no-cache, must-revalidate");
|
88 |
|
89 |
if (format.equalsIgnoreCase("xml")) {
|
90 |
resp.setContentType("text/xml");
|
91 |
resp.getWriter().print( formatXml(beans, arrival) );
|
92 |
} else if (format.equalsIgnoreCase("html")) {
|
93 |
|
94 |
String advStr = req.getParameter("advanced");
|
95 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false;
|
96 |
|
97 |
req.setAttribute("advanced", advanced);
|
98 |
req.setAttribute("departurebeans", beans);
|
99 |
req.setAttribute("stationID", station );
|
100 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
|
101 |
} else {
|
102 |
resp.sendError(400, "Unknown format");
|
103 |
}
|
104 |
|
105 |
}
|
106 |
|
107 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{
|
108 |
|
109 |
Serializer serializer = new Persister();
|
110 |
|
111 |
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
112 |
|
113 |
try {
|
114 |
serializer.write(beans, out);
|
115 |
} catch (Exception e) {
|
116 |
throw new ServletException(e);
|
117 |
}
|
118 |
|
119 |
return out.toString();
|
120 |
}
|
121 |
|
122 |
}
|