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