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.checksum;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.List;
026import java.util.Map;
027
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.repository.RemoteRepository;
031import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource;
032import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
033import org.eclipse.aether.spi.connector.ArtifactDownload;
034import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
035
036import static java.util.Objects.requireNonNull;
037
038/**
039 * Adapter that adapts {@link TrustedChecksumsSource} to {@link ProvidedChecksumsSource} used by connector. Hence, any
040 * "trusted" source exist that is enabled, automatically becomes "provided" source as well.
041 *
042 * @since 1.9.0
043 */
044@Singleton
045@Named(TrustedToProvidedChecksumsSourceAdapter.NAME)
046public final class TrustedToProvidedChecksumsSourceAdapter implements ProvidedChecksumsSource {
047    public static final String NAME = "trusted2provided";
048
049    private final Map<String, TrustedChecksumsSource> trustedChecksumsSources;
050
051    @Inject
052    public TrustedToProvidedChecksumsSourceAdapter(Map<String, TrustedChecksumsSource> trustedChecksumsSources) {
053        this.trustedChecksumsSources = requireNonNull(trustedChecksumsSources);
054    }
055
056    @Override
057    public Map<String, String> getProvidedArtifactChecksums(
058            RepositorySystemSession session,
059            ArtifactDownload transfer,
060            RemoteRepository repository,
061            List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
062        Artifact artifact = transfer.getArtifact();
063        Map<String, String> trustedChecksums;
064        // check for connector repository
065        for (TrustedChecksumsSource trustedChecksumsSource : trustedChecksumsSources.values()) {
066            trustedChecksums = trustedChecksumsSource.getTrustedArtifactChecksums(
067                    session, artifact, repository, checksumAlgorithmFactories);
068            if (trustedChecksums != null && !trustedChecksums.isEmpty()) {
069                return trustedChecksums;
070            }
071        }
072        // if repo above is "mirrorOf", this one kicks in
073        if (!transfer.getRepositories().isEmpty()) {
074            for (RemoteRepository remoteRepository : transfer.getRepositories()) {
075                for (TrustedChecksumsSource trustedChecksumsSource : trustedChecksumsSources.values()) {
076                    trustedChecksums = trustedChecksumsSource.getTrustedArtifactChecksums(
077                            session, artifact, remoteRepository, checksumAlgorithmFactories);
078                    if (trustedChecksums != null && !trustedChecksums.isEmpty()) {
079                        return trustedChecksums;
080                    }
081                }
082            }
083        }
084        return null;
085    }
086}