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.model.maven2.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 Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
111 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
117 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
123 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
129 public List<? extends Artifact> getAllArtifacts( ItemSelector selector ) throws ContentAccessException
135 public Stream<? extends Artifact> getAllArtifactStream( ItemSelector selector ) throws ContentAccessException
141 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
147 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
153 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
159 public List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse ) throws ContentAccessException
165 public Stream<? extends Artifact> getArtifactStream( ContentItem item ) throws ContentAccessException
171 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse ) throws ContentAccessException
177 public boolean hasContent( ItemSelector selector )
183 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
189 public ContentItem toItem( String path ) throws LayoutException
195 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
201 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
207 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
213 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
219 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
225 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
231 public String getId( )
233 return repository.getId();
237 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
243 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
249 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
255 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
261 public String getRepoRoot( )
263 return getRepoRootAsset().getFilePath().toString();
266 private StorageAsset getRepoRootAsset() {
267 if (fsStorage==null) {
269 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
270 } catch (IOException e) {
274 return fsStorage.getAsset("");
278 public ManagedRepository getRepository( )
284 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
290 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
296 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
302 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
308 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
314 public void setRepository( ManagedRepository repo )
316 this.repository = repo;
320 public StorageAsset toFile( VersionedReference reference )
325 private Map<ArtifactReference, String> refs = new HashMap<>();
328 public ArtifactReference toArtifactReference( String path ) throws LayoutException
330 if ( StringUtils.isBlank( path ) )
332 throw new LayoutException( "Unable to convert blank path." );
335 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
337 ArtifactReference artifact = new ArtifactReference();
338 artifact.setGroupId( metadata.getNamespace() );
339 artifact.setArtifactId( metadata.getProject() );
340 artifact.setVersion( metadata.getVersion() );
341 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
344 artifact.setClassifier( facet.getClassifier() );
345 artifact.setType( facet.getType() );
347 refs.put(artifact, path);
351 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
353 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
355 int len = parts.length;
358 throw new IllegalArgumentException(
359 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
362 String id = parts[--len];
363 String baseVersion = parts[--len];
364 String artifactId = parts[--len];
365 StringBuilder groupIdBuilder = new StringBuilder();
366 for ( int i = 0; i < len - 1; i++ )
368 groupIdBuilder.append( parts[i] );
369 groupIdBuilder.append( '.' );
371 groupIdBuilder.append( parts[len - 1] );
373 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
376 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
380 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
383 if ( !id.startsWith( projectId + "-" ) )
385 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
386 + "' doesn't start with artifact ID '" + projectId + "'" );
389 MavenArtifactFacet facet = new MavenArtifactFacet();
391 int index = projectId.length() + 1;
393 String idSubStrFromVersion = id.substring( index );
394 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
396 // non-snapshot versions, or non-timestamped snapshot versions
397 version = projectVersion;
399 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
401 // timestamped snapshots
404 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
405 if ( mainVersionLength == 0 )
407 throw new IllegalArgumentException(
408 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
411 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
413 String timestamp = m.group( 1 );
414 String buildNumber = m.group( 2 );
415 facet.setTimestamp( timestamp );
416 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
417 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
419 catch ( IllegalStateException e )
421 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
422 + "' doesn't contain a timestamped version matching snapshot '"
423 + projectVersion + "'", e);
429 throw new IllegalArgumentException(
430 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
431 + projectVersion + "'" );
436 index += version.length();
437 if ( index == id.length() )
439 // no classifier or extension
445 char c = id.charAt( index );
448 // classifier up until '.'
449 int extIndex = id.indexOf( '.', index );
452 classifier = id.substring( index + 1, extIndex );
453 ext = id.substring( extIndex + 1 );
457 classifier = id.substring( index + 1 );
463 // rest is the extension
465 ext = id.substring( index + 1 );
469 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
470 + "' expected classifier or extension but got '"
471 + id.substring( index ) + "'" );
475 ArtifactMetadata metadata = new ArtifactMetadata();
476 metadata.setId( id );
477 metadata.setNamespace( namespace );
478 metadata.setProject( projectId );
479 metadata.setRepositoryId( repoId );
480 metadata.setProjectVersion( projectVersion );
481 metadata.setVersion( version );
483 facet.setClassifier( classifier );
485 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
486 // to select the correct order to apply multiple extensions mappings to a preferred type
487 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
488 // perhaps the plugins could register missing entries in configuration, then we just use configuration
494 // use extension as default
500 // TODO: should we allow this instead?
503 throw new IllegalArgumentException(
504 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
507 facet.setType( type );
508 metadata.addFacet( facet );
515 public StorageAsset toFile( ArtifactReference reference )
517 return getRepoRootAsset().resolve( refs.get(reference));
521 public StorageAsset toFile( ArchivaArtifact reference )
526 private String formatAsDirectory( String directory )
528 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
531 public String toMetadataPath( ProjectReference reference )
533 StringBuilder path = new StringBuilder();
535 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
536 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
537 path.append( MAVEN_METADATA );
539 return path.toString();
542 public String toMetadataPath( VersionedReference reference )
544 StringBuilder path = new StringBuilder();
546 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
547 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
548 if ( reference.getVersion() != null )
550 // add the version only if it is present
551 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
553 path.append( MAVEN_METADATA );
555 return path.toString();
559 public String toPath( ArtifactReference reference )
565 public String toPath( ItemSelector selector )
571 public ItemSelector toItemSelector( String path ) throws LayoutException
577 public String toPath( ArchivaArtifact reference )