5 |
import java.net.URL; |
import java.net.URL; |
6 |
import java.util.ArrayList; |
import java.util.ArrayList; |
7 |
import java.util.List; |
import java.util.List; |
8 |
|
import java.util.Map; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
import com.gargoylesoftware.htmlunit.*; |
import com.gargoylesoftware.htmlunit.Page; |
12 |
import com.gargoylesoftware.htmlunit.html.*; |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
13 |
|
import com.gargoylesoftware.htmlunit.WebClient; |
14 |
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
15 |
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
16 |
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
17 |
|
|
18 |
|
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
19 |
|
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
20 |
|
|
21 |
public class TimetableFetcher { |
public class TimetableFetcher { |
22 |
|
|
25 |
} |
} |
26 |
|
|
27 |
} |
} |
28 |
|
|
29 |
|
Map<String, List<TimetableBean>> cache = new TimeoutMap<String,List<TimetableBean>>(120 * 1000); |
30 |
|
|
31 |
|
|
32 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
33 |
|
|
34 |
|
|
35 |
|
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
36 |
|
String key = trainID+type; |
37 |
|
List<TimetableBean> list = cache.get(key); |
38 |
|
|
39 |
|
if (list == null) { |
40 |
|
list = lookupTimetable(trainID,type); |
41 |
|
cache.put(key, list); |
42 |
|
} else { |
43 |
|
logger.info("Timetable: Cache hit " + trainID); |
44 |
|
} |
45 |
|
return list; |
46 |
|
} |
47 |
|
|
48 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
49 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
50 |
|
|
51 |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
52 |
|
|
53 |
|
|
54 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
55 |
webClient.setTimeout(1000); |
webClient.setTimeout(2500); |
56 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
57 |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
webClient.setRefreshHandler( new NullRefreshHandler() ); |
58 |
webClient.setCssEnabled(false); |
webClient.setCssEnabled(false); |
59 |
|
|
60 |
|
|
61 |
final HtmlPage page = webClient.getPage(url); |
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
62 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
63 |
|
|
64 |
|
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
65 |
|
|
66 |
|
|
67 |
boolean currentStation = false; |
boolean currentStation = false; |
68 |
boolean currentStationSaved = false; |
boolean currentStationSaved = false; |