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