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 void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
160 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
166 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
173 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
179 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
185 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
191 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
197 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
203 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
209 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
215 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
221 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
227 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
233 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
239 public boolean hasContent( ItemSelector selector )
245 public ContentItem getParent( ContentItem item )
249 return toItem( item.getAsset( ).getParent( ) );
251 catch ( LayoutException e )
253 throw new RuntimeException( "Bad layout error " + e.getMessage( ) );
258 public List<? extends ContentItem> getChildren( ContentItem item )
264 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
270 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
276 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
282 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
288 public DataItem getMetadataItem( Version version )
290 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
291 .withDataType( BaseDataItemTypes.METADATA ).build();
295 public DataItem getMetadataItem( Project project )
297 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
298 .withDataType( BaseDataItemTypes.METADATA ).build( );
303 public ContentItem toItem( String path ) throws LayoutException
305 StorageAsset asset = repository.getAsset( "" ).resolve( path );
306 return toItem( asset );
310 public ContentItem toItem( StorageAsset asset ) throws LayoutException
312 if (asset.isLeaf()) {
313 return ArchivaDataItem.withAsset( asset ).withId( asset.getName() ).build();
316 return ArchivaContentItem.withRepository( this )
317 .withAsset( asset ).build( );
323 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
330 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
336 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
342 public String toPath( ContentItem item )
348 public String getId( )
354 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
360 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
366 public String getRepoRoot( )
372 public ManagedRepository getRepository( )
378 public void setRepository( ManagedRepository repo )
380 this.repository = repo;
384 public StorageAsset toFile( VersionedReference reference )
390 public ArtifactReference toArtifactReference( String path ) throws LayoutException
396 public StorageAsset toFile( ArtifactReference reference )
402 public String toPath( ArtifactReference reference )
408 public String toPath( ItemSelector selector )
414 public ItemSelector toItemSelector( String path ) throws LayoutException
420 public ManagedRepositoryContent getGenericContent( )