1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
|
4 |
import java.io.IOException; |
5 |
import java.net.URL; |
6 |
import java.util.ArrayList; |
7 |
import java.util.List; |
8 |
import java.util.logging.Logger; |
9 |
|
10 |
import com.gargoylesoftware.htmlunit.Page; |
11 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
12 |
import com.gargoylesoftware.htmlunit.WebClient; |
13 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
14 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
15 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
16 |
|
17 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
18 |
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
19 |
|
20 |
public class TimetableFetcher { |
21 |
|
22 |
class NullRefreshHandler implements RefreshHandler { |
23 |
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
24 |
} |
25 |
|
26 |
} |
27 |
|
28 |
TimeoutCache<String, List<TimetableBean>> cache = new TimeoutCache<String,List<TimetableBean>>(120 * 1000); |
29 |
|
30 |
|
31 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
32 |
|
33 |
|
34 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Throwable { |
35 |
String key = trainID+type; |
36 |
List<TimetableBean> list = cache.get(key); |
37 |
|
38 |
if (list == null) { |
39 |
list = lookupTimetable(trainID,type); |
40 |
cache.put(key, list); |
41 |
} else { |
42 |
logger.info("Timetable: Cache hit " + trainID); |
43 |
} |
44 |
return list; |
45 |
} |
46 |
|
47 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Throwable { |
48 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
49 |
|
50 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
51 |
|
52 |
|
53 |
final WebClient webClient = new WebClient(); |
54 |
webClient.setTimeout(2500); |
55 |
webClient.setJavaScriptEnabled(false); |
56 |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
57 |
webClient.setCssEnabled(false); |
58 |
|
59 |
|
60 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
61 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
62 |
|
63 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
64 |
|
65 |
|
66 |
boolean currentStation = false; |
67 |
boolean currentStationSaved = false; |
68 |
|
69 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
70 |
if (tables.size() == 1) { |
71 |
HtmlElement timetable = tables.get(0); |
72 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
73 |
|
74 |
for (int i=0; i<rows.size(); i++) { |
75 |
if (i==0) //First row is column headers |
76 |
continue; |
77 |
|
78 |
|
79 |
HtmlElement row = rows.get(i); |
80 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
81 |
|
82 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
83 |
currentStation = true; |
84 |
continue; |
85 |
} |
86 |
|
87 |
TimetableBean bean = new TimetableBean(); |
88 |
bean.setStation( fields.get(0).asText() ); |
89 |
bean.setArrival( fields.get(1).asText() ); |
90 |
bean.setDeparture( fields.get(2).asText() ); |
91 |
|
92 |
if (currentStation == true && currentStationSaved == false ) { |
93 |
bean.setCurrent(currentStation); |
94 |
currentStationSaved = true; |
95 |
} |
96 |
|
97 |
timetableList.add(bean); |
98 |
|
99 |
} |
100 |
|
101 |
} else { |
102 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
103 |
} |
104 |
|
105 |
return timetableList; |
106 |
} |
107 |
|
108 |
} |