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.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.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 Namespace getNamespace( ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
107 public Project getProject( ItemSelector projectSelector ) throws ContentAccessException, IllegalArgumentException
113 public Version getVersion( ItemSelector versionCoordinates ) throws ContentAccessException, IllegalArgumentException
119 public Artifact getArtifact( ItemSelector selector ) throws ContentAccessException
125 public List<? extends Artifact> getAllArtifacts( ItemSelector selector ) throws ContentAccessException
131 public Stream<? extends Artifact> getAllArtifactStream( ItemSelector selector ) throws ContentAccessException
137 public List<? extends Project> getProjects( Namespace namespace ) throws ContentAccessException
143 public List<? extends Version> getVersions( Project project ) throws ContentAccessException
149 public List<? extends Artifact> getArtifacts( ContentItem item ) throws ContentAccessException
155 public List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse ) throws ContentAccessException
161 public Stream<? extends Artifact> getArtifactStream( ContentItem item ) throws ContentAccessException
167 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse ) throws ContentAccessException
173 public boolean hasContent( ItemSelector selector )
179 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
185 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
191 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
197 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
203 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
209 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
215 public String getId( )
217 return repository.getId();
221 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
227 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
233 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
239 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
245 public String getRepoRoot( )
247 return getRepoRootAsset().getFilePath().toString();
250 private StorageAsset getRepoRootAsset() {
251 if (fsStorage==null) {
253 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
254 } catch (IOException e) {
258 return fsStorage.getAsset("");
262 public ManagedRepository getRepository( )
268 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
274 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
280 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
286 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
292 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
298 public void setRepository( ManagedRepository repo )
300 this.repository = repo;
304 public StorageAsset toFile( VersionedReference reference )
309 private Map<ArtifactReference, String> refs = new HashMap<>();
312 public ArtifactReference toArtifactReference( String path ) throws LayoutException
314 if ( StringUtils.isBlank( path ) )
316 throw new LayoutException( "Unable to convert blank path." );
319 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
321 ArtifactReference artifact = new ArtifactReference();
322 artifact.setGroupId( metadata.getNamespace() );
323 artifact.setArtifactId( metadata.getProject() );
324 artifact.setVersion( metadata.getVersion() );
325 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
328 artifact.setClassifier( facet.getClassifier() );
329 artifact.setType( facet.getType() );
331 refs.put(artifact, path);
335 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
337 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
339 int len = parts.length;
342 throw new IllegalArgumentException(
343 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
346 String id = parts[--len];
347 String baseVersion = parts[--len];
348 String artifactId = parts[--len];
349 StringBuilder groupIdBuilder = new StringBuilder();
350 for ( int i = 0; i < len - 1; i++ )
352 groupIdBuilder.append( parts[i] );
353 groupIdBuilder.append( '.' );
355 groupIdBuilder.append( parts[len - 1] );
357 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
360 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
364 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
367 if ( !id.startsWith( projectId + "-" ) )
369 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
370 + "' doesn't start with artifact ID '" + projectId + "'" );
373 MavenArtifactFacet facet = new MavenArtifactFacet();
375 int index = projectId.length() + 1;
377 String idSubStrFromVersion = id.substring( index );
378 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
380 // non-snapshot versions, or non-timestamped snapshot versions
381 version = projectVersion;
383 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
385 // timestamped snapshots
388 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
389 if ( mainVersionLength == 0 )
391 throw new IllegalArgumentException(
392 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
395 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
397 String timestamp = m.group( 1 );
398 String buildNumber = m.group( 2 );
399 facet.setTimestamp( timestamp );
400 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
401 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
403 catch ( IllegalStateException e )
405 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
406 + "' doesn't contain a timestamped version matching snapshot '"
407 + projectVersion + "'", e);
413 throw new IllegalArgumentException(
414 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
415 + projectVersion + "'" );
420 index += version.length();
421 if ( index == id.length() )
423 // no classifier or extension
429 char c = id.charAt( index );
432 // classifier up until '.'
433 int extIndex = id.indexOf( '.', index );
436 classifier = id.substring( index + 1, extIndex );
437 ext = id.substring( extIndex + 1 );
441 classifier = id.substring( index + 1 );
447 // rest is the extension
449 ext = id.substring( index + 1 );
453 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
454 + "' expected classifier or extension but got '"
455 + id.substring( index ) + "'" );
459 ArtifactMetadata metadata = new ArtifactMetadata();
460 metadata.setId( id );
461 metadata.setNamespace( namespace );
462 metadata.setProject( projectId );
463 metadata.setRepositoryId( repoId );
464 metadata.setProjectVersion( projectVersion );
465 metadata.setVersion( version );
467 facet.setClassifier( classifier );
469 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
470 // to select the correct order to apply multiple extensions mappings to a preferred type
471 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
472 // perhaps the plugins could register missing entries in configuration, then we just use configuration
478 // use extension as default
484 // TODO: should we allow this instead?
487 throw new IllegalArgumentException(
488 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
491 facet.setType( type );
492 metadata.addFacet( facet );
499 public StorageAsset toFile( ArtifactReference reference )
501 return getRepoRootAsset().resolve(refs.get(reference));
505 public StorageAsset toFile( ArchivaArtifact reference )
510 private String formatAsDirectory( String directory )
512 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
515 public String toMetadataPath( ProjectReference reference )
517 StringBuilder path = new StringBuilder();
519 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
520 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
521 path.append( MAVEN_METADATA );
523 return path.toString();
526 public String toMetadataPath( VersionedReference reference )
528 StringBuilder path = new StringBuilder();
530 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
531 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
532 if ( reference.getVersion() != null )
534 // add the version only if it is present
535 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
537 path.append( MAVEN_METADATA );
539 return path.toString();
543 public String toPath( ArtifactReference reference )
549 public String toPath( ItemSelector selector )
555 public ItemSelector toItemSelector( String path ) throws LayoutException
561 public String toPath( ArchivaArtifact reference )