View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl.checksum;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.repository.RemoteRepository;
31  import org.eclipse.aether.spi.checksums.ProvidedChecksumsSource;
32  import org.eclipse.aether.spi.checksums.TrustedChecksumsSource;
33  import org.eclipse.aether.spi.connector.ArtifactDownload;
34  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
35  
36  import static java.util.Objects.requireNonNull;
37  
38  /**
39   * Adapter that adapts {@link TrustedChecksumsSource} to {@link ProvidedChecksumsSource} used by connector. Hence, any
40   * "trusted" source exist that is enabled, automatically becomes "provided" source as well.
41   *
42   * @since 1.9.0
43   */
44  @Singleton
45  @Named(TrustedToProvidedChecksumsSourceAdapter.NAME)
46  public final class TrustedToProvidedChecksumsSourceAdapter implements ProvidedChecksumsSource {
47      public static final String NAME = "trusted2provided";
48  
49      private final Map<String, TrustedChecksumsSource> trustedChecksumsSources;
50  
51      @Inject
52      public TrustedToProvidedChecksumsSourceAdapter(Map<String, TrustedChecksumsSource> trustedChecksumsSources) {
53          this.trustedChecksumsSources = requireNonNull(trustedChecksumsSources);
54      }
55  
56      @Override
57      public Map<String, String> getProvidedArtifactChecksums(
58              RepositorySystemSession session,
59              ArtifactDownload transfer,
60              RemoteRepository repository,
61              List<ChecksumAlgorithmFactory> checksumAlgorithmFactories) {
62          Artifact artifact = transfer.getArtifact();
63          Map<String, String> trustedChecksums;
64          // check for connector repository
65          for (TrustedChecksumsSource trustedChecksumsSource : trustedChecksumsSources.values()) {
66              trustedChecksums = trustedChecksumsSource.getTrustedArtifactChecksums(
67                      session, artifact, repository, checksumAlgorithmFactories);
68              if (trustedChecksums != null && !trustedChecksums.isEmpty()) {
69                  return trustedChecksums;
70              }
71          }
72          // if repo above is "mirrorOf", this one kicks in
73          if (!transfer.getRepositories().isEmpty()) {
74              for (RemoteRepository remoteRepository : transfer.getRepositories()) {
75                  for (TrustedChecksumsSource trustedChecksumsSource : trustedChecksumsSources.values()) {
76                      trustedChecksums = trustedChecksumsSource.getTrustedArtifactChecksums(
77                              session, artifact, remoteRepository, checksumAlgorithmFactories);
78                      if (trustedChecksums != null && !trustedChecksums.isEmpty()) {
79                          return trustedChecksums;
80                      }
81                  }
82              }
83          }
84          return null;
85      }
86  }