1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
import java.io.ByteArrayOutputStream; |
|
|
import java.io.File; |
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
|
import java.io.StringWriter; |
5 |
import java.util.logging.Level; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
10 |
import javax.servlet.http.HttpServlet; |
import javax.servlet.http.HttpServlet; |
11 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
13 |
|
import javax.xml.transform.Templates; |
|
import javax.xml.transform.Source; |
|
|
import javax.xml.transform.Transformer; |
|
14 |
import javax.xml.transform.TransformerFactory; |
import javax.xml.transform.TransformerFactory; |
|
import javax.xml.transform.stream.StreamResult; |
|
|
import javax.xml.transform.stream.StreamSource; |
|
15 |
|
|
16 |
import org.simpleframework.xml.Serializer; |
import org.simpleframework.xml.Serializer; |
17 |
import org.simpleframework.xml.core.Persister; |
import org.simpleframework.xml.core.Persister; |
18 |
|
|
19 |
import dk.thoerup.android.traininfo.common.DepartureBean; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
21 |
|
import dk.thoerup.traininfoservice.StationDAO; |
22 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
23 |
|
import dk.thoerup.traininfoservice.banedk.DepartureFetcher.FetchTrainType; |
24 |
|
|
25 |
/** |
/** |
26 |
* Servlet implementation class DepartureServlet |
* Servlet implementation class DepartureServlet |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
String xslPath; |
TransformerFactory xslTransFact; |
39 |
|
Templates xslTemplate; |
40 |
|
|
41 |
|
|
42 |
@Override |
@Override |
49 |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
logger.info( "DepartureServlet, use azure site=" + useAzureSite + ", cache=" + cacheTimeout); |
50 |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
fetcher = new DepartureFetcher(useAzureSite, cacheTimeout, replyTimeout); |
51 |
|
|
52 |
xslPath = getServletContext().getRealPath("/departures.xsl"); |
/* |
53 |
|
xslTransFact = TransformerFactory.newInstance(); |
54 |
|
|
55 |
|
String xslPath = getServletContext().getRealPath("/departures.xsl"); |
56 |
|
Source stylesheet = new StreamSource( new File(xslPath) ); |
57 |
|
|
58 |
|
try { |
59 |
|
xslTemplate = xslTransFact.newTemplates(stylesheet); |
60 |
|
} catch (Exception e) { |
61 |
|
throw new ServletException(e); |
62 |
|
}*/ |
63 |
} |
} |
64 |
|
|
65 |
@Override |
@Override |
66 |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
67 |
if (req.getParameter("station") == null) { |
if (req.getParameter("station") == null) { |
68 |
resp.sendError(400, "not enough parameters"); |
resp.sendError(400, "not enough parameters (station)"); |
69 |
return; |
return; |
70 |
} |
} |
71 |
|
if (req.getParameter("format") == null) { |
72 |
|
resp.sendError(400, "not enough parameters (format)"); |
73 |
|
return; |
74 |
|
} |
75 |
|
|
76 |
boolean arrival = false; |
boolean arrival = false; |
77 |
try { |
try { |
83 |
int station = Integer.parseInt( req.getParameter("station") ); |
int station = Integer.parseInt( req.getParameter("station") ); |
84 |
String format = req.getParameter("format"); |
String format = req.getParameter("format"); |
85 |
|
|
86 |
DepartureBean beans; |
DepartureBean beans; |
87 |
|
|
88 |
|
DepartureFetcher.FetchTrainType type = FetchTrainType.BOTH; |
89 |
|
if ( req.getParameter("type") != null) { |
90 |
|
type = FetchTrainType.valueOf( req.getParameter("type") ); |
91 |
|
} |
92 |
|
|
93 |
try { |
try { |
94 |
beans = fetcher.cachedLookupDepartures(station, arrival); |
beans = fetcher.cachedLookupDepartures(station, arrival, type); |
95 |
beans.arrival = arrival; |
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() ); |
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(); |
122 |
resp.setContentType("text/xml"); |
resp.setContentType("text/xml"); |
123 |
resp.getWriter().print( formatXml(beans) ); |
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"); |
/*resp.setContentType("text/html"); |
134 |
resp.getWriter().print( xmlToHtml(formatXml(beans)) ); |
resp.getWriter().print( xmlToHtml(formatXml(beans)) );*/ |
135 |
} else { |
} else { |
136 |
resp.sendError(400, "Unknown format"); |
resp.sendError(400, "Unknown format"); |
137 |
} |
} |
142 |
|
|
143 |
Serializer serializer = new Persister(); |
Serializer serializer = new Persister(); |
144 |
|
|
145 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
StringWriter out = new StringWriter(); |
146 |
|
|
147 |
try { |
try { |
148 |
serializer.write(beans, out); |
serializer.write(beans, out); |
152 |
|
|
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(); |
160 |
|
|
|
|
|
|
TransformerFactory transFact = TransformerFactory.newInstance(); |
|
|
|
|
|
Transformer trans = transFact.newTransformer(stylesheet); |
|
161 |
|
|
162 |
Source xml = new StreamSource( new ByteArrayInputStream(input.getBytes() )); |
Source xml = new StreamSource( new StringReader(input)); |
163 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
StringWriter out = new StringWriter(); |
164 |
trans.transform(xml, new StreamResult(out)); |
trans.transform(xml, new StreamResult(out)); |
165 |
|
|
166 |
return out.toString(); |
return out.toString(); |
167 |
} catch (Exception e) { |
} catch (Exception e) { |
168 |
return e.toString(); |
throw new ServletException(e); |
169 |
} |
} |
170 |
} |
}*/ |
171 |
|
|
172 |
} |
} |