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
177 StorageAsset asset = fsStorage.getAsset( path.toString( ) );
178 String artifactId = asset.getName( );
179 StorageAsset namespacePath = asset.getParent( ).getParent( ).getParent( );
180 String namespaceId = namespacePath.getPath( ).replace( "/", "." );
181 StorageAsset projectPath = asset.getParent( ).getParent( );
182 String projectId = projectPath.getName( );
183 StorageAsset versionPath = asset.getParent( );
184 String versionId = versionPath.getName( );
185 ArchivaNamespace ns = ArchivaNamespace.withRepository( repository.getContent( ) ).withAsset( namespacePath ).withNamespace( namespaceId ).build( );
186 ArchivaProject project = ArchivaProject.withRepository( repository.getContent( ) ).withAsset( projectPath ).withNamespace( ns ).withId( projectId ).build( );
187 ArchivaVersion version = ArchivaVersion.withRepository( repository.getContent( ) ).withAsset( versionPath ).withProject( project ).withVersion( versionId ).build( );
188 ArchivaArtifact artifact = ArchivaArtifact.withAsset( asset ).withVersion( version ).withId( projectId ).withArtifactVersion( versionId ).build( );
193 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
199 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
205 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
211 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
217 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
223 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
229 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
235 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
241 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
247 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
253 public boolean hasContent( ItemSelector selector )
259 public ContentItem getParent( ContentItem item )
263 return toItem( item.getAsset( ).getParent( ) );
265 catch ( LayoutException e )
267 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
272 public List<? extends ContentItem> getChildren( ContentItem item )
278 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
284 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
290 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
296 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
302 public DataItem getMetadataItem( Version version )
304 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
305 .withDataType( BaseDataItemTypes.METADATA ).build();
309 public DataItem getMetadataItem( Project project )
311 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
312 .withDataType( BaseDataItemTypes.METADATA ).build( );
316 public ContentItem toItem( String path ) throws LayoutException
318 StorageAsset asset = repository.getRoot().resolve( path );
319 return toItem( asset );
323 public ContentItem toItem( StorageAsset asset ) throws LayoutException
325 if (asset.isLeaf()) {
326 return ArchivaDataItem.withAsset( asset ).withId( asset.getName( ) ).build();
329 return ArchivaContentItem.withRepository( this )
336 public String toPath( ContentItem item )
338 return item.getAsset( ).getPath( );
342 public String getId( )
344 return repository.getId();
347 private StorageAsset getRepoRootAsset() {
348 if (fsStorage==null) {
350 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
351 } catch (IOException e) {
355 return fsStorage.getRoot();
359 public ManagedRepository getRepository( )
365 public void setRepository( ManagedRepository repo )
367 this.repository = repo;
370 private Map<ArtifactReference, String> refs = new HashMap<>();
372 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
374 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
376 int len = parts.length;
379 throw new IllegalArgumentException(
380 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
383 String id = parts[--len];
384 String baseVersion = parts[--len];
385 String artifactId = parts[--len];
386 StringBuilder groupIdBuilder = new StringBuilder();
387 for ( int i = 0; i < len - 1; i++ )
389 groupIdBuilder.append( parts[i] );
390 groupIdBuilder.append( '.' );
392 groupIdBuilder.append( parts[len - 1] );
394 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
397 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
401 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
404 if ( !id.startsWith( projectId + "-" ) )
406 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
407 + "' doesn't start with artifact ID '" + projectId + "'" );
410 MavenArtifactFacet facet = new MavenArtifactFacet();
412 int index = projectId.length() + 1;
414 String idSubStrFromVersion = id.substring( index );
415 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
417 // non-snapshot versions, or non-timestamped snapshot versions
418 version = projectVersion;
420 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
422 // timestamped snapshots
425 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
426 if ( mainVersionLength == 0 )
428 throw new IllegalArgumentException(
429 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
432 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
434 String timestamp = m.group( 1 );
435 String buildNumber = m.group( 2 );
436 facet.setTimestamp( timestamp );
437 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
438 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
440 catch ( IllegalStateException e )
442 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
443 + "' doesn't contain a timestamped version matching snapshot '"
444 + projectVersion + "'", e);
450 throw new IllegalArgumentException(
451 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
452 + projectVersion + "'" );
457 index += version.length();
458 if ( index == id.length() )
460 // no classifier or extension
466 char c = id.charAt( index );
469 // classifier up until '.'
470 int extIndex = id.indexOf( '.', index );
473 classifier = id.substring( index + 1, extIndex );
474 ext = id.substring( extIndex + 1 );
478 classifier = id.substring( index + 1 );
484 // rest is the extension
486 ext = id.substring( index + 1 );
490 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
491 + "' expected classifier or extension but got '"
492 + id.substring( index ) + "'" );
496 ArtifactMetadata metadata = new ArtifactMetadata();
497 metadata.setId( id );
498 metadata.setNamespace( namespace );
499 metadata.setProject( projectId );
500 metadata.setRepositoryId( repoId );
501 metadata.setProjectVersion( projectVersion );
502 metadata.setVersion( version );
504 facet.setClassifier( classifier );
506 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
507 // to select the correct order to apply multiple extensions mappings to a preferred type
508 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
509 // perhaps the plugins could register missing entries in configuration, then we just use configuration
515 // use extension as default
521 // TODO: should we allow this instead?
524 throw new IllegalArgumentException(
525 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
528 facet.setType( type );
529 metadata.addFacet( facet );
535 private String formatAsDirectory( String directory )
537 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
541 public String toPath( ArtifactReference reference )
547 public String toPath( ItemSelector selector )
553 public ItemSelector toItemSelector( String path ) throws LayoutException
559 public ManagedRepositoryContent getGenericContent( )