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.repository.ContentAccessException;
24 import org.apache.archiva.repository.ManagedRepositoryContent;
25 import org.apache.archiva.repository.ItemDeleteStatus;
26 import org.apache.archiva.repository.LayoutException;
27 import org.apache.archiva.repository.ManagedRepository;
28 import org.apache.archiva.repository.BaseRepositoryContentLayout;
29 import org.apache.archiva.repository.ManagedRepositoryContentLayout;
30 import org.apache.archiva.repository.content.Artifact;
31 import org.apache.archiva.repository.content.BaseDataItemTypes;
32 import org.apache.archiva.repository.content.ContentItem;
33 import org.apache.archiva.repository.content.DataItem;
34 import org.apache.archiva.repository.content.ItemNotFoundException;
35 import org.apache.archiva.repository.content.ItemSelector;
36 import org.apache.archiva.repository.content.Namespace;
37 import org.apache.archiva.repository.content.Project;
38 import org.apache.archiva.repository.content.Version;
39 import org.apache.archiva.repository.content.base.ArchivaContentItem;
40 import org.apache.archiva.repository.content.base.ArchivaDataItem;
41 import org.apache.archiva.repository.content.base.ArchivaNamespace;
42 import org.apache.archiva.repository.content.base.ArchivaProject;
43 import org.apache.archiva.repository.content.base.ArchivaVersion;
44 import org.apache.archiva.repository.storage.StorageAsset;
45 import org.springframework.stereotype.Service;
47 import java.nio.file.Path;
48 import java.util.List;
49 import java.util.function.Consumer;
50 import java.util.stream.Stream;
53 * @author Martin Stockhammer <martin_s@apache.org>
55 @Service("managedRepositoryContent#mock")
56 public class ManagedRepositoryContentMock implements BaseRepositoryContentLayout, ManagedRepositoryContent
58 private ManagedRepository repository;
61 public void deleteAllItems( ItemSelector selector, Consumer<ItemDeleteStatus> consumer ) throws ContentAccessException, IllegalArgumentException
67 public <T extends ContentItem> T adaptItem( Class<T> clazz, ContentItem item ) throws LayoutException
69 if (clazz.isAssignableFrom( Version.class ))
71 if ( !item.hasCharacteristic( Version.class ) )
73 item.setCharacteristic( Version.class, createVersionFromPath( item.getAsset() ) );
75 return (T) item.adapt( Version.class );
76 } else if ( clazz.isAssignableFrom( Project.class )) {
77 if ( !item.hasCharacteristic( Project.class ) )
79 item.setCharacteristic( Project.class, createProjectFromPath( item.getAsset() ) );
81 return (T) item.adapt( Project.class );
82 } else if ( clazz.isAssignableFrom( Namespace.class )) {
83 if ( !item.hasCharacteristic( Namespace.class ) )
85 item.setCharacteristic( Namespace.class, createNamespaceFromPath( item.getAsset() ) );
87 return (T) item.adapt( Namespace.class );
89 throw new LayoutException( "Could not convert item to class " + clazz);
92 private Version createVersionFromPath( StorageAsset asset )
94 Project proj = createProjectFromPath( asset.getParent( ) );
95 return ArchivaVersion.withRepository( this ).withAsset( asset )
96 .withProject( proj ).withVersion( asset.getName( ) ).build();
99 private Project createProjectFromPath( StorageAsset asset) {
100 Namespace ns = createNamespaceFromPath( asset );
101 return ArchivaProject.withRepository( this ).withAsset( asset )
102 .withNamespace( ns ).withId( asset.getName( ) ).build( );
105 private Namespace createNamespaceFromPath( StorageAsset asset) {
106 String namespace = asset.getPath( ).replace( "/", "." );
107 return ArchivaNamespace.withRepository( this )
108 .withAsset( asset ).withNamespace( namespace ).build();
113 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
119 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
125 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
131 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
138 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
145 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
151 public Artifact getArtifact( String path ) throws LayoutException, ContentAccessException
157 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
163 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
169 public Stream<? extends ContentItem> newItemStream( ItemSelector selector, boolean parallel ) throws ContentAccessException, IllegalArgumentException
175 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
181 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
187 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
193 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
199 public List<String> getArtifactVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
205 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
211 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
217 public boolean hasContent( ItemSelector selector )
223 public ContentItem getParent( ContentItem item )
227 return toItem( item.getAsset( ).getParent( ) );
229 catch ( LayoutException e )
231 throw new RuntimeException( "Bad layout error " + e.getMessage( ) );
236 public List<? extends ContentItem> getChildren( ContentItem item )
242 public <T extends ContentItem> T applyCharacteristic( Class<T> clazz, ContentItem item ) throws LayoutException
248 public <T extends ManagedRepositoryContentLayout> T getLayout( Class<T> clazz ) throws LayoutException
254 public <T extends ManagedRepositoryContentLayout> boolean supportsLayout( Class<T> clazz )
260 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
266 public DataItem getMetadataItem( Version version )
268 return ArchivaDataItem.withAsset( version.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
269 .withDataType( BaseDataItemTypes.METADATA ).build();
273 public DataItem getMetadataItem( Project project )
275 return ArchivaDataItem.withAsset( project.getAsset( ).resolve( "maven-metadata.xml" ) ).withId( "maven-metadata.xml" )
276 .withDataType( BaseDataItemTypes.METADATA ).build( );
281 public ContentItem toItem( String path ) throws LayoutException
283 StorageAsset asset = repository.getRoot().resolve( path );
284 return toItem( asset );
288 public ContentItem toItem( StorageAsset asset ) throws LayoutException
290 if (asset.isLeaf()) {
291 return ArchivaDataItem.withAsset( asset ).withId( asset.getName() ).build();
294 return ArchivaContentItem.withRepository( this )
295 .withAsset( asset ).build( );
301 public String toPath( ContentItem item )
307 public String getId( )
313 public ManagedRepository getRepository( )
319 public void setRepository( ManagedRepository repo )
321 this.repository = repo;
325 public String toPath( ArtifactReference reference )
331 public String toPath( ItemSelector selector )
337 public ItemSelector toItemSelector( String path ) throws LayoutException
343 public ManagedRepositoryContent getGenericContent( )