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; |
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 |
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 { |
92 |
|
|
93 |
try { |
try { |
94 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival); |
95 |
|
beans.arrival = arrival; |
96 |
} catch (java.io.IOException ioe) { |
} catch (java.io.IOException ioe) { |
97 |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
logger.warning("Read failed, station="+station + ". " + ioe.getMessage() ); |
98 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
99 |
resp.sendError(500); |
resp.sendError(500); |
100 |
return; |
return; |
101 |
} catch (CircuitBreakerException cbe) { |
} catch (CircuitBreakerException cbe) { |
102 |
logger.warning("Circuitbreaker - failing fast"); |
logger.warning("Circuitbreaker - failing fast, station=" +station); |
103 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
104 |
resp.sendError(500); |
resp.sendError(500); |
105 |
return; |
return; |
106 |
|
} catch (StationDAO.NostationException nse) { |
107 |
|
logger.log(Level.WARNING, "Station not in Database, station=" +station); |
108 |
|
Statistics.getInstance().incrementDepartureErrors(); |
109 |
|
resp.sendError(400, "invalid station ID"); |
110 |
|
return; |
111 |
} catch (Exception e) { |
} catch (Exception e) { |
112 |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
logger.log(Level.WARNING, "Unknown exception, station=" +station, e); |
113 |
Statistics.getInstance().incrementDepartureErrors(); |
Statistics.getInstance().incrementDepartureErrors(); |
120 |
|
|
121 |
if (format.equalsIgnoreCase("xml")) { |
if (format.equalsIgnoreCase("xml")) { |
122 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
123 |
resp.getWriter().print( formatXml(beans, arrival) ); |
resp.getWriter().print( formatXml(beans) ); |
124 |
} else if (format.equalsIgnoreCase("html")) { |
} else if (format.equalsIgnoreCase("html")) { |
125 |
|
/* |
126 |
String advStr = req.getParameter("advanced"); |
String advStr = req.getParameter("advanced"); |
127 |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
boolean advanced = advStr != null ? Boolean.parseBoolean(advStr) : false; |
128 |
|
|
129 |
req.setAttribute("advanced", advanced); |
req.setAttribute("advanced", advanced); |
130 |
req.setAttribute("departurebeans", beans); |
req.setAttribute("departurebeans", beans); |
131 |
req.setAttribute("stationID", station ); |
req.setAttribute("stationID", station ); |
132 |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp); |
getServletContext().getRequestDispatcher("/ViewDepartures.jsp").forward(req,resp);*/ |
133 |
|
resp.setContentType("text/html"); |
134 |
|
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
135 |
} else { |
} else { |
136 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
137 |
} |
} |
138 |
|
|
139 |
} |
} |
140 |
|
|
141 |
protected String formatXml(DepartureBean beans, boolean arrival) throws ServletException{ |
protected String formatXml(DepartureBean beans) throws ServletException{ |
142 |
|
|
143 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
144 |
|
|
152 |
|
|
153 |
return out.toString(); |
return out.toString(); |
154 |
} |
} |
155 |
|
|
156 |
|
protected String xmlToHtml(String input) throws ServletException { |
157 |
|
|
158 |
|
try { |
159 |
|
Transformer trans = xslTemplate.newTransformer(); |
160 |
|
|
161 |
|
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
162 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
163 |
|
trans.transform(xml, new StreamResult(out)); |
164 |
|
|
165 |
|
return out.toString(); |
166 |
|
} catch (Exception e) { |
167 |
|
throw new ServletException(e); |
168 |
|
} |
169 |
|
} |
170 |
|
|
171 |
} |
} |