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"; 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, DEFAULT_CHECKSUMS_ALGORITHMS, CONFIG_PROP_CHECKSUMS_ALGORITHMS))); 107 108 // ensure uniqueness of (potentially user set) extension list 109 Set<String> omitChecksumsForExtensions = Arrays.stream(ConfigUtils.getString( 110 session, 111 DEFAULT_OMIT_CHECKSUMS_FOR_EXTENSIONS, 112 CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS) 113 .split(",")) 114 .filter(s -> s != null && !s.trim().isEmpty()) 115 .collect(Collectors.toSet()); 116 117 // validation: enforce that all strings in this set are having leading dot 118 if (omitChecksumsForExtensions.stream().anyMatch(s -> !s.startsWith("."))) { 119 throw new IllegalArgumentException(String.format( 120 "The configuration %s contains illegal values: %s (all entries must start with '.' (dot))", 121 CONFIG_PROP_OMIT_CHECKSUMS_FOR_EXTENSIONS, omitChecksumsForExtensions)); 122 } 123 124 return new Maven2RepositoryLayout( 125 checksumAlgorithmFactorySelector, checksumsAlgorithms, omitChecksumsForExtensions); 126 } 127 128 private static class Maven2RepositoryLayout implements RepositoryLayout { 129 private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector; 130 131 private final List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms; 132 133 private final Set<String> extensionsWithoutChecksums; 134 135 private Maven2RepositoryLayout( 136 ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector, 137 List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms, 138 Set<String> extensionsWithoutChecksums) { 139 this.checksumAlgorithmFactorySelector = requireNonNull(checksumAlgorithmFactorySelector); 140 this.configuredChecksumAlgorithms = Collections.unmodifiableList(configuredChecksumAlgorithms); 141 this.extensionsWithoutChecksums = requireNonNull(extensionsWithoutChecksums); 142 } 143 144 private URI toUri(String path) { 145 try { 146 return new URI(null, null, path, null); 147 } catch (URISyntaxException e) { 148 throw new IllegalStateException(e); 149 } 150 } 151 152 @Override 153 public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories() { 154 return configuredChecksumAlgorithms; 155 } 156 157 @Override 158 public boolean hasChecksums(Artifact artifact) { 159 String artifactExtension = artifact.getExtension(); // ie. pom.asc 160 for (String extensionWithoutChecksums : extensionsWithoutChecksums) { 161 if (artifactExtension.endsWith(extensionWithoutChecksums)) { 162 return false; 163 } 164 } 165 return true; 166 } 167 168 @Override 169 public URI getLocation(Artifact artifact, boolean upload) { 170 StringBuilder path = new StringBuilder(128); 171 172 path.append(artifact.getGroupId().replace('.', '/')).append('/'); 173 174 path.append(artifact.getArtifactId()).append('/'); 175 176 path.append(artifact.getBaseVersion()).append('/'); 177 178 path.append(artifact.getArtifactId()).append('-').append(artifact.getVersion()); 179 180 if (artifact.getClassifier().length() > 0) { 181 path.append('-').append(artifact.getClassifier()); 182 } 183 184 if (artifact.getExtension().length() > 0) { 185 path.append('.').append(artifact.getExtension()); 186 } 187 188 return toUri(path.toString()); 189 } 190 191 @Override 192 public URI getLocation(Metadata metadata, boolean upload) { 193 StringBuilder path = new StringBuilder(128); 194 195 if (metadata.getGroupId().length() > 0) { 196 path.append(metadata.getGroupId().replace('.', '/')).append('/'); 197 198 if (metadata.getArtifactId().length() > 0) { 199 path.append(metadata.getArtifactId()).append('/'); 200 201 if (metadata.getVersion().length() > 0) { 202 path.append(metadata.getVersion()).append('/'); 203 } 204 } 205 } 206 207 path.append(metadata.getType()); 208 209 return toUri(path.toString()); 210 } 211 212 @Override 213 public List<ChecksumLocation> getChecksumLocations(Artifact artifact, boolean upload, URI location) { 214 if (!hasChecksums(artifact) || isChecksum(artifact.getExtension())) { 215 return Collections.emptyList(); 216 } 217 return getChecksumLocations(location); 218 } 219 220 @Override 221 public List<ChecksumLocation> getChecksumLocations(Metadata metadata, boolean upload, URI location) { 222 return getChecksumLocations(location); 223 } 224 225 private List<ChecksumLocation> getChecksumLocations(URI location) { 226 List<ChecksumLocation> checksumLocations = new ArrayList<>(configuredChecksumAlgorithms.size()); 227 for (ChecksumAlgorithmFactory checksumAlgorithmFactory : configuredChecksumAlgorithms) { 228 checksumLocations.add(ChecksumLocation.forLocation(location, checksumAlgorithmFactory)); 229 } 230 return checksumLocations; 231 } 232 233 private boolean isChecksum(String extension) { 234 return checksumAlgorithmFactorySelector.isChecksumExtension(extension); 235 } 236 } 237}