Changing content item API

This commit is contained in:
Martin Stockhammer 2020-05-24 18:18:37 +02:00
parent 56de9e590b
commit ac25c7a86f
21 changed files with 1588 additions and 718 deletions

View File

@ -0,0 +1,141 @@
package org.apache.archiva.repository;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
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.storage.StorageAsset;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
public interface GenericManagedRepositoryContent
{
/**
* Delete all items that match the given selector. The type and number of deleted items
* depend on the specific selector:
* <ul>
* <li>namespace: the complete namespace is deleted (recursively if the recurse flag is set)</li>
* <li>project: the complete project and all contained versions are deleted</li>
* <li>version: the version inside the project is deleted (project is required)</li>
* <li>artifactId: all artifacts that match the id (project and version are required)</li>
* <li>artifactVersion: all artifacts that match the version (project and version are required)</li>
* <li></li>
* </ul>
*
* @param selector the item selector that selects the artifacts to delete
* @param consumer a consumer of the items that will be called after deletion
* @returns the list of items that are deleted
* @throws ContentAccessException if the deletion was not possible or only partly successful, because the access
* to the artifacts failed
* @throws IllegalArgumentException if the selector does not specify valid artifacts to delete
*/
void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException;
/**
* Removes the specified content item and if the item is a container or directory,
* all content stored under the given item.
*
* @param item the item.
* @throws ItemNotFoundException if the item cannot be found
* @throws ContentAccessException if the deletion was not possible or only partly successful, because the access
* to the artifacts failed
*/
void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException;
/**
* Returns a item for the given selector. The type of the returned item depends on the
* selector.
*
* @param selector the item selector
* @return the content item that matches the given selector
* @throws ContentAccessException if an error occured while accessing the backend
* @throws IllegalArgumentException if the selector does not select a valid content item
*/
ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException;
/**
* Returns a stream of items that match the given selector. It may return a stream of mixed types,
* like namespaces, projects, versions and artifacts. It will not select a specific type.
* The selector can specify the '*' pattern for all fields.
* The returned elements will be provided by depth first.
*
* @param selector the item selector that specifies the items
* @return the stream of content items
* @throws ContentAccessException if the access to the underlying storage failed
* @throws IllegalArgumentException if a illegal coordinate combination was provided
*/
Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException;
/**
* Returns the item that matches the given path. The item at the path must not exist.
*
* @param path the path string that points to the item
* @return the content item if the path is a valid item path
* @throws LayoutException if the path is not valid for the repository layout
*/
ContentItem toItem( String path ) throws LayoutException;
/**
* Returns the item that matches the given asset path. The asset must not exist.
*
* @param assetPath the path to the artifact or directory
* @return the item, if it is a valid path for the repository layout
* @throws LayoutException if the path is not valid for the repository
*/
ContentItem toItem( StorageAsset assetPath ) throws LayoutException;
/**
* Returns true, if the selector coordinates point to a existing item in the repository.
*
* @param selector the item selector
* @return <code>true</code>, if there exists such a item, otherwise <code>false</code>
*/
boolean hasContent( ItemSelector selector );
/**
* Returns the parent of the item.
* @param item the current item
* @return the parent item, or <code>null</code> if no such item exists
*/
ContentItem getParent(ContentItem item);
/**
* Returns the list of children items.
* @param item the current item
* @return the list of children, or a empty list, if no children exist
*/
List<? extends ContentItem> getChildren( ContentItem item);
/**
* Tries to apply the given characteristic to the content item. If the layout does not allow this,
* it will throw a <code>LayoutException</code>.
*
* @param clazz the characteristic class to apply
* @param item the content item
* @param <T> The characteristic
* @return the applied characteristic
*/
<T extends ContentItem> T applyCharacteristic(Class<T> clazz, ContentItem item) throws LayoutException;
}

View File

@ -0,0 +1,49 @@
package org.apache.archiva.repository;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
public class ItemConversionException extends RuntimeException
{
public ItemConversionException( )
{
}
public ItemConversionException( String message )
{
super( message );
}
public ItemConversionException( String message, Throwable cause )
{
super( message, cause );
}
public ItemConversionException( Throwable cause )
{
super( cause );
}
public ItemConversionException( String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace )
{
super( message, cause, enableSuppression, writableStackTrace );
}
}

View File

