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.maven.model.MavenArtifactFacet;
25 import org.apache.archiva.metadata.model.ArtifactMetadata;
26 import org.apache.archiva.repository.content.BaseRepositoryContentLayout;
27 import org.apache.archiva.repository.content.ContentAccessException;
28 import org.apache.archiva.repository.ItemDeleteStatus;
29 import org.apache.archiva.repository.content.LayoutException;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.content.ManagedRepositoryContentLayout;
33 import org.apache.archiva.repository.content.Artifact;
34 import org.apache.archiva.repository.content.BaseDataItemTypes;
35 import org.apache.archiva.repository.content.ContentItem;
36 import org.apache.archiva.repository.content.DataItem;
37 import org.apache.archiva.repository.content.ItemNotFoundException;
38 import org.apache.archiva.repository.content.ItemSelector;
39 import org.apache.archiva.repository.content.Namespace;
40 import org.apache.archiva.repository.content.Project;
41 import org.apache.archiva.repository.content.Version;
42 import org.apache.archiva.repository.content.base.ArchivaDataItem;
43 import org.apache.archiva.repository.content.base.ArchivaNamespace;
44 import org.apache.archiva.repository.content.base.ArchivaProject;
45 import org.apache.archiva.repository.content.base.ArchivaVersion;
46 import org.apache.archiva.repository.storage.StorageAsset;
47 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
49 import java.io.IOException;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.List;
53 import java.util.function.Consumer;
54 import java.util.regex.Matcher;
55 import java.util.regex.Pattern;
56 import java.util.stream.Stream;
59 * @author Martin Stockhammer <martin_s@apache.org>
61 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
63 private static final String PATH_SEPARATOR = "/";
64 private static final String GROUP_SEPARATOR = ".";
65 public static final String MAVEN_METADATA = "maven-metadata.xml";
68 private ManagedRepository repository;
69 private FilesystemStorage fsStorage;
71 public ManagedRepositoryContentMock(ManagedRepository repo) {
72 this.repository = repo;
76 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
78 if (clazz.isAssignableFrom( Version.class ))
80 if ( !item.hasCharacteristic( Version.class ) )
82 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
84 return (T) item.adapt( Version.class );
85 } else if ( clazz.isAssignableFrom( Project.class )) {
86 if ( !item.hasCharacteristic( Project.class ) )
88 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
90 return (T) item.adapt( Project.class );
91 } else if ( clazz.isAssignableFrom( Namespace.class )) {
92 if ( !item.hasCharacteristic( Namespace.class ) )
94 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
96 return (T) item.adapt( Namespace.class );
98 throw new LayoutException( "Could not convert item to class " + clazz);
101 private Version createVersionFromPath( StorageAsset asset )
103 Project proj = createProjectFromPath( asset.getParent( ) );
104 return ArchivaVersion.withRepository( this ).withAsset( asset )
105 .withProject( proj ).withVersion( asset.getName( ) ).build();
108 private Project createProjectFromPath( StorageAsset asset) {
109 Namespace ns = createNamespaceFromPath( asset );
110 return ArchivaProject.withRepository( this ).withAsset( asset )
111 .withNamespace( ns ).withId( asset.getName( ) ).build( );
114 private Namespace createNamespaceFromPath( StorageAsset asset) {
115 String namespace = asset.getPath( ).replace( "/", "." );
116 return ArchivaNamespace.withRepository( this )
117 .withAsset( asset ).withNamespace( namespace ).build();
122 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
128 public void deleteItem( ContentItem item ) throws ContentAccessException
134 public void copyItem( ContentItem item, ManagedRepository destinationRepository ) throws ContentAccessException
140 public void copyItem( ContentItem item, ManagedRepository destinationRepository, boolean updateMetadata ) throws ContentAccessException
146 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
152 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
158 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
164 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
170 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
176 public Artifact getArtifact( String path ) throws ContentAccessException
182 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
188 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
194 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
200 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
206 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
212 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
218 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
224 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
230 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
236 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
242 public boolean hasContent( ItemSelector selector )
248 public ContentItem getParent( ContentItem item )
252 return toItem( item.getAsset( ).getParent( ) );
254 catch ( LayoutException e )
256 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
261 public List<? extends ContentItem> getChildren( ContentItem item )
267 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item )
273 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz )
279 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
285 public List<Class<? extends ManagedRepositoryContentLayout>> getSupportedLayouts( )
291 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
297 public DataItem getMetadataItem( Version version )
303 public DataItem getMetadataItem( Project project )
305 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
306 .withDataType( BaseDataItemTypes.METADATA ).build( );
311 public ContentItem toItem( String path )
317 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
323 public String toPath( ContentItem item )
329 public String getId( )
331 return repository.getId();
334 private StorageAsset getRepoRootAsset() {
335 if (fsStorage==null) {
337 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
338 } catch (IOException e) {
342 return fsStorage.getRoot();
346 public ManagedRepository getRepository( )
352 public void setRepository( ManagedRepository repo )
354 this.repository = repo;
357 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
359 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
361 int len = parts.length;
364 throw new IllegalArgumentException(
365 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
368 String id = parts[--len];
369 String baseVersion = parts[--len];
370 String artifactId = parts[--len];
371 StringBuilder groupIdBuilder = new StringBuilder();
372 for ( int i = 0; i < len - 1; i++ )
374 groupIdBuilder.append( parts[i] );
375 groupIdBuilder.append( '.' );
377 groupIdBuilder.append( parts[len - 1] );
379 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
382 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
386 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
389 if ( !id.startsWith( projectId + "-" ) )
391 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
392 + "' doesn't start with artifact ID '" + projectId + "'" );
395 MavenArtifactFacet facet = new MavenArtifactFacet();
397 int index = projectId.length() + 1;
399 String idSubStrFromVersion = id.substring( index );
400 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
402 // non-snapshot versions, or non-timestamped snapshot versions
403 version = projectVersion;
405 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
407 // timestamped snapshots
410 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
411 if ( mainVersionLength == 0 )
413 throw new IllegalArgumentException(
414 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
417 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
419 String timestamp = m.group( 1 );
420 String buildNumber = m.group( 2 );
421 facet.setTimestamp( timestamp );
422 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
423 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
425 catch ( IllegalStateException e )
427 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
428 + "' doesn't contain a timestamped version matching snapshot '"
429 + projectVersion + "'", e);
435 throw new IllegalArgumentException(
436 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
437 + projectVersion + "'" );
442 index += version.length();
443 if ( index == id.length() )
445 // no classifier or extension
451 char c = id.charAt( index );
454 // classifier up until '.'
455 int extIndex = id.indexOf( '.', index );
458 classifier = id.substring( index + 1, extIndex );
459 ext = id.substring( extIndex + 1 );
463 classifier = id.substring( index + 1 );
469 // rest is the extension
471 ext = id.substring( index + 1 );
475 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
476 + "' expected classifier or extension but got '"
477 + id.substring( index ) + "'" );
481 ArtifactMetadata metadata = new ArtifactMetadata();
482 metadata.setId( id );
483 metadata.setNamespace( namespace );
484 metadata.setProject( projectId );
485 metadata.setRepositoryId( repoId );
486 metadata.setProjectVersion( projectVersion );
487 metadata.setVersion( version );
489 facet.setClassifier( classifier );
491 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
492 // to select the correct order to apply multiple extensions mappings to a preferred type
493 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
494 // perhaps the plugins could register missing entries in configuration, then we just use configuration
500 // use extension as default
506 // TODO: should we allow this instead?
509 throw new IllegalArgumentException(
510 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
513 facet.setType( type );
514 metadata.addFacet( facet );
520 private String formatAsDirectory( String directory )
522 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
526 public String toPath( ItemSelector selector )
532 public ItemSelector toItemSelector( String path ) throws LayoutException
538 public ManagedRepositoryContent getGenericContent( )