001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Arrays;
026import java.util.Set;
027import java.util.stream.Collectors;
028
029import org.eclipse.aether.ConfigurationProperties;
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.spi.artifact.ArtifactPredicate;
032import org.eclipse.aether.spi.artifact.ArtifactPredicateFactory;
033import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
034import org.eclipse.aether.util.ConfigUtils;
035
036@Singleton
037@Named
038public final class DefaultArtifactPredicateFactory implements ArtifactPredicateFactory {
039    private static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_CHECKSUMS;
040
041    /**
042     * Comma-separated list of extensions with leading dot (example ".asc") that should have checksums omitted.
043     * These are applied to sub-artifacts only. Note: to achieve 1.7.x aether.checksums.forSignature=true behaviour,
044     * pass empty string as value for this property.
045     *
046     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
047     * @configurationType {@link java.lang.String}
048     * @configurationDefaultValue {@link #DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS}
049     * @configurationRepoIdSuffix No
050     */
051    public static final String CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS =
052            CONFIG_PROPS_PREFIX + "omitChecksumsForExtensions";
053
054    public static final String DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS = ".asc,.sigstore";
055
056    private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
057
058    @Inject
059    public DefaultArtifactPredicateFactory(ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector) {
060        this.checksumAlgorithmFactorySelector = checksumAlgorithmFactorySelector;
061    }
062
063    @Override
064    public ArtifactPredicate newInstance(RepositorySystemSession session) {
065        // ensure uniqueness of (potentially user set) extension list
066        Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString(
067                                session,
068                                DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS,
069                                CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS)
070                        .split(","))
071                .filter(s -> s != null && !s.trim().isEmpty())
072                .collect(Collectors.toSet());
073
074        // validation: enforce that all strings in this set are having leading dot
075        if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) {
076            throw new IllegalArgumentException(String.format(
077                    "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))",
078                    CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions));
079        }
080        return new DefaultArtifactPredicate(checksumAlgorithmFactorySelector, omitChecksumsForExtensions);
081    }
082}