@ -45,54 +45,11 @@ import java.util.stream.Stream;
*
* This interface
*/
public interface ManagedRepositoryContent extends RepositoryContent
public interface ManagedRepositoryContent extends RepositoryContent, GenericManagedRepositoryContent
{
/// ***************** New generation interface **********************
/**
* Delete all items that match the given selector. The type and number of deleted items
* depend on the specific selector:
* <ul>
* <li>namespace: the complete namespace is deleted (recursively if the recurse flag is set)</li>
* <li>project: the complete project and all contained versions are deleted</li>
* <li>version: the version inside the project is deleted (project is required)</li>
* <li>artifactId: all artifacts that match the id (project and version are required)</li>
* <li>artifactVersion: all artifacts that match the version (project and version are required)</li>
* <li></li>
* </ul>
*
* @param selector the item selector that selects the artifacts to delete
* @param consumer a consumer of the items that will be called after deletion
* @returns the list of items that are deleted
* @throws ContentAccessException if the deletion was not possible or only partly successful, because the access
* to the artifacts failed
* @throws IllegalArgumentException if the selector does not specify valid artifacts to delete
*/
void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer) throws ContentAccessException, IllegalArgumentException;
/**
* Removes the specified content item and if the item is a container or directory,
* all content stored under the given item.
*
* @param item the item.
* @throws ItemNotFoundException if the item cannot be found
* @throws ContentAccessException if the deletion was not possible or only partly successful, because the access
* to the artifacts failed
*/
void deleteItem( ContentItem item) throws ItemNotFoundException, ContentAccessException;
/**
* Returns a item for the given selector. The type of the returned item depends on the
* selector.
*
* @param selector the item selector
* @return the content item that matches the given selector
* @throws ContentAccessException if an error occured while accessing the backend
* @throws IllegalArgumentException if the selector does not select a valid content item
*/
ContentItem getItem(ItemSelector selector) throws ContentAccessException, IllegalArgumentException;
/**
* Returns the namespace for the given selected coordinates. The selector must specify a namespace. All other
@ -211,19 +168,6 @@ public interface ManagedRepositoryContent extends RepositoryContent
*/
Stream<? extends Artifact> newArtifactStream( ItemSelector selector) throws ContentAccessException;
/**
* Returns a stream of items that match the given selector. It may return a stream of mixed types,
* like namespaces, projects, versions and artifacts. It will not select a specific type.
* The selector can specify the '*' pattern for all fields.
* The returned elements will be provided by depth first.
*
* @param selector the item selector that specifies the items
* @return the stream of content items
* @throws ContentAccessException if the access to the underlying storage failed
* @throws IllegalArgumentException if a illegal coordinate combination was provided
*/
Stream<? extends ContentItem> newItemStream(ItemSelector selector, boolean parallel) throws ContentAccessException, IllegalArgumentException;
/**
* Return the projects that are part of the given namespace.
@ -264,6 +208,16 @@ public interface ManagedRepositoryContent extends RepositoryContent
*/
List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException;
/**
* Returns all found artifact versions that can be found for the given selector. The selector must specify at least
* a project.
*
* @param selector the item selector that must specify at least a project
* @return the list of artifact versions
* @throws ContentAccessException if the access to the underlying storage failed
* @throws IllegalArgumentException if the selector does not have project information
*/
List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException;
/**
* Return all the artifacts of a given content item (namespace, project, version)
@ -288,14 +242,6 @@ public interface ManagedRepositoryContent extends RepositoryContent
Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException;
/**
* Returns true, if the selector coordinates point to a existing item in the repository.
*
* @param selector the item selector
* @return <code>true</code>, if there exists such a item, otherwise <code>false</code>
*/
boolean hasContent( ItemSelector selector );
/**
* Copies the artifact to the given destination coordinates
*
@ -306,26 +252,6 @@ public interface ManagedRepositoryContent extends RepositoryContent
void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException, ContentAccessException;
/**
* Returns the item that matches the given path. The item at the path must not exist.
*
* @param path the path string that points to the item
* @return the content item if the path is a valid item path
* @throws LayoutException if the path is not valid for the repository layout
*/
ContentItem toItem(String path) throws LayoutException;
/**
* Returns the item that matches the given asset path. The asset must not exist.
*
* @param assetPath the path to the artifact or directory
* @return the item, if it is a valid path for the repository layout
* @throws LayoutException if the path is not valid for the repository
*/
ContentItem toItem(StorageAsset assetPath) throws LayoutException;
/// ***************** End of new generation interface **********************

View File

