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.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.*;
30 import org.apache.archiva.repository.content.Artifact;
31 import org.apache.archiva.repository.content.BaseDataItemTypes;
32 import org.apache.archiva.repository.content.ContentItem;
33 import org.apache.archiva.repository.content.DataItem;
34 import org.apache.archiva.repository.content.ItemNotFoundException;
35 import org.apache.archiva.repository.content.ItemSelector;
36 import org.apache.archiva.repository.content.Namespace;
37 import org.apache.archiva.repository.content.Project;
38 import org.apache.archiva.repository.content.Version;
39 import org.apache.archiva.repository.content.base.ArchivaArtifact;
40 import org.apache.archiva.repository.content.base.ArchivaContentItem;
41 import org.apache.archiva.repository.content.base.ArchivaDataItem;
42 import org.apache.archiva.repository.content.base.ArchivaNamespace;
43 import org.apache.archiva.repository.content.base.ArchivaProject;
44 import org.apache.archiva.repository.content.base.ArchivaVersion;
45 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
46 import org.apache.archiva.repository.storage.RepositoryStorage;
47 import org.apache.archiva.repository.storage.StorageAsset;
48 import org.apache.commons.lang3.StringUtils;
49 import org.springframework.stereotype.Service;
51 import java.io.IOException;
52 import java.nio.file.Path;
53 import java.nio.file.Paths;
54 import java.util.HashMap;
55 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 VersionedReference toVersion( String groupId, String artifactId, String version )
88 public VersionedReference toVersion( ArtifactReference artifactReference )
94 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
96 if (clazz.isAssignableFrom( Version.class ))
98 if ( !item.hasCharacteristic( Version.class ) )
100 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
102 return (T) item.adapt( Version.class );
103 } else if ( clazz.isAssignableFrom( Project.class )) {
104 if ( !item.hasCharacteristic( Project.class ) )
106 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
108 return (T) item.adapt( Project.class );
109 } else if ( clazz.isAssignableFrom( Namespace.class )) {
110 if ( !item.hasCharacteristic( Namespace.class ) )
112 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
114 return (T) item.adapt( Namespace.class );
116 throw new LayoutException( "Could not convert item to class " + clazz);
119 private Version createVersionFromPath( StorageAsset asset )
121 Project proj = createProjectFromPath( asset.getParent( ) );
122 return ArchivaVersion.withRepository( this ).withAsset( asset )
123 .withProject( proj ).withVersion( asset.getName( ) ).build();
126 private Project createProjectFromPath( StorageAsset asset) {
127 Namespace ns = createNamespaceFromPath( asset );
128 return ArchivaProject.withRepository( this ).withAsset( asset )
129 .withNamespace( ns ).withId( asset.getName( ) ).build( );
132 private Namespace createNamespaceFromPath( StorageAsset asset) {
133 String namespace = asset.getPath( ).replace( "/", "." );
134 return ArchivaNamespace.withRepository( this )
135 .withAsset( asset ).withNamespace( namespace ).build();
139 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
145 public void deleteItem( ContentItem item ) 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 List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
195 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
201 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
207 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
213 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
219 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
225 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
231 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
237 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
243 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
249 public boolean hasContent( ItemSelector selector )
255 public ContentItem getParent( ContentItem item )
259 return toItem( item.getAsset( ).getParent( ) );
261 catch ( LayoutException e )
263 throw new RuntimeException( "Bad layout conversion " + e.getMessage( ) );
268 public List<? extends ContentItem> getChildren( ContentItem item )
274 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
280 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
286 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
292 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
298 public DataItem getMetadataItem( Version version )
300 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
301 .withDataType( BaseDataItemTypes.METADATA ).build();
305 public DataItem getMetadataItem( Project project )
307 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
308 .withDataType( BaseDataItemTypes.METADATA ).build( );
312 public ContentItem toItem( String path ) throws LayoutException
314 StorageAsset asset = repository.getAsset( "" ).resolve( path );
315 return toItem( asset );
319 public ContentItem toItem( StorageAsset asset ) throws LayoutException
321 if (asset.isLeaf()) {
322 return ArchivaDataItem.withAsset( asset ).withId( asset.getName( ) ).build();
325 return ArchivaContentItem.withRepository( this )
332 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
338 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
344 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
350 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
356 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
362 public String toPath( ContentItem item )
364 return item.getAsset( ).getPath( );
368 public String getId( )
370 return repository.getId();
374 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
380 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
386 public String getRepoRoot( )
388 return getRepoRootAsset().getFilePath().toString();
391 private StorageAsset getRepoRootAsset() {
392 if (fsStorage==null) {
394 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
395 } catch (IOException e) {
399 return fsStorage.getAsset("");
403 public ManagedRepository getRepository( )
409 public void setRepository( ManagedRepository repo )
411 this.repository = repo;
415 public StorageAsset toFile( VersionedReference reference )
420 private Map<ArtifactReference, String> refs = new HashMap<>();
423 public ArtifactReference toArtifactReference( String path ) throws LayoutException
425 if ( StringUtils.isBlank( path ) )
427 throw new LayoutException( "Unable to convert blank path." );
430 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
432 ArtifactReference artifact = new ArtifactReference();
433 artifact.setGroupId( metadata.getNamespace() );
434 artifact.setArtifactId( metadata.getProject() );
435 artifact.setVersion( metadata.getVersion() );
436 artifact.setProjectVersion( metadata.getProjectVersion( ) );
437 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
440 artifact.setClassifier( facet.getClassifier() );
441 artifact.setType( facet.getType() );
443 refs.put(artifact, path);
447 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
449 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
451 int len = parts.length;
454 throw new IllegalArgumentException(
455 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
458 String id = parts[--len];
459 String baseVersion = parts[--len];
460 String artifactId = parts[--len];
461 StringBuilder groupIdBuilder = new StringBuilder();
462 for ( int i = 0; i < len - 1; i++ )
464 groupIdBuilder.append( parts[i] );
465 groupIdBuilder.append( '.' );
467 groupIdBuilder.append( parts[len - 1] );
469 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
472 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
476 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
479 if ( !id.startsWith( projectId + "-" ) )
481 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
482 + "' doesn't start with artifact ID '" + projectId + "'" );
485 MavenArtifactFacet facet = new MavenArtifactFacet();
487 int index = projectId.length() + 1;
489 String idSubStrFromVersion = id.substring( index );
490 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
492 // non-snapshot versions, or non-timestamped snapshot versions
493 version = projectVersion;
495 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
497 // timestamped snapshots
500 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
501 if ( mainVersionLength == 0 )
503 throw new IllegalArgumentException(
504 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
507 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
509 String timestamp = m.group( 1 );
510 String buildNumber = m.group( 2 );
511 facet.setTimestamp( timestamp );
512 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
513 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
515 catch ( IllegalStateException e )
517 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
518 + "' doesn't contain a timestamped version matching snapshot '"
519 + projectVersion + "'", e);
525 throw new IllegalArgumentException(
526 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
527 + projectVersion + "'" );
532 index += version.length();
533 if ( index == id.length() )
535 // no classifier or extension
541 char c = id.charAt( index );
544 // classifier up until '.'
545 int extIndex = id.indexOf( '.', index );
548 classifier = id.substring( index + 1, extIndex );
549 ext = id.substring( extIndex + 1 );
553 classifier = id.substring( index + 1 );
559 // rest is the extension
561 ext = id.substring( index + 1 );
565 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
566 + "' expected classifier or extension but got '"
567 + id.substring( index ) + "'" );
571 ArtifactMetadata metadata = new ArtifactMetadata();
572 metadata.setId( id );
573 metadata.setNamespace( namespace );
574 metadata.setProject( projectId );
575 metadata.setRepositoryId( repoId );
576 metadata.setProjectVersion( projectVersion );
577 metadata.setVersion( version );
579 facet.setClassifier( classifier );
581 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
582 // to select the correct order to apply multiple extensions mappings to a preferred type
583 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
584 // perhaps the plugins could register missing entries in configuration, then we just use configuration
590 // use extension as default
596 // TODO: should we allow this instead?
599 throw new IllegalArgumentException(
600 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
603 facet.setType( type );
604 metadata.addFacet( facet );
611 public StorageAsset toFile( ArtifactReference reference )
613 return getRepoRootAsset().resolve( refs.get(reference));
616 private String formatAsDirectory( String directory )
618 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
622 public String toPath( ArtifactReference reference )
628 public String toPath( ItemSelector selector )
634 public ItemSelector toItemSelector( String path ) throws LayoutException
640 public ManagedRepositoryContent getGenericContent( )