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.ItemSelector;
32 import org.apache.archiva.repository.storage.FilesystemStorage;
33 import org.apache.archiva.repository.storage.StorageAsset;
34 import org.apache.commons.lang3.StringUtils;
36 import java.io.IOException;
37 import java.nio.file.Paths;
38 import java.util.HashMap;
39 import java.util.List;
42 import java.util.regex.Matcher;
43 import java.util.regex.Pattern;
46 * @author Martin Stockhammer <martin_s@apache.org>
48 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
50 private static final String PATH_SEPARATOR = "/";
51 private static final String GROUP_SEPARATOR = ".";
52 public static final String MAVEN_METADATA = "maven-metadata.xml";
55 private ManagedRepository repository;
56 private FilesystemStorage fsStorage;
58 public ManagedRepositoryContentMock(ManagedRepository repo) {
59 this.repository = repo;
63 public VersionedReference toVersion( String groupId, String artifactId, String version )
69 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
75 public VersionedReference toVersion( ArtifactReference artifactReference )
81 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
87 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException
93 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException, ContentAccessException
99 public void deleteGroupId( String groupId ) throws ContentNotFoundException, ContentAccessException
105 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
111 public void deleteProject( ProjectReference reference ) throws ContentNotFoundException, ContentAccessException
117 public String getId( )
119 return repository.getId();
123 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
129 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
135 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
141 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
147 public String getRepoRoot( )
149 return getRepoRootAsset().getFilePath().toString();
152 private StorageAsset getRepoRootAsset() {
153 if (fsStorage==null) {
155 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
156 } catch (IOException e) {
160 return fsStorage.getAsset("");
164 public ManagedRepository getRepository( )
170 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
176 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException, ContentAccessException, LayoutException
182 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
188 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
194 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
200 public void setRepository( ManagedRepository repo )
202 this.repository = repo;
206 public StorageAsset toFile( VersionedReference reference )
211 private Map<ArtifactReference, String> refs = new HashMap<>();
214 public ArtifactReference toArtifactReference( String path ) throws LayoutException
216 if ( StringUtils.isBlank( path ) )
218 throw new LayoutException( "Unable to convert blank path." );
221 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
223 ArtifactReference artifact = new ArtifactReference();
224 artifact.setGroupId( metadata.getNamespace() );
225 artifact.setArtifactId( metadata.getProject() );
226 artifact.setVersion( metadata.getVersion() );
227 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
230 artifact.setClassifier( facet.getClassifier() );
231 artifact.setType( facet.getType() );
233 refs.put(artifact, path);
237 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
239 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
241 int len = parts.length;
244 throw new IllegalArgumentException(
245 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
248 String id = parts[--len];
249 String baseVersion = parts[--len];
250 String artifactId = parts[--len];
251 StringBuilder groupIdBuilder = new StringBuilder();
252 for ( int i = 0; i < len - 1; i++ )
254 groupIdBuilder.append( parts[i] );
255 groupIdBuilder.append( '.' );
257 groupIdBuilder.append( parts[len - 1] );
259 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
262 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
266 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
269 if ( !id.startsWith( projectId + "-" ) )
271 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
272 + "' doesn't start with artifact ID '" + projectId + "'" );
275 MavenArtifactFacet facet = new MavenArtifactFacet();
277 int index = projectId.length() + 1;
279 String idSubStrFromVersion = id.substring( index );
280 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
282 // non-snapshot versions, or non-timestamped snapshot versions
283 version = projectVersion;
285 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
287 // timestamped snapshots
290 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
291 if ( mainVersionLength == 0 )
293 throw new IllegalArgumentException(
294 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
297 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
299 String timestamp = m.group( 1 );
300 String buildNumber = m.group( 2 );
301 facet.setTimestamp( timestamp );
302 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
303 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
305 catch ( IllegalStateException e )
307 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
308 + "' doesn't contain a timestamped version matching snapshot '"
309 + projectVersion + "'", e);
315 throw new IllegalArgumentException(
316 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
317 + projectVersion + "'" );
322 index += version.length();
323 if ( index == id.length() )
325 // no classifier or extension
331 char c = id.charAt( index );
334 // classifier up until '.'
335 int extIndex = id.indexOf( '.', index );
338 classifier = id.substring( index + 1, extIndex );
339 ext = id.substring( extIndex + 1 );
343 classifier = id.substring( index + 1 );
349 // rest is the extension
351 ext = id.substring( index + 1 );
355 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
356 + "' expected classifier or extension but got '"
357 + id.substring( index ) + "'" );
361 ArtifactMetadata metadata = new ArtifactMetadata();
362 metadata.setId( id );
363 metadata.setNamespace( namespace );
364 metadata.setProject( projectId );
365 metadata.setRepositoryId( repoId );
366 metadata.setProjectVersion( projectVersion );
367 metadata.setVersion( version );
369 facet.setClassifier( classifier );
371 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
372 // to select the correct order to apply multiple extensions mappings to a preferred type
373 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
374 // perhaps the plugins could register missing entries in configuration, then we just use configuration
380 // use extension as default
386 // TODO: should we allow this instead?
389 throw new IllegalArgumentException(
390 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
393 facet.setType( type );
394 metadata.addFacet( facet );
401 public StorageAsset toFile( ArtifactReference reference )
403 return getRepoRootAsset().resolve(refs.get(reference));
407 public StorageAsset toFile( ArchivaArtifact reference )
412 private String formatAsDirectory( String directory )
414 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
417 public String toMetadataPath( ProjectReference reference )
419 StringBuilder path = new StringBuilder();
421 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
422 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
423 path.append( MAVEN_METADATA );
425 return path.toString();
428 public String toMetadataPath( VersionedReference reference )
430 StringBuilder path = new StringBuilder();
432 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
433 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
434 if ( reference.getVersion() != null )
436 // add the version only if it is present
437 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
439 path.append( MAVEN_METADATA );
441 return path.toString();
445 public String toPath( ArtifactReference reference )
451 public String toPath( ItemSelector selector )
457 public String toPath( ArchivaArtifact reference )