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.*;
026import java.util.stream.Collectors;
027
028import org.eclipse.aether.ConfigurationProperties;
029import org.eclipse.aether.RepositorySystemSession;
030import org.eclipse.aether.spi.artifact.ArtifactPredicate;
031import org.eclipse.aether.spi.artifact.ArtifactPredicateFactory;
032import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
033import org.eclipse.aether.util.ConfigUtils;
034
035@Singleton
036@Named
037public final class DefaultArtifactPredicateFactory implements ArtifactPredicateFactory {
038    private static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_CHECKSUMS;
039
040    /**
041     * Comma-separated list of extensions with leading dot (example ".asc") that should have checksums omitted.
042     * These are applied to sub-artifacts only. Note: to achieve 1.7.x aether.checksums.forSignature=true behaviour,
043     * pass empty string as value for this property.
044     *
045     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
046     * @configurationType {@link java.lang.String}
047     * @configurationDefaultValue {@link #DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS}
048     * @configurationRepoIdSuffix No
049     */
050    public static final String CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS =
051            CONFIG_PROPS_PREFIX + "omitChecksumsForExtensions";
052
053    public static final String DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS = ".asc,.sigstore";
054
055    private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
056
057    @Inject
058    public DefaultArtifactPredicateFactory(ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector) {
059        this.checksumAlgorithmFactorySelector = checksumAlgorithmFactorySelector;
060    }
061
062    @Override
063    public ArtifactPredicate newInstance(RepositorySystemSession session) {
064        // ensure uniqueness of (potentially user set) extension list
065        Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString(
066                                session,
067                                DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS,
068                                CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS)
069                        .split(","))
070                .filter(s -> s != null && !s.trim().isEmpty())
071                .collect(Collectors.toSet());
072
073        // validation: enforce that all strings in this set are having leading dot
074        if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) {
075            throw new IllegalArgumentException(String.format(
076                    "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))",
077                    CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions));
078        }
079        return new DefaultArtifactPredicate(checksumAlgorithmFactorySelector, omitChecksumsForExtensions);
080    }
081}