1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
import java.io.ByteArrayOutputStream; |
4 |
import java.io.IOException; |
import java.io.IOException; |
5 |
import java.net.URL; |
import java.util.logging.Level; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
8 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
11 |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletRequest; |
12 |
import javax.servlet.http.HttpServletResponse; |
import javax.servlet.http.HttpServletResponse; |
13 |
|
|
14 |
import org.jsoup.Jsoup; |
import org.simpleframework.xml.Serializer; |
15 |
import org.jsoup.nodes.Document; |
import org.simpleframework.xml.core.Persister; |
16 |
import org.jsoup.nodes.Element; |
|
17 |
import org.jsoup.select.Elements; |
import dk.thoerup.circuitbreaker.CircuitBreakerException; |
18 |
|
import dk.thoerup.traininfoservice.Statistics; |
19 |
|
import dk.thoerup.traininfoservice.banedk.MetroFetcher.MetroBean; |
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
@WebServlet(urlPatterns={"/MetroServlet"}) |
@WebServlet(urlPatterns={"/MetroServlet"}) |
25 |
public class MetroServlet extends HttpServlet { |
public class MetroServlet extends HttpServlet { |
26 |
|
|
27 |
|
private static final long serialVersionUID = 1L; |
28 |
|
|
29 |
Logger logger = Logger.getLogger(MetroServlet.class.getName()); |
Logger logger = Logger.getLogger(MetroServlet.class.getName()); |
30 |
|
|
31 |
|
MetroFetcher fetcher = new MetroFetcher(); |
32 |
|
|
33 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
|
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=7&Congestion=true&Application=MyMetro&Expiration=true"); |
|
|
Document doc = Jsoup.parse(url, 5000); |
|
|
|
|
|
String head = doc.getElementsByClass("metro-widget-headline").get(0).text(); |
|
|
|
|
|
logger.info("head: " + head); |
|
|
|
|
|
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
|
|
Elements contentElems = content.children(); |
|
|
|
|
|
logger.info("tbody"+ content.getElementsByTag("table").size() ); |
|
34 |
|
|
35 |
|
if (req.getParameter("station") == null) { |
36 |
|
resp.sendError(400, "not enough parameters"); |
37 |
|
return; |
38 |
|
} |
39 |
|
|
40 |
Element departureTable = content.getElementsByTag("table").get(0); |
//TODO: How should statistics be handled for the metro |
41 |
|
//Statistics.getInstance().incrementDepartureLookups(); |
42 |
|
|
43 |
logger.info(departureTable.text()); |
int station = Integer.parseInt( req.getParameter("station") ); |
|
logger.info(departureTable.html()); |
|
44 |
|
|
|
Elements rows = departureTable.getElementsByTag("tr"); |
|
45 |
|
|
46 |
|
MetroBean bean; |
47 |
|
|
48 |
for (int i=1; i<rows.size(); i++) { |
try { |
|
Element row = rows.get(i); |
|
|
|
|
|
Elements fields = row.children(); |
|
49 |
|
|
50 |
String metro = fields.get(0).text(); |
bean = fetcher.cachedLookupMetroDepartures(station); |
51 |
String destination = fields.get(1).text(); |
String xml = formatXml(bean); |
|
Elements minuteFields = fields.get(2).children(); |
|
|
String minutes =""; |
|
52 |
|
|
53 |
for (Element elm : minuteFields) { |
resp.setDateHeader("Expires", 0); |
54 |
minutes = minutes + elm.text() + " "; |
resp.setHeader("Cache-Control", "no-cache, must-revalidate"); |
55 |
} |
resp.setContentType("text/xml"); |
56 |
|
resp.getWriter().print(xml); |
57 |
|
|
58 |
logger.info("Row:" + metro + "/" + destination + "/" + minutes); |
} catch (java.io.IOException ioe) { |
59 |
|
logger.warning("Metro read failed, station="+station + ". " + ioe.getMessage() ); |
60 |
|
//Statistics.getInstance().incrementDepartureErrors(); |
61 |
|
resp.sendError(500); |
62 |
|
return; |
63 |
|
} catch (CircuitBreakerException cbe) { |
64 |
|
logger.warning("Circuitbreaker - failing fast"); |
65 |
|
//Statistics.getInstance().incrementDepartureErrors(); |
66 |
|
resp.sendError(500); |
67 |
|
return; |
68 |
|
} catch (Exception e) { |
69 |
|
logger.log(Level.WARNING, "Unknown exception, metro-station=" +station, e); |
70 |
|
Statistics.getInstance().incrementDepartureErrors(); |
71 |
|
resp.sendError(500); |
72 |
|
return; |
73 |
} |
} |
|
|
|
|
String driftsInfo = contentElems.get(3).text(); |
|
|
logger.info("Driftsinfo: " + driftsInfo); |
|
74 |
|
|
75 |
String koereplan = contentElems.get(5).text(); |
|
76 |
logger.info("Køreplan:" + koereplan); |
} |
77 |
|
|
78 |
|
String formatXml(MetroBean metro) throws ServletException { |
79 |
|
|
80 |
|
Serializer serializer = new Persister(); |
81 |
|
|
82 |
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
83 |
|
|
84 |
|
try { |
85 |
|
serializer.write(metro, out); |
86 |
|
} catch (Exception e) { |
87 |
|
throw new ServletException(e); |
88 |
|
} |
89 |
|
|
90 |
response.getWriter().print( doc.html() ); |
return out.toString(); |
91 |
} |
} |
92 |
|
|
93 |
} |
} |