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