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.maven.model.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.content.Artifact;
32 import org.apache.archiva.repository.content.ContentItem;
33 import org.apache.archiva.repository.content.ItemNotFoundException;
34 import org.apache.archiva.repository.content.ItemSelector;
35 import org.apache.archiva.repository.content.Namespace;
36 import org.apache.archiva.repository.content.Project;
37 import org.apache.archiva.repository.content.Version;
38 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
39 import org.apache.archiva.repository.storage.StorageAsset;
40 import org.apache.commons.lang3.StringUtils;
42 import java.io.IOException;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.HashMap;
46 import java.util.List;
49 import java.util.regex.Matcher;
50 import java.util.regex.Pattern;
51 import java.util.stream.Stream;
54 * @author Martin Stockhammer <martin_s@apache.org>
56 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
58 private static final String PATH_SEPARATOR = "/";
59 private static final String GROUP_SEPARATOR = ".";
60 public static final String MAVEN_METADATA = "maven-metadata.xml";
63 private ManagedRepository repository;
64 private FilesystemStorage fsStorage;
66 public ManagedRepositoryContentMock(ManagedRepository repo) {
67 this.repository = repo;
71 public VersionedReference toVersion( String groupId, String artifactId, String version )
77 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
83 public VersionedReference toVersion( ArtifactReference artifactReference )
89 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
95 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
101 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
107 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
113 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
119 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
125 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
131 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
137 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
143 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
149 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
155 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
161 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
167 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
173 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
179 public boolean hasContent( ItemSelector selector )
185 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
191 public ContentItem toItem( String path ) throws LayoutException
197 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
203 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
209 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
215 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
221 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
227 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
233 public String getId( )
235 return repository.getId();
239 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
245 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
251 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
257 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
263 public String getRepoRoot( )
265 return getRepoRootAsset().getFilePath().toString();
268 private StorageAsset getRepoRootAsset() {
269 if (fsStorage==null) {
271 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
272 } catch (IOException e) {
276 return fsStorage.getAsset("");
280 public ManagedRepository getRepository( )
286 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
292 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
298 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
304 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
310 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
316 public void setRepository( ManagedRepository repo )
318 this.repository = repo;
322 public StorageAsset toFile( VersionedReference reference )
327 private Map<ArtifactReference, String> refs = new HashMap<>();
330 public ArtifactReference toArtifactReference( String path ) throws LayoutException
332 if ( StringUtils.isBlank( path ) )
334 throw new LayoutException( "Unable to convert blank path." );
337 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
339 ArtifactReference artifact = new ArtifactReference();
340 artifact.setGroupId( metadata.getNamespace() );
341 artifact.setArtifactId( metadata.getProject() );
342 artifact.setVersion( metadata.getVersion() );
343 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
346 artifact.setClassifier( facet.getClassifier() );
347 artifact.setType( facet.getType() );
349 refs.put(artifact, path);
353 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
355 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
357 int len = parts.length;
360 throw new IllegalArgumentException(
361 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
364 String id = parts[--len];
365 String baseVersion = parts[--len];
366 String artifactId = parts[--len];
367 StringBuilder groupIdBuilder = new StringBuilder();
368 for ( int i = 0; i < len - 1; i++ )
370 groupIdBuilder.append( parts[i] );
371 groupIdBuilder.append( '.' );
373 groupIdBuilder.append( parts[len - 1] );
375 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
378 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
382 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
385 if ( !id.startsWith( projectId + "-" ) )
387 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
388 + "' doesn't start with artifact ID '" + projectId + "'" );
391 MavenArtifactFacet facet = new MavenArtifactFacet();
393 int index = projectId.length() + 1;
395 String idSubStrFromVersion = id.substring( index );
396 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
398 // non-snapshot versions, or non-timestamped snapshot versions
399 version = projectVersion;
401 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
403 // timestamped snapshots
406 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
407 if ( mainVersionLength == 0 )
409 throw new IllegalArgumentException(
410 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
413 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
415 String timestamp = m.group( 1 );
416 String buildNumber = m.group( 2 );
417 facet.setTimestamp( timestamp );
418 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
419 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
421 catch ( IllegalStateException e )
423 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
424 + "' doesn't contain a timestamped version matching snapshot '"
425 + projectVersion + "'", e);
431 throw new IllegalArgumentException(
432 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
433 + projectVersion + "'" );
438 index += version.length();
439 if ( index == id.length() )
441 // no classifier or extension
447 char c = id.charAt( index );
450 // classifier up until '.'
451 int extIndex = id.indexOf( '.', index );
454 classifier = id.substring( index + 1, extIndex );
455 ext = id.substring( extIndex + 1 );
459 classifier = id.substring( index + 1 );
465 // rest is the extension
467 ext = id.substring( index + 1 );
471 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
472 + "' expected classifier or extension but got '"
473 + id.substring( index ) + "'" );
477 ArtifactMetadata metadata = new ArtifactMetadata();
478 metadata.setId( id );
479 metadata.setNamespace( namespace );
480 metadata.setProject( projectId );
481 metadata.setRepositoryId( repoId );
482 metadata.setProjectVersion( projectVersion );
483 metadata.setVersion( version );
485 facet.setClassifier( classifier );
487 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
488 // to select the correct order to apply multiple extensions mappings to a preferred type
489 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
490 // perhaps the plugins could register missing entries in configuration, then we just use configuration
496 // use extension as default
502 // TODO: should we allow this instead?
505 throw new IllegalArgumentException(
506 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
509 facet.setType( type );
510 metadata.addFacet( facet );
517 public StorageAsset toFile( ArtifactReference reference )
519 return getRepoRootAsset().resolve(refs.get(reference));
523 public StorageAsset toFile( ArchivaArtifact reference )
528 private String formatAsDirectory( String directory )
530 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
533 public String toMetadataPath( ProjectReference reference )
535 StringBuilder path = new StringBuilder();
537 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
538 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
539 path.append( MAVEN_METADATA );
541 return path.toString();
544 public String toMetadataPath( VersionedReference reference )
546 StringBuilder path = new StringBuilder();
548 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
549 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
550 if ( reference.getVersion() != null )
552 // add the version only if it is present
553 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
555 path.append( MAVEN_METADATA );
557 return path.toString();
561 public String toPath( ArtifactReference reference )
567 public String toPath( ItemSelector selector )
573 public ItemSelector toItemSelector( String path ) throws LayoutException
579 public String toPath( ArchivaArtifact reference )