From: gmungoc Date: Wed, 4 Oct 2017 12:55:17 +0000 (+0100) Subject: JAL-2523 start of refactoring... X-Git-Tag: Release_2_10_3b1~115^2~2 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=2af6c8f2c62b0e37c422d6d491a12ffe9ba026a7;p=jalview.git JAL-2523 start of refactoring... --- diff --git a/src/jalview/ext/ensembl/EnsemblRestClient.java b/src/jalview/ext/ensembl/EnsemblRestClient.java index ad6c70c..81bc560 100644 --- a/src/jalview/ext/ensembl/EnsemblRestClient.java +++ b/src/jalview/ext/ensembl/EnsemblRestClient.java @@ -31,6 +31,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; +import java.net.ProtocolException; import java.net.URL; import java.util.HashMap; import java.util.List; @@ -296,29 +297,12 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher int readTimeout) throws IOException { // long now = System.currentTimeMillis(); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - - /* - * POST method allows multiple queries in one request; it is supported for - * sequence queries, but not for overlap - */ - boolean multipleIds = ids != null && ids.size() > 1; - connection.setRequestMethod( - multipleIds ? HttpMethod.POST : HttpMethod.GET); - connection.setRequestProperty("Content-Type", - getRequestMimeType(multipleIds)); - connection.setRequestProperty("Accept", getResponseMimeType()); - - connection.setUseCaches(false); - connection.setDoInput(true); - connection.setDoOutput(multipleIds); - - connection.setConnectTimeout(CONNECT_TIMEOUT_MS); - connection.setReadTimeout(readTimeout); - - if (multipleIds) + int maxRetries = 3; + int retriesLeft = maxRetries; + HttpURLConnection connection = null; + while (retriesLeft > 0) { - writePostBody(connection, ids); + connection = tryConnection(url, ids, readTimeout); } int responseCode = connection.getResponseCode(); @@ -346,6 +330,44 @@ abstract class EnsemblRestClient extends EnsemblSequenceFetcher } /** + * @param url + * @param ids + * @param readTimeout + * @return + * @throws IOException + * @throws ProtocolException + */ + protected HttpURLConnection tryConnection(URL url, List ids, + int readTimeout) throws IOException, ProtocolException + { + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + /* + * POST method allows multiple queries in one request; it is supported for + * sequence queries, but not for overlap + */ + boolean multipleIds = ids != null && ids.size() > 1; + connection.setRequestMethod( + multipleIds ? HttpMethod.POST : HttpMethod.GET); + connection.setRequestProperty("Content-Type", + getRequestMimeType(multipleIds)); + connection.setRequestProperty("Accept", getResponseMimeType()); + + connection.setUseCaches(false); + connection.setDoInput(true); + connection.setDoOutput(multipleIds); + + connection.setConnectTimeout(CONNECT_TIMEOUT_MS); + connection.setReadTimeout(readTimeout); + + if (multipleIds) + { + writePostBody(connection, ids); + } + return connection; + } + + /** * Inspect response headers for any sign of server overload and respect any * 'retry-after' directive *