JAL-3829 abstract essential TDB metadata from the user configurable data array displa...
[jalview.git] / src / jalview / fts / service / threedbeacons / TDBeaconsFTSRestClient.java
index a57cf3b..ccdc525 100644 (file)
@@ -34,14 +34,21 @@ import jalview.util.Platform;
 public class TDBeaconsFTSRestClient extends FTSRestClient
         implements StructureFTSRestClientI
 {
-  private static final String DEFAULT_THREEDBEACONS_DOMAIN = "https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
-
-  private static FTSRestClientI instance = null;
+  /**
+   * production server URI
+   */
+  private static String TDB_PROD_API="https://www.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
+  /**
+   * dev server URI
+   */
+  private static String TDB_DEV_API="https://wwwdev.ebi.ac.uk/pdbe/pdbe-kb/3dbeacons/api/uniprot/summary/";
+  private static String DEFAULT_THREEDBEACONS_DOMAIN = TDB_PROD_API; 
+
+  public static FTSRestClientI instance = null;
 
   protected TDBeaconsFTSRestClient()
   {
   }
-
   @SuppressWarnings("unchecked")
   @Override
   public FTSRestResponse executeRequest(FTSRestRequest tdbRestRequest)
@@ -68,15 +75,23 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         client = Client.create(new DefaultClientConfig());
         clientResponseClass = ClientResponse.class;
       }
+
       WebResource webResource;
-      webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN)
-              .path(query);
+      webResource = client.resource(DEFAULT_THREEDBEACONS_DOMAIN+query);
+
       URI uri = webResource.getURI();
       System.out.println(uri.toString());
 
       // Execute the REST request
-      ClientResponse clientResponse = webResource
-              .accept(MediaType.APPLICATION_JSON).get(clientResponseClass);
+      ClientResponse clientResponse;
+      if (isMocked()) { 
+        clientResponse = null;
+      }
+      else
+      {
+        clientResponse = webResource.accept(MediaType.APPLICATION_JSON)
+                .get(clientResponseClass);
+      }
 
       // Get the JSON string from the response object or directly from the
       // client (JavaScript)
@@ -84,7 +99,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
       String responseString = null;
 
       // Check the response status and report exception if one occurs
-      int responseStatus = clientResponse.getStatus();
+      int responseStatus = isMocked() ? (mockQueries.containsKey(query) ? 200 : 404) : clientResponse.getStatus();
       switch (responseStatus)
       {
       // if success
@@ -95,11 +110,13 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
         }
         else
         {
-          responseString = clientResponse.getEntity(String.class);
+          responseString = isMocked() ? mockQueries.get(query): clientResponse.getEntity(String.class);
         }
         break;
       case 400:
         throw new Exception(parseJsonExceptionString(responseString));
+      case 404:
+        return emptyTDBeaconsJsonResponse();
       default:
         throw new Exception(
                 getMessageByHTTPStatusCode(responseStatus, "3DBeacons"));
@@ -110,26 +127,38 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
     } catch (Exception e)
     {
       String exceptionMsg = e.getMessage();
-      if (exceptionMsg.contains("SocketException"))
-      {
-        // No internet connection
-        throw new Exception(MessageManager.getString(
-                "exception.unable_to_detect_internet_connection"));
-      }
-      else if (exceptionMsg.contains("UnknownHostException"))
-      {
-        // The server is unreachable
-        throw new Exception(MessageManager.formatMessage(
-                "exception.fts_server_unreachable", "3DB Hub"));
-      }
-      else
+      if (exceptionMsg != null)
       {
-        throw e;
+        if (exceptionMsg.contains("SocketException"))
+        {
+          // No internet connection
+          throw new Exception(MessageManager.getString(
+                  "exception.unable_to_detect_internet_connection"));
+        }
+        else if (exceptionMsg.contains("UnknownHostException"))
+        {
+          // The server is unreachable
+          throw new Exception(MessageManager.formatMessage(
+                  "exception.fts_server_unreachable", "3DB Hub"));
+        }
       }
+      throw e;
+      
     }
 
   }
 
+  /**
+   * returns response for when the 3D-Beacons service doesn't have a record for
+   * the given query - in 2.11.2 this triggers a failover to the PDBe FTS 
+   * 
+   * @return null
+   */
+  private FTSRestResponse emptyTDBeaconsJsonResponse()
+  {
+    return null;
+  }
+
   public String setSearchTerm(String term)
   {
     return term;
@@ -190,8 +219,6 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
   private static FTSData getFTSData(Map<String, Object> tdbJsonStructure,
           FTSRestRequest tdbRequest)
   {
-    // TODO: consider reusing PDBFTSRestClient.getFTSData ?
-
     String primaryKey = null;
     Object[] summaryRowData;
 
@@ -246,52 +273,7 @@ public class TDBeaconsFTSRestClient extends FTSRestClient
     final String primaryKey1 = primaryKey;
     final Object[] summaryRowData1 = summaryRowData;
 
-    return new FTSData()
-    {
-
-      @Override
-      public Object[] getSummaryData()
-      {
-        return summaryRowData1;
-      }
-
-      @Override
-      public Object getPrimaryKey()
-      {
-        return primaryKey1;
-      }
-
-      /**
-       * Returns a string representation of this object;
-       */
-      @Override
-      public String toString()
-      {
-        StringBuilder summaryFieldValues = new StringBuilder();
-        for (Object summaryField : summaryRowData1)
-        {
-          summaryFieldValues.append(
-                  summaryField == null ? " " : summaryField.toString())
-                  .append("\t");
-        }
-        return summaryFieldValues.toString();
-      }
-
-      /**
-       * Returns hash code value for this object
-       */
-      @Override
-      public int hashCode()
-      {
-        return Objects.hash(primaryKey1, this.toString());
-      }
-
-      @Override
-      public boolean equals(Object that)
-      {
-        return this.toString().equals(that.toString());
-      }
-    };
+    return new TDB_FTSData(primaryKey, tdbJsonStructure, summaryRowData1);
   }
 
   // private static FTSData getFTSData(Map<String, Object> doc,