All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.restheart.graphql.predicates.FieldExists Maven / Gradle / Ivy

There is a newer version: 8.1.5
Show newest version
/*-
 * ========================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.Collections;
import java.util.Map;
import java.util.Set;

import org.apache.commons.jxpath.JXPathContext;
import org.restheart.utils.BsonUtils;

import com.google.common.collect.Sets;

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 FieldExists implements PredicateOverJxPathCtx {
    private final Set fields;

    public FieldExists(String[] fields) {
        if (fields == null || fields.length < 1) {
            throw new IllegalArgumentException("field-exists predicate must specify a list of fields");
        }

        this.fields = Sets.newHashSet(fields);
    }

    @Override
    public boolean resolve(JXPathContext ctx) {
            return this.fields.stream().allMatch(f -> BsonUtils.get(ctx, f).isPresent());
    }

    public static class Builder implements PredicateBuilder {
        @Override
        public String name() {
            return "field-exists";
        }

        @Override
        public Map> parameters() {
            return Collections.>singletonMap("fields", String[].class);
        }

        @Override
        public Set requiredParameters() {
            return Collections.singleton("fields");
        }

        @Override
        public String defaultParameter() {
            return "fields";
        }

        @Override
        public Predicate build(Map config) {
            return new FieldExists((String[]) config.get("fields"));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy