Search in sources :

Example 1 with RepresentationWriteHandler

use of org.neo4j.server.rest.repr.RepresentationWriteHandler in project neo4j by neo4j.

the class TransactionalRequestDispatcher method dispatch.

@Override
public void dispatch(Object o, final HttpContext httpContext) {
    RepresentationWriteHandler representationWriteHandler = DO_NOTHING;
    SecurityContext securityContext = AuthorizedRequestWrapper.getSecurityContextFromHttpContext(httpContext);
    final GraphDatabaseFacade graph = database.getGraph();
    if (o instanceof RestfulGraphDatabase) {
        RestfulGraphDatabase restfulGraphDatabase = (RestfulGraphDatabase) o;
        final Transaction transaction = graph.beginTransaction(KernelTransaction.Type.implicit, securityContext);
        restfulGraphDatabase.getOutputFormat().setRepresentationWriteHandler(representationWriteHandler = new CommitOnSuccessfulStatusCodeRepresentationWriteHandler(httpContext, transaction));
    } else if (o instanceof BatchOperationService) {
        BatchOperationService batchOperationService = (BatchOperationService) o;
        final Transaction transaction = graph.beginTransaction(KernelTransaction.Type.explicit, securityContext);
        batchOperationService.setRepresentationWriteHandler(representationWriteHandler = new CommitOnSuccessfulStatusCodeRepresentationWriteHandler(httpContext, transaction));
    } else if (o instanceof CypherService) {
        CypherService cypherService = (CypherService) o;
        final Transaction transaction = graph.beginTransaction(KernelTransaction.Type.explicit, securityContext);
        cypherService.getOutputFormat().setRepresentationWriteHandler(representationWriteHandler = new CommitOnSuccessfulStatusCodeRepresentationWriteHandler(httpContext, transaction));
    } else if (o instanceof DatabaseMetadataService) {
        DatabaseMetadataService databaseMetadataService = (DatabaseMetadataService) o;
        final Transaction transaction = graph.beginTransaction(KernelTransaction.Type.implicit, securityContext);
        databaseMetadataService.setRepresentationWriteHandler(representationWriteHandler = new RepresentationWriteHandler() {

            @Override
            public void onRepresentationStartWriting() {
            // do nothing
            }

            @Override
            public void onRepresentationWritten() {
            // doesn't need to commit
            }

            @Override
            public void onRepresentationFinal() {
                transaction.close();
            }
        });
    } else if (o instanceof ExtensionService) {
        ExtensionService extensionService = (ExtensionService) o;
        extensionService.getOutputFormat().setRepresentationWriteHandler(representationWriteHandler = new RepresentationWriteHandler() {

            Transaction transaction;

            @Override
            public void onRepresentationStartWriting() {
                transaction = graph.beginTransaction(KernelTransaction.Type.implicit, securityContext);
            }

            @Override
            public void onRepresentationWritten() {
            // doesn't need to commit
            }

            @Override
            public void onRepresentationFinal() {
                if (transaction != null) {
                    transaction.close();
                }
            }
        });
    }
    try {
        requestDispatcher.dispatch(o, httpContext);
    } catch (RuntimeException e) {
        representationWriteHandler.onRepresentationFinal();
        throw e;
    }
}
Also used : ExtensionService(org.neo4j.server.rest.web.ExtensionService) CypherService(org.neo4j.server.rest.web.CypherService) RestfulGraphDatabase(org.neo4j.server.rest.web.RestfulGraphDatabase) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) Transaction(org.neo4j.graphdb.Transaction) DatabaseMetadataService(org.neo4j.server.rest.web.DatabaseMetadataService) SecurityContext(org.neo4j.kernel.api.security.SecurityContext) RepresentationWriteHandler(org.neo4j.server.rest.repr.RepresentationWriteHandler) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) BatchOperationService(org.neo4j.server.rest.web.BatchOperationService)

Aggregations

Transaction (org.neo4j.graphdb.Transaction)1 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)1 SecurityContext (org.neo4j.kernel.api.security.SecurityContext)1 GraphDatabaseFacade (org.neo4j.kernel.impl.factory.GraphDatabaseFacade)1 RepresentationWriteHandler (org.neo4j.server.rest.repr.RepresentationWriteHandler)1 BatchOperationService (org.neo4j.server.rest.web.BatchOperationService)1 CypherService (org.neo4j.server.rest.web.CypherService)1 DatabaseMetadataService (org.neo4j.server.rest.web.DatabaseMetadataService)1 ExtensionService (org.neo4j.server.rest.web.ExtensionService)1 RestfulGraphDatabase (org.neo4j.server.rest.web.RestfulGraphDatabase)1