001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.scim;
018
019import com.google.errorprone.annotations.CheckReturnValue;
020import org.jspecify.annotations.NonNull;
021import javax.annotation.concurrent.Immutable;
022import static java.util.Objects.requireNonNull;
023
024/** Optional bounded evaluation of schema-bound, policy-approved filters against application data.
025 * Each evaluation rechecks the entire filter under this evaluator's provider. All clauses and
026 * matching array elements are visited, including clauses whose truth value cannot change the
027 * verdict, so unsupported application matching cannot become a successful non-match.
028 * <p>String matching uses the provider's explicitly configured strategy, shared with PATCH.
029 * Missing/null values and empty multi-values are unassigned; empty strings are assigned for null
030 * comparisons but fail presence. For assigned multi-values, comparisons use any-element semantics,
031 * including {@code ne}; {@code not} negates the complete predicate. An unassigned attribute is
032 * unequal to a nonnull literal. This evaluator does not authorize a resource, execute storage
033 * queries, or establish authoritative identity uniqueness.
034 * @since 1.0.0
035 */
036@Immutable
037@CheckReturnValue
038public final class ScimFilterEvaluator {
039    private final @NonNull ScimServiceProvider provider;
040    private ScimFilterEvaluator(@NonNull ScimServiceProvider provider) { this.provider = provider; }
041
042    /** Creates an evaluator using this provider's matching strategy, policies and limits.
043     * @param provider the registered semantic context
044     * @return an immutable evaluator
045     * @throws IllegalArgumentException if the provider has no application string matching strategy
046     * @since 1.0.0
047     */
048    public static @NonNull ScimFilterEvaluator fromServiceProvider(@NonNull ScimServiceProvider provider) {
049        requireNonNull(provider);
050        if (provider.getStringMatching().isEmpty())
051            throw new IllegalArgumentException("SCIM filter evaluation requires an application matching strategy.");
052        return new ScimFilterEvaluator(provider);
053    }
054
055    /** Rebinds, approves and evaluates a complete filter with one shared work budget.
056     * A foreign registered resource context rejects; a syntax-only or foreign filter is rechecked.
057     * Parsed/bound roots retain original syntax privately so canonical expansion does not
058     * consume their input-character allowance. Original syntax is reparsed under this provider;
059     * extracted subtrees without that provenance use a bounded canonical fallback.
060     * Unsupported matching rejects with invalidFilter. Ordinary application faults are contained
061     * as fixed internalError results and redacted observer events; interruption is preserved and
062     * fatal VM errors escape.
063     * @param resource the immutable application resource
064     * @param filter the bounded filter tree to recheck
065     * @return the complete verdict or fixed rejection
066     * @since 1.0.0
067     */
068    public @NonNull ScimFilterEvaluationResult evaluateResult(@NonNull ScimResource resource,
069            @NonNull ScimFilter filter) {
070        return provider.evaluateFilterResult(requireNonNull(resource), requireNonNull(filter));
071    }
072
073    /** Returns a fixed redacted description.
074     * @return the description
075     * @since 1.0.0
076     */
077    @Override public @NonNull String toString() { return "ScimFilterEvaluator{<redacted>}"; }
078}