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.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.ArchivaArtifact;
38 import org.apache.archiva.repository.content.base.ArchivaContentItem;
39 import org.apache.archiva.repository.content.base.ArchivaDataItem;
40 import org.apache.archiva.repository.content.base.ArchivaNamespace;
41 import org.apache.archiva.repository.content.base.ArchivaProject;
42 import org.apache.archiva.repository.content.base.ArchivaVersion;
43 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
44 import org.apache.archiva.repository.storage.RepositoryStorage;
45 import org.apache.archiva.repository.storage.StorageAsset;
46 import org.apache.commons.lang3.StringUtils;
47 import org.springframework.stereotype.Service;
49 import java.io.IOException;
50 import java.nio.file.Path;
51 import java.nio.file.Paths;
52 import java.util.HashMap;
53 import java.util.List;
55 import java.util.function.Consumer;
56 import java.util.regex.Matcher;
57 import java.util.regex.Pattern;
58 import java.util.stream.Stream;
61 * @author Martin Stockhammer <martin_s@apache.org>
63 @Service("managedRepositoryContent#mock")
64 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
66 private static final String PATH_SEPARATOR = "/";
67 private static final String GROUP_SEPARATOR = ".";
68 public static final String MAVEN_METADATA = "maven-metadata.xml";
71 private ManagedRepository repository;
72 private RepositoryStorage fsStorage;
74 ManagedRepositoryContentMock(ManagedRepository repo) {
75 this.repository = repo;
76 this.fsStorage = repo;
80 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
82 if (clazz.isAssignableFrom( Version.class ))
84 if ( !item.hasCharacteristic( Version.class ) )
86 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
88 return (T) item.adapt( Version.class );
89 } else if ( clazz.isAssignableFrom( Project.class )) {
90 if ( !item.hasCharacteristic( Project.class ) )
92 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
94 return (T) item.adapt( Project.class );
95 } else if ( clazz.isAssignableFrom( Namespace.class )) {
96 if ( !item.hasCharacteristic( Namespace.class ) )
98 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
100 return (T) item.adapt( Namespace.class );
102 throw new LayoutException( "Could not convert item to class " + clazz);
105 private Version createVersionFromPath( StorageAsset asset )
107 Project proj = createProjectFromPath( asset.getParent( ) );
108 return ArchivaVersion.withRepository( this ).withAsset( asset )
109 .withProject( proj ).withVersion( asset.getName( ) ).build();
112 private Project createProjectFromPath( StorageAsset asset) {
113 Namespace ns = createNamespaceFromPath( asset );
114 return ArchivaProject.withRepository( this ).withAsset( asset )
115 .withNamespace( ns ).withId( asset.getName( ) ).build( );
118 private Namespace createNamespaceFromPath( StorageAsset asset) {
119 String namespace = asset.getPath( ).replace( "/", "." );
120 return ArchivaNamespace.withRepository( this )
121 .withAsset( asset ).withNamespace( namespace ).build();
125 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
131 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
137 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
143 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
149 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
155 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
161 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
163 StringBuilder path = new StringBuilder(selector.getNamespace( ).replace( ".", "/" ));
164 path.append( "/" ).append( selector.getProjectId( ) ).append( "/" ).append( selector.getVersion( ) );
165 path.append( "/" ).append( selector.getArtifactId( ) ).append( "-" ).append( selector.getArtifactVersion( ) ).append( "." ).append( selector.getType( ) );
166 StorageAsset asset = fsStorage.getAsset( path.toString( ) );
167 ArchivaNamespace ns = ArchivaNamespace.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ).getParent( ) ).withNamespace( selector.getNamespace( ) ).build( );
168 ArchivaProject project = ArchivaProject.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ).getParent( ) ).withNamespace( ns ).withId( selector.getProjectId( ) ).build( );
169 ArchivaVersion version = ArchivaVersion.withRepository( repository.getContent( ) ).withAsset( asset.getParent( ) ).withProject( project ).withVersion( selector.getVersion( ) ).build( );
170 ArchivaArtifact artifact = ArchivaArtifact.withAsset( asset ).withVersion( version ).withId( selector.getArtifactId( ) ).withArtifactVersion( selector.getArtifactVersion( ) ).withType( selector.getType( ) ).build( );
175 public Artifact getArtifact( String path ) throws LayoutException, ContentAccessException
181 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
187 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
193 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
199 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
205 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
211 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
217 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
223 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
229 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
235 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
241 public boolean hasContent( ItemSelector selector )
247 public ContentItem getParent( ContentItem item )
251 return toItem( item.getAsset( ).getParent( ) );
253 catch ( LayoutException e )
255 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
260 public List<? extends ContentItem> getChildren( ContentItem item )
266 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
272 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
278 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
284 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
290 public DataItem getMetadataItem( Version version )
292 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
293 .withDataType( BaseDataItemTypes.METADATA ).build();
297 public DataItem getMetadataItem( Project project )
299 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
300 .withDataType( BaseDataItemTypes.METADATA ).build( );
304 public ContentItem toItem( String path ) throws LayoutException
306 StorageAsset asset = repository.getRoot().resolve( path );
307 return toItem( asset );
311 public ContentItem toItem( StorageAsset asset ) throws LayoutException
313 if (asset.isLeaf()) {
314 return ArchivaDataItem.withAsset( asset ).withId( asset.getName( ) ).build();
317 return ArchivaContentItem.withRepository( this )
324 public String toPath( ContentItem item )
326 return item.getAsset( ).getPath( );
330 public String getId( )
332 return repository.getId();
335 private StorageAsset getRepoRootAsset() {
336 if (fsStorage==null) {
338 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
339 } catch (IOException e) {
343 return fsStorage.getRoot();
347 public ManagedRepository getRepository( )
353 public void setRepository( ManagedRepository repo )
355 this.repository = repo;
358 private Map<ArtifactReference, String> refs = new HashMap<>();
361 public ArtifactReference toArtifactReference( String path ) throws LayoutException
363 if ( StringUtils.isBlank( path ) )
365 throw new LayoutException( "Unable to convert blank path." );
368 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
370 ArtifactReference artifact = new ArtifactReference();
371 artifact.setGroupId( metadata.getNamespace() );
372 artifact.setArtifactId( metadata.getProject() );
373 artifact.setVersion( metadata.getVersion() );
374 artifact.setProjectVersion( metadata.getProjectVersion( ) );
375 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
378 artifact.setClassifier( facet.getClassifier() );
379 artifact.setType( facet.getType() );
381 refs.put(artifact, path);
385 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
387 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
389 int len = parts.length;
392 throw new IllegalArgumentException(
393 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
396 String id = parts[--len];
397 String baseVersion = parts[--len];
398 String artifactId = parts[--len];
399 StringBuilder groupIdBuilder = new StringBuilder();
400 for ( int i = 0; i < len - 1; i++ )
402 groupIdBuilder.append( parts[i] );
403 groupIdBuilder.append( '.' );
405 groupIdBuilder.append( parts[len - 1] );
407 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
410 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
414 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
417 if ( !id.startsWith( projectId + "-" ) )
419 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
420 + "' doesn't start with artifact ID '" + projectId + "'" );
423 MavenArtifactFacet facet = new MavenArtifactFacet();
425 int index = projectId.length() + 1;
427 String idSubStrFromVersion = id.substring( index );
428 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
430 // non-snapshot versions, or non-timestamped snapshot versions
431 version = projectVersion;
433 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
435 // timestamped snapshots
438 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
439 if ( mainVersionLength == 0 )
441 throw new IllegalArgumentException(
442 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
445 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
447 String timestamp = m.group( 1 );
448 String buildNumber = m.group( 2 );
449 facet.setTimestamp( timestamp );
450 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
451 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
453 catch ( IllegalStateException e )
455 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
456 + "' doesn't contain a timestamped version matching snapshot '"
457 + projectVersion + "'", e);
463 throw new IllegalArgumentException(
464 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
465 + projectVersion + "'" );
470 index += version.length();
471 if ( index == id.length() )
473 // no classifier or extension
479 char c = id.charAt( index );
482 // classifier up until '.'
483 int extIndex = id.indexOf( '.', index );
486 classifier = id.substring( index + 1, extIndex );
487 ext = id.substring( extIndex + 1 );
491 classifier = id.substring( index + 1 );
497 // rest is the extension
499 ext = id.substring( index + 1 );
503 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
504 + "' expected classifier or extension but got '"
505 + id.substring( index ) + "'" );
509 ArtifactMetadata metadata = new ArtifactMetadata();
510 metadata.setId( id );
511 metadata.setNamespace( namespace );
512 metadata.setProject( projectId );
513 metadata.setRepositoryId( repoId );
514 metadata.setProjectVersion( projectVersion );
515 metadata.setVersion( version );
517 facet.setClassifier( classifier );
519 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
520 // to select the correct order to apply multiple extensions mappings to a preferred type
521 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
522 // perhaps the plugins could register missing entries in configuration, then we just use configuration
528 // use extension as default
534 // TODO: should we allow this instead?
537 throw new IllegalArgumentException(
538 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
541 facet.setType( type );
542 metadata.addFacet( facet );
548 private String formatAsDirectory( String directory )
550 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
554 public String toPath( ArtifactReference reference )
560 public String toPath( ItemSelector selector )
566 public ItemSelector toItemSelector( String path ) throws LayoutException
572 public ManagedRepositoryContent getGenericContent( )