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; |
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; |
29 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
30 |
|
|
31 |
/** |
/** |
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 |
54 |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
int replyTimeout = Integer.parseInt( getServletContext().getInitParameter("reply_timeout") ); |
55 |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
56 |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
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 |
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, arrival) ); |
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); |
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(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
138 |
|
|
139 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
140 |
|
|
148 |
|
|
149 |
return out.toString(); |
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) { |
163 |
|
throw new ServletException(e); |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
} |
} |