1 package org.apache.archiva.repository.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.maven.metadata.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.ArchivaArtifact;
43 import org.apache.archiva.repository.content.base.ArchivaContentItem;
44 import org.apache.archiva.repository.content.base.ArchivaDataItem;
45 import org.apache.archiva.repository.content.base.ArchivaNamespace;
46 import org.apache.archiva.repository.content.base.ArchivaProject;
47 import org.apache.archiva.repository.content.base.ArchivaVersion;
48 import org.apache.archiva.repository.storage.RepositoryStorage;
49 import org.apache.archiva.repository.storage.StorageAsset;
50 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
51 import org.springframework.stereotype.Service;
53 import java.io.IOException;
54 import java.nio.file.Path;
55 import java.nio.file.Paths;
56 import java.util.List;
57 import java.util.function.Consumer;
58 import java.util.regex.Matcher;
59 import java.util.regex.Pattern;
60 import java.util.stream.Stream;
63 * @author Martin Stockhammer <martin_s@apache.org>
65 @Service("managedRepositoryContent#mock")
66 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
68 private static final String PATH_SEPARATOR = "/";
69 private static final String GROUP_SEPARATOR = ".";
70 public static final String MAVEN_METADATA = "maven-metadata.xml";
73 private ManagedRepository repository;
74 private RepositoryStorage fsStorage;
76 ManagedRepositoryContentMock(ManagedRepository repo) {
77 this.repository = repo;
78 this.fsStorage = repo;
82 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
84 if (clazz.isAssignableFrom( Version.class ))
86 if ( !item.hasCharacteristic( Version.class ) )
88 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
90 return (T) item.adapt( Version.class );
91 } else if ( clazz.isAssignableFrom( Project.class )) {
92 if ( !item.hasCharacteristic( Project.class ) )
94 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
96 return (T) item.adapt( Project.class );
97 } else if ( clazz.isAssignableFrom( Namespace.class )) {
98 if ( !item.hasCharacteristic( Namespace.class ) )
100 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
102 return (T) item.adapt( Namespace.class );
104 throw new LayoutException( "Could not convert item to class " + clazz);
107 private Version createVersionFromPath( StorageAsset asset )
109 Project proj = createProjectFromPath( asset.getParent( ) );
110 return ArchivaVersion.withRepository( this ).withAsset( asset )
111 .withProject( proj ).withVersion( asset.getName( ) ).build();
114 private Project createProjectFromPath( StorageAsset asset) {
115 Namespace ns = createNamespaceFromPath( asset );
116 return ArchivaProject.withRepository( this ).withAsset( asset )
117 .withNamespace( ns ).withId( asset.getName( ) ).build( );
120 private Namespace createNamespaceFromPath( StorageAsset asset) {
121 String namespace = asset.getPath( ).replace( "/", "." );
122 return ArchivaNamespace.withRepository( this )
123 .withAsset( asset ).withNamespace( namespace ).build();
127 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
133 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
139 public void copyItem( ContentItem item, ManagedRepository destinationRepository ) throws ItemNotFoundException, ContentAccessException
145 public void copyItem( ContentItem item, ManagedRepository destinationRepository, boolean updateMetadata ) throws ItemNotFoundException, ContentAccessException
151 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
157 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
163 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
169 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
175 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
177 StringBuilder path = new StringBuilder(selector.getNamespace( ).replace( ".", "/" ));
178 path.append( "/" ).append( selector.getProjectId( ) ).append( "/" ).append( selector.getVersion( ) );
179 path.append( "/" ).append( selector.getArtifactId( ) ).append( "-" ).append( selector.getArtifactVersion( ) ).append( "." ).append( selector.getType( ) );
180 StorageAsset asset = fsStorage.getAsset( path.toString( ) );
181 ArchivaNamespace ns = ArchivaNamespace.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ).getParent( ) ).withNamespace( selector.getNamespace( ) ).build( );
182 ArchivaProject project = ArchivaProject.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ) ).withNamespace( ns ).withId( selector.getProjectId( ) ).build( );
183 ArchivaVersion version = ArchivaVersion.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ) ).withProject( project ).withVersion( selector.getVersion( ) ).build( );
184 ArchivaArtifact artifact = ArchivaArtifact.withAsset( asset ).withVersion( version ).withId( selector.getArtifactId( ) ).withArtifactVersion( selector.getArtifactVersion( ) ).withType( selector.getType( ) ).build( );
189 public Artifact getArtifact( String path ) throws LayoutException, ContentAccessException
191 StorageAsset asset = fsStorage.getAsset( path.toString( ) );
192 String artifactId = asset.getName( );
193 StorageAsset namespacePath = asset.getParent( ).getParent( ).getParent( );
194 String namespaceId = namespacePath.getPath( ).replace( "/", "." );
195 StorageAsset projectPath = asset.getParent( ).getParent( );
196 String projectId = projectPath.getName( );
197 StorageAsset versionPath = asset.getParent( );
198 String versionId = versionPath.getName( );
199 ArchivaNamespace ns = ArchivaNamespace.withRepository( repository.getContent( ) ).withAsset( namespacePath ).withNamespace( namespaceId ).build( );
200 ArchivaProject project = ArchivaProject.withRepository( repository.getContent( ) ).withAsset( projectPath ).withNamespace( ns ).withId( projectId ).build( );
201 ArchivaVersion version = ArchivaVersion.withRepository( repository.getContent( ) ).withAsset( versionPath ).withProject( project ).withVersion( versionId ).build( );
202 ArchivaArtifact artifact = ArchivaArtifact.withAsset( asset ).withVersion( version ).withId( projectId ).withArtifactVersion( versionId ).build( );
207 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
213 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
219 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
225 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
231 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
237 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
243 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
249 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
255 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
261 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
267 public boolean hasContent( ItemSelector selector )
273 public ContentItem getParent( ContentItem item )
277 return toItem( item.getAsset( ).getParent( ) );
279 catch ( LayoutException e )
281 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
286 public List<? extends ContentItem> getChildren( ContentItem item )
292 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
298 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
304 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
310 public List<Class<? extends ManagedRepositoryContentLayout>> getSupportedLayouts( )
316 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
322 public DataItem getMetadataItem( Version version )
324 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
325 .withDataType( BaseDataItemTypes.METADATA ).build();
329 public DataItem getMetadataItem( Project project )
331 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
332 .withDataType( BaseDataItemTypes.METADATA ).build( );
336 public ContentItem toItem( String path ) throws LayoutException
338 StorageAsset asset = repository.getRoot().resolve( path );
339 return toItem( asset );
343 public ContentItem toItem( StorageAsset asset ) throws LayoutException
345 if (asset.isLeaf()) {
346 return ArchivaDataItem.withAsset( asset ).withId( asset.getName( ) ).build();
349 return ArchivaContentItem.withRepository( this )
356 public String toPath( ContentItem item )
358 return item.getAsset( ).getPath( );
362 public String getId( )
364 return repository.getId();
367 private StorageAsset getRepoRootAsset() {
368 if (fsStorage==null) {
370 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
371 } catch (IOException e) {
375 return fsStorage.getRoot();
379 public ManagedRepository getRepository( )
385 public void setRepository( ManagedRepository repo )
387 this.repository = repo;
390 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
392 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
394 int len = parts.length;
397 throw new IllegalArgumentException(
398 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
401 String id = parts[--len];
402 String baseVersion = parts[--len];
403 String artifactId = parts[--len];
404 StringBuilder groupIdBuilder = new StringBuilder();
405 for ( int i = 0; i < len - 1; i++ )
407 groupIdBuilder.append( parts[i] );
408 groupIdBuilder.append( '.' );
410 groupIdBuilder.append( parts[len - 1] );
412 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
415 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
419 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
422 if ( !id.startsWith( projectId + "-" ) )
424 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
425 + "' doesn't start with artifact ID '" + projectId + "'" );
428 MavenArtifactFacet facet = new MavenArtifactFacet();
430 int index = projectId.length() + 1;
432 String idSubStrFromVersion = id.substring( index );
433 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
435 // non-snapshot versions, or non-timestamped snapshot versions
436 version = projectVersion;
438 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
440 // timestamped snapshots
443 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
444 if ( mainVersionLength == 0 )
446 throw new IllegalArgumentException(
447 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
450 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
452 String timestamp = m.group( 1 );
453 String buildNumber = m.group( 2 );
454 facet.setTimestamp( timestamp );
455 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
456 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
458 catch ( IllegalStateException e )
460 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
461 + "' doesn't contain a timestamped version matching snapshot '"
462 + projectVersion + "'", e);
468 throw new IllegalArgumentException(
469 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
470 + projectVersion + "'" );
475 index += version.length();
476 if ( index == id.length() )
478 // no classifier or extension
484 char c = id.charAt( index );
487 // classifier up until '.'
488 int extIndex = id.indexOf( '.', index );
491 classifier = id.substring( index + 1, extIndex );
492 ext = id.substring( extIndex + 1 );
496 classifier = id.substring( index + 1 );
502 // rest is the extension
504 ext = id.substring( index + 1 );
508 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
509 + "' expected classifier or extension but got '"
510 + id.substring( index ) + "'" );
514 ArtifactMetadata metadata = new ArtifactMetadata();
515 metadata.setId( id );
516 metadata.setNamespace( namespace );
517 metadata.setProject( projectId );
518 metadata.setRepositoryId( repoId );
519 metadata.setProjectVersion( projectVersion );
520 metadata.setVersion( version );
522 facet.setClassifier( classifier );
524 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
525 // to select the correct order to apply multiple extensions mappings to a preferred type
526 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
527 // perhaps the plugins could register missing entries in configuration, then we just use configuration
533 // use extension as default
539 // TODO: should we allow this instead?
542 throw new IllegalArgumentException(
543 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
546 facet.setType( type );
547 metadata.addFacet( facet );
553 private String formatAsDirectory( String directory )
555 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
559 public String toPath( ItemSelector selector )
565 public ItemSelector toItemSelector( String path ) throws LayoutException
571 public ManagedRepositoryContent getGenericContent( )