Switching to itemselector for repository API

This commit is contained in:
Martin Stockhammer 2020-06-16 10:32:06 +02:00
parent fbca0e89f9
commit 7a9803bf0f
10 changed files with 33 additions and 96 deletions

View File

@ -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

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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 )
{

View File

@ -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" );
}

View File

@ -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
{

View File

@ -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() )
{