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.common.filelock.DefaultFileLockManager;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.metadata.model.ArtifactMetadata;
25 import org.apache.archiva.metadata.maven.model.MavenArtifactFacet;
26 import org.apache.archiva.model.ArchivaArtifact;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.*;
31 import org.apache.archiva.repository.content.Artifact;
32 import org.apache.archiva.repository.content.ContentItem;
33 import org.apache.archiva.repository.content.ItemNotFoundException;
34 import org.apache.archiva.repository.content.ItemSelector;
35 import org.apache.archiva.repository.content.Namespace;
36 import org.apache.archiva.repository.content.Project;
37 import org.apache.archiva.repository.content.Version;
38 import org.apache.archiva.repository.storage.fs.FilesystemStorage;
39 import org.apache.archiva.repository.storage.RepositoryStorage;
40 import org.apache.archiva.repository.storage.StorageAsset;
41 import org.apache.commons.lang3.StringUtils;
42 import org.springframework.stereotype.Service;
44 import java.io.IOException;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.util.HashMap;
48 import java.util.List;
51 import java.util.regex.Matcher;
52 import java.util.regex.Pattern;
53 import java.util.stream.Stream;
56 * @author Martin Stockhammer <martin_s@apache.org>
58 @Service("managedRepositoryContent#mock")
59 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
61 private static final String PATH_SEPARATOR = "/";
62 private static final String GROUP_SEPARATOR = ".";
63 public static final String MAVEN_METADATA = "maven-metadata.xml";
66 private ManagedRepository repository;
67 private RepositoryStorage fsStorage;
69 ManagedRepositoryContentMock(ManagedRepository repo) {
70 this.repository = repo;
71 this.fsStorage = repo;
75 public VersionedReference toVersion( String groupId, String artifactId, String version )
81 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
87 public VersionedReference toVersion( ArtifactReference artifactReference )
93 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
99 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
105 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
111 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
117 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
123 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
129 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
135 public List<? extends Artifact> getAllArtifacts( ItemSelector selector ) throws ContentAccessException
141 public Stream<? extends Artifact> getAllArtifactStream( ItemSelector selector ) throws ContentAccessException
147 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
153 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
159 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
165 public List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse ) throws ContentAccessException
171 public Stream<? extends Artifact> getArtifactStream( ContentItem item ) throws ContentAccessException
177 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse ) throws ContentAccessException
183 public boolean hasContent( ItemSelector selector )
189 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
195 public ContentItem toItem( String path ) throws LayoutException
201 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
207 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
213 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
219 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
225 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
231 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
237 public String getId( )
239 return repository.getId();
243 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
249 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
255 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
261 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
267 public String getRepoRoot( )
269 return getRepoRootAsset().getFilePath().toString();
272 private StorageAsset getRepoRootAsset() {
273 if (fsStorage==null) {
275 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
276 } catch (IOException e) {
280 return fsStorage.getAsset("");
284 public ManagedRepository getRepository( )
290 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
296 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
302 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
308 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
314 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
320 public void setRepository( ManagedRepository repo )
322 this.repository = repo;
326 public StorageAsset toFile( VersionedReference reference )
331 private Map<ArtifactReference, String> refs = new HashMap<>();
334 public ArtifactReference toArtifactReference( String path ) throws LayoutException
336 if ( StringUtils.isBlank( path ) )
338 throw new LayoutException( "Unable to convert blank path." );
341 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
343 ArtifactReference artifact = new ArtifactReference();
344 artifact.setGroupId( metadata.getNamespace() );
345 artifact.setArtifactId( metadata.getProject() );
346 artifact.setVersion( metadata.getVersion() );
347 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
350 artifact.setClassifier( facet.getClassifier() );
351 artifact.setType( facet.getType() );
353 refs.put(artifact, path);
357 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
359 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
361 int len = parts.length;
364 throw new IllegalArgumentException(
365 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
368 String id = parts[--len];
369 String baseVersion = parts[--len];
370 String artifactId = parts[--len];
371 StringBuilder groupIdBuilder = new StringBuilder();
372 for ( int i = 0; i < len - 1; i++ )
374 groupIdBuilder.append( parts[i] );
375 groupIdBuilder.append( '.' );
377 groupIdBuilder.append( parts[len - 1] );
379 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
382 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
386 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
389 if ( !id.startsWith( projectId + "-" ) )
391 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
392 + "' doesn't start with artifact ID '" + projectId + "'" );
395 MavenArtifactFacet facet = new MavenArtifactFacet();
397 int index = projectId.length() + 1;
399 String idSubStrFromVersion = id.substring( index );
400 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
402 // non-snapshot versions, or non-timestamped snapshot versions
403 version = projectVersion;
405 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
407 // timestamped snapshots
410 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
411 if ( mainVersionLength == 0 )
413 throw new IllegalArgumentException(
414 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
417 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
419 String timestamp = m.group( 1 );
420 String buildNumber = m.group( 2 );
421 facet.setTimestamp( timestamp );
422 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
423 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
425 catch ( IllegalStateException e )
427 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
428 + "' doesn't contain a timestamped version matching snapshot '"
429 + projectVersion + "'", e);
435 throw new IllegalArgumentException(
436 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
437 + projectVersion + "'" );
442 index += version.length();
443 if ( index == id.length() )
445 // no classifier or extension
451 char c = id.charAt( index );
454 // classifier up until '.'
455 int extIndex = id.indexOf( '.', index );
458 classifier = id.substring( index + 1, extIndex );
459 ext = id.substring( extIndex + 1 );
463 classifier = id.substring( index + 1 );
469 // rest is the extension
471 ext = id.substring( index + 1 );
475 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
476 + "' expected classifier or extension but got '"
477 + id.substring( index ) + "'" );
481 ArtifactMetadata metadata = new ArtifactMetadata();
482 metadata.setId( id );
483 metadata.setNamespace( namespace );
484 metadata.setProject( projectId );
485 metadata.setRepositoryId( repoId );
486 metadata.setProjectVersion( projectVersion );
487 metadata.setVersion( version );
489 facet.setClassifier( classifier );
491 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
492 // to select the correct order to apply multiple extensions mappings to a preferred type
493 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
494 // perhaps the plugins could register missing entries in configuration, then we just use configuration
500 // use extension as default
506 // TODO: should we allow this instead?
509 throw new IllegalArgumentException(
510 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
513 facet.setType( type );
514 metadata.addFacet( facet );
521 public StorageAsset toFile( ArtifactReference reference )
523 return getRepoRootAsset().resolve( refs.get(reference));
527 public StorageAsset toFile( ArchivaArtifact reference )
532 private String formatAsDirectory( String directory )
534 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
537 public String toMetadataPath( ProjectReference reference )
539 StringBuilder path = new StringBuilder();
541 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
542 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
543 path.append( MAVEN_METADATA );
545 return path.toString();
548 public String toMetadataPath( VersionedReference reference )
550 StringBuilder path = new StringBuilder();
552 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
553 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
554 if ( reference.getVersion() != null )
556 // add the version only if it is present
557 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
559 path.append( MAVEN_METADATA );
561 return path.toString();
565 public String toPath( ArtifactReference reference )
571 public String toPath( ItemSelector selector )
577 public ItemSelector toItemSelector( String path ) throws LayoutException
583 public String toPath( ArchivaArtifact reference )