Search in sources :

Example 6 with ConnectionException

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!");
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpEntity(org.apache.http.HttpEntity) NoHttpResponseException(org.apache.http.NoHttpResponseException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectionException(org.neo4j.ogm.exception.ConnectionException) NoHttpResponseException(org.apache.http.NoHttpResponseException) ResultProcessingException(org.neo4j.ogm.exception.ResultProcessingException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) HttpResponseException(org.apache.http.client.HttpResponseException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ClientProtocolException(org.apache.http.client.ClientProtocolException) StatusLine(org.apache.http.StatusLine) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BasicHeader(org.apache.http.message.BasicHeader) ConnectionException(org.neo4j.ogm.exception.ConnectionException)

Aggregations

ConnectionException (org.neo4j.ogm.exception.ConnectionException)6 ClientException (org.neo4j.driver.exceptions.ClientException)3 ServiceUnavailableException (org.neo4j.driver.exceptions.ServiceUnavailableException)3 IOException (java.io.IOException)2 URI (java.net.URI)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 Path (java.nio.file.Path)1 HttpEntity (org.apache.http.HttpEntity)1 NoHttpResponseException (org.apache.http.NoHttpResponseException)1 StatusLine (org.apache.http.StatusLine)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpResponseException (org.apache.http.client.HttpResponseException)1