1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.ByteArrayInputStream; |
4 |
|
import java.io.ByteArrayOutputStream; |
5 |
|
import java.io.File; |
6 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.util.List; |
|
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; |
|
16 |
import javax.xml.parsers.DocumentBuilderFactory; |
import javax.xml.transform.Source; |
17 |
import javax.xml.transform.OutputKeys; |
import javax.xml.transform.Templates; |
18 |
import javax.xml.transform.Transformer; |
import javax.xml.transform.Transformer; |
19 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.dom.DOMSource; |
|
20 |
import javax.xml.transform.stream.StreamResult; |
import javax.xml.transform.stream.StreamResult; |
21 |
|
import javax.xml.transform.stream.StreamSource; |
22 |
|
|
23 |
import org.w3c.dom.DOMImplementation; |
import org.simpleframework.xml.Serializer; |
24 |
import org.w3c.dom.Document; |
import org.simpleframework.xml.core.Persister; |
|
import org.w3c.dom.Element; |
|
25 |
|
|
26 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
27 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
28 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
29 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
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 |
84 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
85 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
86 |
|
|
87 |
List<DepartureBean> beans; |
DepartureBean beans; |
|
|
|
|
String stationName = StationDAO.getStationName(station); |
|
88 |
|
|
89 |
try { |
try { |
90 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
91 |
|
beans.arrival = arrival; |
92 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
93 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
94 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
99 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
100 |
resp.sendError(500); |
resp.sendError(500); |
101 |
return; |
return; |
102 |
|
} catch (StationDAO.NostationException nse) { |
103 |
|
logger.log(Level.WARNING, "Station not in Database, station=" +station); |
104 |
|
Statistics.getInstance().incrementDepartureErrors(); |
105 |
|
resp.sendError(400, "invalid station ID"); |
106 |
|
return; |
107 |
} catch (Exception e) { |
} catch (Exception e) { |
108 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
109 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
116 |
|
|
117 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
118 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
119 |
resp.getWriter().print( formatXml(beans, stationName) ); |
resp.getWriter().print( formatXml(beans) ); |
120 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
121 |
|
/* |
122 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
123 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
124 |
|
|
125 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
|
req.setAttribute("stationname", stationName ); |
|
126 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
127 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
128 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/ |
129 |
|
resp.setContentType("text/html"); |
130 |
|
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
131 |
} else { |
} else { |
132 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
133 |
} |
} |
134 |
|
|
135 |
} |
} |
136 |
|
|
137 |
protected String formatXml(List<DepartureBean> beans, String stationName) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
138 |
String xml = ""; |
|
139 |
try { |
Serializer serializer = new Persister(); |
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
|
|
DOMImplementation impl = builder.getDOMImplementation(); |
|
140 |
|
|
141 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
Document doc = impl.createDocument(null,null,null); |
|
|
Element root = doc.createElement("departureinfo"); |
|
|
root.setAttribute("station", stationName); |
|
|
for (DepartureBean departure : beans) { |
|
|
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(); |
|
142 |
|
|
143 |
|
try { |
144 |
|
serializer.write(beans, out); |
145 |
|
} catch (Exception e) { |
146 |
|
throw new ServletException(e); |
147 |
|
} |
148 |
|
|
149 |
|
return out.toString(); |
150 |
|
} |
151 |
|
|
152 |
|
protected String xmlToHtml(String input) throws ServletException { |
153 |
|
|
154 |
|
try { |
155 |
|
Transformer trans = xslTemplate.newTransformer(); |
156 |
|
|
157 |
|
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
158 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
159 |
|
trans.transform(xml, new StreamResult(out)); |
160 |
|
|
161 |
|
return out.toString(); |
162 |
} catch (Exception e) { |
} catch (Exception e) { |
163 |
throw new ServletException(e); |
throw new ServletException(e); |
164 |
} |
} |
|
return xml; |
|
165 |
} |
} |
166 |
|
|
167 |
} |
} |