1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.util.List; |
|
4 |
import java.util.logging.Level; |
import java.util.logging.Level; |
5 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
6 |
|
|
7 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
8 |
|
import javax.servlet.annotation.WebServlet; |
9 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
10 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
11 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
22 |
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
23 |
|
|
24 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
|
import dk.thoerup.traininfoservice.StationDAO; |
|
25 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
26 |
|
|
27 |
/** |
/** |
28 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
29 |
*/ |
*/ |
30 |
|
@WebServlet(urlPatterns={"/DepartureServlet"}) |
31 |
public class DepartureServlet extends HttpServlet { |
public class DepartureServlet extends HttpServlet { |
32 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
33 |
|
|
42 |
public void init() throws ServletException { |
public void init() throws ServletException { |
43 |
super.init(); |
super.init(); |
44 |
|
|
45 |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); |
46 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
47 |
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
48 |
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
49 |
|
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
50 |
} |
} |
51 |
|
|
52 |
@Override |
@Override |
55 |
resp.sendError(400, "not enough parameters"); |
resp.sendError(400, "not enough parameters"); |
56 |
return; |
return; |
57 |
} |
} |
58 |
|
|
59 |
|
boolean arrival = false; |
60 |
|
try { |
61 |
|
arrival = Integer.parseInt( req.getParameter("arrival")) != 0; |
62 |
|
} catch (Exception e) {} |
63 |
|
|
64 |
Statistics.getInstance().incrementDepartureLookups(); |
Statistics.getInstance().incrementDepartureLookups(); |
65 |
|
|
66 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
67 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
68 |
|
|
69 |
List<DepartureBean> beans; |
DepartureBean beans; |
|
|
|
|
String stationName = StationDAO.getStationName(station); |
|
70 |
|
|
71 |
try { |
try { |
72 |
beans = fetcher.cachedLookupDepartures(station); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
73 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
74 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
75 |
|
Statistics.getInstance().incrementDepartureErrors(); |
76 |
resp.sendError(500); |
resp.sendError(500); |
77 |
return; |
return; |
78 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
79 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast"); |
80 |
|
Statistics.getInstance().incrementDepartureErrors(); |
81 |
resp.sendError(500); |
resp.sendError(500); |
82 |
return; |
return; |
83 |
} catch (Exception e) { |
} catch (Exception e) { |
84 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
85 |
|
Statistics.getInstance().incrementDepartureErrors(); |
86 |
resp.sendError(500); |
resp.sendError(500); |
87 |
return; |
return; |
88 |
} |
} |
92 |
|
|
93 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
94 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
95 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans, arrival) ); |
96 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
97 |
|
|
98 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
99 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
100 |
|
|
101 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
|
req.setAttribute("stationname", stationName ); |
|
102 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
103 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
104 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
105 |
} else { |
} else { |
106 |
throw new ServletException("Unknown format"); |
resp.sendError(400, "Unknown format"); |
107 |
} |
} |
108 |
|
|
109 |
} |
} |
110 |
|
|
111 |
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{ |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
112 |
String xml = ""; |
String xml = ""; |
113 |
try { |
try { |
114 |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
117 |
|
|
118 |
Document doc = impl.createDocument(null,null,null); |
Document doc = impl.createDocument(null,null,null); |
119 |
Element root = doc.createElement("departureinfo"); |
Element root = doc.createElement("departureinfo"); |
120 |
root.setAttribute("station", stationName); |
root.setAttribute("station", beans.getStationName()); |
121 |
for (DepartureBean departure : beans) { |
root.setAttribute("arrival", Boolean.toString(arrival) ); |
122 |
|
|
123 |
|
for (String notif : beans.notifications) { |
124 |
|
Element notElem = doc.createElement("notification"); |
125 |
|
notElem.setTextContent(notif); |
126 |
|
root.appendChild(notElem); |
127 |
|
} |
128 |
|
|
129 |
|
for (DepartureEntry departure : beans.departureEntries) { |
130 |
Element train = doc.createElement("train"); |
Element train = doc.createElement("train"); |
131 |
|
|
132 |
Element time = doc.createElement("time"); |
Element time = doc.createElement("time"); |
177 |
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
178 |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
179 |
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
180 |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); |
181 |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
182 |
java.io.StringWriter sw = new java.io.StringWriter(); |
java.io.StringWriter sw = new java.io.StringWriter(); |
183 |
StreamResult sr = new StreamResult(sw); |
StreamResult sr = new StreamResult(sw); |