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; |
22 |
import org.w3c.dom.Element; |
import org.w3c.dom.Element; |
23 |
|
|
24 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
25 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.StationDAO; |
26 |
|
import dk.thoerup.traininfoservice.Statistics; |
27 |
|
|
28 |
/** |
/** |
29 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
30 |
*/ |
*/ |
31 |
|
@WebServlet(urlPatterns={"/DepartureServlet"}) |
32 |
public class DepartureServlet extends HttpServlet { |
public class DepartureServlet extends HttpServlet { |
33 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
34 |
|
|
42 |
@Override |
@Override |
43 |
public void init() throws ServletException { |
public void init() throws ServletException { |
44 |
super.init(); |
super.init(); |
45 |
|
|
46 |
boolean useTempSite = Boolean.parseBoolean( getInitParameter("usetempsite") ); |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
47 |
logger.info( "DepartureServlet, use temp site=" + useTempSite ); |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
48 |
fetcher = new DepartureFetcher(useTempSite); |
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
49 |
|
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
50 |
} |
} |
51 |
|
|
52 |
@Override |
@Override |
53 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
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; |
70 |
|
|
71 |
String stationName = getStationName(station); |
String stationName = StationDAO.getStationName(station); |
72 |
|
|
73 |
try { |
try { |
74 |
beans = fetcher.cachedLookupDepartures(station); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
75 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
76 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
77 |
|
Statistics.getInstance().incrementDepartureErrors(); |
78 |
resp.sendError(500); |
resp.sendError(500); |
79 |
return; |
return; |
80 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
81 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast"); |
82 |
|
Statistics.getInstance().incrementDepartureErrors(); |
83 |
resp.sendError(500); |
resp.sendError(500); |
84 |
return; |
return; |
85 |
} catch (Exception e) { |
} catch (Exception e) { |
86 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
87 |
|
Statistics.getInstance().incrementDepartureErrors(); |
88 |
resp.sendError(500); |
resp.sendError(500); |
89 |
return; |
return; |
90 |
} |
} |
94 |
|
|
95 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
96 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
97 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans, stationName, arrival) ); |
98 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
99 |
|
|
100 |
|
String advStr = req.getParameter("advanced"); |
101 |
|
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
102 |
|
|
103 |
|
req.setAttribute("advanced", advanced); |
104 |
req.setAttribute("stationname", stationName ); |
req.setAttribute("stationname", stationName ); |
105 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
106 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
107 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
108 |
} else { |
} else { |
109 |
throw new ServletException("Unknown format"); |
resp.sendError(400, "Unknown format"); |
110 |
} |
} |
111 |
|
|
112 |
} |
} |
113 |
|
|
114 |
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{ |
protected String formatXml(DepartureBean beans, String stationName, boolean arrival) throws ServletException{ |
115 |
String xml = ""; |
String xml = ""; |
116 |
try { |
try { |
117 |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
121 |
Document doc = impl.createDocument(null,null,null); |
Document doc = impl.createDocument(null,null,null); |
122 |
Element root = doc.createElement("departureinfo"); |
Element root = doc.createElement("departureinfo"); |
123 |
root.setAttribute("station", stationName); |
root.setAttribute("station", stationName); |
124 |
for (DepartureBean departure : beans) { |
root.setAttribute("arrival", Boolean.toString(arrival) ); |
125 |
|
|
126 |
|
for (String notif : beans.notifications) { |
127 |
|
Element notElem = doc.createElement("notification"); |
128 |
|
notElem.setTextContent(notif); |
129 |
|
root.appendChild(notElem); |
130 |
|
} |
131 |
|
|
132 |
|
for (DepartureEntry departure : beans.departureEntries) { |
133 |
Element train = doc.createElement("train"); |
Element train = doc.createElement("train"); |
134 |
|
|
135 |
Element time = doc.createElement("time"); |
Element time = doc.createElement("time"); |
164 |
note.setTextContent( departure.getNote() ); |
note.setTextContent( departure.getNote() ); |
165 |
train.appendChild(note); |
train.appendChild(note); |
166 |
|
|
167 |
|
Element type = doc.createElement("type"); |
168 |
|
type.setTextContent( departure.getType() ); |
169 |
|
train.appendChild(type); |
170 |
|
|
171 |
root.appendChild(train); |
root.appendChild(train); |
172 |
} |
} |
173 |
|
|
180 |
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
181 |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
182 |
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
183 |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); |
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); |
184 |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
185 |
java.io.StringWriter sw = new java.io.StringWriter(); |
java.io.StringWriter sw = new java.io.StringWriter(); |
186 |
StreamResult sr = new StreamResult(sw); |
StreamResult sr = new StreamResult(sw); |
194 |
return xml; |
return xml; |
195 |
} |
} |
196 |
|
|
|
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; |
|
|
} |
|
|
|
|
197 |
} |
} |