1 package org.apache.archiva.repository.scanner.mock;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import org.apache.archiva.common.filelock.DefaultFileLockManager;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
26 import org.apache.archiva.model.ArchivaArtifact;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.*;
31 import org.apache.archiva.repository.storage.FilesystemStorage;
32 import org.apache.archiva.repository.storage.StorageAsset;
33 import org.apache.commons.lang3.StringUtils;
35 import java.io.IOException;
36 import java.nio.file.Paths;
37 import java.util.HashMap;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
44 * @author Martin Stockhammer <martin_s@apache.org>
46 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
48 private static final String PATH_SEPARATOR = "/";
49 private static final String GROUP_SEPARATOR = ".";
50 public static final String MAVEN_METADATA = "maven-metadata.xml";
53 private ManagedRepository repository;
54 private FilesystemStorage fsStorage;
56 public ManagedRepositoryContentMock(ManagedRepository repo) {
57 this.repository = repo;
61 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
67 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
73 public void deleteGroupId( String groupId ) throws ContentNotFoundException
79 public void deleteProject( String namespace, String projectId ) throws RepositoryException
85 public String getId( )
87 return repository.getId();
91 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException
97 public String getRepoRoot( )
99 return getRepoRootAsset().getFilePath().toString();
102 private StorageAsset getRepoRootAsset() {
103 if (fsStorage==null) {
105 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
106 } catch (IOException e) {
110 return fsStorage.getAsset("");
114 public ManagedRepository getRepository( )
120 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
126 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
132 public boolean hasContent( ArtifactReference reference )
138 public boolean hasContent( ProjectReference reference )
144 public boolean hasContent( VersionedReference reference )
150 public void setRepository( ManagedRepository repo )
152 this.repository = repo;
155 private Map<ArtifactReference, String> refs = new HashMap<>();
158 public ArtifactReference toArtifactReference( String path ) throws LayoutException
160 if ( StringUtils.isBlank( path ) )
162 throw new LayoutException( "Unable to convert blank path." );
165 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
167 ArtifactReference artifact = new ArtifactReference();
168 artifact.setGroupId( metadata.getNamespace() );
169 artifact.setArtifactId( metadata.getProject() );
170 artifact.setVersion( metadata.getVersion() );
171 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
174 artifact.setClassifier( facet.getClassifier() );
175 artifact.setType( facet.getType() );
177 refs.put(artifact, path);
181 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
183 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
185 int len = parts.length;
188 throw new IllegalArgumentException(
189 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
192 String id = parts[--len];
193 String baseVersion = parts[--len];
194 String artifactId = parts[--len];
195 StringBuilder groupIdBuilder = new StringBuilder();
196 for ( int i = 0; i < len - 1; i++ )
198 groupIdBuilder.append( parts[i] );
199 groupIdBuilder.append( '.' );
201 groupIdBuilder.append( parts[len - 1] );
203 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
206 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
210 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
213 if ( !id.startsWith( projectId + "-" ) )
215 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
216 + "' doesn't start with artifact ID '" + projectId + "'" );
219 MavenArtifactFacet facet = new MavenArtifactFacet();
221 int index = projectId.length() + 1;
223 String idSubStrFromVersion = id.substring( index );
224 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
226 // non-snapshot versions, or non-timestamped snapshot versions
227 version = projectVersion;
229 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
231 // timestamped snapshots
234 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
235 if ( mainVersionLength == 0 )
237 throw new IllegalArgumentException(
238 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
241 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
243 String timestamp = m.group( 1 );
244 String buildNumber = m.group( 2 );
245 facet.setTimestamp( timestamp );
246 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
247 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
249 catch ( IllegalStateException e )
251 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
252 + "' doesn't contain a timestamped version matching snapshot '"
253 + projectVersion + "'", e);
259 throw new IllegalArgumentException(
260 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
261 + projectVersion + "'" );
266 index += version.length();
267 if ( index == id.length() )
269 // no classifier or extension
275 char c = id.charAt( index );
278 // classifier up until '.'
279 int extIndex = id.indexOf( '.', index );
282 classifier = id.substring( index + 1, extIndex );
283 ext = id.substring( extIndex + 1 );
287 classifier = id.substring( index + 1 );
293 // rest is the extension
295 ext = id.substring( index + 1 );
299 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
300 + "' expected classifier or extension but got '"
301 + id.substring( index ) + "'" );
305 ArtifactMetadata metadata = new ArtifactMetadata();
306 metadata.setId( id );
307 metadata.setNamespace( namespace );
308 metadata.setProject( projectId );
309 metadata.setRepositoryId( repoId );
310 metadata.setProjectVersion( projectVersion );
311 metadata.setVersion( version );
313 facet.setClassifier( classifier );
315 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
316 // to select the correct order to apply multiple extensions mappings to a preferred type
317 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
318 // perhaps the plugins could register missing entries in configuration, then we just use configuration
324 // use extension as default
330 // TODO: should we allow this instead?
333 throw new IllegalArgumentException(
334 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
337 facet.setType( type );
338 metadata.addFacet( facet );
345 public StorageAsset toFile( ArtifactReference reference )
347 return getRepoRootAsset().resolve(refs.get(reference));
351 public StorageAsset toFile( ArchivaArtifact reference )
356 private String formatAsDirectory( String directory )
358 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
361 public String toMetadataPath( ProjectReference reference )
363 StringBuilder path = new StringBuilder();
365 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
366 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
367 path.append( MAVEN_METADATA );
369 return path.toString();
372 public String toMetadataPath( VersionedReference reference )
374 StringBuilder path = new StringBuilder();
376 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
377 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
378 if ( reference.getVersion() != null )
380 // add the version only if it is present
381 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
383 path.append( MAVEN_METADATA );
385 return path.toString();
389 public String toPath( ArtifactReference reference )
395 public String toPath( ArchivaArtifact reference )