3253-omnibus save
[jalview.git] / src / jalview / ext / ensembl / EnsemblRestClient.java
index 56d63bb..771980c 100644 (file)
  */
 package jalview.ext.ensembl;
 
-import jalview.bin.Cache;
-import jalview.javascript.json.JSON;
-import jalview.util.JSONUtils;
-import jalview.util.Platform;
-import jalview.util.StringUtils;
-
 import java.io.BufferedReader;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.Reader;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.ProtocolException;
@@ -44,6 +36,9 @@ import javax.ws.rs.HttpMethod;
 
 import org.json.simple.parser.ParseException;
 
+import jalview.util.Platform;
+import jalview.util.StringUtils;
+
 /**
  * Base class for Ensembl REST service clients
  * 
@@ -54,8 +49,8 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
 
   static
   {
-    Cache.addJ2SDirectDatabaseCall("http://rest.ensembl");
-    Cache.addJ2SDirectDatabaseCall("https://rest.ensembl");
+    Platform.addJ2SDirectDatabaseCall("http://rest.ensembl");
+    Platform.addJ2SDirectDatabaseCall("https://rest.ensembl");
   }
 
   private static final int DEFAULT_READ_TIMEOUT = 5 * 60 * 1000; // 5 minutes
@@ -74,9 +69,9 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
    * @see https://github.com/Ensembl/ensembl-rest/wiki/Change-log
    * @see http://rest.ensembl.org/info/rest?content-type=application/json
    */
-  private static final String LATEST_ENSEMBLGENOMES_REST_VERSION = "9.0";
+  private static final String LATEST_ENSEMBLGENOMES_REST_VERSION = "10.0";
 
-  private static final String LATEST_ENSEMBL_REST_VERSION = "9.0";
+  private static final String LATEST_ENSEMBL_REST_VERSION = "10.0";
 
   private static final String REST_CHANGE_LOG = "https://github.com/Ensembl/ensembl-rest/wiki/Change-log";
 
@@ -282,10 +277,12 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
    *          in milliseconds
    * @return
    * @throws IOException
+   * @throws ParseException
    */
-  private BufferedReader getHttpResponse(URL url, List<String> ids,
-          int readTimeout) throws IOException
+  private Object getJSON(URL url, List<String> ids, int readTimeout)
+          throws IOException, ParseException
   {
+
     if (readTimeout < 0)
     {
       readTimeout = DEFAULT_READ_TIMEOUT;
@@ -294,10 +291,7 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
     HttpURLConnection connection = null;
     int responseCode = 0;
 
-    if (Platform.isJS())
-    {
-      JSON.setAjax(url);
-    }
+    Platform.setAjaxJSON(url);
 
     while (retriesLeft > 0)
     {
@@ -325,15 +319,12 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
 
     InputStream response = connection.getInputStream();
 
-    if (Platform.isJS())
-    {
-      return JSON.getJSONReader(response);
-    }
-
-    // System.out.println(getClass().getName() + " took "
-    // + (System.currentTimeMillis() - now) + "ms to fetch");
+    // Platform.timeCheck(null, Platform.TIME_MARK);
+    Object ret = Platform.parseJSON(response);
+    // Platform.timeCheck("EnsemblRestClient.getJSON " + url,
+    // Platform.TIME_MARK);
 
-    return new BufferedReader(new InputStreamReader(response, "UTF-8"));
+    return ret;
   }
 
   /**
@@ -515,50 +506,26 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher
       url = getUrl(ids);
     }
 
-    Reader br = null;
-    try
-    {
-      Platform.timeCheck(null, Platform.TIME_MARK);
-
-      br = (url == null ? null : getHttpResponse(url, ids, msDelay));
+    Object json = (url == null ? null : getJSON(url, ids, msDelay));
 
-      Object ret = (br == null ? null : JSONUtils.parse(br));
-
-      Platform.timeCheck("EnsemblRestClient.getJSON " + url,
-              Platform.TIME_MARK);
-
-      if (ret != null && mapKey != null)
-      {
-        ret = ((Map<String, Object>) ret).get(mapKey);
-      }
-      if (ret == null)
-      {
-        return null;
-      }
-      switch (mode)
-      {
-      case MODE_ARRAY:
-      case MODE_MAP:
-        break;
-      case MODE_ITERATOR:
-        ret = ((List<Object>) ret).iterator();
-        break;
-      }
-      return ret;
-
-    } finally
+    if (json != null && mapKey != null)
     {
-      if (br != null)
-      {
-        try
-        {
-          br.close();
-        } catch (IOException e)
-        {
-          // ignore
-        }
-      }
+      json = ((Map<String, Object>) json).get(mapKey);
+    }
+    if (json == null)
+    {
+      return null;
+    }
+    switch (mode)
+    {
+    case MODE_ARRAY:
+    case MODE_MAP:
+      break;
+    case MODE_ITERATOR:
+      json = ((List<Object>) json).iterator();
+      break;
     }
+    return json;
   }
 
   /**