001/*
002 * Copyright 2023 the original author or authors.
003 * <p>
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 * <p>
008 * https://www.apache.org/licenses/LICENSE-2.0
009 * <p>
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 */
016package de.cuioss.tools.net;
017
018import static java.util.Objects.requireNonNull;
019
020import java.io.Serializable;
021import java.util.List;
022
023import lombok.EqualsAndHashCode;
024import lombok.Getter;
025import lombok.NonNull;
026import lombok.RequiredArgsConstructor;
027import lombok.ToString;
028
029/**
030 * Defines a filter identifying which parameters are not to be included within
031 * url parameter handling. Therefore, it filters parameter prefixed with
032 * "javax.faces", depending on <code>excludeFacesParameter</code> and
033 * additionally a given list of parameter names.
034 *
035 * @author Oliver Wolff
036 */
037@RequiredArgsConstructor
038@EqualsAndHashCode
039@ToString
040public class ParameterFilter implements Serializable {
041
042    private static final long serialVersionUID = -4780294784318006024L;
043
044    private static final String JAVAX_FACES = "javax.faces";
045
046    /**
047     * The list of string to be excluded from the parameter-list. Because the test
048     * utilizes toLowerCase() the members of the list must all be lowercase.
049     * Otherwise, they are not considered.
050     */
051    @NonNull
052    @Getter
053    private final List<String> excludes;
054
055    /** Flag indicating whether to exclude technical jsf parameters. */
056    private final boolean excludeFacesParameter;
057
058    /**
059     * @param value as key of view parameter. Must not be null
060     * @return true if value belongs to excluded values
061     */
062    public boolean isExcluded(final String value) {
063        requireNonNull(value);
064        var excluded = false;
065        if (excludeFacesParameter) {
066            excluded = value.startsWith(JAVAX_FACES);
067        }
068        if (!excluded) {
069            excluded = excludes.contains(value.toLowerCase());
070        }
071        return excluded;
072    }
073
074}