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;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.Arrays;
26  import java.util.Set;
27  import java.util.stream.Collectors;
28  
29  import org.eclipse.aether.ConfigurationProperties;
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.spi.artifact.ArtifactPredicate;
32  import org.eclipse.aether.spi.artifact.ArtifactPredicateFactory;
33  import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
34  import org.eclipse.aether.util.ConfigUtils;
35  
36  @Singleton
37  @Named
38  public final class DefaultArtifactPredicateFactory implements ArtifactPredicateFactory {
39      private static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_CHECKSUMS;
40  
41      /**
42       * Comma-separated list of extensions with leading dot (example ".asc") that should have checksums omitted.
43       * These are applied to sub-artifacts only. Note: to achieve 1.7.x aether.checksums.forSignature=true behaviour,
44       * pass empty string as value for this property.
45       *
46       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
47       * @configurationType {@link java.lang.String}
48       * @configurationDefaultValue {@link #DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS}
49       * @configurationRepoIdSuffix No
50       */
51      public static final String CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS =
52              CONFIG_PROPS_PREFIX + "omitChecksumsForExtensions";
53  
54      public static final String DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS = ".asc,.sigstore";
55  
56      private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
57  
58      @Inject
59      public DefaultArtifactPredicateFactory(ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector) {
60          this.checksumAlgorithmFactorySelector = checksumAlgorithmFactorySelector;
61      }
62  
63      @Override
64      public ArtifactPredicate newInstance(RepositorySystemSession session) {
65          // ensure uniqueness of (potentially user set) extension list
66          Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString(
67                                  session,
68                                  DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS,
69                                  CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS)
70                          .split(","))
71                  .filter(s -> s != null && !s.trim().isEmpty())
72                  .collect(Collectors.toSet());
73  
74          // validation: enforce that all strings in this set are having leading dot
75          if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) {
76              throw new IllegalArgumentException(String.format(
77                      "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))",
78                      CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions));
79          }
80          return new DefaultArtifactPredicate(checksumAlgorithmFactorySelector, omitChecksumsForExtensions);
81      }
82  }