@ -19,6 +19,7 @@ package org.apache.archiva.repository.content;
* under the License.
*/
import org.apache.archiva.repository.ItemConversionException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.UnsupportedConversionException;
import org.apache.archiva.repository.storage.StorageAsset;
@ -34,7 +35,7 @@ public interface ContentItem
* @param <T> the class or interface
* @return the specific project implementation
*/
<T extends Project> T adapt( Class<T> clazz ) throws UnsupportedConversionException;
<T extends ContentItem> T adapt( Class<T> clazz ) throws ItemConversionException;
/**
* Returns <code>true</code>, if this project supports the given adaptor class.
@ -43,7 +44,9 @@ public interface ContentItem
* @param <T> the type
* @return <code>true/code>, if the implementation is supported, otherwise false
*/
<T extends Project> boolean supports( Class<T> clazz );
<T extends ContentItem> boolean hasCharacteristic( Class<T> clazz );
<T extends ContentItem> void setCharacteristic( Class<T> clazz, T item );
/**
* Additional attributes

View File

@ -28,12 +28,6 @@ package org.apache.archiva.repository.content;
public interface DataItem extends ContentItem
{
/**
* Returns the parent of the data item
* @return the parent item, which is either a Version, Project or Namespace
*/
ContentItem getParent( );
/**
* Returns the identifier of the data item.
* @return the identifier string

View File

@ -22,8 +22,6 @@ package org.apache.archiva.repository.content.base;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.ArtifactType;
import org.apache.archiva.repository.content.BaseArtifactTypes;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.DataItem;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.builder.ArtifactOptBuilder;
import org.apache.archiva.repository.content.base.builder.ArtifactVersionBuilder;
@ -52,7 +50,7 @@ import org.apache.commons.lang3.StringUtils;
*
* @author Martin Stockhammer <martin_s@apache.org>
*/
public class ArchivaArtifact extends ArchivaContentItem implements Artifact
public class ArchivaArtifact extends BaseContentItem implements Artifact
{
private String id;
private String artifactVersion;
@ -135,8 +133,6 @@ public class ArchivaArtifact extends ArchivaContentItem implements Artifact
{
if ( this == o ) return true;
if ( o == null || getClass( ) != o.getClass( ) ) return false;
if ( !super.equals( o ) ) return false;
ArchivaArtifact that = (ArchivaArtifact) o;
if ( !id.equals( that.id ) ) return false;

View File

@ -21,105 +21,39 @@ package org.apache.archiva.repository.content.base;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.Namespace;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.base.builder.OptBuilder;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.builder.ArchivaContentItemOptBuilder;
import org.apache.archiva.repository.content.base.builder.WithAssetBuilder;
import org.apache.archiva.repository.content.base.builder.WithRepositoryBuilder;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Abstract implementation of ContentItem interface.
* <p>
* The attribute map is created, when the first values are put to the map.
*/
public abstract class ArchivaContentItem implements ContentItem
public class ArchivaContentItem extends BaseContentItem implements ContentItem
{
private Map<String, String> attributes;
private ManagedRepositoryContent repository;
private StorageAsset asset;
@Override
public <T extends Project> T adapt( Class<T> clazz )
{
return (T) this;
}
@Override
public <T extends Project> boolean supports( Class<T> clazz )
{
return clazz != null && clazz.isAssignableFrom( this.getClass( ) );
}
/**
* Does lazy initialization of the attributes map.
* Returns a unmodifiable map.
* Creates the builder for creating new archiva project instances.
* You have to set all required attributes before you can call the build() method.
*
* @return unmodifiable map of attributes
* @param storageAsset the asset
* @return a builder instance
*/
@Override
public Map<String, String> getAttributes( )
public static ArchivaContentItemOptBuilder withAsset( StorageAsset storageAsset )
{
if ( this.attributes == null )
{
return Collections.emptyMap( );
}
else
{
return Collections.unmodifiableMap( this.attributes );
}
return new ArchivaContentItemBuilder().withAsset( storageAsset );
}
/**
* Adds a attribute value. The key must not be <code>null</code>.
*
* @param key the attribute key
* @param value the attribute value
* @throws IllegalArgumentException if the key is <code>null</code> or empty
*/
public void putAttribute( String key, String value ) throws IllegalArgumentException
public static WithAssetBuilder<ArchivaContentItemOptBuilder> withRepository( ManagedRepositoryContent repository )
{
if ( this.attributes == null )
{
this.attributes = new HashMap<>( );
}
if ( StringUtils.isEmpty( key ) )
{
throw new IllegalArgumentException( "Key value must not be empty or null" );
}
this.attributes.put( key, value );
return new ArchivaContentItemBuilder().withRepository( repository );
}
@Override
public String getAttribute( String key )
{
if ( this.attributes == null )
{
return null;
}
else
{
return this.attributes.get( key );
}
}
@Override
public ManagedRepositoryContent getRepository( )
{
return repository;
}
@Override
public StorageAsset getAsset( )
{
return asset;
}
@Override
public boolean equals( Object o )
@ -150,86 +84,56 @@ public abstract class ArchivaContentItem implements ContentItem
*/
/**
* Builder for content item. Must be extended by subclasses.
* The builder uses chained interfaces for building the required attributes. That means you have to set
* some certain attributes, before you can build the content item instance via the {@link #build()} method.
* <p>
* Subclasses should extend from this class and provide the interface/class for the destination item,
* a interface for the optional attributes and a interface that is returned after the last required attribute is
* set.
* <p>
* The interface for optional attributes should inherit from {@link OptBuilder}
*
* @param <I> the item class that should be built
* @param <O> the class/interface for the optional attributes
* @param <N> the class/interface for the next (required) attribute after the base attributes are set
*/
protected abstract static class ContentItemBuilder<I extends ArchivaContentItem, O extends OptBuilder<I, O>, N>
implements WithRepositoryBuilder, WithAssetBuilder<N>,
OptBuilder<I, O>
public static final class ArchivaContentItemBuilder extends ContentItemBuilder<ArchivaContentItem, ArchivaContentItemOptBuilder, ArchivaContentItemOptBuilder>
implements ArchivaContentItemOptBuilder
{
protected I item;
protected ContentItemBuilder( I item )
private ArchivaContentItemBuilder( )
{
this.item = item;
super( new ArchivaContentItem() );
}
protected abstract O getOptBuilder( );
protected abstract N getNextBuilder( );
@Override
public WithAssetBuilder<N> withRepository( ManagedRepositoryContent repository )
public ArchivaContentItemOptBuilder getOptBuilder( )
{
if ( repository == null )
{
throw new IllegalArgumentException( "Repository may not be null" );
}
( (ArchivaContentItem) item ).repository = repository;
return this;
}
@Override
public N withAsset( StorageAsset asset )
public ArchivaContentItemOptBuilder getNextBuilder( )
{
if ( asset == null )
{
throw new IllegalArgumentException( "Asset may not be null" );
}
( (ArchivaContentItem) item ).asset = asset;
return getNextBuilder( );
return this;
}
@Override
public ArchivaContentItemOptBuilder withNamespace( Namespace namespace )
{
item.setCharacteristic( Namespace.class, namespace );
return this;
}
@Override
public O withAttribute( String key, String value )
public ArchivaContentItemOptBuilder withProject( Project project )
{
if ( StringUtils.isEmpty( key ) )
{
throw new IllegalArgumentException( "Attribute key may not be null" );
}
item.putAttribute( key, value );
return getOptBuilder( );
}
protected void setRepository( ManagedRepositoryContent repository )
{
( (ArchivaContentItem) item ).repository = repository;
item.setCharacteristic( Project.class, project );
return this;
}
@Override
public I build( )
public ArchivaContentItemOptBuilder withVersion( Version version )
{
item.setCharacteristic( Version.class, version );
return this;
}
@Override
public ArchivaContentItem build( )
{
super.build( );
return item;
}
}
@Override
public boolean exists( )
{
return asset.exists( );
}
}

View File

@ -19,9 +19,7 @@ package org.apache.archiva.repository.content.base;
* under the License.
*/
import org.apache.archiva.repository.content.BaseArtifactTypes;
import org.apache.archiva.repository.content.BaseDataItemTypes;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.DataItem;
import org.apache.archiva.repository.content.DataItemType;
import org.apache.archiva.repository.content.base.builder.DataItemOptBuilder;
@ -49,10 +47,9 @@ import org.apache.commons.lang3.StringUtils;
*
* @author Martin Stockhammer <martin_s@apache.org>
*/
public class ArchivaDataItem extends ArchivaContentItem implements DataItem
public class ArchivaDataItem extends BaseContentItem implements DataItem
{
private String id;
private ContentItem parent;
private String contentType;
private DataItemType dataItemType;
@ -67,12 +64,6 @@ public class ArchivaDataItem extends ArchivaContentItem implements DataItem
return id;
}
@Override
public ContentItem getParent( )
{
return parent;
}
@Override
public String getContentType( )
{
@ -109,7 +100,6 @@ public class ArchivaDataItem extends ArchivaContentItem implements DataItem
ArchivaDataItem that = (ArchivaDataItem) o;
if ( !id.equals( that.id ) ) return false;
if ( !parent.equals( that.parent ) ) return false;
return dataItemType.equals(that.dataItemType );
}
@ -118,7 +108,6 @@ public class ArchivaDataItem extends ArchivaContentItem implements DataItem
{
int result = super.hashCode( );
result = 31 * result + id.hashCode( );
result = 31 * result + parent.hashCode( );
result = 31 * result + dataItemType.hashCode( );
return result;
}
@ -126,9 +115,8 @@ public class ArchivaDataItem extends ArchivaContentItem implements DataItem
@Override
public String toString( )
{
final StringBuilder sb = new StringBuilder( "ArchivaArtifact{" );
final StringBuilder sb = new StringBuilder( "ArchivaDataItem{" );
sb.append( "id='" ).append( id ).append( '\'' );
sb.append( ", parent=" ).append( parent );
sb.append( ", contentType='" ).append( contentType ).append( '\'' );
sb.append( ", artifactType=" ).append( dataItemType );
sb.append( '}' );
@ -167,18 +155,6 @@ public class ArchivaDataItem extends ArchivaContentItem implements DataItem
return this;
}
@Override
public DataItemOptBuilder withParent( ContentItem parent )
{
if ( parent == null )
{
throw new IllegalArgumentException( "version may not be null" );
}
item.parent = parent;
super.setRepository( parent.getRepository( ) );
return this;
}
@Override
public DataItemOptBuilder withId( String id )
{

View File

@ -38,7 +38,7 @@ import java.util.regex.PatternSyntaxException;
* @author Martin Stockhammer <martin_s@apache.org>
* @since 3.0
*/
public class ArchivaNamespace extends ArchivaContentItem implements Namespace
public class ArchivaNamespace extends BaseContentItem implements Namespace
{
private String namespace;
private List<String> namespacePath;
@ -72,10 +72,9 @@ public class ArchivaNamespace extends ArchivaContentItem implements Namespace
{
if ( this == o ) return true;
if ( o == null || getClass( ) != o.getClass( ) ) return false;
if ( !super.equals( o ) ) return false;
ArchivaNamespace that = (ArchivaNamespace) o;
if (!repository.equals( that.repository )) return false;
return namespace.equals( that.namespace );
}

View File

@ -19,11 +19,15 @@ package org.apache.archiva.repository.content.base;
* under the License.
*/
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.Namespace;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.base.builder.ProjectOptBuilder;
import org.apache.archiva.repository.content.base.builder.ProjectWithIdBuilder;
import org.apache.archiva.repository.content.base.builder.WithAssetBuilder;
import org.apache.archiva.repository.content.base.builder.WithNamespaceBuilder;
import org.apache.archiva.repository.content.base.builder.WithNamespaceObjectBuilder;
import org.apache.archiva.repository.content.base.builder.WithProjectBuilder;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
@ -38,7 +42,7 @@ import org.apache.commons.lang3.StringUtils;
* @author Martin Stockhammer <martin_s@apache.org>
* @since 3.0
*/
public class ArchivaProject extends ArchivaContentItem implements Project
public class ArchivaProject extends BaseContentItem implements Project
{
private Namespace namespace;
private String id;
@ -62,6 +66,11 @@ public class ArchivaProject extends ArchivaContentItem implements Project
return new Builder( ).withAsset( storageAsset );
}
public static WithAssetBuilder<WithNamespaceObjectBuilder> withRepository( ManagedRepositoryContent repository )
{
return new ArchivaProject.Builder( ).withRepository( repository );
}
@Override
public Namespace getNamespace( )
{
@ -80,10 +89,10 @@ public class ArchivaProject extends ArchivaContentItem implements Project
{
if ( this == o ) return true;
if ( o == null || getClass( ) != o.getClass( ) ) return false;
if ( !super.equals( o ) ) return false;
ArchivaProject that = (ArchivaProject) o;
if (!repository.equals( that.repository )) return false;
if ( !namespace.equals( that.namespace ) ) return false;
return id.equals( that.id );
}
@ -100,7 +109,7 @@ public class ArchivaProject extends ArchivaContentItem implements Project
@Override
public String toString( )
{
return id + ", namespace="+namespace.toString();
return "ArchivaProject{ "+id + ", namespace="+namespace.toString()+"}";
}
/*

View File

@ -19,9 +19,12 @@ package org.apache.archiva.repository.content.base;
* under the License.
*/
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.builder.VersionOptBuilder;
import org.apache.archiva.repository.content.base.builder.WithAssetBuilder;
import org.apache.archiva.repository.content.base.builder.WithNamespaceBuilder;
import org.apache.archiva.repository.content.base.builder.WithProjectBuilder;
import org.apache.archiva.repository.content.base.builder.WithVersionBuilder;
import org.apache.archiva.repository.storage.StorageAsset;
@ -41,7 +44,7 @@ import java.util.regex.PatternSyntaxException;
* <p>
* Two instances are equal, if the project and the version match in addition to the base attributes repository and asset.
*/
public class ArchivaVersion extends ArchivaContentItem implements Version
public class ArchivaVersion extends BaseContentItem implements Version
{
private String version;
@ -66,6 +69,11 @@ public class ArchivaVersion extends ArchivaContentItem implements Version
return new Builder( ).withAsset( storageAsset );
}
public static WithAssetBuilder<WithProjectBuilder> withRepository( ManagedRepositoryContent repository )
{
return new ArchivaVersion.Builder( ).withRepository( repository );
}
@Override
public List<String> getVersionSegments( )
{
@ -90,10 +98,9 @@ public class ArchivaVersion extends ArchivaContentItem implements Version
{
if ( this == o ) return true;
if ( o == null || getClass( ) != o.getClass( ) ) return false;
if ( !super.equals( o ) ) return false;
ArchivaVersion that = (ArchivaVersion) o;
if (!repository.equals( that.repository )) return false;
if ( !version.equals( that.version ) ) return false;
return project.equals( that.project );
}
@ -110,7 +117,7 @@ public class ArchivaVersion extends ArchivaContentItem implements Version
@Override
public String toString( )
{
return version+", project="+project.toString();
return "ArchivaVersion{ "+version+", project="+project.toString()+"}";
}
private static final class Builder extends ContentItemBuilder<ArchivaVersion, VersionOptBuilder, WithProjectBuilder>

View File

@ -0,0 +1,151 @@
package org.apache.archiva.repository.content.base;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.archiva.repository.ItemConversionException;
import org.apache.archiva.repository.LayoutException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
public abstract class BaseContentItem implements ContentItem
{
protected ManagedRepositoryContent repository;
protected StorageAsset asset;
private Map<String, String> attributes;
private Map<Class<?>, ContentItem> characteristics = new HashMap<>( );
private Set<Class<?>> failedConversions = new HashSet<>( );
@Override
public <T extends ContentItem> T adapt( Class<T> clazz ) throws ItemConversionException
{
if (characteristics.containsKey( clazz )) {
return (T) characteristics.get( clazz );
} else {
for ( Map.Entry<Class<?>, ? extends ContentItem> cEntry : characteristics.entrySet()) {
if (clazz.isAssignableFrom( cEntry.getKey() )) {
return (T) cEntry.getValue( );
}
}
try {
return repository.applyCharacteristic( clazz, this );
} catch ( LayoutException e ) {
throw new ItemConversionException( "Could not convert item to " + clazz, e );
}
}
}
@Override
public <T extends ContentItem> boolean hasCharacteristic( Class<T> clazz )
{
if (clazz == null) {
return false;
}
if ( characteristics.containsKey( clazz )
|| characteristics.keySet( ).stream( ).anyMatch( cClass -> clazz.isAssignableFrom( cClass ) ) ) {
return true;
};
return false;
}
/**
* Does lazy initialization of the attributes map.
* Returns a unmodifiable map.
*
* @return unmodifiable map of attributes
*/
@Override
public Map<String, String> getAttributes( )
{
if ( this.attributes == null )
{
return Collections.emptyMap( );
}
else
{
return Collections.unmodifiableMap( this.attributes );
}
}
/**
* Adds a attribute value. The key must not be <code>null</code>.
*
* @param key the attribute key
* @param value the attribute value
* @throws IllegalArgumentException if the key is <code>null</code> or empty
*/
public void putAttribute( String key, String value ) throws IllegalArgumentException
{
if ( this.attributes == null )
{
this.attributes = new HashMap<>( );
}
if ( StringUtils.isEmpty( key ) )
{
throw new IllegalArgumentException( "Key value must not be empty or null" );
}
this.attributes.put( key, value );
}
@Override
public String getAttribute( String key )
{
if ( this.attributes == null )
{
return null;
}
else
{
return this.attributes.get( key );
}
}
@Override
public ManagedRepositoryContent getRepository( )
{
return repository;
}
@Override
public StorageAsset getAsset( )
{
return asset;
}
@Override
public boolean exists( )
{
return asset.exists( );
}
public <T extends ContentItem> void setCharacteristic( Class<T> clazz, T item )
{
this.characteristics.put( clazz, item );
}
}

View File

@ -0,0 +1,103 @@
package org.apache.archiva.repository.content.base;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.content.base.builder.OptBuilder;
import org.apache.archiva.repository.content.base.builder.WithAssetBuilder;
import org.apache.archiva.repository.content.base.builder.WithRepositoryBuilder;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.lang3.StringUtils;
/**
* Builder for content item. Must be extended by subclasses.
* The builder uses chained interfaces for building the required attributes. That means you have to set
* some certain attributes, before you can build the content item instance via the {@link #build()} method.
* <p>
* Subclasses should extend from this class and provide the interface/class for the destination item,
* a interface for the optional attributes and a interface that is returned after the last required attribute is
* set.
* <p>
* The interface for optional attributes should inherit from {@link OptBuilder}
*
* @param <I> the item class that should be built
* @param <O> the class/interface for the optional attributes
* @param <N> the class/interface for the next (required) attribute after the base attributes are set
*/
abstract class ContentItemBuilder<I extends BaseContentItem, O extends OptBuilder<I, O>, N>
implements WithRepositoryBuilder, WithAssetBuilder<N>,
OptBuilder<I, O>
{
protected I item;
protected ContentItemBuilder( I item )
{
this.item = item;
}
protected abstract O getOptBuilder( );
protected abstract N getNextBuilder( );
@Override
public WithAssetBuilder<N> withRepository( ManagedRepositoryContent repository )
{
if ( repository == null )
{
throw new IllegalArgumentException( "Repository may not be null" );
}
( (BaseContentItem) item ).repository = repository;
return this;
}
@Override
public N withAsset( StorageAsset asset )
{
if ( asset == null )
{
throw new IllegalArgumentException( "Asset may not be null" );
}
( (BaseContentItem) item ).asset = asset;
return getNextBuilder( );
}
@Override
public O withAttribute( String key, String value )
{
if ( StringUtils.isEmpty( key ) )
{
throw new IllegalArgumentException( "Attribute key may not be null" );
}
item.putAttribute( key, value );
return getOptBuilder( );
}
protected void setRepository( ManagedRepositoryContent repository )
{
item.repository = repository;
}
@Override
public I build( )
{
return item;
}
}

View File

@ -0,0 +1,38 @@
package org.apache.archiva.repository.content.base.builder;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.archiva.repository.content.Namespace;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.ArchivaContentItem;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
public interface ArchivaContentItemOptBuilder extends OptBuilder<ArchivaContentItem, ArchivaContentItemOptBuilder>
{
ArchivaContentItemOptBuilder withNamespace( Namespace namespace );
ArchivaContentItemOptBuilder withProject( Project project );
ArchivaContentItemOptBuilder withVersion( Version version );
ArchivaContentItem build();
}

View File

@ -32,8 +32,6 @@ public interface DataItemOptBuilder
extends OptBuilder<ArchivaDataItem, DataItemOptBuilder>
{
DataItemOptBuilder withParent( ContentItem parent );
DataItemOptBuilder withContentType( String contentType );
DataItemOptBuilder withDataType( DataItemType type );

View File

@ -163,6 +163,12 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{
@ -181,6 +187,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return false;
}
@Override
public ContentItem getParent( ContentItem item )
{
return null;
}
@Override
public List<? extends ContentItem> getChildren( ContentItem item )
{
return null;
}
@Override
public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
{
return null;
}
@Override
public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
{

View File

@ -163,6 +163,12 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{
@ -181,6 +187,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return false;
}
@Override
public ContentItem getParent( ContentItem item )
{
return null;
}
@Override
public List<? extends ContentItem> getChildren( ContentItem item )
{
return null;
}
@Override
public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
{
return null;
}
@Override
public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
{

View File

@ -47,6 +47,7 @@ import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
@ -82,6 +83,12 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
{
}
@Override
public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
{
@ -130,6 +137,12 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
{
@ -154,6 +167,12 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return null;
}
@Override
public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
{
return null;
}
@Override
public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
{
@ -172,6 +191,24 @@ public class ManagedRepositoryContentMock implements ManagedRepositoryContent
return false;
}
@Override
public ContentItem getParent( ContentItem item )
{
return null;
}
@Override
public List<? extends ContentItem> getChildren( ContentItem item )
{
return null;
}
@Override
public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
{
return null;
}
@Override
public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
{

View File

@ -23,6 +23,7 @@ import org.apache.archiva.common.utils.VersionComparator;
import org.apache.archiva.configuration.ArchivaConfiguration;
import org.apache.archiva.configuration.FileType;
import org.apache.archiva.configuration.FileTypes;
import org.apache.archiva.metadata.maven.MavenMetadataReader;
import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.model.ProjectReference;
import org.apache.archiva.model.VersionedReference;
@ -34,17 +35,20 @@ import org.apache.archiva.repository.RepositoryContent;
import org.apache.archiva.repository.content.Artifact;
import org.apache.archiva.repository.content.BaseArtifactTypes;
import org.apache.archiva.repository.content.ContentItem;
import org.apache.archiva.repository.content.DataItem;
import org.apache.archiva.repository.content.ItemNotFoundException;
import org.apache.archiva.repository.content.ItemSelector;
import org.apache.archiva.repository.content.Namespace;
import org.apache.archiva.repository.content.Project;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.ArchivaContentItem;
import org.apache.archiva.repository.content.base.ArchivaDataItem;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.maven.MavenManagedRepository;
import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
import org.apache.archiva.repository.metadata.MetadataReader;
import org.apache.archiva.repository.storage.StorageAsset;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
@ -59,11 +63,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -90,6 +90,10 @@ public class ManagedDefaultRepositoryContentTest
@Inject
MavenContentHelper contentHelper;
@Inject
@Named( "metadataReader#maven" )
MavenMetadataReader metadataReader;
@Inject
FileLockManager fileLockManager;
@ -120,6 +124,7 @@ public class ManagedDefaultRepositoryContentTest
repoContent = new ManagedDefaultRepositoryContent(repository, artifactMappingProviders, fileTypes, fileLockManager);
repoContent.setMavenContentHelper( contentHelper );
repoContent.setMetadataReader( metadataReader );
//repoContent = (ManagedRepositoryContent) lookup( ManagedRepositoryContent.class, "default" );
}
@ -128,7 +133,7 @@ public class ManagedDefaultRepositoryContentTest
public void testGetVersionsSnapshotA()
throws Exception
{
assertVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
assertArtifactVersions( "snap_shots_a", "1.0-alpha-11-SNAPSHOT",
new String[]{ "1.0-alpha-11-SNAPSHOT", "1.0-alpha-11-20070221.194724-2",
"1.0-alpha-11-20070302.212723-3", "1.0-alpha-11-20070303.152828-4",
"1.0-alpha-11-20070305.215149-5", "1.0-alpha-11-20070307.170909-6",
@ -203,6 +208,31 @@ public class ManagedDefaultRepositoryContentTest
assertArrayEquals( expectedVersions, versions.toArray( ) );
}
private void assertArtifactVersions( String artifactId, String version, String[] expectedVersions )
throws Exception
{
// Use the test metadata-repository, which is already setup for
// These kind of version tests.
Path repoDir = getRepositoryPath( "metadata-repository" );
((EditableManagedRepository)repoContent.getRepository()).setLocation( repoDir.toAbsolutePath().toUri() );
// Request the versions.
// Sort the list (for asserts later)
final VersionComparator comparator = new VersionComparator( );
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( "org.apache.archiva.metadata.tests" )
.withProjectId( artifactId )
.withVersion( version )
.build( );
List<String> versions = repoContent.getArtifactVersions( selector ).stream()
.sorted( comparator ).collect( Collectors.toList());
assertArrayEquals( expectedVersions, versions.toArray( ) );
}
@Test
@ -558,10 +588,10 @@ public class ManagedDefaultRepositoryContentTest
assertEquals( 2, results.size( ) );
Artifact mainArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "jdbc-2.0.jar" ) ).findFirst( ).get( );
assertNotNull( mainArtifact );
assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, mainArtifact.getDataType( ) );
Artifact metaArtifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata-repository.xml" ) ).findFirst( ).get( );
assertNotNull( metaArtifact );
assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getArtifactType( ) );
assertEquals( MavenTypes.REPOSITORY_METADATA, metaArtifact.getDataType( ) );
}
@Test
@ -598,7 +628,7 @@ public class ManagedDefaultRepositoryContentTest
assertNotNull( artifact );
assertEquals( "pom", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
assertEquals( ".pom", artifact.getRemainder( ) );
@ -614,7 +644,7 @@ public class ManagedDefaultRepositoryContentTest
assertNotNull( artifact );
assertEquals( "md5", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
assertEquals( "1.3-20070725.210059-1", artifact.getArtifactVersion( ) );
assertEquals( ".pom.md5", artifact.getRemainder( ) );
@ -629,7 +659,7 @@ public class ManagedDefaultRepositoryContentTest
artifact = results.stream( ).filter( a -> a.getFileName( ).equals( "maven-metadata.xml" ) )
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
assertEquals( "1.3-SNAPSHOT", artifact.getVersion( ).getVersion( ) );
assertEquals( "xml", artifact.getExtension( ) );
}
@ -668,7 +698,7 @@ public class ManagedDefaultRepositoryContentTest
assertEquals( 1, results.size( ) );
Artifact artifact = results.get( 0 );
assertEquals( "pom", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
}
@Test
@ -711,13 +741,13 @@ public class ManagedDefaultRepositoryContentTest
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( "pom", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "axis2-1.3-20070731.113304-21.pom.sha1" ) )
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( "sha1", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
}
@ -752,19 +782,19 @@ public class ManagedDefaultRepositoryContentTest
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( "xml", artifact.getExtension( ) );
assertEquals( BaseArtifactTypes.METADATA, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.METADATA, artifact.getDataType( ) );
artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar" ) )
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
assertEquals( "sources", artifact.getClassifier( ) );
assertEquals( "java-source", artifact.getType( ) );
artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "maven-downloader-1.0-sources.jar.sha1" ) )
.findFirst( ).get( );
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
assertEquals( "sources", artifact.getClassifier( ) );
assertEquals( "sha1", artifact.getType( ) );
assertEquals( ".jar.sha1", artifact.getRemainder( ) );
@ -994,6 +1024,59 @@ public class ManagedDefaultRepositoryContentTest
assertNotNull( artifact );
}
@Test
public void testNewItemStreamWithNamespace1() {
ItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( "org.apache.axis2" )
.build();
Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
assertEquals( 41, result.size( ) );
ContentItem item = result.get( 39 );
Version version = item.adapt( Version.class );
assertNotNull( version );
assertEquals( "1.3-SNAPSHOT", version.getVersion( ) );
Project project = result.get( 40 ).adapt( Project.class );
assertNotNull( project );
assertEquals( "axis2", project.getId( ) );
assertTrue( result.stream( ).anyMatch( a -> "axis2-1.3-20070725.210059-1.pom".equals( a.getAsset( ).getName( ) ) ) );
}
// @Test
// public void testNewItemStreamWithNamespace2() {
// ItemSelector selector = ArchivaItemSelector.builder( )
// .withNamespace( "org.apache.maven" )
// .build();
//
// Stream<? extends ContentItem> stream = repoContent.newItemStream( selector, false );
// List<? extends ContentItem> result = stream.collect( Collectors.toList( ) );
// int versions = 0;
// int projects = 0;
// int artifacts = 0;
// int namespaces = 0;
// int dataitems = 0;
// for (int i=0; i<result.size(); i++) {
// ContentItem ci = result.get( i );
// System.out.println( i + ": " + result.get( i ) + " - " +result.get(i).getClass().getName() + " - " + result.get(i).getAsset().getPath() );
// if (ci instanceof Version) {
// versions++;
// } else if (ci instanceof Project) {
// projects++;
// } else if (ci instanceof Namespace) {
// namespaces++;
// } else if (ci instanceof Artifact) {
// artifacts++;
// } else if (ci instanceof DataItem ) {
// dataitems++;
// }
// }
// System.out.println( "namespaces=" + namespaces + ", projects=" + projects + ", versions=" + versions + ", artifacts=" + artifacts + ", dataitems=" + dataitems );
// assertEquals( 170, result.size( ) );
// assertEquals( 92, result.stream( ).filter( a -> a instanceof Artifact ).count( ) );
// }
@Test
public void testGetArtifactFromContentItem() {
ItemSelector selector = ArchivaItemSelector.builder( )
@ -1053,17 +1136,17 @@ public class ManagedDefaultRepositoryContentTest
String path = "/org/apache/maven/shared";
ContentItem item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Namespace );
assertTrue( item instanceof ArchivaContentItem );
path = "/org/apache/maven/shared/maven-downloader";
item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Project );
assertTrue( item instanceof ArchivaContentItem );
path = "/org/apache/maven/shared/maven-downloader/1.1";
item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Version );
assertTrue( item instanceof ArchivaContentItem );
path = "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar";
item = repoContent.toItem( path );
@ -1078,22 +1161,22 @@ public class ManagedDefaultRepositoryContentTest
StorageAsset path = repoContent.getRepository().getAsset("/org/apache/maven/shared");
ContentItem item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Namespace );
assertTrue( item instanceof ArchivaContentItem );
path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader" );
item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Project );
assertTrue( item instanceof ArchivaContentItem );
path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1" );
item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Version );
assertTrue( item instanceof ArchivaContentItem );
path = repoContent.getRepository( ).getAsset( "/org/apache/maven/shared/maven-downloader/1.1/maven-downloader-1.1.jar" );
item = repoContent.toItem( path );
assertNotNull( item );
assertTrue( item instanceof Artifact );
assertTrue( item instanceof DataItem );
}
@ -1197,18 +1280,18 @@ public class ManagedDefaultRepositoryContentTest
Artifact artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar" ) )
.findFirst().get();
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.MAIN, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.MAIN, artifact.getDataType( ) );
artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.md5" ) )
.findFirst().get();
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
assertEquals( "md5", artifact.getExtension( ) );
artifact = results.stream( ).filter( a -> a.getFileName( ).equalsIgnoreCase( "samplejar-1.0.jar.sha1" ) )
.findFirst().get();
assertNotNull( artifact );
assertEquals( BaseArtifactTypes.RELATED, artifact.getArtifactType( ) );
assertEquals( BaseArtifactTypes.RELATED, artifact.getDataType( ) );
assertEquals( "sha1", artifact.getExtension( ) );
}
@ -1546,4 +1629,5 @@ public class ManagedDefaultRepositoryContentTest
assertTrue( new String( content ).startsWith( "test.test.test" ) );
}
}

