From: gmungoc Date: Mon, 18 Jul 2016 15:39:34 +0000 (+0100) Subject: JAL-2105 check content of Ensembl REST ping response X-Git-Tag: Release_2_10_0~138^2^2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=a96386b344a49391588762301ad71f1d39c11ca7;p=jalview.git JAL-2105 check content of Ensembl REST ping response --- diff --git a/src/jalview/ext/ensembl/EnsemblInfo.java b/src/jalview/ext/ensembl/EnsemblInfo.java index 950b658..88b5ac4 100644 --- a/src/jalview/ext/ensembl/EnsemblInfo.java +++ b/src/jalview/ext/ensembl/EnsemblInfo.java @@ -33,7 +33,7 @@ class EnsemblInfo /* * true when http://rest.ensembl.org/info/ping/?content-type=application/json - * returns response code 200 + * returns response code 200 and not {"error":"Database is unavailable"} */ boolean restAvailable; diff --git a/src/jalview/ext/ensembl/EnsemblRestClient.java b/src/jalview/ext/ensembl/EnsemblRestClient.java index e651ddf..f9cfe05 100644 --- a/src/jalview/ext/ensembl/EnsemblRestClient.java +++ b/src/jalview/ext/ensembl/EnsemblRestClient.java @@ -155,30 +155,40 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher protected abstract String getResponseMimeType(); /** - * Tries to connect to Ensembl's REST 'ping' endpoint, and returns true if - * successful, else false + * Checks Ensembl's REST 'ping' endpoint, and returns true if response + * indicates available, else false * + * @see http://rest.ensembl.org/documentation/info/ping * @return */ private boolean checkEnsembl() { + HttpURLConnection conn = null; try { // note this format works for both ensembl and ensemblgenomes // info/ping.json works for ensembl only (March 2016) URL ping = new URL(getDomain() + "/info/ping?content-type=application/json"); - HttpURLConnection conn = (HttpURLConnection) ping.openConnection(); - int rc = conn.getResponseCode(); - conn.disconnect(); - if (rc >= 200 && rc < 300) - { - return true; - } + + /* + * expect {"ping":1} if ok + */ + BufferedReader br = getHttpResponse(ping, null); + JSONParser jp = new JSONParser(); + JSONObject val = (JSONObject) jp.parse(br); + String pingString = val.get("ping").toString(); + return pingString != null; } catch (Throwable t) { System.err.println("Error connecting to " + PING_URL + ": " + t.getMessage()); + } finally + { + if (conn != null) + { + conn.disconnect(); + } } return false; }