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