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;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.net.URI;
026import java.net.URISyntaxException;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.Collections;
030import java.util.List;
031import java.util.Set;
032import java.util.stream.Collectors;
033
034import org.eclipse.aether.RepositorySystemSession;
035import org.eclipse.aether.artifact.Artifact;
036import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector;
037import org.eclipse.aether.metadata.Metadata;
038import org.eclipse.aether.repository.RemoteRepository;
039import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
040import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
041import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
042import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
043import org.eclipse.aether.transfer.NoRepositoryLayoutException;
044import org.eclipse.aether.util.ConfigUtils;
045
046import static java.util.Objects.requireNonNull;
047
048/**
049 * Provides a Maven-2 repository layout for repositories with content type {@code "default"}.
050 */
051@Singleton
052@Named("maven2")
053public final class Maven2RepositoryLayoutFactory implements RepositoryLayoutFactory {
054
055    public static final String CONFIG_PROP_CHECKSUMS_ALGORITHMS = "aether.checksums.algorithms";
056
057    private static final String DEFAULT_CHECKSUMS_ALGORITHMS = "SHA-1,MD5";
058
059    public static final String CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS =
060            "aether.checksums.omitChecksumsForExtensions";
061
062    private static final String DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS = ".asc,.sigstore";
063
064    private float priority;
065
066    private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
067
068    public float getPriority() {
069        return priority;
070    }
071
072    /**
073     * Service locator ctor.
074     */
075    @Deprecated
076    public Maven2RepositoryLayoutFactory() {
077        this(new DefaultChecksumAlgorithmFactorySelector());
078    }
079
080    @Inject
081    public Maven2RepositoryLayoutFactory(ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector) {
082        this.checksumAlgorithmFactorySelector = requireNonNull(checksumAlgorithmFactorySelector);
083    }
084
085    /**
086     * Sets the priority of this component.
087     *
088     * @param priority The priority.
089     * @return This component for chaining, never {@code null}.
090     */
091    public Maven2RepositoryLayoutFactory setPriority(float priority) {
092        this.priority = priority;
093        return this;
094    }
095
096    public RepositoryLayout newInstance(RepositorySystemSession session, RemoteRepository repository)
097            throws NoRepositoryLayoutException {
098        requireNonNull(session, "session cannot be null");
099        requireNonNull(repository, "repository cannot be null");
100        if (!"default".equals(repository.getContentType())) {
101            throw new NoRepositoryLayoutException(repository);
102        }
103
104        List<ChecksumAlgorithmFactory> checksumsAlgorithms = checksumAlgorithmFactorySelector.selectList(
105                ConfigUtils.parseCommaSeparatedUniqueNames(ConfigUtils.getString(
106                        session,
107                        DEFAULT_CHECKSUMS_ALGORITHMS,
108                        CONFIG_PROP_CHECKSUMS_ALGORITHMS + "." + repository.getId(),
109                        CONFIG_PROP_CHECKSUMS_ALGORITHMS)));
110
111        // ensure uniqueness of (potentially user set) extension list
112        Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString(
113                                session,
114                                DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS,
115                                CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS)
116                        .split(","))
117                .filter(s -> s != null && !s.trim().isEmpty())
118                .collect(Collectors.toSet());
119
120        // validation: enforce that all strings in this set are having leading dot
121        if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) {
122            throw new IllegalArgumentException(String.format(
123                    "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))",
124                    CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions));
125        }
126
127        return new Maven2RepositoryLayout(
128                checksumAlgorithmFactorySelector, checksumsAlgorithms, omitChecksumsForExtensions);
129    }
130
131    private static class Maven2RepositoryLayout implements RepositoryLayout {
132        private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
133
134        private final List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms;
135
136        private final Set<String> extensionsWithoutChecksums;
137
138        private Maven2RepositoryLayout(
139                ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector,
140                List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms,
141                Set<String> extensionsWithoutChecksums) {
142            this.checksumAlgorithmFactorySelector = requireNonNull(checksumAlgorithmFactorySelector);
143            this.configuredChecksumAlgorithms = Collections.unmodifiableList(configuredChecksumAlgorithms);
144            this.extensionsWithoutChecksums = requireNonNull(extensionsWithoutChecksums);
145        }
146
147        private URI toUri(String path) {
148            try {
149                return new URI(null, null, path, null);
150            } catch (URISyntaxException e) {
151                throw new IllegalStateException(e);
152            }
153        }
154
155        @Override
156        public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() {
157            return configuredChecksumAlgorithms;
158        }
159
160        @Override
161        public boolean hasChecksums(Artifact artifact) {
162            String artifactExtension = artifact.getExtension(); // ie. pom.asc
163            for (String extensionWithoutChecksums : extensionsWithoutChecksums) {
164                if (artifactExtension.endsWith(extensionWithoutChecksums)) {
165                    return false;
166                }
167            }
168            return true;
169        }
170
171        @Override
172        public URI getLocation(Artifact artifact, boolean upload) {
173            StringBuilder path = new StringBuilder(128);
174
175            path.append(artifact.getGroupId().replace('.', '/')).append('/');
176
177            path.append(artifact.getArtifactId()).append('/');
178
179            path.append(artifact.getBaseVersion()).append('/');
180
181            path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion());
182
183            if (artifact.getClassifier().length() > 0) {
184                path.append('-').append(artifact.getClassifier());
185            }
186
187            if (artifact.getExtension().length() > 0) {
188                path.append('.').append(artifact.getExtension());
189            }
190
191            return toUri(path.toString());
192        }
193
194        @Override
195        public URI getLocation(Metadata metadata, boolean upload) {
196            StringBuilder path = new StringBuilder(128);
197
198            if (metadata.getGroupId().length() > 0) {
199                path.append(metadata.getGroupId().replace('.', '/')).append('/');
200
201                if (metadata.getArtifactId().length() > 0) {
202                    path.append(metadata.getArtifactId()).append('/');
203
204                    if (metadata.getVersion().length() > 0) {
205                        path.append(metadata.getVersion()).append('/');
206                    }
207                }
208            }
209
210            path.append(metadata.getType());
211
212            return toUri(path.toString());
213        }
214
215        @Override
216        public List<ChecksumLocation> getChecksumLocations(Artifact artifact, boolean upload, URI location) {
217            if (!hasChecksums(artifact) || isChecksum(artifact.getExtension())) {
218                return Collections.emptyList();
219            }
220            return getChecksumLocations(location);
221        }
222
223        @Override
224        public List<ChecksumLocation> getChecksumLocations(Metadata metadata, boolean upload, URI location) {
225            return getChecksumLocations(location);
226        }
227
228        private List<ChecksumLocation> getChecksumLocations(URI location) {
229            List<ChecksumLocation> checksumLocations = new ArrayList<>(configuredChecksumAlgorithms.size());
230            for (ChecksumAlgorithmFactory checksumAlgorithmFactory : configuredChecksumAlgorithms) {
231                checksumLocations.add(ChecksumLocation.forLocation(location, checksumAlgorithmFactory));
232            }
233            return checksumLocations;
234        }
235
236        private boolean isChecksum(String extension) {
237            return checksumAlgorithmFactorySelector.isChecksumExtension(extension);
238        }
239    }
240}