Search in sources :

Example 1 with InvalidArgumentsException

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

the class CypherService method cypher.

@POST
@SuppressWarnings({ "unchecked", "ParameterCanBeLocal" })
public Response cypher(String body, @Context HttpServletRequest request, @QueryParam(INCLUDE_STATS_PARAM) boolean includeStats, @QueryParam(INCLUDE_PLAN_PARAM) boolean includePlan, @QueryParam(PROFILE_PARAM) boolean profile) throws BadInputException {
    usage.get(features).flag(http_cypher_endpoint);
    Map<String, Object> command = input.readMap(body);
    if (!command.containsKey(QUERY_KEY)) {
        return output.badRequest(new InvalidArgumentsException("You have to provide the 'query' parameter."));
    }
    String query = (String) command.get(QUERY_KEY);
    Map<String, Object> params;
    try {
        params = (Map<String, Object>) (command.containsKey(PARAMS_KEY) && command.get(PARAMS_KEY) != null ? command.get(PARAMS_KEY) : new HashMap<String, Object>());
    } catch (ClassCastException e) {
        return output.badRequest(new IllegalArgumentException("Parameters must be a JSON map"));
    }
    try {
        QueryExecutionEngine executionEngine = cypherExecutor.getExecutionEngine();
        boolean periodicCommitQuery = executionEngine.isPeriodicCommit(query);
        CommitOnSuccessfulStatusCodeRepresentationWriteHandler handler = (CommitOnSuccessfulStatusCodeRepresentationWriteHandler) output.getRepresentationWriteHandler();
        if (periodicCommitQuery) {
            handler.closeTransaction();
        }
        TransactionalContext tc = cypherExecutor.createTransactionContext(query, params, request);
        Result result;
        if (profile) {
            result = executionEngine.profileQuery(query, params, tc);
            includePlan = true;
        } else {
            result = executionEngine.executeQuery(query, params, tc);
            includePlan = result.getQueryExecutionType().requestedExecutionPlanDescription();
        }
        if (periodicCommitQuery) {
            handler.setTransaction(database.beginTx());
        }
        return output.ok(new CypherResultRepresentation(result, includeStats, includePlan));
    } catch (Throwable e) {
        if (e.getCause() instanceof CypherException) {
            return output.badRequest(e.getCause());
        } else {
            return output.badRequest(e);
        }
    }
}
Also used : CypherResultRepresentation(org.neo4j.server.rest.repr.CypherResultRepresentation) HashMap(java.util.HashMap) Result(org.neo4j.graphdb.Result) QueryExecutionEngine(org.neo4j.kernel.impl.query.QueryExecutionEngine) CommitOnSuccessfulStatusCodeRepresentationWriteHandler(org.neo4j.server.rest.transactional.CommitOnSuccessfulStatusCodeRepresentationWriteHandler) TransactionalContext(org.neo4j.kernel.impl.query.TransactionalContext) CypherException(org.neo4j.cypher.CypherException) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) POST(javax.ws.rs.POST)

Example 2 with InvalidArgumentsException

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

the class RestfulGraphDatabase method addNodeLabel.

// Node Labels
@POST
@Path(PATH_NODE_LABELS)
public Response addNodeLabel(@PathParam("nodeId") long nodeId, String body) {
    try {
        Object rawInput = input.readValue(body);
        if (rawInput instanceof String) {
            ArrayList<String> s = new ArrayList<>();
            s.add((String) rawInput);
            actions.addLabelToNode(nodeId, s);
        } else if (rawInput instanceof Collection) {
            actions.addLabelToNode(nodeId, (Collection<String>) rawInput);
        } else {
            throw new InvalidArgumentsException(format("Label name must be a string. Got: '%s'", rawInput));
        }
    } catch (BadInputException e) {
        return output.badRequest(e);
    } catch (ArrayStoreException ase) {
        return generateBadRequestDueToMangledJsonResponse(body);
    } catch (NodeNotFoundException e) {
        return output.notFound(e);
    }
    return nothing();
}
Also used : BadInputException(org.neo4j.server.rest.repr.BadInputException) StartNodeNotFoundException(org.neo4j.server.rest.domain.StartNodeNotFoundException) EndNodeNotFoundException(org.neo4j.server.rest.domain.EndNodeNotFoundException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 3 with InvalidArgumentsException

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

the class DatabaseActions method getOrCreateIndexedNode.

public Pair<IndexedEntityRepresentation, Boolean> getOrCreateIndexedNode(String indexName, String key, String value, Long nodeOrNull, Map<String, Object> properties) throws BadInputException, NodeNotFoundException {
    assertIsLegalIndexName(indexName);
    Node result;
    boolean created;
    if (nodeOrNull != null) {
        if (properties != null) {
            throw new InvalidArgumentsException("Cannot specify properties for a new node, " + "when a node to index is specified.");
        }
        Node node = node(nodeOrNull);
        result = graphDb.index().forNodes(indexName).putIfAbsent(node, key, value);
        created = result == null;
        if (created) {
            UniqueNodeFactory factory = new UniqueNodeFactory(indexName, properties);
            UniqueEntity<Node> entity = factory.getOrCreateWithOutcome(key, value);
            // when given a node id, return as created if that node was newly added to the index
            created = entity.entity().getId() == node.getId() || entity.wasCreated();
            result = entity.entity();
        }
    } else {
        if (properties != null) {
            for (Map.Entry<String, Object> entry : properties.entrySet()) {
                entry.setValue(propertySetter.convert(entry.getValue()));
            }
        }
        UniqueNodeFactory factory = new UniqueNodeFactory(indexName, properties);
        UniqueEntity<Node> entity = factory.getOrCreateWithOutcome(key, value);
        result = entity.entity();
        created = entity.wasCreated();
    }
    return Pair.of(new IndexedEntityRepresentation(result, key, value, new NodeIndexRepresentation(indexName, Collections.<String, String>emptyMap())), created);
}
Also used : NodeIndexRepresentation(org.neo4j.server.rest.repr.NodeIndexRepresentation) Node(org.neo4j.graphdb.Node) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException) Map(java.util.Map)

