From a96386b344a49391588762301ad71f1d39c11ca7 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Mon, 18 Jul 2016 16:39:34 +0100 Subject: [PATCH] JAL-2105 check content of Ensembl REST ping response --- src/jalview/ext/ensembl/EnsemblInfo.java | 2 +- src/jalview/ext/ensembl/EnsemblRestClient.java | 28 ++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) 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; } -- 1.7.10.2