X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fws%2Fdbsources%2FPDBRestClient.java;h=16b3f55fb7d30b1bac3ceef1c73ef5d70507a065;hb=c19d2a91ca05e052e3408bf5852d88eb5d0608f1;hp=1ab6125578086144beb29c4b41fb05c2d5c341b3;hpb=be32c14cd8e48fe0a207cd7030cb9cd46f894678;p=jalview.git
diff --git a/src/jalview/ws/dbsources/PDBRestClient.java b/src/jalview/ws/dbsources/PDBRestClient.java
index 1ab6125..16b3f55 100644
--- a/src/jalview/ws/dbsources/PDBRestClient.java
+++ b/src/jalview/ws/dbsources/PDBRestClient.java
@@ -1,5 +1,26 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
+ * Copyright (C) 2015 The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
package jalview.ws.dbsources;
+import jalview.util.MessageManager;
import jalview.ws.uimodel.PDBRestRequest;
import jalview.ws.uimodel.PDBRestResponse;
import jalview.ws.uimodel.PDBRestResponse.PDBResponseSummary;
@@ -21,7 +42,6 @@ import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
-import com.sun.jersey.api.json.JSONConfiguration;
/**
* A rest client for querying the Search endpoing of the PDB REST API
@@ -31,7 +51,7 @@ import com.sun.jersey.api.json.JSONConfiguration;
*/
public class PDBRestClient
{
- private static String PDB_SEARCH_ENDPOINT = "http://wwwdev.ebi.ac.uk/pdbe/search/pdb/select?";
+ public static final String PDB_SEARCH_ENDPOINT = "http://www.ebi.ac.uk/pdbe/search/pdb/select?";
private static int DEFAULT_RESPONSE_SIZE = 200;
@@ -41,61 +61,114 @@ 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
{
- String errorMessage = "";
- if (clientResponse.getStatus() == 400)
+ ClientConfig clientConfig = new DefaultClientConfig();
+ 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)
{
- errorMessage = parseJsonExceptionString(responseString);
- throw new RuntimeException(errorMessage);
+ 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 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 'www.ebi.ac.uk' is unreachable
+ throw new Exception(
+ MessageManager
+ .getString("exception.pdb_server_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;
+ public String getMessageByHTTPStatusCode(int code)
+ {
+ String message = "";
+ switch (code)
+ {
+ case 410:
+ message = MessageManager
+ .getString("exception.pdb_rest_service_no_longer_available");
+ break;
+ case 403:
+ case 404:
+ message = MessageManager.getString("exception.resource_not_be_found");
+ break;
+ case 408:
+ case 409:
+ case 500:
+ case 501:
+ case 502:
+ case 503:
+ case 504:
+ case 505:
+ message = MessageManager.getString("exception.pdb_server_error");
+ break;
- // Process the response and return the result to the caller.
- return parsePDBJsonResponse(responseString, pdbRestRequest);
+ default:
+ break;
+ }
+ return message;
}
/**
@@ -107,22 +180,33 @@ public class PDBRestClient
*/
public static String parseJsonExceptionString(String jsonErrorResponse)
{
- String errorMessage = "RunTime error";
+ StringBuilder errorMessage = new StringBuilder(
+ "\n============= PDB Rest Client RunTime error =============\n");
+
try
{
JSONParser jsonParser = new JSONParser();
JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonErrorResponse);
JSONObject errorResponse = (JSONObject) jsonObj.get("error");
- errorMessage = errorResponse.get("msg").toString();
JSONObject responseHeader = (JSONObject) jsonObj
.get("responseHeader");
- errorMessage += responseHeader.get("params").toString();
+ JSONObject paramsObj = (JSONObject) responseHeader.get("params");
+ String status = responseHeader.get("status").toString();
+ String message = errorResponse.get("msg").toString();
+ String query = paramsObj.get("q").toString();
+ String fl = paramsObj.get("fl").toString();
+
+ errorMessage.append("Status: ").append(status).append("\n");
+ errorMessage.append("Message: ").append(message).append("\n");
+ errorMessage.append("query: ").append(query).append("\n");
+ errorMessage.append("fl: ").append(fl).append("\n");
+
} catch (ParseException e)
{
e.printStackTrace();
}
- return errorMessage;
+ return errorMessage.toString();
}
/**
@@ -206,27 +290,27 @@ public class PDBRestClient
* table. The PDB Id serves as a unique identifier for a given row in the
* summary table
*
- * @param wantedFeilds
+ * @param wantedFields
* the available table columns in no particular order
* @return the pdb id field column index
*/
public static int getPDBIdColumIndex(
- Collection wantedFeilds, boolean hasRefSeq)
+ Collection wantedFields, boolean hasRefSeq)
{
// If a reference sequence is attached then start counting from 1 else
// start from zero
- int pdbFeildIndexCounter = hasRefSeq ? 1 : 0;
+ int pdbFieldIndexCounter = hasRefSeq ? 1 : 0;
- for (PDBDocField feild : wantedFeilds)
+ for (PDBDocField field : wantedFields)
{
- if (feild.equals(PDBDocField.PDB_ID))
+ if (field.equals(PDBDocField.PDB_ID))
{
break; // Once PDB Id index is determined exit iteration
}
- ++pdbFeildIndexCounter;
+ ++pdbFieldIndexCounter;
}
- return pdbFeildIndexCounter;
+ return pdbFieldIndexCounter;
}
/**
@@ -335,4 +419,4 @@ public class PDBRestClient
return name;
}
}
-}
\ No newline at end of file
+}