JAL-2523 start of refactoring...
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 4 Oct 2017 12:55:17 +0000 (13:55 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Wed, 4 Oct 2017 12:55:17 +0000 (13:55 +0100)
src/jalview/ext/ensembl/EnsemblRestClient.java

index ad6c70c..81bc560 100644 (file)
@@ -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<String> 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
    *