org.restheart.graphql.predicates.FieldEqPredicate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of restheart-graphql Show documentation
Show all versions of restheart-graphql Show documentation
RESTHeart MongoDB - GraphQL plugin
/*-
* ========================LICENSE_START=================================
* restheart-security
* %%
* Copyright (C) 2018 - 2024 SoftInstigate
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
* =========================LICENSE_END==================================
*/
package org.restheart.graphql.predicates;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.commons.jxpath.JXPathContext;
import org.bson.BsonValue;
import org.restheart.utils.BsonUtils;
import io.undertow.predicate.Predicate;
import io.undertow.predicate.PredicateBuilder;
/**
* a predicate that resolve to true if the request contains the specified keys
*/
public class FieldEqPredicate implements PredicateOverJxPathCtx {
private final String key;
private final BsonValue value;
public FieldEqPredicate(String key, String value) {
if (key == null || value == null) {
throw new IllegalArgumentException("field-eq predicate must specify the field and the value");
}
this.key = key;
this.value = BsonUtils.parse(value);
}
@Override
public boolean resolve(JXPathContext ctx) {
var _v = BsonUtils.get(ctx, key);
if (_v.isPresent()) {
return this.value.equals(_v.get());
} else {
return false;
}
}
public static class Builder implements PredicateBuilder {
@Override
public String name() {
return "field-eq";
}
@Override
public Map> parameters() {
var params = new HashMap>();
params.put("field", String.class);
params.put("value", String.class);
return params;
}
@Override
public Set requiredParameters() {
var params = new HashSet();
params.add("field");
params.add("value");
return params;
}
@Override
public String defaultParameter() {
return "field";
}
@Override
public Predicate build(Map config) {
return new FieldEqPredicate((String) config.get("field"), (String) config.get("value"));
}
}
}