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> getArtifactsStartingWith( Namespace namespace ) throws ContentAccessException
165 public Stream<? extends Artifact> getArtifactStream( ContentItem item ) throws ContentAccessException
171 public Stream<? extends Artifact> getArtifactStreamStartingWith( Namespace namespace ) throws ContentAccessException
177 public boolean hasContent( ItemSelector selector )
183 public void copyArtifact( Path sourceFile, ItemSelector destination ) throws IllegalArgumentException
189 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
195 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
201 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
207 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
213 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
219 public String getId( )
221 return repository.getId();
225 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
231 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
237 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
243 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
249 public String getRepoRoot( )
251 return getRepoRootAsset().getFilePath().toString();
254 private StorageAsset getRepoRootAsset() {
255 if (fsStorage==null) {
257 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
258 } catch (IOException e) {
262 return fsStorage.getAsset("");
266 public ManagedRepository getRepository( )
272 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
278 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
284 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
290 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
296 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
302 public void setRepository( ManagedRepository repo )
304 this.repository = repo;
308 public StorageAsset toFile( VersionedReference reference )
313 private Map<ArtifactReference, String> refs = new HashMap<>();
316 public ArtifactReference toArtifactReference( String path ) throws LayoutException
318 if ( StringUtils.isBlank( path ) )
320 throw new LayoutException( "Unable to convert blank path." );
323 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
325 ArtifactReference artifact = new ArtifactReference();
326 artifact.setGroupId( metadata.getNamespace() );
327 artifact.setArtifactId( metadata.getProject() );
328 artifact.setVersion( metadata.getVersion() );
329 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
332 artifact.setClassifier( facet.getClassifier() );
333 artifact.setType( facet.getType() );
335 refs.put(artifact, path);
339 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
341 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
343 int len = parts.length;
346 throw new IllegalArgumentException(
347 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
350 String id = parts[--len];
351 String baseVersion = parts[--len];
352 String artifactId = parts[--len];
353 StringBuilder groupIdBuilder = new StringBuilder();
354 for ( int i = 0; i < len - 1; i++ )
356 groupIdBuilder.append( parts[i] );
357 groupIdBuilder.append( '.' );
359 groupIdBuilder.append( parts[len - 1] );
361 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
364 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
368 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
371 if ( !id.startsWith( projectId + "-" ) )
373 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
374 + "' doesn't start with artifact ID '" + projectId + "'" );
377 MavenArtifactFacet facet = new MavenArtifactFacet();
379 int index = projectId.length() + 1;
381 String idSubStrFromVersion = id.substring( index );
382 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
384 // non-snapshot versions, or non-timestamped snapshot versions
385 version = projectVersion;
387 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
389 // timestamped snapshots
392 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
393 if ( mainVersionLength == 0 )
395 throw new IllegalArgumentException(
396 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
399 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
401 String timestamp = m.group( 1 );
402 String buildNumber = m.group( 2 );
403 facet.setTimestamp( timestamp );
404 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
405 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
407 catch ( IllegalStateException e )
409 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
410 + "' doesn't contain a timestamped version matching snapshot '"
411 + projectVersion + "'", e);
417 throw new IllegalArgumentException(
418 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
419 + projectVersion + "'" );
424 index += version.length();
425 if ( index == id.length() )
427 // no classifier or extension
433 char c = id.charAt( index );
436 // classifier up until '.'
437 int extIndex = id.indexOf( '.', index );
440 classifier = id.substring( index + 1, extIndex );
441 ext = id.substring( extIndex + 1 );
445 classifier = id.substring( index + 1 );
451 // rest is the extension
453 ext = id.substring( index + 1 );
457 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
458 + "' expected classifier or extension but got '"
459 + id.substring( index ) + "'" );
463 ArtifactMetadata metadata = new ArtifactMetadata();
464 metadata.setId( id );
465 metadata.setNamespace( namespace );
466 metadata.setProject( projectId );
467 metadata.setRepositoryId( repoId );
468 metadata.setProjectVersion( projectVersion );
469 metadata.setVersion( version );
471 facet.setClassifier( classifier );
473 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
474 // to select the correct order to apply multiple extensions mappings to a preferred type
475 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
476 // perhaps the plugins could register missing entries in configuration, then we just use configuration
482 // use extension as default
488 // TODO: should we allow this instead?
491 throw new IllegalArgumentException(
492 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
495 facet.setType( type );
496 metadata.addFacet( facet );
503 public StorageAsset toFile( ArtifactReference reference )
505 return getRepoRootAsset().resolve( refs.get(reference));
509 public StorageAsset toFile( ArchivaArtifact reference )
514 private String formatAsDirectory( String directory )
516 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
519 public String toMetadataPath( ProjectReference reference )
521 StringBuilder path = new StringBuilder();
523 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
524 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
525 path.append( MAVEN_METADATA );
527 return path.toString();
530 public String toMetadataPath( VersionedReference reference )
532 StringBuilder path = new StringBuilder();
534 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
535 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
536 if ( reference.getVersion() != null )
538 // add the version only if it is present
539 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
541 path.append( MAVEN_METADATA );
543 return path.toString();
547 public String toPath( ArtifactReference reference )
553 public String toPath( ItemSelector selector )
559 public ItemSelector toItemSelector( String path ) throws LayoutException
565 public String toPath( ArchivaArtifact reference )