- ClientConfig clientConfig = new DefaultClientConfig();
- clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
- Boolean.TRUE);
- Client client = Client.create(clientConfig);
-
- String query = pdbRestRequest.getFieldToSearchBy()
- + pdbRestRequest.getSearchTerm()
- + ((pdbRestRequest.isAllowEmptySeq()) ? ""
- : " AND molecule_sequence:['' TO *]");
-
- String wantedFields = getPDBDocFieldsAsCommaDelimitedString(pdbRestRequest
- .getWantedFields());
-
- String responseSize = (pdbRestRequest.getResponseSize() == 0) ? "200"
- : String.valueOf(pdbRestRequest.getResponseSize());
- String sortParam = (pdbRestRequest.getFieldToSortBy() == null || pdbRestRequest
- .getFieldToSortBy().trim().isEmpty()) ? ""
- : (pdbRestRequest
- .getFieldToSortBy() + (pdbRestRequest.isAscending() ? " asc"
- : " desc"));
-
- WebResource webResource = client.resource(pdbSearchEndpoint)
- .queryParam("wt", "json").queryParam("fl", wantedFields)
- .queryParam("rows", responseSize)
- .queryParam("q", query)
- .queryParam("sort", sortParam);
- ClientResponse clientResponse = webResource.accept(
- MediaType.APPLICATION_JSON).get(ClientResponse.class);
-
- String responseString = clientResponse.getEntity(String.class);
- if (clientResponse.getStatus() != 200)
+ try
+ {
+ 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)
+ {
+ 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)