1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.ext.ensembl; |
22 |
|
|
23 |
|
import jalview.bin.Jalview; |
24 |
|
import jalview.io.DataSourceType; |
25 |
|
import jalview.io.FileParse; |
26 |
|
import jalview.util.StringUtils; |
27 |
|
|
28 |
|
import java.io.BufferedReader; |
29 |
|
import java.io.DataOutputStream; |
30 |
|
import java.io.IOException; |
31 |
|
import java.io.InputStream; |
32 |
|
import java.io.InputStreamReader; |
33 |
|
import java.net.HttpURLConnection; |
34 |
|
import java.net.MalformedURLException; |
35 |
|
import java.net.ProtocolException; |
36 |
|
import java.net.URL; |
37 |
|
import java.util.HashMap; |
38 |
|
import java.util.List; |
39 |
|
import java.util.Map; |
40 |
|
|
41 |
|
import javax.ws.rs.HttpMethod; |
42 |
|
|
43 |
|
import org.json.simple.JSONArray; |
44 |
|
import org.json.simple.JSONObject; |
45 |
|
import org.json.simple.parser.JSONParser; |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@author |
51 |
|
|
|
|
| 5.9% |
Uncovered Elements: 191 (203) |
Complexity: 48 |
Complexity Density: 0.34 |
|
52 |
|
abstract class EnsemblRestClient extends EnsemblSequenceFetcher |
53 |
|
{ |
54 |
|
private static final int DEFAULT_READ_TIMEOUT = 5 * 60 * 1000; |
55 |
|
|
56 |
|
private static final int CONNECT_TIMEOUT_MS = 10 * 1000; |
57 |
|
|
58 |
|
private static final int MAX_RETRIES = 3; |
59 |
|
|
60 |
|
private static final int HTTP_OK = 200; |
61 |
|
|
62 |
|
private static final int HTTP_OVERLOAD = 429; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
@see |
68 |
|
@see |
69 |
|
|
70 |
|
private static final String LATEST_ENSEMBLGENOMES_REST_VERSION = "6.3"; |
71 |
|
|
72 |
|
private static final String LATEST_ENSEMBL_REST_VERSION = "6.3"; |
73 |
|
|
74 |
|
private static final String REST_CHANGE_LOG = "https://github.com/Ensembl/ensembl-rest/wiki/Change-log"; |
75 |
|
|
76 |
|
private static Map<String, EnsemblData> domainData; |
77 |
|
|
78 |
|
private final static long AVAILABILITY_RETEST_INTERVAL = 10000L; |
79 |
|
|
80 |
|
private final static long VERSION_RETEST_INTERVAL = 1000L * 3600; |
81 |
|
|
82 |
|
protected static final String CONTENT_TYPE_JSON = "?content-type=application/json"; |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
84 |
1 |
static... |
85 |
|
{ |
86 |
1 |
domainData = new HashMap<>(); |
87 |
1 |
domainData.put(DEFAULT_ENSEMBL_BASEURL, |
88 |
|
new EnsemblData(DEFAULT_ENSEMBL_BASEURL, LATEST_ENSEMBL_REST_VERSION)); |
89 |
1 |
domainData.put(DEFAULT_ENSEMBL_GENOMES_BASEURL, new EnsemblData( |
90 |
|
DEFAULT_ENSEMBL_GENOMES_BASEURL, LATEST_ENSEMBLGENOMES_REST_VERSION)); |
91 |
|
} |
92 |
|
|
93 |
|
protected volatile boolean inProgress = false; |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
|
|
| 55.6% |
Uncovered Elements: 4 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
98 |
36 |
public EnsemblRestClient()... |
99 |
|
{ |
100 |
36 |
super(); |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
36 |
if (!domainData.containsKey(ensemblDomain)) |
106 |
|
{ |
107 |
0 |
domainData.put(ensemblDomain, |
108 |
|
new EnsemblData(ensemblDomain, LATEST_ENSEMBL_REST_VERSION)); |
109 |
|
} |
110 |
36 |
if (!domainData.containsKey(ensemblGenomesDomain)) |
111 |
|
{ |
112 |
0 |
domainData.put(ensemblGenomesDomain, new EnsemblData( |
113 |
|
ensemblGenomesDomain, LATEST_ENSEMBLGENOMES_REST_VERSION)); |
114 |
|
} |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
@param |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
122 |
1 |
public EnsemblRestClient(String d)... |
123 |
|
{ |
124 |
1 |
setDomain(d); |
125 |
|
} |
126 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
127 |
0 |
@Override... |
128 |
|
public boolean queryInProgress() |
129 |
|
{ |
130 |
0 |
return inProgress; |
131 |
|
} |
132 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
133 |
0 |
@Override... |
134 |
|
public StringBuffer getRawRecords() |
135 |
|
{ |
136 |
0 |
return null; |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
@param |
143 |
|
@return |
144 |
|
@throws |
145 |
|
|
146 |
|
protected abstract URL getUrl(List<String> ids) |
147 |
|
throws MalformedURLException; |
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
@return |
153 |
|
|
154 |
|
protected abstract boolean useGetRequest(); |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
@param |
160 |
|
|
161 |
|
@return |
162 |
|
@see |
163 |
|
|
164 |
|
protected abstract String getRequestMimeType(boolean multipleIds); |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@return |
170 |
|
@see |
171 |
|
|
172 |
|
protected abstract String getResponseMimeType(); |
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@see |
179 |
|
@return |
180 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0.31 |
|
181 |
0 |
boolean checkEnsembl()... |
182 |
|
{ |
183 |
0 |
BufferedReader br = null; |
184 |
0 |
String pingUrl = getDomain() + "/info/ping" + CONTENT_TYPE_JSON; |
185 |
0 |
try |
186 |
|
{ |
187 |
|
|
188 |
|
|
189 |
0 |
URL ping = new URL(pingUrl); |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
0 |
br = getHttpResponse(ping, null, 2 * 1000); |
196 |
0 |
if (br == null) |
197 |
|
{ |
198 |
|
|
199 |
0 |
return false; |
200 |
|
} |
201 |
0 |
JSONParser jp = new JSONParser(); |
202 |
0 |
JSONObject val = (JSONObject) jp.parse(br); |
203 |
0 |
String pingString = val.get("ping").toString(); |
204 |
0 |
return pingString != null; |
205 |
|
} catch (Throwable t) |
206 |
|
{ |
207 |
0 |
System.err.println( |
208 |
|
"Error connecting to " + pingUrl + ": " + t.getMessage()); |
209 |
|
} finally |
210 |
|
{ |
211 |
0 |
if (br != null) |
212 |
|
{ |
213 |
0 |
try |
214 |
|
{ |
215 |
0 |
br.close(); |
216 |
|
} catch (IOException e) |
217 |
|
{ |
218 |
|
|
219 |
|
} |
220 |
|
} |
221 |
|
} |
222 |
0 |
return false; |
223 |
|
} |
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
@param |
229 |
|
@return |
230 |
|
@throws |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
232 |
0 |
protected FileParse getSequenceReader(List<String> ids) throws IOException... |
233 |
|
{ |
234 |
0 |
URL url = getUrl(ids); |
235 |
|
|
236 |
0 |
BufferedReader reader = getHttpResponse(url, ids); |
237 |
0 |
if (reader == null) |
238 |
|
{ |
239 |
|
|
240 |
0 |
return null; |
241 |
|
} |
242 |
0 |
FileParse fp = new FileParse(reader, url.toString(), |
243 |
|
DataSourceType.URL); |
244 |
0 |
return fp; |
245 |
|
} |
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
@param |
252 |
|
@param |
253 |
|
@return |
254 |
|
@throws |
255 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
256 |
0 |
protected BufferedReader getHttpResponse(URL url, List<String> ids)... |
257 |
|
throws IOException |
258 |
|
{ |
259 |
0 |
return getHttpResponse(url, ids, DEFAULT_READ_TIMEOUT); |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
@param |
267 |
|
@param |
268 |
|
|
269 |
|
@param |
270 |
|
|
271 |
|
@return |
272 |
|
@throws |
273 |
|
|
|
|
| 0% |
Uncovered Elements: 23 (23) |
Complexity: 4 |
Complexity Density: 0.24 |
|
274 |
0 |
protected BufferedReader getHttpResponse(URL url, List<String> ids,... |
275 |
|
int readTimeout) throws IOException |
276 |
|
{ |
277 |
0 |
int retriesLeft = MAX_RETRIES; |
278 |
0 |
HttpURLConnection connection = null; |
279 |
0 |
int responseCode = 0; |
280 |
|
|
281 |
0 |
while (retriesLeft > 0) |
282 |
|
{ |
283 |
0 |
connection = tryConnection(url, ids, readTimeout); |
284 |
0 |
responseCode = connection.getResponseCode(); |
285 |
0 |
if (responseCode == HTTP_OVERLOAD) |
286 |
|
{ |
287 |
0 |
retriesLeft--; |
288 |
0 |
checkRetryAfter(connection); |
289 |
|
} |
290 |
|
else |
291 |
|
{ |
292 |
0 |
retriesLeft = 0; |
293 |
|
} |
294 |
|
} |
295 |
0 |
if (responseCode != HTTP_OK) |
296 |
|
{ |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
0 |
System.err.println("Response code " + responseCode + " for " + url); |
302 |
0 |
return null; |
303 |
|
} |
304 |
|
|
305 |
0 |
InputStream response = connection.getInputStream(); |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
0 |
BufferedReader reader = null; |
311 |
0 |
reader = new BufferedReader(new InputStreamReader(response, "UTF-8")); |
312 |
0 |
return reader; |
313 |
|
} |
314 |
|
|
315 |
|
|
316 |
|
@param |
317 |
|
@param |
318 |
|
@param |
319 |
|
@return |
320 |
|
@throws |
321 |
|
@throws |
322 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 4 |
Complexity Density: 0.29 |
|
323 |
0 |
protected HttpURLConnection tryConnection(URL url, List<String> ids,... |
324 |
|
int readTimeout) throws IOException, ProtocolException |
325 |
|
{ |
326 |
|
|
327 |
0 |
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
0 |
boolean multipleIds = ids != null && ids.size() > 1; |
334 |
0 |
connection.setRequestMethod( |
335 |
0 |
multipleIds ? HttpMethod.POST : HttpMethod.GET); |
336 |
0 |
connection.setRequestProperty("Content-Type", |
337 |
|
getRequestMimeType(multipleIds)); |
338 |
0 |
connection.setRequestProperty("Accept", getResponseMimeType()); |
339 |
|
|
340 |
0 |
connection.setDoInput(true); |
341 |
0 |
connection.setDoOutput(multipleIds); |
342 |
|
|
343 |
0 |
if (!Jalview.isJS()) |
344 |
|
{ |
345 |
0 |
connection.setUseCaches(false); |
346 |
0 |
connection.setConnectTimeout(CONNECT_TIMEOUT_MS); |
347 |
0 |
connection.setReadTimeout(readTimeout); |
348 |
|
} |
349 |
|
|
350 |
0 |
if (multipleIds) |
351 |
|
{ |
352 |
0 |
writePostBody(connection, ids); |
353 |
|
} |
354 |
0 |
return connection; |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
@see |
362 |
|
@param |
363 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 5 |
Complexity Density: 0.62 |
|
364 |
0 |
void checkRetryAfter(HttpURLConnection connection)... |
365 |
|
{ |
366 |
0 |
String retryDelay = connection.getHeaderField("Retry-After"); |
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
0 |
if (retryDelay != null) |
372 |
|
{ |
373 |
0 |
try |
374 |
|
{ |
375 |
0 |
int retrySecs = Integer.valueOf(retryDelay); |
376 |
0 |
if (retrySecs > 0 && retrySecs < 10) |
377 |
|
{ |
378 |
0 |
System.err |
379 |
|
.println("Ensembl REST service rate limit exceeded, waiting " |
380 |
|
+ retryDelay + " seconds before retrying"); |
381 |
0 |
Thread.sleep(1000 * retrySecs); |
382 |
|
} |
383 |
|
} catch (NumberFormatException | InterruptedException e) |
384 |
|
{ |
385 |
0 |
System.err.println("Error handling Retry-After: " + e.getMessage()); |
386 |
|
} |
387 |
|
} |
388 |
|
} |
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
|
@return |
397 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
398 |
0 |
protected boolean isEnsemblAvailable()... |
399 |
|
{ |
400 |
0 |
EnsemblData info = domainData.get(getDomain()); |
401 |
|
|
402 |
0 |
long now = System.currentTimeMillis(); |
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
|
407 |
0 |
boolean retestAvailability = (now |
408 |
|
- info.lastAvailableCheckTime) > AVAILABILITY_RETEST_INTERVAL; |
409 |
0 |
if (!info.restAvailable || retestAvailability) |
410 |
|
{ |
411 |
0 |
info.restAvailable = checkEnsembl(); |
412 |
0 |
info.lastAvailableCheckTime = now; |
413 |
|
} |
414 |
|
|
415 |
|
|
416 |
|
|
417 |
|
|
418 |
0 |
boolean refetchVersion = (now |
419 |
|
- info.lastVersionCheckTime) > VERSION_RETEST_INTERVAL; |
420 |
0 |
if (refetchVersion) |
421 |
|
{ |
422 |
0 |
checkEnsemblRestVersion(); |
423 |
0 |
checkEnsemblDataVersion(); |
424 |
0 |
info.lastVersionCheckTime = now; |
425 |
|
} |
426 |
|
|
427 |
0 |
return info.restAvailable; |
428 |
|
} |
429 |
|
|
430 |
|
|
431 |
|
|
432 |
|
|
433 |
|
|
434 |
|
@param |
435 |
|
@param |
436 |
|
@throws |
437 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 2 |
Complexity Density: 0.11 |
|
438 |
0 |
protected void writePostBody(HttpURLConnection connection,... |
439 |
|
List<String> ids) throws IOException |
440 |
|
{ |
441 |
0 |
boolean first; |
442 |
0 |
StringBuilder postBody = new StringBuilder(64); |
443 |
0 |
postBody.append("{\"ids\":["); |
444 |
0 |
first = true; |
445 |
0 |
for (String id : ids) |
446 |
|
{ |
447 |
0 |
if (!first) |
448 |
|
{ |
449 |
0 |
postBody.append(","); |
450 |
|
} |
451 |
0 |
first = false; |
452 |
0 |
postBody.append("\""); |
453 |
0 |
postBody.append(id.trim()); |
454 |
0 |
postBody.append("\""); |
455 |
|
} |
456 |
0 |
postBody.append("]}"); |
457 |
0 |
byte[] thepostbody = postBody.toString().getBytes(); |
458 |
0 |
connection.setRequestProperty("Content-Length", |
459 |
|
Integer.toString(thepostbody.length)); |
460 |
0 |
DataOutputStream wr = new DataOutputStream( |
461 |
|
connection.getOutputStream()); |
462 |
0 |
wr.write(thepostbody); |
463 |
0 |
wr.flush(); |
464 |
0 |
wr.close(); |
465 |
|
} |
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
@return |
471 |
|
|
|
|
| 0% |
Uncovered Elements: 29 (29) |
Complexity: 6 |
Complexity Density: 0.26 |
|
472 |
0 |
private void checkEnsemblRestVersion()... |
473 |
|
{ |
474 |
0 |
EnsemblData info = domainData.get(getDomain()); |
475 |
|
|
476 |
0 |
JSONParser jp = new JSONParser(); |
477 |
0 |
URL url = null; |
478 |
0 |
try |
479 |
|
{ |
480 |
0 |
url = new URL(getDomain() + "/info/rest" + CONTENT_TYPE_JSON); |
481 |
0 |
BufferedReader br = getHttpResponse(url, null); |
482 |
0 |
if (br == null) |
483 |
|
{ |
484 |
0 |
return; |
485 |
|
} |
486 |
0 |
JSONObject val = (JSONObject) jp.parse(br); |
487 |
0 |
String version = val.get("release").toString(); |
488 |
0 |
String majorVersion = version.substring(0, version.indexOf(".")); |
489 |
0 |
String expected = info.expectedRestVersion; |
490 |
0 |
String expectedMajorVersion = expected.substring(0, |
491 |
|
expected.indexOf(".")); |
492 |
0 |
info.restMajorVersionMismatch = false; |
493 |
0 |
try |
494 |
|
{ |
495 |
|
|
496 |
|
|
497 |
|
|
498 |
|
|
499 |
0 |
if (Float.valueOf(majorVersion) > Float |
500 |
|
.valueOf(expectedMajorVersion)) |
501 |
|
{ |
502 |
0 |
info.restMajorVersionMismatch = true; |
503 |
|
} |
504 |
|
} catch (NumberFormatException e) |
505 |
|
{ |
506 |
0 |
System.err.println("Error in REST version: " + e.toString()); |
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
|
|
511 |
|
|
512 |
|
|
513 |
|
|
514 |
0 |
boolean laterVersion = StringUtils.compareVersions(version, |
515 |
|
expected) == 1; |
516 |
0 |
if (laterVersion) |
517 |
|
{ |
518 |
0 |
System.err.println(String.format( |
519 |
|
"EnsemblRestClient expected %s REST version %s but found %s, see %s", |
520 |
|
getDbSource(), expected, version, REST_CHANGE_LOG)); |
521 |
|
} |
522 |
0 |
info.restVersion = version; |
523 |
|
} catch (Throwable t) |
524 |
|
{ |
525 |
0 |
System.err.println( |
526 |
|
"Error checking Ensembl REST version: " + t.getMessage()); |
527 |
|
} |
528 |
|
} |
529 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
530 |
0 |
public boolean isRestMajorVersionMismatch()... |
531 |
|
{ |
532 |
0 |
return domainData.get(getDomain()).restMajorVersionMismatch; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
|
|
537 |
|
|
538 |
|
@return |
539 |
|
|
|
|
| 0% |
Uncovered Elements: 18 (18) |
Complexity: 5 |
Complexity Density: 0.36 |
|
540 |
0 |
private void checkEnsemblDataVersion()... |
541 |
|
{ |
542 |
0 |
JSONParser jp = new JSONParser(); |
543 |
0 |
URL url = null; |
544 |
0 |
BufferedReader br = null; |
545 |
|
|
546 |
0 |
try |
547 |
|
{ |
548 |
0 |
url = new URL(getDomain() + "/info/data" + CONTENT_TYPE_JSON); |
549 |
0 |
br = getHttpResponse(url, null); |
550 |
0 |
if (br != null) |
551 |
|
{ |
552 |
0 |
JSONObject val = (JSONObject) jp.parse(br); |
553 |
0 |
JSONArray versions = (JSONArray) val.get("releases"); |
554 |
0 |
domainData.get(getDomain()).dataVersion = versions.get(0) |
555 |
|
.toString(); |
556 |
|
} |
557 |
|
} catch (Throwable t) |
558 |
|
{ |
559 |
0 |
System.err.println( |
560 |
|
"Error checking Ensembl data version: " + t.getMessage()); |
561 |
|
} finally |
562 |
|
{ |
563 |
0 |
if (br != null) |
564 |
|
{ |
565 |
0 |
try |
566 |
|
{ |
567 |
0 |
br.close(); |
568 |
|
} catch (IOException e) |
569 |
|
{ |
570 |
|
|
571 |
|
} |
572 |
|
} |
573 |
|
} |
574 |
|
} |
575 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
576 |
0 |
public String getEnsemblDataVersion()... |
577 |
|
{ |
578 |
0 |
return domainData.get(getDomain()).dataVersion; |
579 |
|
} |
580 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
581 |
0 |
@Override... |
582 |
|
public String getDbVersion() |
583 |
|
{ |
584 |
0 |
return getEnsemblDataVersion(); |
585 |
|
} |
586 |
|
|
587 |
|
} |