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.TrustedChecksumsSource;
032import org.eclipse.aether.spi.connector.ArtifactDownload;
033import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
034import org.eclipse.aether.spi.connector.checksum.ProvidedChecksumsSource;
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            List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
061        Artifact artifact = transfer.getArtifact();
062        for (RemoteRepository remoteRepository : transfer.getRepositories()) {
063            for (TrustedChecksumsSource trustedChecksumsSource : trustedChecksumsSources.values()) {
064                Map<String, String> trustedChecksums = trustedChecksumsSource.getTrustedArtifactChecksums(
065                        session, artifact, remoteRepository, checksumAlgorithmFactories);
066                if (trustedChecksums != null && !trustedChecksums.isEmpty()) {
067                    return trustedChecksums;
068                }
069            }
070        }
071        return null;
072    }
073}