+ /**
+ * Primary access point to parsed JSON data, including the call to retrieve and
+ * parsing.
+ *
+ * @param url request url; if null, getUrl(ids) will be used
+ * @param ids optional; may be null
+ * @param msDelay -1 for default delay
+ * @param mode map, array, or array iterator
+ * @param mapKey an optional key for an outer map
+ * @return a Map, List, Iterator, or null
+ * @throws IOException
+ * @throws ParseException
+ *
+ * @author Bob Hanson 2019
+ */
+ @SuppressWarnings("unchecked")
+ protected Object getJSON(URL url, List<String> ids, int msDelay, int mode, String mapKey) throws IOException, ParseException {
+
+ if (url == null)
+ url = getUrl(ids);
+
+ Reader br = null;
+ try {
+
+ Platform.timeCheck(null, Platform.TIME_MARK);
+
+ br = (url == null ? null : getHttpResponse(url, ids, msDelay));
+
+ Object ret = (br == null ? null : JSONUtils.parse(br));
+
+ Platform.timeCheck("EnsembleRestClient.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 (br != null)
+ {
+ try
+ {
+ br.close();
+ } catch (IOException e)
+ {
+ // ignore
+ }
+ }
+ }
+ }
+
+
+
+ /**
+ * Fetches and checks Ensembl's REST version number
+ *
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ private void checkEnsemblRestVersion()
+ {
+ EnsemblData info = domainData.get(getDomain());
+
+ try
+ {
+ Map<String, Object> val = (Map<String, Object>) getJSON(new URL(getDomain() + "/info/rest" + CONTENT_TYPE_JSON), null, -1, MODE_MAP, null);
+ if (val == null)
+ return;
+ String version = val.get("release").toString();
+ String majorVersion = version.substring(0, version.indexOf("."));
+ String expected = info.expectedRestVersion;
+ String expectedMajorVersion = expected.substring(0,
+ expected.indexOf("."));
+ info.restMajorVersionMismatch = false;
+ try
+ {
+ /*
+ * if actual REST major version is ahead of what we expect,
+ * record this in case we want to warn the user
+ */
+ if (Float.valueOf(majorVersion) > Float
+ .valueOf(expectedMajorVersion))
+ {
+ info.restMajorVersionMismatch = true;
+ }
+ } catch (NumberFormatException e)
+ {
+ System.err.println("Error in REST version: " + e.toString());
+ }
+
+ /*
+ * check if REST version is later than what Jalview has tested against,
+ * if so warn; we don't worry if it is earlier (this indicates Jalview has
+ * been tested in advance against the next pending REST version)
+ */
+ boolean laterVersion = StringUtils.compareVersions(version,
+ expected) == 1;
+ if (laterVersion)
+ {
+ System.err.println(String.format(
+ "EnsemblRestClient expected %s REST version %s but found %s, see %s",
+ getDbSource(), expected, version, REST_CHANGE_LOG));
+ }
+ info.restVersion = version;
+ } catch (Throwable t)
+ {
+ System.err.println(
+ "Error checking Ensembl REST version: " + t.getMessage());
+ }
+ }
+
+ public boolean isRestMajorVersionMismatch()
+ {
+ return domainData.get(getDomain()).restMajorVersionMismatch;
+ }
+
+ /**
+ * Fetches and checks Ensembl's data version number
+ *
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ private void checkEnsemblDataVersion()
+ {
+ Map<String, Object> val;
+ try
+ {
+ val = (Map<String, Object>) getJSON(
+ new URL(getDomain() + "/info/data" + CONTENT_TYPE_JSON), null, -1, MODE_MAP, null);
+ if (val == null)
+ return;
+ List<Object> versions = (List<Object>) val.get("releases");
+ domainData.get(getDomain()).dataVersion = versions.get(0).toString();
+ } catch (Throwable e) {//could be IOException | ParseException e) {
+ System.err.println("Error checking Ensembl data version: " + e.getMessage());
+ }
+ }
+
+ public String getEnsemblDataVersion()
+ {
+ return domainData.get(getDomain()).dataVersion;
+ }
+
+ @Override
+ public String getDbVersion()
+ {
+ return getEnsemblDataVersion();
+ }
+