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