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