use of org.neo4j.ogm.exception.ConnectionException in project neo4j-ogm by neo4j.
the class HttpRequest method execute.
public static CloseableHttpResponse execute(CloseableHttpClient httpClient, HttpRequestBase request, Credentials credentials) throws HttpRequestException {
LOGGER.debug("Thread: {}, request: {}", Thread.currentThread().getId(), request);
CloseableHttpResponse response;
request.setHeader(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
request.setHeader(new BasicHeader(HTTP.USER_AGENT, "neo4j-ogm.java/2.0"));
request.setHeader(new BasicHeader("Accept", "application/json;charset=UTF-8"));
HttpAuthorization.authorize(request, credentials);
// use defaults: 3 retries, 2 second wait between attempts
RetryOnExceptionStrategy retryStrategy = new RetryOnExceptionStrategy();
while (retryStrategy.shouldRetry()) {
try {
response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
HttpEntity responseEntity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
String responseText = statusLine.getReasonPhrase();
if (responseEntity != null) {
responseText = parseError(EntityUtils.toString(responseEntity));
LOGGER.warn("Thread: {}, response: {}", Thread.currentThread().getId(), responseText);
}
throw new HttpResponseException(statusLine.getStatusCode(), responseText);
}
if (responseEntity == null) {
throw new ClientProtocolException("Response contains no content");
}
// don't close response yet, it is not consumed!
return response;
} catch (NoHttpResponseException nhre) {
// if we didn't get a response at all, try again
LOGGER.warn("Thread: {}, No response from server: Retrying in {} milliseconds, retries left: {}", Thread.currentThread().getId(), retryStrategy.getTimeToWait(), retryStrategy.numberOfTriesLeft);
retryStrategy.errorOccurred();
} catch (RetryException re) {
throw new HttpRequestException(request, re);
} catch (ClientProtocolException uhe) {
throw new ConnectionException(request.getURI().toString(), uhe);
} catch (IOException ioe) {
throw new HttpRequestException(request, ioe);
} catch (Exception exception) {
// here we catch any exception we throw above (plus any we didn't throw ourselves),
// log the problem, close any connection held by the request
// and then rethrow the exception to the caller.
LOGGER.warn("Thread: {}, exception: {}", Thread.currentThread().getId(), exception.getCause().getLocalizedMessage());
request.releaseConnection();
throw exception;
}
}
throw new RuntimeException("Fatal Exception: Should not have occurred!");
}
Aggregations