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.ArtifactReference;
23 import org.apache.archiva.model.ProjectReference;
24 import org.apache.archiva.model.VersionedReference;
25 import org.apache.archiva.repository.ContentAccessException;
26 import org.apache.archiva.repository.ContentNotFoundException;
27 import org.apache.archiva.repository.ManagedRepositoryContent;
28 import org.apache.archiva.repository.ItemDeleteStatus;
29 import org.apache.archiva.repository.LayoutException;
30 import org.apache.archiva.repository.ManagedRepository;
31 import org.apache.archiva.repository.BaseRepositoryContentLayout;
32 import org.apache.archiva.repository.ManagedRepositoryContentLayout;
33 import org.apache.archiva.repository.content.Artifact;
34 import org.apache.archiva.repository.content.BaseDataItemTypes;
35 import org.apache.archiva.repository.content.ContentItem;
36 import org.apache.archiva.repository.content.DataItem;
37 import org.apache.archiva.repository.content.ItemNotFoundException;
38 import org.apache.archiva.repository.content.ItemSelector;
39 import org.apache.archiva.repository.content.Namespace;
40 import org.apache.archiva.repository.content.Project;
41 import org.apache.archiva.repository.content.Version;
42 import org.apache.archiva.repository.content.base.ArchivaContentItem;
43 import org.apache.archiva.repository.content.base.ArchivaDataItem;
44 import org.apache.archiva.repository.content.base.ArchivaNamespace;
45 import org.apache.archiva.repository.content.base.ArchivaProject;
46 import org.apache.archiva.repository.content.base.ArchivaVersion;
47 import org.apache.archiva.repository.storage.StorageAsset;
48 import org.springframework.stereotype.Service;
50 import java.nio.file.Path;
51 import java.util.List;
52 import java.util.function.Consumer;
53 import java.util.stream.Stream;
56 * @author Martin Stockhammer <martin_s@apache.org>
58 @Service("managedRepositoryContent#mock")
59 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
61 private ManagedRepository repository;
64 public VersionedReference toVersion( String groupId, String artifactId, String version )
70 public VersionedReference toVersion( ArtifactReference artifactReference )
76 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
82 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
84 if (clazz.isAssignableFrom( Version.class ))
86 if ( !item.hasCharacteristic( Version.class ) )
88 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
90 return (T) item.adapt( Version.class );
91 } else if ( clazz.isAssignableFrom( Project.class )) {
92 if ( !item.hasCharacteristic( Project.class ) )
94 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
96 return (T) item.adapt( Project.class );
97 } else if ( clazz.isAssignableFrom( Namespace.class )) {
98 if ( !item.hasCharacteristic( Namespace.class ) )
100 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
102 return (T) item.adapt( Namespace.class );
104 throw new LayoutException( "Could not convert item to class " + clazz);
107 private Version createVersionFromPath( StorageAsset asset )
109 Project proj = createProjectFromPath( asset.getParent( ) );
110 return ArchivaVersion.withRepository( this ).withAsset( asset )
111 .withProject( proj ).withVersion( asset.getName( ) ).build();
114 private Project createProjectFromPath( StorageAsset asset) {
115 Namespace ns = createNamespaceFromPath( asset );
116 return ArchivaProject.withRepository( this ).withAsset( asset )
117 .withNamespace( ns ).withId( asset.getName( ) ).build( );
120 private Namespace createNamespaceFromPath( StorageAsset asset) {
121 String namespace = asset.getPath( ).replace( "/", "." );
122 return ArchivaNamespace.withRepository( this )
123 .withAsset( asset ).withNamespace( namespace ).build();
128 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
134 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
140 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
146 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
153 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
159 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
166 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
172 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
178 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
184 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
190 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
196 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
202 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
208 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
214 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
220 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
226 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
232 public boolean hasContent( ItemSelector selector )
238 public ContentItem getParent( ContentItem item )
242 return toItem( item.getAsset( ).getParent( ) );
244 catch ( LayoutException e )
246 throw new RuntimeException( "Bad layout error " + e.getMessage( ) );
251 public List<? extends ContentItem> getChildren( ContentItem item )
257 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
263 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
269 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
275 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
281 public DataItem getMetadataItem( Version version )
283 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
284 .withDataType( BaseDataItemTypes.METADATA ).build();
288 public DataItem getMetadataItem( Project project )
290 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
291 .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 void setRepository( ManagedRepository repo )
373 this.repository = repo;
377 public StorageAsset toFile( VersionedReference reference )
383 public ArtifactReference toArtifactReference( String path ) throws LayoutException
389 public String toPath( ArtifactReference reference )
395 public String toPath( ItemSelector selector )
401 public ItemSelector toItemSelector( String path ) throws LayoutException
407 public ManagedRepositoryContent getGenericContent( )