JAL-3193 removal of rest.ensemblgenomes.org
[jalview.git] / src / jalview / ext / ensembl / EnsemblInfo.java
index 950b658..9b97080 100644 (file)
 package jalview.ext.ensembl;
 
-/**
- * A data class to model the data and rest version of one Ensembl domain,
- * currently for rest.ensembl.org and rest.ensemblgenomes.org
- * 
- * @author gmcarstairs
- */
-class EnsemblInfo
+import jalview.datamodel.AlignmentI;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.json.simple.JSONArray;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+public class EnsemblInfo extends EnsemblRestClient
 {
-  /*
-   * The http domain this object is holding data values for
-   */
-  String domain;
 
   /*
-   * The latest version Jalview has tested for, e.g. "4.5"; a minor version change should be
-   * ok, a major version change may break stuff 
+   * cached upper-cased results of REST /info/divisions service, 
+   * currently (from April 2019)
+   *  { "ENSEMBLFUNGI", "http://rest.ensembl.org",
+   *    "ENSEMBLBACTERIA", "http://rest.ensembl.org",
+   *    "ENSEMBLPROTISTS", "http://rest.ensembl.org",
+   *    "ENSEMBLMETAZOA", "http://rest.ensembl.org",
+   *    "ENSEMBLPLANTS",  "http://rest.ensembl.org",
+   *    "ENSEMBL", "http://rest.ensembl.org" }
+   *  }
    */
-  String expectedRestVersion;
+  private static Map<String, String> divisions;
 
-  /*
-   * Major / minor / point version e.g. "4.5.1"
-   * @see http://rest.ensembl.org/info/rest/?content-type=application/json
+  /**
+   * Constructor
    */
-  String restVersion;
+  public EnsemblInfo()
+  {
+    super();
+  }
 
-  /*
-   * data version
-   * @see http://rest.ensembl.org/info/data/?content-type=application/json
-   */
-  String dataVersion;
+  @Override
+  public String getDbName()
+  {
+    return "ENSEMBL";
+  }
 
-  /*
-   * true when http://rest.ensembl.org/info/ping/?content-type=application/json
-   * returns response code 200
-   */
-  boolean restAvailable;
+  @Override
+  public AlignmentI getSequenceRecords(String queries) throws Exception
+  {
+    return null;
+  }
 
-  /*
-   * absolute time when availability was last checked
+  @Override
+  protected URL getUrl(List<String> ids) throws MalformedURLException
+  {
+    return null;
+  }
+
+  @Override
+  protected boolean useGetRequest()
+  {
+    return true;
+  }
+
+  /**
+   * Answers the domain (e.g. http://rest.ensembl.org) for the given division,
+   * or null if not recognised by Ensembl
+   * 
+   * @param division
+   * @return
    */
-  long lastAvailableCheckTime;
+  public String getDomain(String division)
+  {
+    if (divisions == null)
+    {
+      fetchDivisions();
+    }
+    return divisions.get(division.toUpperCase());
+  }
 
-  /*
-   * absolute time when version numbers were last checked
+  /**
+   * On first request only, populate the lookup map by fetching the list of
+   * divisions known to Ensembl
    */
-  long lastVersionCheckTime;
+  void fetchDivisions()
+  {
+    divisions = new HashMap<>();
 
-  // flag set to true if REST major version is not the one expected
-  boolean restMajorVersionMismatch;
+    BufferedReader br = null;
+    try
+    {
+      URL url = getDivisionsUrl();
+      if (url != null)
+      {
+        br = getHttpResponse(url, null);
+      }
+      parseResponse(br);
+    } catch (IOException e)
+    {
+      // ignore
+    } finally
+    {
+      if (br != null)
+      {
+        try
+        {
+          br.close();
+        } catch (IOException e)
+        {
+          // ignore
+        }
+      }
+    }
+  }
 
-  /*
-   * absolute time to wait till if we overloaded the REST service
+  /**
+   * Parses the JSON response to /info/divisions, and add each to the lookup map
+   * 
+   * @param br
    */
-  long retryAfter;
+  void parseResponse(BufferedReader br)
+  {
+    JSONParser jp = new JSONParser();
+
+    try
+    {
+      JSONArray parsed = (JSONArray) jp.parse(br);
+      String domain = getDomain();
+
+      Iterator rvals = parsed.iterator();
+      while (rvals.hasNext())
+      {
+        String division = rvals.next().toString();
+        divisions.put(division.toUpperCase(), domain);
+      }
+    } catch (IOException | ParseException | NumberFormatException e)
+    {
+      // ignore
+    }
+  }
 
   /**
-   * Constructor given expected REST version number e.g 4.5 or 3.4.3
+   * Constructs the URL for the Ensembl /info/divisions REST service
    * 
-   * @param restExpected
+   * @return
+   * @throws MalformedURLException
    */
-  EnsemblInfo(String theDomain, String restExpected)
+  URL getDivisionsUrl() throws MalformedURLException
   {
-    domain = theDomain;
-    expectedRestVersion = restExpected;
-    lastAvailableCheckTime = -1;
-    lastVersionCheckTime = -1;
+    return new URL(
+            getDomain()
+            + "/info/divisions?content-type=application/json");
   }
 
+  /**
+   * Returns the set of 'divisions' recognised by Ensembl
+   * 
+   * @return
+   */
+  public Set<String> getDivisions() {
+    if (divisions == null)
+    {
+      fetchDivisions();
+    }
+
+    return divisions.keySet();
+  }
 }