protected Optional<ThrowingConsumer<Context>> validateEvalMessage(final RequestMessage message) throws OpProcessorException { if (!message.optionalArgs(Tokens.ARGS_GREMLIN).isPresent()) { final String msg = String.format("A message with an [%s] op code requires a [%s] argument.", Tokens.OPS_EVAL, Tokens.ARGS_GREMLIN); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } if (message.optionalArgs(Tokens.ARGS_BINDINGS).isPresent()) { final Map bindings = (Map) message.getArgs().get(Tokens.ARGS_BINDINGS); if (IteratorUtils.anyMatch(bindings.keySet().iterator(), k -> null == k || !(k instanceof String))) { final String msg = String.format("The [%s] message is using one or more invalid binding keys - they must be of type String and cannot be null", Tokens.OPS_EVAL); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } final Set<String> badBindings = IteratorUtils.set(IteratorUtils.<String>filter(bindings.keySet().iterator(), INVALID_BINDINGS_KEYS::contains)); if (!badBindings.isEmpty()) { final String msg = String.format("The [%s] message supplies one or more invalid parameters key of [%s] - these are reserved names.", Tokens.OPS_EVAL, badBindings); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } // ignore control bindings that get passed in with the "#jsr223" prefix - those aren't used in compilation if (IteratorUtils.count(IteratorUtils.filter(bindings.keySet().iterator(), k -> !k.toString().startsWith("#jsr223"))) > maxParameters) { final String msg = String.format("The [%s] message contains %s bindings which is more than is allowed by the server %s configuration", Tokens.OPS_EVAL, bindings.size(), maxParameters); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } } return Optional.empty(); }
public HasContainer(final String key, final P<?> predicate) { this.key = key; this.predicate = predicate; if (!this.key.equals(T.id.getAccessor())) testingIdString = false; else { // the values should be homogenous if a collection is submitted final Object predicateValue = this.predicate.getValue(); // enforce a homogenous collection of values when testing ids enforceHomogenousCollectionIfPresent(predicateValue); // grab an instance of a value which is either the first item in a homogeneous collection or the value itself final Object valueInstance = this.predicate.getValue() instanceof Collection ? ((Collection) this.predicate.getValue()).isEmpty() ? new Object() : ((Collection) this.predicate.getValue()).toArray()[0] : this.predicate.getValue(); // if the key being evaluated is id then the has() test can evaluate as a toString() representation of the // identifier. this could be done in the test() method but it seems cheaper to do the conversion once in // the constructor. the original value in P is maintained separately this.testingIdString = this.key.equals(T.id.getAccessor()) && valueInstance instanceof String; if (this.testingIdString) this.predicate.setValue(this.predicate.getValue() instanceof Collection ? IteratorUtils.set(IteratorUtils.map(((Collection<Object>) this.predicate.getValue()).iterator(), Object::toString)) : this.predicate.getValue().toString()); } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_DUPLICATE_MULTI_PROPERTIES) public void shouldAllowIdenticalValuedMultiProperties() { final Vertex v = graph.addVertex(); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "name", "steve"); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "color", "red"); tryCommit(graph, g -> { final Vertex vertex = graph.vertices(v).next(); assertEquals(4, IteratorUtils.count(vertex.properties("name"))); assertEquals(1, IteratorUtils.count(vertex.properties("color"))); assertEquals(5, IteratorUtils.count(vertex.properties())); assertThat(IteratorUtils.set(vertex.values("name")), contains("stephen", "steve")); }); } }
protected Optional<ThrowingConsumer<Context>> validateEvalMessage(final RequestMessage message) throws OpProcessorException { if (!message.optionalArgs(Tokens.ARGS_GREMLIN).isPresent()) { final String msg = String.format("A message with an [%s] op code requires a [%s] argument.", Tokens.OPS_EVAL, Tokens.ARGS_GREMLIN); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } if (message.optionalArgs(Tokens.ARGS_BINDINGS).isPresent()) { final Map bindings = (Map) message.getArgs().get(Tokens.ARGS_BINDINGS); if (IteratorUtils.anyMatch(bindings.keySet().iterator(), k -> null == k || !(k instanceof String))) { final String msg = String.format("The [%s] message is using one or more invalid binding keys - they must be of type String and cannot be null", Tokens.OPS_EVAL); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } final Set<String> badBindings = IteratorUtils.set(IteratorUtils.<String>filter(bindings.keySet().iterator(), INVALID_BINDINGS_KEYS::contains)); if (!badBindings.isEmpty()) { final String msg = String.format("The [%s] message supplies one or more invalid parameters key of [%s] - these are reserved names.", Tokens.OPS_EVAL, badBindings); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } // ignore control bindings that get passed in with the "#jsr223" prefix - those aren't used in compilation if (IteratorUtils.count(IteratorUtils.filter(bindings.keySet().iterator(), k -> !k.toString().startsWith("#jsr223"))) > maxParameters) { final String msg = String.format("The [%s] message contains %s bindings which is more than is allowed by the server %s configuration", Tokens.OPS_EVAL, bindings.size(), maxParameters); throw new OpProcessorException(msg, ResponseMessage.build(message).code(ResponseStatusCode.REQUEST_ERROR_INVALID_REQUEST_ARGUMENTS).statusMessage(msg).create()); } } return Optional.empty(); }
public HasContainer(final String key, final P<?> predicate) { this.key = key; this.predicate = predicate; if (!this.key.equals(T.id.getAccessor())) testingIdString = false; else { // the values should be homogenous if a collection is submitted final Object predicateValue = this.predicate.getValue(); // enforce a homogenous collection of values when testing ids enforceHomogenousCollectionIfPresent(predicateValue); // grab an instance of a value which is either the first item in a homogeneous collection or the value itself final Object valueInstance = this.predicate.getValue() instanceof Collection ? ((Collection) this.predicate.getValue()).isEmpty() ? new Object() : ((Collection) this.predicate.getValue()).toArray()[0] : this.predicate.getValue(); // if the key being evaluated is id then the has() test can evaluate as a toString() representation of the // identifier. this could be done in the test() method but it seems cheaper to do the conversion once in // the constructor. the original value in P is maintained separately this.testingIdString = this.key.equals(T.id.getAccessor()) && valueInstance instanceof String; if (this.testingIdString) this.predicate.setValue(this.predicate.getValue() instanceof Collection ? IteratorUtils.set(IteratorUtils.map(((Collection<Object>) this.predicate.getValue()).iterator(), Object::toString)) : this.predicate.getValue().toString()); } }
@Test @FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_DUPLICATE_MULTI_PROPERTIES) public void shouldAllowIdenticalValuedMultiProperties() { final Vertex v = graph.addVertex(); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "name", "steve"); v.property(VertexProperty.Cardinality.list, "name", "stephen"); v.property(VertexProperty.Cardinality.list, "color", "red"); tryCommit(graph, g -> { final Vertex vertex = graph.vertices(v).next(); assertEquals(4, IteratorUtils.count(vertex.properties("name"))); assertEquals(1, IteratorUtils.count(vertex.properties("color"))); assertEquals(5, IteratorUtils.count(vertex.properties())); assertThat(IteratorUtils.set(vertex.values("name")), contains("stephen", "steve")); }); } }