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;
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();
}
/**
+ * @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
*