Search in sources :

Example 1 with HttpRequestException

use of org.neo4j.ogm.drivers.http.request.HttpRequestException in project neo4j-ogm by neo4j.

the class HttpDriver method newTransactionUrl.

private String newTransactionUrl(Transaction.Type type) {
    String url = transactionEndpoint(configuration.getURI());
    LOGGER.debug("Thread: {}, POST {}", Thread.currentThread().getId(), url);
    HttpPost request = new HttpPost(url);
    request.setHeader("X-WRITE", type == Transaction.Type.READ_ONLY ? "0" : "1");
    try (CloseableHttpResponse response = executeHttpRequest(request)) {
        Header location = response.getHeaders("Location")[0];
        return location.getValue();
    } catch (IOException ioe) {
        throw new HttpRequestException(request, ioe);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) Header(org.apache.http.Header) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpRequestException(org.neo4j.ogm.drivers.http.request.HttpRequestException) IOException(java.io.IOException)

Example 2 with HttpRequestException

use of org.neo4j.ogm.drivers.http.request.HttpRequestException in project neo4j-ogm by neo4j.

the class HttpDriver method executeHttpRequest.

public CloseableHttpResponse executeHttpRequest(HttpRequestBase request) throws HttpRequestException {
    try (CloseableHttpResponse response = HttpRequest.execute(httpClient(), request, configuration.getCredentials())) {
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            JsonNode responseNode = mapper.readTree(EntityUtils.toString(responseEntity));
            LOGGER.debug("Response: {}", responseNode);
            JsonNode errors = responseNode.findValue("errors");
            if (errors.elements().hasNext()) {
                JsonNode errorNode = errors.elements().next();
                throw new CypherException(errorNode.findValue("code").asText(), errorNode.findValue("message").asText());
            }
        }
        return response;
    } catch (IOException ioe) {
        throw new HttpRequestException(request, ioe);
    } finally {
        request.releaseConnection();
        LOGGER.debug("Thread: {}, Connection released", Thread.currentThread().getId());
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpRequestException(org.neo4j.ogm.drivers.http.request.HttpRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) CypherException(org.neo4j.ogm.exception.CypherException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpRequestException (org.neo4j.ogm.drivers.http.request.HttpRequestException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 HttpPost (org.apache.http.client.methods.HttpPost)1 CypherException (org.neo4j.ogm.exception.CypherException)1