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.model.ArchivaArtifact;
23 import org.apache.archiva.model.ArtifactReference;
24 import org.apache.archiva.model.ProjectReference;
25 import org.apache.archiva.model.VersionedReference;
26 import org.apache.archiva.repository.ContentAccessException;
27 import org.apache.archiva.repository.ContentNotFoundException;
28 import org.apache.archiva.repository.ManagedRepositoryContent;
29 import org.apache.archiva.repository.ItemDeleteStatus;
30 import org.apache.archiva.repository.LayoutException;
31 import org.apache.archiva.repository.ManagedRepository;
32 import org.apache.archiva.repository.BaseRepositoryContentLayout;
33 import org.apache.archiva.repository.ManagedRepositoryContentLayout;
34 import org.apache.archiva.repository.content.Artifact;
35 import org.apache.archiva.repository.content.BaseDataItemTypes;
36 import org.apache.archiva.repository.content.ContentItem;
37 import org.apache.archiva.repository.content.DataItem;
38 import org.apache.archiva.repository.content.ItemNotFoundException;
39 import org.apache.archiva.repository.content.ItemSelector;
40 import org.apache.archiva.repository.content.Namespace;
41 import org.apache.archiva.repository.content.Project;
42 import org.apache.archiva.repository.content.Version;
43 import org.apache.archiva.repository.content.base.ArchivaContentItem;
44 import org.apache.archiva.repository.content.base.ArchivaDataItem;
45 import org.apache.archiva.repository.content.base.ArchivaNamespace;
46 import org.apache.archiva.repository.content.base.ArchivaProject;
47 import org.apache.archiva.repository.content.base.ArchivaVersion;
48 import org.apache.archiva.repository.storage.StorageAsset;
49 import org.springframework.stereotype.Service;
51 import java.nio.file.Path;
52 import java.util.List;
53 import java.util.function.Consumer;
54 import java.util.stream.Stream;
57 * @author Martin Stockhammer <martin_s@apache.org>
59 @Service("managedRepositoryContent#mock")
60 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
62 private ManagedRepository repository;
65 public VersionedReference toVersion( String groupId, String artifactId, String version )
71 public VersionedReference toVersion( ArtifactReference artifactReference )
77 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
83 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
85 if (clazz.isAssignableFrom( Version.class ))
87 if ( !item.hasCharacteristic( Version.class ) )
89 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
91 return (T) item.adapt( Version.class );
92 } else if ( clazz.isAssignableFrom( Project.class )) {
93 if ( !item.hasCharacteristic( Project.class ) )
95 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
97 return (T) item.adapt( Project.class );
98 } else if ( clazz.isAssignableFrom( Namespace.class )) {
99 if ( !item.hasCharacteristic( Namespace.class ) )
101 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
103 return (T) item.adapt( Namespace.class );
105 throw new LayoutException( "Could not convert item to class " + clazz);
108 private Version createVersionFromPath( StorageAsset asset )
110 Project proj = createProjectFromPath( asset.getParent( ) );
111 return ArchivaVersion.withRepository( this ).withAsset( asset )
112 .withProject( proj ).withVersion( asset.getName( ) ).build();
115 private Project createProjectFromPath( StorageAsset asset) {
116 Namespace ns = createNamespaceFromPath( asset );
117 return ArchivaProject.withRepository( this ).withAsset( asset )
118 .withNamespace( ns ).withId( asset.getName( ) ).build( );
121 private Namespace createNamespaceFromPath( StorageAsset asset) {
122 String namespace = asset.getPath( ).replace( "/", "." );
123 return ArchivaNamespace.withRepository( this )
124 .withAsset( asset ).withNamespace( namespace ).build();
129 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
135 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
141 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
147 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
154 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
161 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
167 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
174 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
180 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
186 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
192 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
198 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
204 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
210 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
216 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
222 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
228 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
234 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
240 public boolean hasContent( ItemSelector selector )
246 public ContentItem getParent( ContentItem item )
250 return toItem( item.getAsset( ).getParent( ) );
252 catch ( LayoutException e )
254 throw new RuntimeException( "Bad layout error " + e.getMessage( ) );
259 public List<? extends ContentItem> getChildren( ContentItem item )
265 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
271 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
277 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
283 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
289 public DataItem getMetadataItem( Version version )
291 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
292 .withDataType( BaseDataItemTypes.METADATA ).build();
296 public ContentItem toItem( String path ) throws LayoutException
298 StorageAsset asset = repository.getAsset( "" ).resolve( path );
299 return toItem( asset );
303 public ContentItem toItem( StorageAsset asset ) throws LayoutException
305 if (asset.isLeaf()) {
306 return ArchivaDataItem.withAsset( asset ).withId( asset.getName() ).build();
309 return ArchivaContentItem.withRepository( this )
310 .withAsset( asset ).build( );
316 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
323 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
329 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
335 public String toPath( ContentItem item )
341 public String getId( )
347 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
353 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
359 public String getRepoRoot( )
365 public ManagedRepository getRepository( )
371 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
377 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
383 public void setRepository( ManagedRepository repo )
385 this.repository = repo;
389 public StorageAsset toFile( VersionedReference reference )
395 public ArtifactReference toArtifactReference( String path ) throws LayoutException
401 public StorageAsset toFile( ArtifactReference reference )
407 public StorageAsset toFile( ArchivaArtifact reference )
413 public String toMetadataPath( ProjectReference reference )
419 public String toPath( ArtifactReference reference )
425 public String toPath( ItemSelector selector )
431 public ItemSelector toItemSelector( String path ) throws LayoutException
437 public ManagedRepositoryContent getGenericContent( )