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;
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations