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> getArtifactStream( 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 List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse ) throws ContentAccessException
179 public Stream<? extends Artifact> getArtifactStream( ContentItem item ) throws ContentAccessException
185 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse ) throws ContentAccessException
191 public boolean hasContent( ItemSelector selector )
197 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
203 public ContentItem toItem( String path ) throws LayoutException
209 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
215 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
221 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
227 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
233 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
239 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
245 public String getId( )
247 return repository.getId();
251 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
257 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
263 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
269 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
275 public String getRepoRoot( )
277 return getRepoRootAsset().getFilePath().toString();
280 private StorageAsset getRepoRootAsset() {
281 if (fsStorage==null) {
283 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
284 } catch (IOException e) {
288 return fsStorage.getAsset("");
292 public ManagedRepository getRepository( )
298 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
304 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
310 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
316 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
322 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
328 public void setRepository( ManagedRepository repo )
330 this.repository = repo;
334 public StorageAsset toFile( VersionedReference reference )
339 private Map<ArtifactReference, String> refs = new HashMap<>();
342 public ArtifactReference toArtifactReference( String path ) throws LayoutException
344 if ( StringUtils.isBlank( path ) )
346 throw new LayoutException( "Unable to convert blank path." );
349 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
351 ArtifactReference artifact = new ArtifactReference();
352 artifact.setGroupId( metadata.getNamespace() );
353 artifact.setArtifactId( metadata.getProject() );
354 artifact.setVersion( metadata.getVersion() );
355 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
358 artifact.setClassifier( facet.getClassifier() );
359 artifact.setType( facet.getType() );
361 refs.put(artifact, path);
365 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
367 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
369 int len = parts.length;
372 throw new IllegalArgumentException(
373 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
376 String id = parts[--len];
377 String baseVersion = parts[--len];
378 String artifactId = parts[--len];
379 StringBuilder groupIdBuilder = new StringBuilder();
380 for ( int i = 0; i < len - 1; i++ )
382 groupIdBuilder.append( parts[i] );
383 groupIdBuilder.append( '.' );
385 groupIdBuilder.append( parts[len - 1] );
387 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
390 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
394 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
397 if ( !id.startsWith( projectId + "-" ) )
399 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
400 + "' doesn't start with artifact ID '" + projectId + "'" );
403 MavenArtifactFacet facet = new MavenArtifactFacet();
405 int index = projectId.length() + 1;
407 String idSubStrFromVersion = id.substring( index );
408 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
410 // non-snapshot versions, or non-timestamped snapshot versions
411 version = projectVersion;
413 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
415 // timestamped snapshots
418 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
419 if ( mainVersionLength == 0 )
421 throw new IllegalArgumentException(
422 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
425 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
427 String timestamp = m.group( 1 );
428 String buildNumber = m.group( 2 );
429 facet.setTimestamp( timestamp );
430 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
431 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
433 catch ( IllegalStateException e )
435 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
436 + "' doesn't contain a timestamped version matching snapshot '"
437 + projectVersion + "'", e);
443 throw new IllegalArgumentException(
444 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
445 + projectVersion + "'" );
450 index += version.length();
451 if ( index == id.length() )
453 // no classifier or extension
459 char c = id.charAt( index );
462 // classifier up until '.'
463 int extIndex = id.indexOf( '.', index );
466 classifier = id.substring( index + 1, extIndex );
467 ext = id.substring( extIndex + 1 );
471 classifier = id.substring( index + 1 );
477 // rest is the extension
479 ext = id.substring( index + 1 );
483 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
484 + "' expected classifier or extension but got '"
485 + id.substring( index ) + "'" );
489 ArtifactMetadata metadata = new ArtifactMetadata();
490 metadata.setId( id );
491 metadata.setNamespace( namespace );
492 metadata.setProject( projectId );
493 metadata.setRepositoryId( repoId );
494 metadata.setProjectVersion( projectVersion );
495 metadata.setVersion( version );
497 facet.setClassifier( classifier );
499 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
500 // to select the correct order to apply multiple extensions mappings to a preferred type
501 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
502 // perhaps the plugins could register missing entries in configuration, then we just use configuration
508 // use extension as default
514 // TODO: should we allow this instead?
517 throw new IllegalArgumentException(
518 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
521 facet.setType( type );
522 metadata.addFacet( facet );
529 public StorageAsset toFile( ArtifactReference reference )
531 return getRepoRootAsset().resolve(refs.get(reference));
535 public StorageAsset toFile( ArchivaArtifact reference )
540 private String formatAsDirectory( String directory )
542 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
545 public String toMetadataPath( ProjectReference reference )
547 StringBuilder path = new StringBuilder();
549 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
550 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
551 path.append( MAVEN_METADATA );
553 return path.toString();
556 public String toMetadataPath( VersionedReference reference )
558 StringBuilder path = new StringBuilder();
560 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
561 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
562 if ( reference.getVersion() != null )
564 // add the version only if it is present
565 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
567 path.append( MAVEN_METADATA );
569 return path.toString();
573 public String toPath( ArtifactReference reference )
579 public String toPath( ItemSelector selector )
585 public ItemSelector toItemSelector( String path ) throws LayoutException
591 public String toPath( ArchivaArtifact reference )