Example 4 with InvalidArgumentsException

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

the class DatabaseActions method getOrCreateIndexedRelationship.

public Pair<IndexedEntityRepresentation, Boolean> getOrCreateIndexedRelationship(String indexName, String key, String value, Long relationshipOrNull, Long startNode, String type, Long endNode, Map<String, Object> properties) throws BadInputException, RelationshipNotFoundException, NodeNotFoundException {
    assertIsLegalIndexName(indexName);
    Relationship result;
    boolean created;
    if (relationshipOrNull != null) {
        if (startNode != null || type != null || endNode != null || properties != null) {
            throw new InvalidArgumentsException("Either specify a relationship to index uniquely, " + "or the means for creating it.");
        }
        Relationship relationship = relationship(relationshipOrNull);
        result = graphDb.index().forRelationships(indexName).putIfAbsent(relationship, key, value);
        if (created = result == null) {
            UniqueRelationshipFactory factory = new UniqueRelationshipFactory(indexName, relationship.getStartNode(), relationship.getEndNode(), relationship.getType().name(), properties);
            UniqueEntity<Relationship> entity = factory.getOrCreateWithOutcome(key, value);
            // when given a relationship id, return as created if that relationship was newly added to the index
            created = entity.entity().getId() == relationship.getId() || entity.wasCreated();
            result = entity.entity();
        }
    } else if (startNode == null || type == null || endNode == null) {
        throw new InvalidArgumentsException("Either specify a relationship to index uniquely, " + "or the means for creating it.");
    } else {
        UniqueRelationshipFactory factory = new UniqueRelationshipFactory(indexName, node(startNode), node(endNode), type, properties);
        UniqueEntity<Relationship> entity = factory.getOrCreateWithOutcome(key, value);
        result = entity.entity();
        created = entity.wasCreated();
    }
    return Pair.of(new IndexedEntityRepresentation(result, key, value, new RelationshipIndexRepresentation(indexName, Collections.<String, String>emptyMap())), created);
}
Also used : RelationshipIndexRepresentation(org.neo4j.server.rest.repr.RelationshipIndexRepresentation) UniqueEntity(org.neo4j.graphdb.index.UniqueFactory.UniqueEntity) Relationship(org.neo4j.graphdb.Relationship) IndexedEntityRepresentation(org.neo4j.server.rest.repr.IndexedEntityRepresentation) InvalidArgumentsException(org.neo4j.server.rest.repr.InvalidArgumentsException)

Aggregations

InvalidArgumentsException (org.neo4j.server.rest.repr.InvalidArgumentsException)4 POST (javax.ws.rs.POST)2 IndexedEntityRepresentation (org.neo4j.server.rest.repr.IndexedEntityRepresentation)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Path (javax.ws.rs.Path)1 CypherException (org.neo4j.cypher.CypherException)1 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1 Result (org.neo4j.graphdb.Result)1 UniqueEntity (org.neo4j.graphdb.index.UniqueFactory.UniqueEntity)1 QueryExecutionEngine (org.neo4j.kernel.impl.query.QueryExecutionEngine)1 TransactionalContext (org.neo4j.kernel.impl.query.TransactionalContext)1 EndNodeNotFoundException (org.neo4j.server.rest.domain.EndNodeNotFoundException)1 StartNodeNotFoundException (org.neo4j.server.rest.domain.StartNodeNotFoundException)1 BadInputException (org.neo4j.server.rest.repr.BadInputException)1 CypherResultRepresentation (org.neo4j.server.rest.repr.CypherResultRepresentation)1 NodeIndexRepresentation (org.neo4j.server.rest.repr.NodeIndexRepresentation)1