1 package org.apache.archiva.repository.scanner.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.StorageAsset;
40 import org.apache.commons.lang3.StringUtils;
42 import java.io.IOException;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.HashMap;
46 import java.util.List;
49 import java.util.regex.Matcher;
50 import java.util.regex.Pattern;
51 import java.util.stream.Stream;
54 * @author Martin Stockhammer <martin_s@apache.org>
56 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
58 private static final String PATH_SEPARATOR = "/";
59 private static final String GROUP_SEPARATOR = ".";
60 public static final String MAVEN_METADATA = "maven-metadata.xml";
63 private ManagedRepository repository;
64 private FilesystemStorage fsStorage;
66 public ManagedRepositoryContentMock(ManagedRepository repo) {
67 this.repository = repo;
71 public VersionedReference toVersion( String groupId, String artifactId, String version )
77 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
83 public VersionedReference toVersion( ArtifactReference artifactReference )
89 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
95 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
101 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
107 public Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
113 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
119 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
125 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
131 public List<? extends Artifact> getArtifacts( ItemSelector selector ) throws ContentAccessException
137 public Stream<? extends Artifact> newArtifactStream( ItemSelector selector ) throws ContentAccessException
143 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
149 public List<? extends Project> getProjects( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
155 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
161 public List<? extends Version> getVersions( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
167 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
173 public Stream<? extends Artifact> newArtifactStream( ContentItem item ) throws ContentAccessException
179 public boolean hasContent( ItemSelector selector )
185 public void addArtifact( Path sourceFile, Artifact destination ) throws IllegalArgumentException
191 public ContentItem toItem( String path ) throws LayoutException
197 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
203 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
209 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
215 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
221 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
227 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
233 public String getId( )
235 return repository.getId();
239 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
245 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
251 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
257 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
263 public String getRepoRoot( )
265 return getRepoRootAsset().getFilePath().toString();
268 private StorageAsset getRepoRootAsset() {
269 if (fsStorage==null) {
271 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
272 } catch (IOException e) {
276 return fsStorage.getAsset("");
280 public ManagedRepository getRepository( )
286 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
292 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
298 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
304 public void setRepository( ManagedRepository repo )
306 this.repository = repo;
310 public StorageAsset toFile( VersionedReference reference )
315 private Map<ArtifactReference, String> refs = new HashMap<>();
318 public ArtifactReference toArtifactReference( String path ) throws LayoutException
320 if ( StringUtils.isBlank( path ) )
322 throw new LayoutException( "Unable to convert blank path." );
325 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
327 ArtifactReference artifact = new ArtifactReference();
328 artifact.setGroupId( metadata.getNamespace() );
329 artifact.setArtifactId( metadata.getProject() );
330 artifact.setVersion( metadata.getVersion() );
331 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
334 artifact.setClassifier( facet.getClassifier() );
335 artifact.setType( facet.getType() );
337 refs.put(artifact, path);
341 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
343 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
345 int len = parts.length;
348 throw new IllegalArgumentException(
349 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
352 String id = parts[--len];
353 String baseVersion = parts[--len];
354 String artifactId = parts[--len];
355 StringBuilder groupIdBuilder = new StringBuilder();
356 for ( int i = 0; i < len - 1; i++ )
358 groupIdBuilder.append( parts[i] );
359 groupIdBuilder.append( '.' );
361 groupIdBuilder.append( parts[len - 1] );
363 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
366 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
370 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
373 if ( !id.startsWith( projectId + "-" ) )
375 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
376 + "' doesn't start with artifact ID '" + projectId + "'" );
379 MavenArtifactFacet facet = new MavenArtifactFacet();
381 int index = projectId.length() + 1;
383 String idSubStrFromVersion = id.substring( index );
384 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
386 // non-snapshot versions, or non-timestamped snapshot versions
387 version = projectVersion;
389 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
391 // timestamped snapshots
394 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
395 if ( mainVersionLength == 0 )
397 throw new IllegalArgumentException(
398 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
401 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
403 String timestamp = m.group( 1 );
404 String buildNumber = m.group( 2 );
405 facet.setTimestamp( timestamp );
406 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
407 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
409 catch ( IllegalStateException e )
411 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
412 + "' doesn't contain a timestamped version matching snapshot '"
413 + projectVersion + "'", e);
419 throw new IllegalArgumentException(
420 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
421 + projectVersion + "'" );
426 index += version.length();
427 if ( index == id.length() )
429 // no classifier or extension
435 char c = id.charAt( index );
438 // classifier up until '.'
439 int extIndex = id.indexOf( '.', index );
442 classifier = id.substring( index + 1, extIndex );
443 ext = id.substring( extIndex + 1 );
447 classifier = id.substring( index + 1 );
453 // rest is the extension
455 ext = id.substring( index + 1 );
459 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
460 + "' expected classifier or extension but got '"
461 + id.substring( index ) + "'" );
465 ArtifactMetadata metadata = new ArtifactMetadata();
466 metadata.setId( id );
467 metadata.setNamespace( namespace );
468 metadata.setProject( projectId );
469 metadata.setRepositoryId( repoId );
470 metadata.setProjectVersion( projectVersion );
471 metadata.setVersion( version );
473 facet.setClassifier( classifier );
475 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
476 // to select the correct order to apply multiple extensions mappings to a preferred type
477 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
478 // perhaps the plugins could register missing entries in configuration, then we just use configuration
484 // use extension as default
490 // TODO: should we allow this instead?
493 throw new IllegalArgumentException(
494 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
497 facet.setType( type );
498 metadata.addFacet( facet );
505 public StorageAsset toFile( ArtifactReference reference )
507 return getRepoRootAsset().resolve(refs.get(reference));
511 public StorageAsset toFile( ArchivaArtifact reference )
516 private String formatAsDirectory( String directory )
518 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
521 public String toMetadataPath( ProjectReference reference )
523 StringBuilder path = new StringBuilder();
525 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
526 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
527 path.append( MAVEN_METADATA );
529 return path.toString();
532 public String toMetadataPath( VersionedReference reference )
534 StringBuilder path = new StringBuilder();
536 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
537 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
538 if ( reference.getVersion() != null )
540 // add the version only if it is present
541 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
543 path.append( MAVEN_METADATA );
545 return path.toString();
549 public String toPath( ArtifactReference reference )
555 public String toPath( ItemSelector selector )
561 public ItemSelector toItemSelector( String path ) throws LayoutException
567 public String toPath( ArchivaArtifact reference )