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.util.ArrayList; |
import java.net.URLEncoder; |
7 |
|
import java.util.Map; |
8 |
|
import java.util.Set; |
9 |
|
import java.util.TreeMap; |
10 |
|
import java.util.TreeSet; |
11 |
|
|
12 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
13 |
import javax.servlet.annotation.WebServlet; |
import javax.servlet.annotation.WebServlet; |
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>(600 * 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 |
|
String data = ""; |
40 |
|
String uri = request.getParameter("uri"); |
41 |
|
|
42 |
|
if (uri != null) { |
43 |
|
boolean require = Boolean.parseBoolean( request.getParameter("requiredata") ); |
44 |
|
data = hasData(uri, require); |
45 |
|
} else { |
46 |
|
data = getData(request); |
47 |
|
} |
48 |
|
|
49 |
|
PrintWriter out = response.getWriter(); |
50 |
|
out.print(data); |
51 |
|
|
52 |
|
} catch (Exception e) { |
53 |
|
throw new ServletException(e); |
54 |
|
} |
55 |
|
} |
56 |
|
|
57 |
|
String getData(HttpServletRequest request) throws Exception { |
58 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
59 |
|
|
60 |
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); |
61 |
|
|
62 |
ArrayList<String> banedkStations = new ArrayList<String>(); |
Map<String,String> banedkStations = new TreeMap<String,String>(); |
63 |
ArrayList<String> dbStations = new ArrayList<String>(); |
Set<String> dbStations = new TreeSet<String>(); |
64 |
|
|
65 |
|
StringBuilder sb = new StringBuilder(); |
66 |
|
sb.append("<html>"); |
67 |
|
sb.append("<head>"); |
68 |
|
sb.append("<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'></script>\n"); |
69 |
|
sb.append("<script type='text/javascript' src='compareloader.js'></script>\n"); |
70 |
|
|
71 |
|
sb.append("</head>"); |
72 |
|
sb.append("<body>"); |
73 |
|
|
74 |
|
StationDAO dao = new StationDAO(); |
75 |
|
|
76 |
try { |
try { |
77 |
Document doc = (Document) cb.invoke(jsoup); |
Document doc = (Document) cb.invoke(jsoup); |
81 |
for(Element e : tables) { |
for(Element e : tables) { |
82 |
if (e.tagName().equals("table") ){ |
if (e.tagName().equals("table") ){ |
83 |
Elements links = e.getElementsByTag("a"); |
Elements links = e.getElementsByTag("a"); |
84 |
for (Element link : links) { |
for (Element link : links) { |
85 |
banedkStations.add( link.text() ); |
banedkStations.put( link.text(), link.attr("href") ); |
86 |
} |
} |
87 |
} |
} |
88 |
} |
} |
89 |
|
|
90 |
StationDAO dao = new StationDAO(); |
sb.append("<h2>Active stations in DB</h2>\n"); |
91 |
|
sb.append("<ul>"); |
92 |
|
|
93 |
StationBean bean = dao.dumpAll(); |
StationBean bean = dao.dumpAll(); |
94 |
for (StationEntry station : bean.entries) { |
for (StationEntry station : bean.entries) { |
95 |
if (station.isRegional() || station.isStrain()) { |
if (station.isRegional() || station.isStrain()) { |
96 |
|
if (station.isRegional() ) { |
97 |
|
//String id = URLEncoder.encode(station.getRegional(), "ISO-8859-1"); |
98 |
|
String id = station.getRegional(); |
99 |
|
String uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/Fjerntog"; |
100 |
|
|
101 |
|
|
102 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> <span class='uri' requiredata='true' uri='" + uri + "'></span> </li>\n" ); |
103 |
|
} |
104 |
|
if (station.isStrain() ) { |
105 |
|
//String id = URLEncoder.encode(station.getStrain(), "ISO-8859-1"); |
106 |
|
String id = station.getStrain(); |
107 |
|
String uri = "/Trafikinformation/AfgangAnkomst/Afgang/" + id + "/S-Tog"; |
108 |
|
|
109 |
|
|
110 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + station.getName() + "</a> <span class='uri' requiredata='true' uri='" + uri + "'></span> </li>\n" ); |
111 |
|
} |
112 |
|
|
113 |
dbStations.add(station.getName()); |
dbStations.add(station.getName()); |
114 |
} |
} |
115 |
} |
} |
116 |
|
sb.append("</ul>\n"); |
117 |
|
|
118 |
} catch (Exception e) { |
} catch (Exception e) { |
119 |
throw new ServletException(e); |
throw new ServletException(e); |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
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>"); |
|
125 |
sb.append("<ul>"); |
sb.append("<ul>"); |
126 |
ArrayList<String> tmpDbStations = new ArrayList<String>( dbStations ); |
Set<String> tmpDbStations = new TreeSet<String>( dbStations ); |
127 |
ArrayList<String> tmpBanedkStations = new ArrayList<String>( banedkStations) ; |
Set<String> tmpBanedkStations = new TreeSet<String>( banedkStations.keySet()) ; |
128 |
tmpBanedkStations.removeAll(tmpDbStations); |
tmpBanedkStations.removeAll(tmpDbStations); |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
for(String s : tmpBanedkStations) { |
for(String s : tmpBanedkStations) { |
133 |
sb.append( "<li>" + s + "</li>" ); |
String uri = banedkStations.get(s); |
134 |
|
String disabled = ""; |
135 |
|
//String data = hasData(uri, false); |
136 |
|
|
137 |
|
try { |
138 |
|
boolean tmpdisabled = dao.hasDisabledStation(s); |
139 |
|
if (tmpdisabled == true) |
140 |
|
disabled = " - disabled"; |
141 |
|
} catch (Exception e) { |
142 |
|
throw new ServletException(e); |
143 |
|
} |
144 |
|
|
145 |
|
sb.append( "<li><a target='_blank' href='http://trafikinfo.bane.dk" + uri + "'>" + s + "</a> <span class='uri' requiredata='false' uri='" + uri + "'></span>" + disabled + "</li>\n" ); |
146 |
} |
} |
147 |
sb.append("</ul>"); |
sb.append("</ul>"); |
148 |
|
|
149 |
|
|
150 |
sb.append("<h2>stations in db not on website </h2>"); |
sb.append("<h2>stations in db not on website </h2>"); |
151 |
sb.append("<ul>"); |
sb.append("<ul>"); |
152 |
tmpDbStations = new ArrayList<String>( dbStations ); |
tmpDbStations = new TreeSet<String>( dbStations ); |
153 |
tmpBanedkStations = new ArrayList<String>( banedkStations) ; |
tmpBanedkStations = new TreeSet<String>( banedkStations.keySet() ) ; |
154 |
tmpDbStations.removeAll(tmpBanedkStations); |
tmpDbStations.removeAll(tmpBanedkStations); |
155 |
for(String s : tmpDbStations) { |
for(String s : tmpDbStations) { |
156 |
sb.append( "<li>" + s + "</li>" ); |
sb.append( "<li>" + s + "</li>\n" ); |
157 |
} |
} |
158 |
sb.append("</ul>"); |
sb.append("</ul>"); |
159 |
|
sb.append("<font size='1' id='antal'></font>"); |
160 |
|
sb.append("</body></html>\n"); |
161 |
|
|
162 |
|
return sb.toString(); |
163 |
|
|
164 |
PrintWriter out = response.getWriter(); |
} |
165 |
out.print(sb.toString()); |
|
166 |
|
String hasData(String uri, boolean requiredata) { |
167 |
|
String key = uri + requiredata; |
168 |
|
|
169 |
|
String data = cache.get(key); |
170 |
|
if (data == null) { |
171 |
|
|
172 |
|
data = "<font color='yellow'>unknown</font>"; |
173 |
|
try { |
174 |
|
data = hasDataWorker(uri,requiredata); |
175 |
|
cache.put(key,data); |
176 |
|
} catch (Exception e) { |
177 |
|
System.out.println( e.getMessage() ); |
178 |
|
} |
179 |
|
} |
180 |
|
return data; |
181 |
|
} |
182 |
|
|
183 |
|
String hasDataWorker(String uri, boolean requiredata) throws Exception { |
184 |
|
String returnVal = ""; |
185 |
|
|
186 |
|
String parts[] = uri.split("\\/"); |
187 |
|
String id = URLEncoder.encode(parts[4], "ISO-8859-1"); |
188 |
|
String uri2 = "/TrafikInformation/AfgangAnkomst/Afgang/" + id + "/" + parts[5]; |
189 |
|
|
190 |
|
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
191 |
|
|
192 |
|
JsoupInvocation jsoup = new JsoupInvocation(new URL("http://trafikinfo.bane.dk" + uri2), 2000); |
193 |
|
|
194 |
|
Document doc = (Document) cb.invoke(jsoup); |
195 |
|
|
196 |
|
Element tabel = doc.getElementById("afgangtabel"); |
197 |
|
if ( tabel != null) { |
198 |
|
if (requiredata == true) { |
199 |
|
returnVal = ""; |
200 |
|
} else { |
201 |
|
returnVal = "<font color='red'>Data!</font>"; |
202 |
|
} |
203 |
|
} else { |
204 |
|
if (requiredata == true) { |
205 |
|
returnVal = "<font color='red'>No data</font>"; |
206 |
|
} else { |
207 |
|
returnVal = ""; |
208 |
|
} |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
return returnVal; |
213 |
} |
} |
214 |
|
|
215 |
|
|
216 |
} |
} |