+ int offSet = uniprotRestRequest.getOffSet();
+ String query;
+ if (isAdvancedQuery(uniprotRestRequest.getSearchTerm()))
+ {
+ query = uniprotRestRequest.getSearchTerm();
+ }
+ else
+ {
+ query = uniprotRestRequest.getFieldToSearchBy().equalsIgnoreCase(
+ "Search All") ? uniprotRestRequest.getSearchTerm()
+ // + " or mnemonic:"
+ // + uniprotRestRequest.getSearchTerm()
+ : uniprotRestRequest.getFieldToSearchBy() + ":"
+ + uniprotRestRequest.getSearchTerm();
+ }
+
+ // BH 2018 the trick here is to coerce the classes in Javascript to be
+ // different from the ones in Java yet still allow this to be correct for
+ // Java
+ Client client;
+ Class<ClientResponse> clientResponseClass;
+ if (Platform.isJS())
+ {
+ // JavaScript only -- coerce types to Java types for Java
+ client = (Client) (Object) new jalview.javascript.web.Client();
+ clientResponseClass = (Class<ClientResponse>) (Object) jalview.javascript.web.ClientResponse.class;
+ }
+ else
+ /**
+ * Java only
+ *
+ * @j2sIgnore
+ */
+ {
+ // Java only
+ client = Client.create(new DefaultClientConfig());
+ clientResponseClass = ClientResponse.class;
+ }
+
+ WebResource webResource = null;
+ webResource = client.resource(uniprotSearchEndpoint)
+ .queryParam("format", "tsv")
+ .queryParam("fields", wantedFields)
+ .queryParam("size", String.valueOf(responseSize))
+ /* 2022 new api has no "sort"
+ * .queryParam("sort", "score")
+ */
+ .queryParam("query", query);
+ if (offSet != 0 && cursor != null && cursor.length() > 0)
+ // 2022 new api does not do pagination with an offset, it requires a
+ // "cursor" parameter with a key (given for the next page).
+ // (see https://www.uniprot.org/help/pagination)
+ {
+ webResource = webResource.queryParam("cursor", cursor);
+ }
+ Console.debug(
+ "Uniprot FTS Request: " + webResource.getURI().toString());
+ // Execute the REST request
+ WebResource.Builder wrBuilder = webResource
+ .accept(MediaType.TEXT_PLAIN);
+ if (!Platform.isJS())
+ /**
+ * Java only
+ *
+ * @j2sIgnore
+ */
+ {
+ wrBuilder.header("User-Agent", USER_AGENT);
+ }
+ ClientResponse clientResponse = wrBuilder.get(clientResponseClass);
+
+ if (!Platform.isJS())
+ /**
+ * Java only
+ *
+ * @j2sIgnore
+ */
+ {
+ if (clientResponse.getHeaders().containsKey("Link"))
+ {
+ // extract the URL from the 'Link: <URL>; ref="stuff"' header
+ String linkHeader = clientResponse.getHeaders().get("Link")
+ .get(0);
+ if (linkHeader.indexOf("<") > -1)
+ {
+ String temp = linkHeader.substring(linkHeader.indexOf("<") + 1);
+ if (temp.indexOf(">") > -1)
+ {
+ String nextUrl = temp.substring(0, temp.indexOf(">"));
+ // then get the cursor value from the query string parameters
+ String nextCursor = getQueryParam("cursor", nextUrl);
+ setCursor(cursorPage + 1, nextCursor);
+ }
+ }
+ }
+ }
+
+ String uniProtTabDelimittedResponseString = clientResponse
+ .getEntity(String.class);
+ // Make redundant objects eligible for garbage collection to conserve
+ // memory
+ // jalview.bin.Console.outPrintln(">>>>> response : "
+ // + uniProtTabDelimittedResponseString);
+ if (clientResponse.getStatus() != 200)
+ {
+ String errorMessage = getMessageByHTTPStatusCode(
+ clientResponse.getStatus(), "Uniprot");
+ throw new Exception(errorMessage);
+
+ }
+ // new Uniprot API is not including a "X-Total-Results" header when there
+ // are 0 results
+ List<String> resultsHeaders = clientResponse.getHeaders()
+ .get("X-Total-Results");
+ int xTotalResults = 0;
+ if (Platform.isJS())
+ {
+ xTotalResults = 1;
+ }
+ else if (resultsHeaders != null && resultsHeaders.size() >= 1)
+ {
+ xTotalResults = Integer.valueOf(resultsHeaders.get(0));
+ }
+ clientResponse = null;
+ client = null;
+ return parseUniprotResponse(uniProtTabDelimittedResponseString,
+ uniprotRestRequest, xTotalResults);
+ } catch (Exception e)
+ {
+ Console.warn("Problem with the query: " + e.getMessage());
+ Console.debug("Exception stacktrace:", 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 'http://www.uniprot.org' is unreachable
+ throw new Exception(MessageManager.formatMessage(
+ "exception.fts_server_unreachable", "Uniprot"));
+ }
+ else
+ {
+ throw e;
+ }
+ }
+ }
+
+ public boolean isAdvancedQuery(String query)
+ {
+ if (query.contains(" AND ") || query.contains(" OR ")
+ || query.contains(" NOT ") || query.contains(" ! ")
+ || query.contains(" || ") || query.contains(" && ")
+ || query.contains(":") || query.contains("-"))
+ {
+ return true;
+ }
+ return false;