JAL-1700 Improved the error handling mechanism for the PDB Rest client
[jalview.git] / src / jalview / ws / dbsources / PDBRestClient.java
index 02523da..da50606 100644 (file)
@@ -41,61 +41,115 @@ public class PDBRestClient
    * @param pdbRestRequest
    *          the PDBRestRequest instance to be processed
    * @return the pdbResponse object for the given request
+   * @throws Exception
    */
   public PDBRestResponse executeRequest(PDBRestRequest pdbRestRequest)
+          throws Exception
   {
-    ClientConfig clientConfig = new DefaultClientConfig();
-    clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
-            Boolean.TRUE);
-    Client client = Client.create(clientConfig);
-
-    String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
-            .getWantedFields());
-    int responseSize = (pdbRestRequest.getResponseSize() == 0) ? DEFAULT_RESPONSE_SIZE
-            : pdbRestRequest.getResponseSize();
-    String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
-            .getFieldToSortBy().trim().isEmpty()) ? "" : (pdbRestRequest
-            .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
-            : " desc"));
-
-    // Build request parameters for the REST Request
-    WebResource webResource = client.resource(PDB_SEARCH_ENDPOINT)
-            .queryParam("wt", "json").queryParam("fl", wantedFields)
-            .queryParam("rows", String.valueOf(responseSize))
-            .queryParam("q", pdbRestRequest.getQuery())
-            .queryParam("sort", sortParam);
-
-    // Execute the REST request
-    ClientResponse clientResponse = webResource.accept(
-            MediaType.APPLICATION_JSON).get(ClientResponse.class);
-
-    // Get the JSON string from the response object
-    String responseString = clientResponse.getEntity(String.class);
-
-    // Check the response status and report exception if one occurs
-    if (clientResponse.getStatus() != 200)
+    try
+    {
+      ClientConfig clientConfig = new DefaultClientConfig();
+      clientConfig.getFeatures().put(
+              JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
+      Client client = Client.create(clientConfig);
+
+      String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
+              .getWantedFields());
+      int responseSize = (pdbRestRequest.getResponseSize() == 0) ? DEFAULT_RESPONSE_SIZE
+              : pdbRestRequest.getResponseSize();
+      String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
+              .getFieldToSortBy().trim().isEmpty()) ? "" : (pdbRestRequest
+              .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
+              : " desc"));
+
+      // Build request parameters for the REST Request
+      WebResource webResource = client.resource(PDB_SEARCH_ENDPOINT)
+              .queryParam("wt", "json").queryParam("fl", wantedFields)
+              .queryParam("rows", String.valueOf(responseSize))
+              .queryParam("q", pdbRestRequest.getQuery())
+              .queryParam("sort", sortParam);
+
+      // Execute the REST request
+      ClientResponse clientResponse = webResource.accept(
+              MediaType.APPLICATION_JSON).get(ClientResponse.class);
+
+      // Get the JSON string from the response object
+      String responseString = clientResponse.getEntity(String.class);
+
+      // Check the response status and report exception if one occurs
+      if (clientResponse.getStatus() != 200)
+      {
+        String errorMessage = "";
+        if (clientResponse.getStatus() == 400)
+        {
+          errorMessage = parseJsonExceptionString(responseString);
+          throw new Exception(errorMessage);
+        }
+        else
+        {
+          errorMessage = getMessageByHTTPStatusCode(clientResponse
+                  .getStatus());
+          throw new Exception(errorMessage);
+        }
+      }
+
+      // Make redundant objects eligible for garbage collection to conserve
+      // memory
+      clientResponse = null;
+      client = null;
+
+      // Process the response and return the result to the caller.
+      return parsePDBJsonResponse(responseString, pdbRestRequest);
+    } catch (Exception e)
     {
-      String errorMessage = "";
-      if (clientResponse.getStatus() == 400)
+      String exceptionMsg = e.getMessage();
+      if (exceptionMsg.contains("SocketException"))
+      {
+        throw new Exception(
+                "Jalview is unable to detect an internet connection");
+        // No internet connection
+      }
+      else if (exceptionMsg.contains("UnknownHostException"))
       {
-        errorMessage = parseJsonExceptionString(responseString);
-        throw new RuntimeException(errorMessage);
+        throw new Exception(
+                "Jalview couldn't reach the host server @ www.ebi.ac.uk"
+                        + "\nPlease ensure that you are connected to the internet.");
+        // The server 'www.ebi.ac.uk' is unreachable
       }
       else
       {
-        errorMessage = "Failed : HTTP error code : "
-                + clientResponse.getStatus();
-        throw new RuntimeException(errorMessage);
+        throw e;
       }
     }
+  }
 
-    // Make redundant objects eligible for garbage collection to conserve
-    // memory
-    clientResponse = null;
-    client = null;
-
-    // Process the response and return the result to the caller.
-    return parsePDBJsonResponse(responseString, pdbRestRequest);
+  public String getMessageByHTTPStatusCode(int code)
+  {
+    String message = "";
+    switch (code)
+    {
+    case 410:
+      message = "PDB Rest Service no longer exists!";
+      break;
+    case 403:
+    case 404:
+      message = "The requested resource could not be found";
+      break;
+    case 408:
+    case 409:
+    case 500:
+    case 501:
+    case 502:
+    case 503:
+    case 504:
+    case 505:
+      message = "There seems to be an error from the PDB Rest API server.";
+      break;
+
+    default:
+      break;
+    }
+    return message;
   }
 
   /**
@@ -240,7 +294,6 @@ public class PDBRestClient
     return pdbFeildIndexCounter;
   }
 
-
   /**
    * This enum represents the fields available in the PDB JSON response
    *