14 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
15 |
|
|
16 |
import javax.xml.transform.Source; |
import javax.xml.transform.Source; |
17 |
|
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; |
20 |
import javax.xml.transform.stream.StreamResult; |
import javax.xml.transform.stream.StreamResult; |
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 |
/** |
/** |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
String xslPath; |
TransformerFactory xslTransFact; |
45 |
|
Templates xslTemplate; |
46 |
|
|
47 |
|
|
48 |
@Override |
@Override |
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 |
xslPath = getServletContext().getRealPath("/departures.xsl"); |
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 { |
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(); |
153 |
return out.toString(); |
return out.toString(); |
154 |
} |
} |
155 |
|
|
156 |
protected String xmlToHtml(String input) { |
protected String xmlToHtml(String input) throws ServletException { |
157 |
|
|
158 |
try { |
try { |
159 |
Source stylesheet = new StreamSource( new File(xslPath) ); |
Transformer trans = xslTemplate.newTransformer(); |
|
|
|
|
|
|
|
TransformerFactory transFact = TransformerFactory.newInstance(); |
|
|
|
|
|
Transformer trans = transFact.newTransformer(stylesheet); |
|
160 |
|
|
161 |
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
162 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
164 |
|
|
165 |
return out.toString(); |
return out.toString(); |
166 |
} catch (Exception e) { |
} catch (Exception e) { |
167 |
return e.toString(); |
throw new ServletException(e); |
168 |
} |
} |
169 |
} |
} |
170 |
|
|