Search in sources :

Example 11 with InputEventStream

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

the class JsonMessageBodyReader method readFrom.

@Override
public InputEventStream readFrom(Class<InputEventStream> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws WebApplicationException {
    Map<Statement, InputStatement> inputStatements = new HashMap<>();
    Map<String, Object> parameters = new HashMap<>();
    parameters.put(STATEMENTS_KEY, inputStatements);
    StatementDeserializer statementDeserializer = new StatementDeserializer(this.jsonFactory, entityStream);
    return new InputEventStream(parameters, () -> {
        InputStatement inputStatement = statementDeserializer.read();
        if (inputStatement == null) {
            return null;
        }
        Statement statement = new Statement(inputStatement.statement(), inputStatement.parameters());
        inputStatements.put(statement, inputStatement);
        return statement;
    });
}
Also used : HashMap(java.util.HashMap) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement)

Example 12 with InputEventStream

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

the class AbstractCypherResource method executeStatementsInNewTransaction.

@POST
public Response executeStatementsInNewTransaction(InputEventStream inputEventStream, @Context HttpServletRequest request, @Context HttpHeaders headers) {
    try (var memoryTracker = createMemoryTracker()) {
        InputEventStream inputStream = ensureNotNull(inputEventStream);
        var graphDatabaseAPI = httpTransactionManager.getGraphDatabaseAPI(databaseName);
        return graphDatabaseAPI.map(databaseAPI -> {
            if (isDatabaseNotAvailable(databaseAPI)) {
                return createNonAvailableDatabaseResponse(inputStream.getParameters());
            }
            memoryTracker.allocateHeap(Invocation.SHALLOW_SIZE);
            final TransactionFacade transactionFacade = httpTransactionManager.createTransactionFacade(databaseAPI, memoryTracker);
            TransactionHandle transactionHandle = createNewTransactionHandle(transactionFacade, request, headers, memoryTracker, false);
            Invocation invocation = new Invocation(log, transactionHandle, uriScheme.txCommitUri(transactionHandle.getId()), memoryPool, inputStream, false);
            OutputEventStreamImpl outputStream = new OutputEventStreamImpl(inputStream.getParameters(), transactionHandle, uriScheme, invocation::execute);
            return Response.created(transactionHandle.uri()).entity(outputStream).build();
        }).orElse(createNonExistentDatabaseResponse(inputStream.getParameters()));
    }
}
Also used : PathParam(javax.ws.rs.PathParam) Neo4jError(org.neo4j.server.rest.Neo4jError) Log(org.neo4j.logging.Log) TransactionUriScheme(org.neo4j.server.http.cypher.format.api.TransactionUriScheme) Status(org.neo4j.kernel.api.exceptions.Status) Path(javax.ws.rs.Path) LoginContext(org.neo4j.internal.kernel.api.security.LoginContext) LocalMemoryTracker(org.neo4j.memory.LocalMemoryTracker) Objects.requireNonNullElse(java.util.Objects.requireNonNullElse) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) MemoryTracker(org.neo4j.memory.MemoryTracker) DELETE(javax.ws.rs.DELETE) HttpHeaderUtils.getTransactionTimeout(org.neo4j.server.web.HttpHeaderUtils.getTransactionTimeout) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Context(javax.ws.rs.core.Context) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) MemoryPool(org.neo4j.memory.MemoryPool) GraphDatabaseAPI(org.neo4j.kernel.internal.GraphDatabaseAPI) AuthorizedRequestWrapper(org.neo4j.server.rest.dbms.AuthorizedRequestWrapper) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) Optional(java.util.Optional) UriInfo(javax.ws.rs.core.UriInfo) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) POST(javax.ws.rs.POST)

Example 13 with InputEventStream

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

the class InvocationTest method shouldResumeTransactionWhenExecutingStatementsOnSecondRequest.

