1 |
torben |
307 |
package dk.thoerup.traininfoservice.banedk;
|
2 |
|
|
|
3 |
|
|
import java.io.IOException;
|
4 |
|
|
import java.sql.Connection;
|
5 |
|
|
import java.sql.ResultSet;
|
6 |
|
|
import java.sql.Statement;
|
7 |
|
|
import java.util.List;
|
8 |
torben |
348 |
import java.util.logging.Level;
|
9 |
|
|
import java.util.logging.Logger;
|
10 |
torben |
307 |
|
11 |
|
|
import javax.servlet.ServletException;
|
12 |
|
|
import javax.servlet.http.HttpServlet;
|
13 |
|
|
import javax.servlet.http.HttpServletRequest;
|
14 |
|
|
import javax.servlet.http.HttpServletResponse;
|
15 |
|
|
import javax.xml.parsers.DocumentBuilder;
|
16 |
|
|
import javax.xml.parsers.DocumentBuilderFactory;
|
17 |
|
|
import javax.xml.transform.OutputKeys;
|
18 |
|
|
import javax.xml.transform.Transformer;
|
19 |
|
|
import javax.xml.transform.TransformerFactory;
|
20 |
|
|
import javax.xml.transform.dom.DOMSource;
|
21 |
|
|
import javax.xml.transform.stream.StreamResult;
|
22 |
|
|
|
23 |
|
|
import org.w3c.dom.DOMImplementation;
|
24 |
|
|
import org.w3c.dom.Document;
|
25 |
|
|
import org.w3c.dom.Element;
|
26 |
|
|
|
27 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreakerException;
|
28 |
torben |
307 |
import dk.thoerup.traininfoservice.DBConnection;
|
29 |
|
|
|
30 |
|
|
/**
|
31 |
|
|
* Servlet implementation class DepartureServlet
|
32 |
|
|
*/
|
33 |
|
|
public class DepartureServlet extends HttpServlet {
|
34 |
|
|
private static final long serialVersionUID = 1L;
|
35 |
torben |
348 |
|
36 |
|
|
Logger logger = Logger.getLogger( DepartureServlet.class.getName() );
|
37 |
torben |
307 |
|
38 |
|
|
DepartureFetcher fetcher = new DepartureFetcher();
|
39 |
|
|
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
40 |
|
|
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
|
41 |
|
|
|
42 |
|
|
|
43 |
torben |
425 |
@Override
|
44 |
torben |
307 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
45 |
torben |
458 |
if (req.getParameter("station") == null) {
|
46 |
|
|
resp.sendError(400, "not enough parameters");
|
47 |
|
|
return;
|
48 |
|
|
}
|
49 |
|
|
|
50 |
torben |
307 |
int station = Integer.parseInt( req.getParameter("station") );
|
51 |
|
|
String format = req.getParameter("format");
|
52 |
|
|
|
53 |
|
|
List<DepartureBean> beans;
|
54 |
|
|
|
55 |
|
|
String stationName = getStationName(station);
|
56 |
|
|
|
57 |
|
|
try {
|
58 |
torben |
387 |
beans = fetcher.cachedLookupDepartures(station);
|
59 |
torben |
462 |
} catch (java.io.IOException ioe) {
|
60 |
|
|
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() );
|
61 |
torben |
348 |
resp.sendError(500);
|
62 |
|
|
return;
|
63 |
torben |
421 |
} catch (CircuitBreakerException cbe) {
|
64 |
|
|
logger.warning("Circuitbreaker - failing fast");
|
65 |
|
|
resp.sendError(500);
|
66 |
|
|
return;
|
67 |
torben |
451 |
} catch (Exception e) {
|
68 |
torben |
348 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e);
|
69 |
|
|
resp.sendError(500);
|
70 |
|
|
return;
|
71 |
torben |
307 |
}
|
72 |
|
|
|
73 |
|
|
resp.setDateHeader("Expires", 0);
|
74 |
|
|
resp.setHeader("Cache-Control", "no-cache, must-revalidate");
|
75 |
|
|
|
76 |
|
|
if (format.equalsIgnoreCase("xml")) {
|
77 |
|
|
resp.setContentType("text/xml");
|
78 |
|
|
resp.getWriter().print( formatXml(beans, stationName) );
|
79 |
|
|
} else if (format.equalsIgnoreCase("html")) {
|
80 |
|
|
req.setAttribute("stationname", stationName );
|
81 |
|
|
req.setAttribute("departurebeans", beans);
|
82 |
|
|
req.setAttribute("stationID", station );
|
83 |
|
|
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);
|
84 |
|
|
} else {
|
85 |
|
|
throw new ServletException("Unknown format");
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
}
|
89 |
|
|
|
90 |
|
|
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{
|
91 |
|
|
String xml = "";
|
92 |
|
|
try {
|
93 |
|
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
|
94 |
|
|
DOMImplementation impl = builder.getDOMImplementation();
|
95 |
|
|
|
96 |
|
|
|
97 |
|
|
Document doc = impl.createDocument(null,null,null);
|
98 |
|
|
Element root = doc.createElement("departureinfo");
|
99 |
|
|
root.setAttribute("station", stationName);
|
100 |
|
|
for (DepartureBean departure : beans) {
|
101 |
|
|
Element train = doc.createElement("train");
|
102 |
|
|
|
103 |
|
|
Element time = doc.createElement("time");
|
104 |
|
|
time.setTextContent( departure.getTime() );
|
105 |
|
|
train.appendChild(time);
|
106 |
|
|
|
107 |
|
|
Element updated = doc.createElement("updated");
|
108 |
|
|
updated.setTextContent( String.valueOf(departure.getUpdated()) );
|
109 |
|
|
train.appendChild(updated);
|
110 |
|
|
|
111 |
|
|
Element trainNumber = doc.createElement("trainnumber");
|
112 |
|
|
trainNumber.setTextContent( departure.getTrainNumber() );
|
113 |
|
|
train.appendChild(trainNumber);
|
114 |
|
|
|
115 |
|
|
Element destination = doc.createElement("destination");
|
116 |
|
|
destination.setTextContent( departure.getDestination());
|
117 |
|
|
train.appendChild(destination);
|
118 |
|
|
|
119 |
|
|
Element origin = doc.createElement("origin");
|
120 |
|
|
origin.setTextContent( departure.getOrigin() );
|
121 |
|
|
train.appendChild(origin);
|
122 |
|
|
|
123 |
|
|
Element location= doc.createElement("location");
|
124 |
|
|
location.setTextContent( departure.getLocation() );
|
125 |
|
|
train.appendChild(location);
|
126 |
|
|
|
127 |
|
|
Element status = doc.createElement("status");
|
128 |
|
|
status.setTextContent( departure.getStatus() );
|
129 |
|
|
train.appendChild(status);
|
130 |
|
|
|
131 |
|
|
Element note = doc.createElement("note");
|
132 |
|
|
note.setTextContent( departure.getNote() );
|
133 |
|
|
train.appendChild(note);
|
134 |
|
|
|
135 |
|
|
root.appendChild(train);
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
doc.appendChild(root);
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
DOMSource domSource = new DOMSource(doc);
|
142 |
|
|
|
143 |
|
|
Transformer transformer = transformerFactory.newTransformer();
|
144 |
|
|
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
|
145 |
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
|
146 |
|
|
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
|
147 |
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
|
148 |
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
149 |
|
|
java.io.StringWriter sw = new java.io.StringWriter();
|
150 |
|
|
StreamResult sr = new StreamResult(sw);
|
151 |
|
|
transformer.transform(domSource, sr);
|
152 |
|
|
xml = sw.toString();
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
} catch (Exception e) {
|
156 |
|
|
throw new ServletException(e);
|
157 |
|
|
}
|
158 |
|
|
return xml;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
protected String getStationName(int stationID) {
|
162 |
|
|
String station = "";
|
163 |
|
|
|
164 |
|
|
Connection conn = null;
|
165 |
|
|
try {
|
166 |
|
|
conn = DBConnection.getConnection();
|
167 |
|
|
Statement stmt = conn.createStatement();
|
168 |
|
|
ResultSet rs = stmt.executeQuery("SELECT name FROM trainstations WHERE id=" + stationID);
|
169 |
|
|
if (rs.next()) {
|
170 |
|
|
station = rs.getString(1);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
} catch (Exception e) {
|
174 |
|
|
} finally {
|
175 |
|
|
try {
|
176 |
|
|
if (conn != null && !conn.isClosed())
|
177 |
|
|
conn.close();
|
178 |
|
|
} catch (Exception e) {}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
return station;
|
182 |
|
|
}
|
183 |
|
|
|
184 |
|
|
}
|