1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.ByteArrayInputStream; |
4 |
import java.io.ByteArrayOutputStream; |
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.DocumentBuilderFactory; |
|
16 |
|
import javax.xml.transform.Source; |
17 |
|
import javax.xml.transform.Templates; |
18 |
|
import javax.xml.transform.Transformer; |
19 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
20 |
|
import javax.xml.transform.stream.StreamResult; |
21 |
|
import javax.xml.transform.stream.StreamSource; |
22 |
|
|
23 |
import org.simpleframework.xml.Serializer; |
import org.simpleframework.xml.Serializer; |
24 |
import org.simpleframework.xml.core.Persister; |
import org.simpleframework.xml.core.Persister; |
37 |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
Logger logger = Logger.getLogger( DepartureServlet.class.getName() ); |
38 |
|
|
39 |
DepartureFetcher fetcher; |
DepartureFetcher fetcher; |
40 |
TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
|
41 |
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
|
42 |
|
|
43 |
|
TransformerFactory xslTransFact; |
44 |
|
Templates xslTemplate; |
45 |
|
|
46 |
|
|
47 |
@Override |
@Override |
53 |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
54 |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
55 |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
56 |
|
|
57 |
|
xslTransFact = TransformerFactory.newInstance(); |
58 |
|
|
59 |
|
String xslPath = getServletContext().getRealPath("/departures.xsl"); |
60 |
|
Source stylesheet = new StreamSource( new File(xslPath) ); |
61 |
|
|
62 |
|
try { |
63 |
|
xslTemplate = xslTransFact.newTemplates(stylesheet); |
64 |
|
} catch (Exception e) { |
65 |
|
throw new ServletException(e); |
66 |
|
} |
67 |
} |
} |
68 |
|
|
69 |
@Override |
@Override |
87 |
|
|
88 |
try { |
try { |
89 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
90 |
|
beans.arrival = arrival; |
91 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
92 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
93 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
110 |
|
|
111 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
112 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
113 |
resp.getWriter().print( formatXml(beans, arrival) ); |
resp.getWriter().print( formatXml(beans) ); |
114 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
115 |
|
/* |
116 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
117 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
118 |
|
|
119 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
120 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
121 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
122 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/ |
123 |
|
resp.setContentType("text/html"); |
124 |
|
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
125 |
} else { |
} else { |
126 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
127 |
} |
} |
128 |
|
|
129 |
} |
} |
130 |
|
|
131 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
132 |
|
|
133 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
134 |
|
|
142 |
|
|
143 |
return out.toString(); |
return out.toString(); |
144 |
} |
} |
145 |
|
|
146 |
|
protected String xmlToHtml(String input) throws ServletException { |
147 |
|
|
148 |
|
try { |
149 |
|
Transformer trans = xslTemplate.newTransformer(); |
150 |
|
|
151 |
|
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
152 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
153 |
|
trans.transform(xml, new StreamResult(out)); |
154 |
|
|
155 |
|
return out.toString(); |
156 |
|
} catch (Exception e) { |
157 |
|
throw new ServletException(e); |
158 |
|
} |
159 |
|
} |
160 |
|
|
161 |
} |
} |