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.storage.FilesystemStorage;
32 import org.apache.archiva.repository.storage.StorageAsset;
33 import org.apache.commons.lang3.StringUtils;
35 import java.io.IOException;
36 import java.nio.file.Paths;
37 import java.util.HashMap;
38 import java.util.List;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
45 * @author Martin Stockhammer <martin_s@apache.org>
47 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
49 private static final String PATH_SEPARATOR = "/";
50 private static final String GROUP_SEPARATOR = ".";
51 public static final String MAVEN_METADATA = "maven-metadata.xml";
54 private ManagedRepository repository;
55 private FilesystemStorage fsStorage;
57 public ManagedRepositoryContentMock(ManagedRepository repo) {
58 this.repository = repo;
62 public VersionedReference toVersion( String groupId, String artifactId, String version )
68 public VersionedReference toVersion( ArtifactReference artifactReference )
74 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
80 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
86 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
92 public void deleteGroupId( String groupId ) throws ContentNotFoundException
98 public void deleteProject( String namespace, String projectId ) throws RepositoryException
104 public String getId( )
106 return repository.getId();
110 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException
116 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException
122 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException
128 public String getRepoRoot( )
130 return getRepoRootAsset().getFilePath().toString();
133 private StorageAsset getRepoRootAsset() {
134 if (fsStorage==null) {
136 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
137 } catch (IOException e) {
141 return fsStorage.getAsset("");
145 public ManagedRepository getRepository( )
151 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
157 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
163 public boolean hasContent( ArtifactReference reference )
169 public boolean hasContent( ProjectReference reference )
175 public boolean hasContent( VersionedReference reference )
181 public void setRepository( ManagedRepository repo )
183 this.repository = repo;
186 private Map<ArtifactReference, String> refs = new HashMap<>();
189 public ArtifactReference toArtifactReference( String path ) throws LayoutException
191 if ( StringUtils.isBlank( path ) )
193 throw new LayoutException( "Unable to convert blank path." );
196 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
198 ArtifactReference artifact = new ArtifactReference();
199 artifact.setGroupId( metadata.getNamespace() );
200 artifact.setArtifactId( metadata.getProject() );
201 artifact.setVersion( metadata.getVersion() );
202 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
205 artifact.setClassifier( facet.getClassifier() );
206 artifact.setType( facet.getType() );
208 refs.put(artifact, path);
212 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
214 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
216 int len = parts.length;
219 throw new IllegalArgumentException(
220 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
223 String id = parts[--len];
224 String baseVersion = parts[--len];
225 String artifactId = parts[--len];
226 StringBuilder groupIdBuilder = new StringBuilder();
227 for ( int i = 0; i < len - 1; i++ )
229 groupIdBuilder.append( parts[i] );
230 groupIdBuilder.append( '.' );
232 groupIdBuilder.append( parts[len - 1] );
234 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
237 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
241 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
244 if ( !id.startsWith( projectId + "-" ) )
246 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
247 + "' doesn't start with artifact ID '" + projectId + "'" );
250 MavenArtifactFacet facet = new MavenArtifactFacet();
252 int index = projectId.length() + 1;
254 String idSubStrFromVersion = id.substring( index );
255 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
257 // non-snapshot versions, or non-timestamped snapshot versions
258 version = projectVersion;
260 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
262 // timestamped snapshots
265 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
266 if ( mainVersionLength == 0 )
268 throw new IllegalArgumentException(
269 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
272 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
274 String timestamp = m.group( 1 );
275 String buildNumber = m.group( 2 );
276 facet.setTimestamp( timestamp );
277 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
278 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
280 catch ( IllegalStateException e )
282 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
283 + "' doesn't contain a timestamped version matching snapshot '"
284 + projectVersion + "'", e);
290 throw new IllegalArgumentException(
291 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
292 + projectVersion + "'" );
297 index += version.length();
298 if ( index == id.length() )
300 // no classifier or extension
306 char c = id.charAt( index );
309 // classifier up until '.'
310 int extIndex = id.indexOf( '.', index );
313 classifier = id.substring( index + 1, extIndex );
314 ext = id.substring( extIndex + 1 );
318 classifier = id.substring( index + 1 );
324 // rest is the extension
326 ext = id.substring( index + 1 );
330 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
331 + "' expected classifier or extension but got '"
332 + id.substring( index ) + "'" );
336 ArtifactMetadata metadata = new ArtifactMetadata();
337 metadata.setId( id );
338 metadata.setNamespace( namespace );
339 metadata.setProject( projectId );
340 metadata.setRepositoryId( repoId );
341 metadata.setProjectVersion( projectVersion );
342 metadata.setVersion( version );
344 facet.setClassifier( classifier );
346 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
347 // to select the correct order to apply multiple extensions mappings to a preferred type
348 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
349 // perhaps the plugins could register missing entries in configuration, then we just use configuration
355 // use extension as default
361 // TODO: should we allow this instead?
364 throw new IllegalArgumentException(
365 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
368 facet.setType( type );
369 metadata.addFacet( facet );
376 public StorageAsset toFile( ArtifactReference reference )
378 return getRepoRootAsset().resolve(refs.get(reference));
382 public StorageAsset toFile( ArchivaArtifact reference )
387 private String formatAsDirectory( String directory )
389 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
392 public String toMetadataPath( ProjectReference reference )
394 StringBuilder path = new StringBuilder();
396 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
397 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
398 path.append( MAVEN_METADATA );
400 return path.toString();
403 public String toMetadataPath( VersionedReference reference )
405 StringBuilder path = new StringBuilder();
407 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
408 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
409 if ( reference.getVersion() != null )
411 // add the version only if it is present
412 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
414 path.append( MAVEN_METADATA );
416 return path.toString();
420 public String toPath( ArtifactReference reference )
426 public String toPath( ArchivaArtifact reference )