View File

@ -52,6 +52,7 @@ import org.apache.archiva.repository.RepositoryNotFoundException;
import org.apache.archiva.repository.RepositoryRegistry;
import org.apache.archiva.repository.RepositoryType;
import org.apache.archiva.repository.content.ItemNotFoundException;
import org.apache.archiva.repository.content.Version;
import org.apache.archiva.repository.content.base.ArchivaItemSelector;
import org.apache.archiva.repository.storage.fs.FsStorageUtil;
import org.apache.archiva.repository.storage.RepositoryStorage;
@ -663,28 +664,19 @@ public class DefaultRepositoriesService
{
ManagedRepositoryContent repository = getManagedRepositoryContent( repositoryId );
VersionedReference ref = new VersionedReference();
ref.setArtifactId( projectId );
ref.setGroupId( namespace );
ref.setVersion( version );
repository.deleteVersion( ref );
ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setGroupId( namespace );
artifactReference.setArtifactId( projectId );
artifactReference.setVersion( version );
ArchivaItemSelector selector = ArchivaItemSelector.builder( )
.withNamespace( namespace )
.withProjectId( projectId )
.withVersion( version )
.build( );
Version versionItem = repository.getVersion( selector );
if (versionItem!=null && versionItem.exists()) {
repository.deleteItem( versionItem );
}
MetadataRepository metadataRepository = repositorySession.getRepository();
List<ArtifactReference> related = repository.getRelatedArtifacts( repository.toVersion(artifactReference) );
log.debug( "related: {}", related );
for ( ArtifactReference artifactRef : related )
{
repository.deleteArtifact( artifactRef );
}
Collection<ArtifactMetadata> artifacts =
metadataRepository.getArtifacts(repositorySession , repositoryId, namespace, projectId, version );
@ -695,7 +687,7 @@ public class DefaultRepositoriesService
metadataRepository.removeProjectVersion(repositorySession , repositoryId, namespace, projectId, version );
}
catch ( MetadataRepositoryException | MetadataResolutionException | RepositoryException | LayoutException e )
catch ( MetadataRepositoryException | MetadataResolutionException | RepositoryException | ItemNotFoundException e )
{
throw new ArchivaRestServiceException( "Repository exception: " + e.getMessage(), 500, e );
}