3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
import java.io.PrintWriter; |
import java.io.PrintWriter; |
5 |
import java.net.URL; |
import java.net.URL; |
6 |
|
import java.net.URLEncoder; |
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.Set; |
import java.util.Set; |
9 |
import java.util.TreeMap; |
import java.util.TreeMap; |
25 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
26 |
import dk.thoerup.traininfoservice.db.StationDAO; |
import dk.thoerup.traininfoservice.db.StationDAO; |
27 |
|
|
28 |
|
import dk.thoerup.genericjavautils.TimeoutMap; |
29 |
|
|
30 |
|
|
31 |
@WebServlet(urlPatterns={"/CompareStations"}) |
@WebServlet(urlPatterns={"/CompareStations"}) |
32 |
public class CompareStations extends HttpServlet { |
public class CompareStations extends HttpServlet { |
33 |
private static final long serialVersionUID = 1L; |
private static final long serialVersionUID = 1L; |
|
|
|
34 |
|
|
35 |
|
Map<String,String> cache = new TimeoutMap<String,String>(300 * 1000); |
36 |
|
|
37 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { |
38 |
|
try { |
39 |
|
|
40 |
|
String data = getData(request); |
41 |
|
|
42 |
|
PrintWriter out = response.getWriter(); |
43 |
|
out.print(data); |
44 |
|
|
45 |
|
System.out.println("CompareStations completed"); |
46 |
|
} catch (Exception e) { |
47 |
|
throw new ServletException(e); |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
|
String getData(HttpServletRequest request) throws Exception { |
52 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
53 |
|
|
54 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000); |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk/Trafikinformation/Stationsliste"), 5000); |
55 |
|
|
56 |
Map<String,String> banedkStations = new TreeMap<String,String>(); |
Map<String,String> banedkStations = new TreeMap<String,String>(); |
57 |
Set<String> dbStations = new TreeSet<String>(); |
Set<String> dbStations = new TreeSet<String>(); |
58 |
|
|
59 |
|
StringBuilder sb = new StringBuilder(); |
60 |
|
sb.append("<html>"); |
61 |
|
sb.append("<head></head>"); |
62 |
|
sb.append("<body>"); |
63 |
|
|
64 |
StationDAO dao = new StationDAO(); |
StationDAO dao = new StationDAO(); |
65 |
|
|
66 |
try { |
try { |
77 |
} |
} |
78 |
} |
} |
79 |
|
|
80 |
|
sb.append("<h2>Active stations in DB</h2>\n"); |
81 |
|
sb.append("<ul>"); |
82 |
|
|
83 |
StationBean bean = dao.dumpAll(); |
StationBean bean = dao.dumpAll(); |
84 |
for (StationEntry station : bean.entries) { |
for (StationEntry station : bean.entries) { |
85 |
if (station.isRegional() || station.isStrain()) { |
if (station.isRegional() || station.isStrain()) { |
86 |
|
if (station.isRegional() ) { |
87 |
|
String id = URLEncoder.encode(station.getRegional(), "ISO-8859-1"); |
88 |
|
String uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/Fjerntog"; |
89 |
|
|
90 |
|
String data = hasData(uri, true); |
91 |
|
|
92 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> " + data + "</li>\n" ); |
93 |
|
} |
94 |
|
if (station.isStrain() ) { |
95 |
|
String id = URLEncoder.encode(station.getStrain(), "ISO-8859-1"); |
96 |
|
String uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/S-Tog"; |
97 |
|
|
98 |
|
String data = hasData(uri, true); |
99 |
|
|
100 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> " + data + "</li>\n" ); |
101 |
|
} |
102 |
|
|
103 |
dbStations.add(station.getName()); |
dbStations.add(station.getName()); |
104 |
} |
} |
105 |
} |
} |
106 |
|
sb.append("</ul>\n"); |
107 |
|
|
108 |
} catch (Exception e) { |
} catch (Exception e) { |
109 |
throw new ServletException(e); |
throw new ServletException(e); |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
StringBuilder sb = new StringBuilder(); |
sb.append("<h2>stations on website not in db</h2>\n"); |
|
|
|
|
sb.append("<h2>stations on website not in db</h2>"); |
|
115 |
sb.append("<ul>"); |
sb.append("<ul>"); |
116 |
Set<String> tmpDbStations = new TreeSet<String>( dbStations ); |
Set<String> tmpDbStations = new TreeSet<String>( dbStations ); |
117 |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ; |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ; |
122 |
for(String s : tmpBanedkStations) { |
for(String s : tmpBanedkStations) { |
123 |
String uri = banedkStations.get(s); |
String uri = banedkStations.get(s); |
124 |
String disabled = ""; |
String disabled = ""; |
125 |
String data = hasData(uri); |
String data = hasData(uri, false); |
126 |
|
|
127 |
try { |
try { |
128 |
boolean tmpdisabled = dao.hasDisabledStation(s); |
boolean tmpdisabled = dao.hasDisabledStation(s); |
132 |
throw new ServletException(e); |
throw new ServletException(e); |
133 |
} |
} |
134 |
|
|
135 |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> " + data + " - " + disabled + "</li>" ); |
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> " + data + " - " + disabled + "</li>\n" ); |
136 |
} |
} |
137 |
sb.append("</ul>"); |
sb.append("</ul>"); |
138 |
|
|
143 |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ; |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ; |
144 |
tmpDbStations.removeAll(tmpBanedkStations); |
tmpDbStations.removeAll(tmpBanedkStations); |
145 |
for(String s : tmpDbStations) { |
for(String s : tmpDbStations) { |
146 |
sb.append( "<li>" + s + "</li>" ); |
sb.append( "<li>" + s + "</li>\n" ); |
147 |
} |
} |
148 |
sb.append("</ul>"); |
sb.append("</ul>"); |
149 |
|
|
150 |
|
sb.append("</body></html>\n"); |
151 |
|
|
152 |
|
return sb.toString(); |
|
PrintWriter out = response.getWriter(); |
|
|
out.print(sb.toString()); |
|
153 |
|
|
154 |
} |
} |
155 |
|
|
156 |
String hasData(String uri) { |
String hasData(String uri, boolean requiredata) { |
157 |
String returnVal = "unknown"; |
String key = uri + requiredata; |
158 |
|
|
159 |
|
String data = cache.get(key); |
160 |
|
if (data == null) { |
161 |
|
|
162 |
try { |
data = "<font color='yellow'>unknown</font>"; |
163 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
try { |
164 |
|
data = hasDataWorker(uri,requiredata); |
165 |
|
cache.put(key,data); |
166 |
|
} catch (Exception e) { |
167 |
|
System.out.println( e.getMessage() ); |
168 |
|
} |
169 |
|
} |
170 |
|
return data; |
171 |
|
} |
172 |
|
|
173 |
|
String hasDataWorker(String uri, boolean requiredata) throws Exception { |
174 |
|
String returnVal = ""; |
175 |
|
|
176 |
|
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
177 |
|
|
178 |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri), 5000); |
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri), 2000); |
179 |
|
|
180 |
Document doc = (Document) cb.invoke(jsoup); |
Document doc = (Document) cb.invoke(jsoup); |
181 |
|
|
182 |
Element tabel = doc.getElementById("afgangtabel"); |
Element tabel = doc.getElementById("afgangtabel"); |
183 |
if ( tabel != null) { |
if ( tabel != null) { |
184 |
|
if (requiredata == true) { |
185 |
|
returnVal = ""; |
186 |
|
} else { |
187 |
returnVal = "<font color='red'>Data!</font>"; |
returnVal = "<font color='red'>Data!</font>"; |
188 |
|
} |
189 |
|
} else { |
190 |
|
if (requiredata == true) { |
191 |
|
returnVal = "<font color='red'>No data</font>"; |
192 |
} else { |
} else { |
193 |
returnVal = ""; |
returnVal = ""; |
194 |
} |
} |
|
|
|
|
} catch (Exception e) { |
|
|
System.out.println( e.getMessage() ); |
|
195 |
} |
} |
196 |
|
|
197 |
|
|
198 |
return returnVal; |
return returnVal; |
199 |
} |
} |