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.ArtifactReference;
27 import org.apache.archiva.repository.*;
28 import org.apache.archiva.repository.content.Artifact;
29 import org.apache.archiva.repository.content.BaseDataItemTypes;
30 import org.apache.archiva.repository.content.ContentItem;
31 import org.apache.archiva.repository.content.DataItem;
32 import org.apache.archiva.repository.content.ItemNotFoundException;
33 import org.apache.archiva.repository.content.ItemSelector;
34 import org.apache.archiva.repository.content.Namespace;
35 import org.apache.archiva.repository.content.Project;
36 import org.apache.archiva.repository.content.Version;
37 import org.apache.archiva.repository.content.base.ArchivaDataItem;
38 import org.apache.archiva.repository.content.base.ArchivaNamespace;
39 import org.apache.archiva.repository.content.base.ArchivaProject;
40 import org.apache.archiva.repository.content.base.ArchivaVersion;
41 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
42 import org.apache.archiva.repository.storage.StorageAsset;
43 import org.apache.commons.lang3.StringUtils;
45 import java.io.IOException;
46 import java.nio.file.Path;
47 import java.nio.file.Paths;
48 import java.util.HashMap;
49 import java.util.List;
51 import java.util.function.Consumer;
52 import java.util.regex.Matcher;
53 import java.util.regex.Pattern;
54 import java.util.stream.Stream;
57 * @author Martin Stockhammer <martin_s@apache.org>
59 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
61 private static final String PATH_SEPARATOR = "/";
62 private static final String GROUP_SEPARATOR = ".";
63 public static final String MAVEN_METADATA = "maven-metadata.xml";
66 private ManagedRepository repository;
67 private FilesystemStorage fsStorage;
69 public ManagedRepositoryContentMock(ManagedRepository repo) {
70 this.repository = repo;
74 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
76 if (clazz.isAssignableFrom( Version.class ))
78 if ( !item.hasCharacteristic( Version.class ) )
80 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
82 return (T) item.adapt( Version.class );
83 } else if ( clazz.isAssignableFrom( Project.class )) {
84 if ( !item.hasCharacteristic( Project.class ) )
86 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
88 return (T) item.adapt( Project.class );
89 } else if ( clazz.isAssignableFrom( Namespace.class )) {
90 if ( !item.hasCharacteristic( Namespace.class ) )
92 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
94 return (T) item.adapt( Namespace.class );
96 throw new LayoutException( "Could not convert item to class " + clazz);
99 private Version createVersionFromPath( StorageAsset asset )
101 Project proj = createProjectFromPath( asset.getParent( ) );
102 return ArchivaVersion.withRepository( this ).withAsset( asset )
103 .withProject( proj ).withVersion( asset.getName( ) ).build();
106 private Project createProjectFromPath( StorageAsset asset) {
107 Namespace ns = createNamespaceFromPath( asset );
108 return ArchivaProject.withRepository( this ).withAsset( asset )
109 .withNamespace( ns ).withId( asset.getName( ) ).build( );
112 private Namespace createNamespaceFromPath( StorageAsset asset) {
113 String namespace = asset.getPath( ).replace( "/", "." );
114 return ArchivaNamespace.withRepository( this )
115 .withAsset( asset ).withNamespace( namespace ).build();
120 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
126 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
132 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
138 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
144 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
150 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
156 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
162 public Artifact getArtifact( String path ) throws LayoutException, ContentAccessException
168 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
174 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
180 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
186 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
192 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
198 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
204 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
210 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
216 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
222 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
228 public boolean hasContent( ItemSelector selector )
234 public ContentItem getParent( ContentItem item )
238 return toItem( item.getAsset( ).getParent( ) );
240 catch ( LayoutException e )
242 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
247 public List<? extends ContentItem> getChildren( ContentItem item )
253 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
259 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
265 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
271 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
277 public DataItem getMetadataItem( Version version )
283 public DataItem getMetadataItem( Project project )
285 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
286 .withDataType( BaseDataItemTypes.METADATA ).build( );
291 public ContentItem toItem( String path ) throws LayoutException
297 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
303 public String toPath( ContentItem item )
309 public String getId( )
311 return repository.getId();
314 private StorageAsset getRepoRootAsset() {
315 if (fsStorage==null) {
317 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
318 } catch (IOException e) {
322 return fsStorage.getRoot();
326 public ManagedRepository getRepository( )
332 public void setRepository( ManagedRepository repo )
334 this.repository = repo;
337 private Map<ArtifactReference, String> refs = new HashMap<>();
339 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
341 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
343 int len = parts.length;
346 throw new IllegalArgumentException(
347 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
350 String id = parts[--len];
351 String baseVersion = parts[--len];
352 String artifactId = parts[--len];
353 StringBuilder groupIdBuilder = new StringBuilder();
354 for ( int i = 0; i < len - 1; i++ )
356 groupIdBuilder.append( parts[i] );
357 groupIdBuilder.append( '.' );
359 groupIdBuilder.append( parts[len - 1] );
361 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
364 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
368 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
371 if ( !id.startsWith( projectId + "-" ) )
373 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
374 + "' doesn't start with artifact ID '" + projectId + "'" );
377 MavenArtifactFacet facet = new MavenArtifactFacet();
379 int index = projectId.length() + 1;
381 String idSubStrFromVersion = id.substring( index );
382 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
384 // non-snapshot versions, or non-timestamped snapshot versions
385 version = projectVersion;
387 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
389 // timestamped snapshots
392 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
393 if ( mainVersionLength == 0 )
395 throw new IllegalArgumentException(
396 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
399 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
401 String timestamp = m.group( 1 );
402 String buildNumber = m.group( 2 );
403 facet.setTimestamp( timestamp );
404 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
405 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
407 catch ( IllegalStateException e )
409 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
410 + "' doesn't contain a timestamped version matching snapshot '"
411 + projectVersion + "'", e);
417 throw new IllegalArgumentException(
418 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
419 + projectVersion + "'" );
424 index += version.length();
425 if ( index == id.length() )
427 // no classifier or extension
433 char c = id.charAt( index );
436 // classifier up until '.'
437 int extIndex = id.indexOf( '.', index );
440 classifier = id.substring( index + 1, extIndex );
441 ext = id.substring( extIndex + 1 );
445 classifier = id.substring( index + 1 );
451 // rest is the extension
453 ext = id.substring( index + 1 );
457 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
458 + "' expected classifier or extension but got '"
459 + id.substring( index ) + "'" );
463 ArtifactMetadata metadata = new ArtifactMetadata();
464 metadata.setId( id );
465 metadata.setNamespace( namespace );
466 metadata.setProject( projectId );
467 metadata.setRepositoryId( repoId );
468 metadata.setProjectVersion( projectVersion );
469 metadata.setVersion( version );
471 facet.setClassifier( classifier );
473 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
474 // to select the correct order to apply multiple extensions mappings to a preferred type
475 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
476 // perhaps the plugins could register missing entries in configuration, then we just use configuration
482 // use extension as default
488 // TODO: should we allow this instead?
491 throw new IllegalArgumentException(
492 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
495 facet.setType( type );
496 metadata.addFacet( facet );
502 private String formatAsDirectory( String directory )
504 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
508 public String toPath( ArtifactReference reference )
514 public String toPath( ItemSelector selector )
520 public ItemSelector toItemSelector( String path ) throws LayoutException
526 public ManagedRepositoryContent getGenericContent( )