Search in sources :

Example 11 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class StatementDeserializer method read.

InputStatement read() {
    switch(state) {
        case BEFORE_OUTER_ARRAY:
            if (!beginsWithCorrectTokens()) {
                return null;
            }
            state = State.IN_BODY;
        case IN_BODY:
            String statement = null;
            Map<String, Object> parameters = null;
            List<Object> resultsDataContents = null;
            boolean includeStats = false;
            JsonToken tok;
            try {
                while ((tok = parser.nextToken()) != null && tok != END_OBJECT) {
                    if (tok == END_ARRAY) {
                        // No more statements
                        state = State.FINISHED;
                        return null;
                    }
                    parser.nextValue();
                    String currentName = parser.getCurrentName();
                    switch(currentName) {
                        case "statement":
                            statement = parser.readValueAs(String.class);
                            break;
                        case "parameters":
                            parameters = readMap();
                            break;
                        case "resultDataContents":
                            resultsDataContents = readArray();
                            break;
                        case "includeStats":
                            includeStats = parser.getBooleanValue();
                            break;
                        default:
                            discardValue();
                    }
                }
                if (statement == null) {
                    throw new InputFormatException("No statement provided.");
                }
                return new InputStatement(statement, parameters == null ? NO_PARAMETERS : parameters, includeStats, ResultDataContent.fromNames(resultsDataContents));
            } catch (JsonParseException e) {
                throw new InputFormatException("Could not parse the incoming JSON", e);
            } catch (JsonMappingException e) {
                throw new InputFormatException("Could not map the incoming JSON", e);
            } catch (IOException e) {
                throw new ConnectionException("An error encountered while reading the inbound entity", e);
            }
        case FINISHED:
            return null;
        default:
            break;
    }
    return null;
}
Also used : JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 12 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class StatementDeserializer method beginsWithCorrectTokens.

private boolean beginsWithCorrectTokens() {
    List<JsonToken> expectedTokens = asList(START_OBJECT, FIELD_NAME, START_ARRAY);
    String expectedField = "statements";
    List<JsonToken> foundTokens = new ArrayList<>();
    try {
        for (int i = 0; i < expectedTokens.size(); i++) {
            JsonToken token = parser.nextToken();
            if (i == 0 && token == null) {
                return false;
            }
            if (token == FIELD_NAME && !expectedField.equals(parser.getText())) {
                throw new InputFormatException(String.format("Unable to deserialize request. " + "Expected first field to be '%s', but was '%s'.", expectedField, parser.getText()));
            }
            foundTokens.add(token);
        }
        if (!expectedTokens.equals(foundTokens)) {
            throw new InputFormatException(String.format("Unable to deserialize request. " + "Expected %s, found %s.", expectedTokens, foundTokens));
        }
    } catch (JsonParseException e) {
        throw new InputFormatException("Could not parse the incoming JSON", e);
    } catch (IOException e) {
        throw new ConnectionException("An error encountered while reading the inbound entity", e);
    }
    return true;
}
Also used : ArrayList(java.util.ArrayList) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) InputFormatException(org.neo4j.server.http.cypher.format.api.InputFormatException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 13 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeTransactionInfo.

protected void writeTransactionInfo(TransactionInfoEvent transactionInfoEvent) {
    try {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("info");
        jsonGenerator.writeStartObject();
        writeNotifications();
        if (transactionInfoEvent.getCommitUri() != null) {
            jsonGenerator.writeStringField("commit", transactionInfoEvent.getCommitUri().toString());
        }
        if (transactionInfoEvent.getNotification() == OPEN) {
            jsonGenerator.writeObjectFieldStart("transaction");
            if (transactionInfoEvent.getExpirationTimestamp() >= 0) {
                String expires = Instant.ofEpochMilli(transactionInfoEvent.getExpirationTimestamp()).atZone(ZoneId.of("GMT")).format(DateTimeFormatter.RFC_1123_DATE_TIME);
                jsonGenerator.writeStringField("expires", expires);
            }
            jsonGenerator.writeEndObject();
        }
        jsonGenerator.writeEndObject();
        jsonGenerator.writeEndObject();
        flush();
    } catch (JsonGenerationException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the connection", e);
    }
}
Also used : IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 14 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeRecord.

protected void writeRecord(RecordEvent recordEvent) {
    try {
        TransactionStateChecker txStateChecker = TransactionStateChecker.create(transactionHandle.getContext());
        jsonGenerator.writeStartObject();
        jsonGenerator.writeFieldName("data");
        try {
            writer.write(jsonGenerator, recordEvent, txStateChecker);
        } finally {
            jsonGenerator.writeEndObject();
            flush();
        }
    } catch (JsonGenerationException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the connection", e);
    }
}
Also used : TransactionStateChecker(org.neo4j.server.http.cypher.TransactionStateChecker) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Example 15 with ConnectionException

use of org.neo4j.server.http.cypher.format.api.ConnectionException in project neo4j by neo4j.

the class LineDelimitedEventSourceJoltSerializer method writeErrors.

private void writeErrors() {
    try {
        jsonGenerator.writeArrayFieldStart("errors");
        try {
            for (FailureEvent error : errors) {
                try {
                    jsonGenerator.writeStartObject();
                    jsonGenerator.writeObjectField("code", error.getStatus().code().serialize());
                    jsonGenerator.writeObjectField("message", error.getMessage());
                } finally {
                    jsonGenerator.writeEndObject();
                }
            }
        } finally {
            jsonGenerator.writeEndArray();
        }
    } catch (IOException e) {
        throw new ConnectionException("Failed to write to the response stream", e);
    }
}
Also used : FailureEvent(org.neo4j.server.http.cypher.format.api.FailureEvent) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException)

Aggregations

IOException (java.io.IOException)21 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)21 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)10 Test (org.junit.jupiter.api.Test)5 MemoryPool (org.neo4j.memory.MemoryPool)4 InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)4 Statement (org.neo4j.server.http.cypher.format.api.Statement)3 JsonParseException (com.fasterxml.jackson.core.JsonParseException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 InOrder (org.mockito.InOrder)2 Notification (org.neo4j.graphdb.Notification)2 TransactionStateChecker (org.neo4j.server.http.cypher.TransactionStateChecker)2 FailureEvent (org.neo4j.server.http.cypher.format.api.FailureEvent)2 InputFormatException (org.neo4j.server.http.cypher.format.api.InputFormatException)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ArrayList (java.util.ArrayList)1 Log (org.neo4j.logging.Log)1