From 7a9803bf0f648db101ff9e76ef6a0f0bb2ecf09d Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Tue, 16 Jun 2020 10:32:06 +0200 Subject: [PATCH] Switching to itemselector for repository API --- .../archiva/repository/RepositoryContent.java | 9 ----- .../mock/ManagedRepositoryContentMock.java | 7 ---- .../mock/RemoteRepositoryContentMock.java | 7 ---- .../mock/ManagedRepositoryContentMock.java | 7 ---- .../mock/ManagedRepositoryContentMock.java | 7 ---- .../mock/RemoteRepositoryContentMock.java | 15 -------- .../AbstractDefaultRepositoryContent.java | 19 ----------- .../ManagedDefaultRepositoryContent.java | 21 +++++++++--- .../ManagedDefaultRepositoryContentTest.java | 3 +- .../services/DefaultRepositoriesService.java | 34 +++++++++---------- 10 files changed, 33 insertions(+), 96 deletions(-) diff --git a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryContent.java b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryContent.java index c30550eb3..a4e98da9f 100644 --- a/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryContent.java +++ b/archiva-modules/archiva-base/archiva-repository-api/src/main/java/org/apache/archiva/repository/RepositoryContent.java @@ -30,15 +30,6 @@ public interface RepositoryContent { - /** - * Given an {@link ArtifactReference}, return the relative path to the artifact. - * - * @param reference the artifact reference to use. - * @return the relative path to the artifact. - */ - String toPath( ArtifactReference reference ); - - /** * Return the path, that represents the item specified by the selector. * @param selector the selector with the artifact coordinates diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java index ebb043aea..e0ba18035 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java @@ -19,7 +19,6 @@ package org.apache.archiva.repository.mock; * under the License. */ -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.ContentAccessException; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.ItemDeleteStatus; @@ -327,12 +326,6 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout this.repository = repo; } - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - @Override public String toPath( ItemSelector selector ) { diff --git a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java index 3fc507a6f..e9410c460 100644 --- a/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java +++ b/archiva-modules/archiva-base/archiva-repository-layer/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java @@ -19,7 +19,6 @@ package org.apache.archiva.repository.mock; * under the License. */ -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.RepositoryURL; import org.apache.archiva.repository.LayoutException; import org.apache.archiva.repository.RemoteRepository; @@ -59,12 +58,6 @@ public class RemoteRepositoryContentMock implements RemoteRepositoryContent this.repository = repo; } - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - @Override public String toPath( ItemSelector selector ) { diff --git a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java index acc4204ca..ec32d1d24 100644 --- a/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-base/archiva-repository-scanner/src/test/java/org/apache/archiva/repository/scanner/mock/ManagedRepositoryContentMock.java @@ -40,7 +40,6 @@ import org.apache.archiva.repository.content.base.ArchivaProject; import org.apache.archiva.repository.content.base.ArchivaVersion; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.storage.StorageAsset; -import org.apache.commons.lang3.StringUtils; import java.io.IOException; import java.nio.file.Path; @@ -510,12 +509,6 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR ); } - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - @Override public String toPath( ItemSelector selector ) { diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java index 11a7bf06f..bc506b15d 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/ManagedRepositoryContentMock.java @@ -43,7 +43,6 @@ import org.apache.archiva.repository.content.base.ArchivaVersion; import org.apache.archiva.repository.storage.fs.FilesystemStorage; import org.apache.archiva.repository.storage.RepositoryStorage; import org.apache.archiva.repository.storage.StorageAsset; -import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import java.io.IOException; @@ -543,12 +542,6 @@ public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR ); } - @Override - public String toPath( ArtifactReference reference ) - { - return null; - } - @Override public String toPath( ItemSelector selector ) { diff --git a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java index 9312aaa68..6e51de0c0 100644 --- a/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java +++ b/archiva-modules/archiva-maven/archiva-maven-proxy/src/test/java/org/apache/archiva/repository/mock/RemoteRepositoryContentMock.java @@ -20,7 +20,6 @@ package org.apache.archiva.repository.mock; */ import org.apache.archiva.common.utils.VersionUtil; -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.RepositoryURL; import org.apache.archiva.repository.LayoutException; import org.apache.archiva.repository.RemoteRepository; @@ -65,20 +64,6 @@ public class RemoteRepositoryContentMock implements RemoteRepositoryContent this.repository = repo; } - @Override - public String toPath( ArtifactReference reference ) - { - String baseVersion; - if (VersionUtil.isSnapshot(reference.getVersion())) { - baseVersion=VersionUtil.getBaseVersion(reference.getVersion()); - } else { - baseVersion=reference.getVersion(); - } - return reference.getGroupId().replaceAll("\\.", "/")+"/"+reference.getArtifactId()+"/"+baseVersion+"/" - +reference.getArtifactId()+"-"+reference.getVersion()+( - StringUtils.isNotEmpty(reference.getClassifier()) ? "-"+reference.getClassifier() : "")+"."+reference.getType(); - } - @Override public String toPath( ItemSelector selector ) { diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContent.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContent.java index 01949dea0..be7ae29ba 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContent.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/AbstractDefaultRepositoryContent.java @@ -22,7 +22,6 @@ import org.apache.archiva.common.utils.VersionUtil; import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator; import org.apache.archiva.repository.content.base.ArchivaItemSelector; import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider; -import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.repository.LayoutException; import org.apache.archiva.repository.RepositoryContent; import org.apache.archiva.repository.content.ItemSelector; @@ -310,24 +309,6 @@ public abstract class AbstractDefaultRepositoryContent implements RepositoryCont return formatAsDirectory( namespace ); } - @Override - public String toPath( ArtifactReference reference ) - { - if ( reference == null ) - { - throw new IllegalArgumentException( "Artifact reference cannot be null" ); - } - if ( reference.getVersion() != null ) - { - String baseVersion = VersionUtil.getBaseVersion( reference.getVersion() ); - return toPath( reference.getGroupId(), reference.getArtifactId(), baseVersion, reference.getVersion(), - reference.getClassifier(), reference.getType() ); - } - return toPath( reference.getGroupId(), reference.getArtifactId(), null, null, - reference.getClassifier(), reference.getType() ); - } - - protected String formatAsDirectory( String directory ) { diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java index cfc7354c8..07affa7c4 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/main/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContent.java @@ -796,17 +796,28 @@ public class ManagedDefaultRepositoryContent } @Override - public Artifact getArtifact( final ItemSelector selector ) throws ContentAccessException + public Artifact getArtifact( final ItemSelector selectorArg ) throws ContentAccessException { - if ( !selector.hasProjectId( ) ) + ItemSelector selector = selectorArg; + if ( !selectorArg.hasProjectId( ) ) { throw new IllegalArgumentException( "Project id must be set" ); } - if ( !selector.hasVersion( ) ) + if ( !selectorArg.hasVersion( ) ) { - throw new IllegalArgumentException( "Version must be set" ); + if (selectorArg.hasArtifactVersion() && VersionUtil.isSnapshot( selectorArg.getArtifactVersion() )) { + selector = ArchivaItemSelector.builder( ).withSelector( selectorArg ) + .withVersion( VersionUtil.getBaseVersion( selectorArg.getArtifactVersion( ) ) ).build(); + } else if (selectorArg.hasArtifactVersion()) { + selector = ArchivaItemSelector.builder( ).withSelector( selectorArg ) + .withVersion( selectorArg.getArtifactVersion( ) ).build(); + + } else + { + throw new IllegalArgumentException( "Version must be set" ); + } } - if ( !selector.hasArtifactId( ) ) + if ( !selectorArg.hasArtifactId( ) ) { throw new IllegalArgumentException( "Artifact id must be set" ); } diff --git a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java index 3344df005..279a26c5d 100644 --- a/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java +++ b/archiva-modules/archiva-maven/archiva-maven-repository/src/test/java/org/apache/archiva/repository/maven/content/ManagedDefaultRepositoryContentTest.java @@ -154,7 +154,7 @@ public class ManagedDefaultRepositoryContentTest { try { - ArtifactReference reference = null; + ItemSelector reference = null; repoContent.toPath( reference ); fail( "Should have failed due to null artifact reference." ); } @@ -297,7 +297,6 @@ public class ManagedDefaultRepositoryContentTest .withNamespace( "org.apache.maven" ) .withProjectId( "shared" ) .withArtifactId( "shared" ) - .withArtifactVersion("1.0") .build(); try { diff --git a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java index 60467bcc7..bf10591f1 100644 --- a/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java +++ b/archiva-modules/archiva-web/archiva-rest/archiva-rest-services/src/main/java/org/apache/archiva/rest/services/DefaultRepositoriesService.java @@ -54,6 +54,7 @@ import org.apache.archiva.repository.RepositoryRegistry; import org.apache.archiva.repository.RepositoryType; import org.apache.archiva.repository.content.ContentItem; import org.apache.archiva.repository.content.ItemNotFoundException; +import org.apache.archiva.repository.content.ItemSelector; import org.apache.archiva.repository.content.Version; import org.apache.archiva.repository.content.base.ArchivaItemSelector; import org.apache.archiva.repository.storage.fs.FsStorageUtil; @@ -361,13 +362,16 @@ public class DefaultRepositoriesService // sounds good we can continue ! - ArtifactReference artifactReference = new ArtifactReference(); - artifactReference.setArtifactId( artifactTransferRequest.getArtifactId() ); - artifactReference.setGroupId( artifactTransferRequest.getGroupId() ); - artifactReference.setVersion( artifactTransferRequest.getVersion() ); - artifactReference.setClassifier( artifactTransferRequest.getClassifier() ); String packaging = StringUtils.trim( artifactTransferRequest.getPackaging() ); - artifactReference.setType( StringUtils.isEmpty( packaging ) ? "jar" : packaging ); + ItemSelector selector = ArchivaItemSelector.builder( ) + .withProjectId( artifactTransferRequest.getArtifactId( ) ) + .withArtifactId( artifactTransferRequest.getArtifactId( ) ) + .withNamespace( artifactTransferRequest.getGroupId( ) ) + .withArtifactVersion( artifactTransferRequest.getVersion( ) ) + .withClassifier( artifactTransferRequest.getClassifier( ) ) + .withExtension( StringUtils.isEmpty( packaging ) ? "jar" : packaging ) + .build( ); + try { @@ -375,28 +379,22 @@ public class DefaultRepositoriesService ManagedRepositoryContent sourceRepository = getManagedRepositoryContent( artifactTransferRequest.getRepositoryId() ); BaseRepositoryContentLayout layout = sourceRepository.getLayout( BaseRepositoryContentLayout.class ); - String artifactSourcePath = sourceRepository.toPath( artifactReference ); + // String artifactSourcePath = sourceRepository.toPath( selector ); + org.apache.archiva.repository.content.Artifact sourceArtifact = layout.getArtifact( selector ); - if ( StringUtils.isEmpty( artifactSourcePath ) ) + if ( !sourceArtifact.exists() ) { log.error( "cannot find artifact {}", artifactTransferRequest ); throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(), null ); } - StorageAsset artifactFile = source.getAsset( artifactSourcePath ); - - if ( !artifactFile.exists() ) - { - log.error( "cannot find artifact {}", artifactTransferRequest ); - throw new ArchivaRestServiceException( "cannot find artifact " + artifactTransferRequest.toString(), - null ); - } + StorageAsset artifactFile = sourceArtifact.getAsset( ); ManagedRepositoryContent targetRepository = getManagedRepositoryContent( artifactTransferRequest.getTargetRepositoryId() ); - String artifactPath = sourceRepository.toPath( artifactReference ); + String artifactPath = artifactFile.getPath( ); int lastIndex = artifactPath.lastIndexOf( '/' ); @@ -444,7 +442,7 @@ public class DefaultRepositoriesService pomFilename = FilenameUtils.removeExtension( pomFilename ) + ".pom"; StorageAsset pomFile = source.getAsset( - artifactSourcePath.substring( 0, artifactPath.lastIndexOf( '/' ) )+"/"+ pomFilename ); + artifactPath.substring( 0, artifactPath.lastIndexOf( '/' ) )+"/"+ pomFilename ); if ( pomFile != null && pomFile.exists() ) { -- 2.39.5