1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.sql.Connection; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.Statement; |
|
|
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; |
21 |
import org.w3c.dom.Document; |
import org.w3c.dom.Document; |
22 |
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
23 |
|
|
24 |
import dk.thoerup.curcuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
25 |
import dk.thoerup.traininfoservice.DBConnection; |
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 |
|
|
34 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
35 |
|
|
36 |
DepartureFetcher fetcher = new DepartureFetcher(); |
DepartureFetcher fetcher; |
37 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
38 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
39 |
|
|
40 |
|
|
41 |
@Override |
@Override |
42 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
public void init() throws ServletException { |
43 |
|
super.init(); |
44 |
|
|
45 |
|
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); |
46 |
|
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
47 |
|
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
48 |
|
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
49 |
|
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
50 |
|
} |
51 |
|
|
52 |
|
@Override |
53 |
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
54 |
if (req.getParameter("station") == null) { |
if (req.getParameter("station") == null) { |
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(); |
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 = getStationName(station); |
|
70 |
|
|
71 |
try { |
try { |
72 |
beans = fetcher.cachedLookupDepartures(station); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
73 |
} catch (java.net.SocketTimeoutException ste) { |
} catch (java.io.IOException ioe) { |
74 |
logger.warning("Read timed out, station="+station); |
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 |
req.setAttribute("stationname", stationName ); |
|
98 |
|
String advStr = req.getParameter("advanced"); |
99 |
|
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
100 |
|
|
101 |
|
req.setAttribute("advanced", advanced); |
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"); |
161 |
note.setTextContent( departure.getNote() ); |
note.setTextContent( departure.getNote() ); |
162 |
train.appendChild(note); |
train.appendChild(note); |
163 |
|
|
164 |
|
Element type = doc.createElement("type"); |
165 |
|
type.setTextContent( departure.getType() ); |
166 |
|
train.appendChild(type); |
167 |
|
|
168 |
root.appendChild(train); |
root.appendChild(train); |
169 |
} |
} |
170 |
|
|
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); |
191 |
return xml; |
return xml; |
192 |
} |
} |
193 |
|
|
|
protected String getStationName(int stationID) { |
|
|
String station = ""; |
|
|
|
|
|
Connection conn = null; |
|
|
try { |
|
|
conn = DBConnection.getConnection(); |
|
|
Statement stmt = conn.createStatement(); |
|
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID); |
|
|
if (rs.next()) { |
|
|
station = rs.getString(1); |
|
|
} |
|
|
|
|
|
} catch (Exception e) { |
|
|
} finally { |
|
|
try { |
|
|
if (conn != null && !conn.isClosed()) |
|
|
conn.close(); |
|
|
} catch (Exception e) {} |
|
|
} |
|
|
|
|
|
return station; |
|
|
} |
|
|
|
|
194 |
} |
} |