@Test
void shouldResumeTransactionWhenExecutingStatementsOnSecondRequest() {
    // given
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, false);
    invocation.execute(outputEventStream);
    reset(transactionContext, registry, internalTransaction, outputEventStream);
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    when(internalTransaction.execute("query", emptyMap())).thenReturn(executionResult);
    // when
    invocation.execute(outputEventStream);
    // then
    InOrder order = inOrder(transactionContext, registry, internalTransaction);
    order.verify(internalTransaction).execute("query", emptyMap());
    order.verify(registry).release(1337L, handle);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeStatementStart(statement, List.of("c1", "c2", "c3"));
    verifyDefaultResultRows(outputOrder);
    outputOrder.verify(outputEventStream).writeStatementEnd(queryExecutionType, queryStatistics, executionPlanDescription, notifications);
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.OPEN, uriScheme.txCommitUri(1337L), 0);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 14 with InputEventStream

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

the class InvocationTest method shouldHandleAuthorizationErrorWhenStartingTransaction.

@Test
void shouldHandleAuthorizationErrorWhenStartingTransaction() {
    // given
    when(databaseFacade.beginTransaction(any(), any(), any())).thenThrow(new AuthorizationViolationException("Forbidden"));
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    Statement statement = new Statement("query", map());
    when(inputEventStream.read()).thenReturn(statement, NULL_STATEMENT);
    mockDefaultResult();
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    invocation.execute(outputEventStream);
    // then
    verifyNoMoreInteractions(log);
    InOrder outputOrder = inOrder(outputEventStream);
    outputOrder.verify(outputEventStream).writeFailure(Status.Security.Forbidden, "Forbidden");
    outputOrder.verify(outputEventStream).writeTransactionInfo(TransactionNotificationState.NO_TRANSACTION, uriScheme.txCommitUri(1337L), -1);
    verifyNoMoreInteractions(outputEventStream);
}
Also used : InOrder(org.mockito.InOrder) InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) Statement(org.neo4j.server.http.cypher.format.api.Statement) AuthorizationViolationException(org.neo4j.graphdb.security.AuthorizationViolationException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Example 15 with InputEventStream

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

the class InvocationTest method shouldHandleConnectionErrorWhenReadingStatementsInImplicitTransaction.

@Test
void shouldHandleConnectionErrorWhenReadingStatementsInImplicitTransaction() {
    // given
    when(registry.begin(any(TransactionHandle.class))).thenReturn(1337L);
    TransactionHandle handle = getTransactionHandle(executionEngine, registry);
    InputEventStream inputEventStream = mock(InputEventStream.class);
    when(inputEventStream.read()).thenThrow(new ConnectionException("Connection error", new IOException("Broken pipe")));
    Invocation invocation = new Invocation(log, handle, uriScheme.txCommitUri(1337L), mock(MemoryPool.class, RETURNS_MOCKS), inputEventStream, true);
    // when
    var e = assertThrows(ConnectionException.class, () -> invocation.execute(outputEventStream));
    assertEquals("Connection error", e.getMessage());
    // then
    verify(internalTransaction).rollback();
    verify(registry).forget(1337L);
    verifyNoInteractions(outputEventStream);
}
Also used : InputEventStream(org.neo4j.server.http.cypher.format.api.InputEventStream) IOException(java.io.IOException) ConnectionException(org.neo4j.server.http.cypher.format.api.ConnectionException) MemoryPool(org.neo4j.memory.MemoryPool) Test(org.junit.jupiter.api.Test)

Aggregations

InputEventStream (org.neo4j.server.http.cypher.format.api.InputEventStream)25 MemoryPool (org.neo4j.memory.MemoryPool)24 Test (org.junit.jupiter.api.Test)21 Statement (org.neo4j.server.http.cypher.format.api.Statement)18 InOrder (org.mockito.InOrder)17 IOException (java.io.IOException)5 LoginContext (org.neo4j.internal.kernel.api.security.LoginContext)5 ConnectionException (org.neo4j.server.http.cypher.format.api.ConnectionException)4 URI (java.net.URI)3 Collections.emptyMap (java.util.Collections.emptyMap)3 Map (java.util.Map)3 Objects.requireNonNullElse (java.util.Objects.requireNonNullElse)3 Optional (java.util.Optional)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 DELETE (javax.ws.rs.DELETE)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 PathParam (javax.ws.rs.PathParam)3 Context (javax.ws.rs.core.Context)3 HttpHeaders (javax.ws.rs.core.HttpHeaders)3