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; |
7 |
import java.util.logging.Level; |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
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; |
|
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; |
|
import dk.thoerup.traininfoservice.StationDAO; |
|
27 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
28 |
|
|
29 |
/** |
/** |
36 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
37 |
|
|
38 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
39 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
40 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
41 |
|
|
42 |
|
String xslPath; |
43 |
|
|
44 |
|
|
45 |
@Override |
@Override |
46 |
public void init() throws ServletException { |
public void init() throws ServletException { |
47 |
super.init(); |
super.init(); |
48 |
|
|
49 |
boolean useTempSite = Boolean.parseBoolean( getServletContext().getInitParameter("usetempsite") ); |
boolean useAzureSite = Boolean.parseBoolean( getServletContext().getInitParameter("useazuresite") ); |
50 |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
int cacheTimeout = Integer.parseInt( getServletContext().getInitParameter("cache_timeout") ); |
51 |
logger.info( "DepartureServlet, use temp site=" + useTempSite + ", cache=" + cacheTimeout); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
52 |
fetcher = new DepartureFetcher(useTempSite, cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
53 |
|
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
54 |
|
|
55 |
|
xslPath = getServletContext().getRealPath("/departures.xsl"); |
56 |
} |
} |
57 |
|
|
58 |
@Override |
@Override |
76 |
|
|
77 |
try { |
try { |
78 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
79 |
|
beans.arrival = arrival; |
80 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
81 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
82 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
99 |
|
|
100 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
101 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
102 |
resp.getWriter().print( formatXml(beans, arrival) ); |
resp.getWriter().print( formatXml(beans) ); |
103 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
104 |
|
/* |
105 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
106 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
107 |
|
|
108 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
109 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
110 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
111 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/ |
112 |
|
resp.setContentType("text/html"); |
113 |
|
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
114 |
} else { |
} else { |
115 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
116 |
} |
} |
117 |
|
|
118 |
} |
} |
119 |
|
|
120 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
121 |
String xml = ""; |
|
122 |
try { |
Serializer serializer = new Persister(); |
|
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); |
|
|
DOMImplementation impl = builder.getDOMImplementation(); |
|
|
|
|
|
|
|
|
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(); |
|
123 |
|
|
124 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
125 |
|
|
126 |
|
try { |
127 |
|
serializer.write(beans, out); |
128 |
} catch (Exception e) { |
} catch (Exception e) { |
129 |
throw new ServletException(e); |
throw new ServletException(e); |
130 |
} |
} |
131 |
return xml; |
|
132 |
|
return out.toString(); |
133 |
|
} |
134 |
|
|
135 |
|
protected String xmlToHtml(String input) { |
136 |
|
|
137 |
|
try { |
138 |
|
Source stylesheet = new StreamSource( new File(xslPath) ); |
139 |
|
|
140 |
|
|
141 |
|
TransformerFactory transFact = TransformerFactory.newInstance(); |
142 |
|
|
143 |
|
Transformer trans = transFact.newTransformer(stylesheet); |
144 |
|
|
145 |
|
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
146 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
147 |
|
trans.transform(xml, new StreamResult(out)); |
148 |
|
|
149 |
|
return out.toString(); |
150 |
|
} catch (Exception e) { |
151 |
|
return e.toString(); |
152 |
|
} |
153 |
} |
} |
154 |
|
|
155 |
} |
} |