1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
|
import java.io.StringReader; |
6 |
|
import java.io.StringWriter; |
7 |
import java.util.logging.Level; |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
9 |
|
|
12 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
13 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
15 |
import javax.xml.parsers.DocumentBuilder; |
import javax.xml.transform.Source; |
16 |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.transform.Templates; |
|
import javax.xml.transform.OutputKeys; |
|
17 |
import javax.xml.transform.Transformer; |
import javax.xml.transform.Transformer; |
18 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.dom.DOMSource; |
|
19 |
import javax.xml.transform.stream.StreamResult; |
import javax.xml.transform.stream.StreamResult; |
20 |
|
import javax.xml.transform.stream.StreamSource; |
21 |
|
|
22 |
import org.w3c.dom.DOMImplementation; |
import org.simpleframework.xml.Serializer; |
23 |
import org.w3c.dom.Document; |
import org.simpleframework.xml.core.Persister; |
|
import org.w3c.dom.Element; |
|
24 |
|
|
25 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
26 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
27 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
28 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
29 |
|
import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType; |
30 |
|
|
31 |
/** |
/** |
32 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
38 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
39 |
|
|
40 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
41 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
42 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
43 |
|
|
44 |
|
TransformerFactory xslTransFact; |
45 |
|
Templates xslTemplate; |
46 |
|
|
47 |
|
|
48 |
@Override |
@Override |
49 |
public void init() throws ServletException { |
public void init() throws ServletException { |
50 |
super.init(); |
super.init(); |
51 |
|
|
52 |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); |
53 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
54 |
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
55 |
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
56 |
|
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
57 |
|
|
58 |
|
xslTransFact = TransformerFactory.newInstance(); |
59 |
|
|
60 |
|
String xslPath = getServletContext().getRealPath("/departures.xsl"); |
61 |
|
Source stylesheet = new StreamSource( new File(xslPath) ); |
62 |
|
|
63 |
|
try { |
64 |
|
xslTemplate = xslTransFact.newTemplates(stylesheet); |
65 |
|
} catch (Exception e) { |
66 |
|
throw new ServletException(e); |
67 |
|
} |
68 |
} |
} |
69 |
|
|
70 |
@Override |
@Override |
71 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
72 |
if (req.getParameter("station") == null) { |
if (req.getParameter("station") == null) { |
73 |
resp.sendError(400, "not enough parameters"); |
resp.sendError(400, "not enough parameters (station)"); |
74 |
return; |
return; |
75 |
} |
} |
76 |
|
if (req.getParameter("format") == null) { |
77 |
|
resp.sendError(400, "not enough parameters (format)"); |
78 |
|
return; |
79 |
|
} |
80 |
|
|
81 |
boolean arrival = false; |
boolean arrival = false; |
82 |
try { |
try { |
88 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
89 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
90 |
|
|
91 |
DepartureBean beans; |
DepartureBean beans; |
92 |
|
|
93 |
|
DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH; |
94 |
|
if ( req.getParameter("type") != null) { |
95 |
|
type = FetchTrainType.valueOf( req.getParameter("type") ); |
96 |
|
} |
97 |
|
|
98 |
try { |
try { |
99 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival, type); |
100 |
|
beans.arrival = arrival; |
101 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
102 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
103 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
104 |
resp.sendError(500); |
resp.sendError(500); |
105 |
return; |
return; |
106 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
107 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast, station=" +station); |
108 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
109 |
resp.sendError(500); |
resp.sendError(500); |
110 |
return; |
return; |
111 |
|
} catch (StationDAO.NostationException nse) { |
112 |
|
logger.log(Level.WARNING, "Station not in Database, station=" +station); |
113 |
|
Statistics.getInstance().incrementDepartureErrors(); |
114 |
|
resp.sendError(400, "invalid station ID"); |
115 |
|
return; |
116 |
} catch (Exception e) { |
} catch (Exception e) { |
117 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
118 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
125 |
|
|
126 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
127 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
128 |
resp.getWriter().print( formatXml(beans, arrival) ); |
resp.getWriter().print( formatXml(beans) ); |
129 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
130 |
|
|
131 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
135 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
136 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
137 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
138 |
|
/*resp.setContentType("text/html"); |
139 |
|
resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/ |
140 |
} else { |
} else { |
141 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
142 |
} |
} |
143 |
|
|
144 |
} |
} |
145 |
|
|
146 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
147 |
String xml = ""; |
|
148 |
try { |
Serializer serializer = new Persister(); |
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
|
|
DOMImplementation impl = builder.getDOMImplementation(); |
|
149 |
|
|
150 |
|
StringWriter out = new StringWriter(); |
|
Document doc = impl.createDocument(null,null,null); |
|
|
Element root = doc.createElement("departureinfo"); |
|
|
root.setAttribute("station", beans.getStationName()); |
|
|
root.setAttribute("arrival", Boolean.toString(arrival) ); |
|
|
|
|
|
for (String notif : beans.notifications) { |
|
|
Element notElem = doc.createElement("notification"); |
|
|
notElem.setTextContent(notif); |
|
|
root.appendChild(notElem); |
|
|
} |
|
|
|
|
|
for (DepartureEntry departure : beans.departureEntries) { |
|
|
Element train = doc.createElement("train"); |
|
|
|
|
|
Element time = doc.createElement("time"); |
|
|
time.setTextContent( departure.getTime() ); |
|
|
train.appendChild(time); |
|
|
|
|
|
Element updated = doc.createElement("updated"); |
|
|
updated.setTextContent( String.valueOf(departure.getUpdated()) ); |
|
|
train.appendChild(updated); |
|
|
|
|
|
Element trainNumber = doc.createElement("trainnumber"); |
|
|
trainNumber.setTextContent( departure.getTrainNumber() ); |
|
|
train.appendChild(trainNumber); |
|
|
|
|
|
Element destination = doc.createElement("destination"); |
|
|
destination.setTextContent( departure.getDestination()); |
|
|
train.appendChild(destination); |
|
|
|
|
|
Element origin = doc.createElement("origin"); |
|
|
origin.setTextContent( departure.getOrigin() ); |
|
|
train.appendChild(origin); |
|
|
|
|
|
Element location= doc.createElement("location"); |
|
|
location.setTextContent( departure.getLocation() ); |
|
|
train.appendChild(location); |
|
|
|
|
|
Element status = doc.createElement("status"); |
|
|
status.setTextContent( departure.getStatus() ); |
|
|
train.appendChild(status); |
|
|
|
|
|
Element note = doc.createElement("note"); |
|
|
note.setTextContent( departure.getNote() ); |
|
|
train.appendChild(note); |
|
|
|
|
|
Element type = doc.createElement("type"); |
|
|
type.setTextContent( departure.getType() ); |
|
|
train.appendChild(type); |
|
|
|
|
|
root.appendChild(train); |
|
|
} |
|
|
|
|
|
doc.appendChild(root); |
|
|
|
|
|
|
|
|
DOMSource domSource = new DOMSource(doc); |
|
|
|
|
|
Transformer transformer = transformerFactory.newTransformer(); |
|
|
//transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
|
|
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); |
|
|
transformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1"); |
|
|
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "1"); |
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
|
java.io.StringWriter sw = new java.io.StringWriter(); |
|
|
StreamResult sr = new StreamResult(sw); |
|
|
transformer.transform(domSource, sr); |
|
|
xml = sw.toString(); |
|
151 |
|
|
152 |
|
try { |
153 |
|
serializer.write(beans, out); |
154 |
|
} catch (Exception e) { |
155 |
|
throw new ServletException(e); |
156 |
|
} |
157 |
|
|
158 |
|
return out.toString(); |
159 |
|
} |
160 |
|
|
161 |
|
protected String xmlToHtml(String input) throws ServletException { |
162 |
|
|
163 |
|
try { |
164 |
|
Transformer trans = xslTemplate.newTransformer(); |
165 |
|
|
166 |
|
|
167 |
|
Source xml = new StreamSource( new StringReader(input)); |
168 |
|
StringWriter out = new StringWriter(); |
169 |
|
trans.transform(xml, new StreamResult(out)); |
170 |
|
|
171 |
|
return out.toString(); |
172 |
} catch (Exception e) { |
} catch (Exception e) { |
173 |
throw new ServletException(e); |
throw new ServletException(e); |
174 |
} |
} |
|
return xml; |
|
175 |
} |
} |
176 |
|
|
177 |
} |
} |