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.storage.FilesystemStorage;
32 import org.apache.archiva.repository.storage.RepositoryStorage;
33 import org.apache.archiva.repository.storage.StorageAsset;
34 import org.apache.commons.lang3.StringUtils;
35 import org.springframework.stereotype.Service;
37 import java.io.IOException;
38 import java.nio.file.Paths;
39 import java.util.HashMap;
40 import java.util.List;
43 import java.util.regex.Matcher;
44 import java.util.regex.Pattern;
47 * @author Martin Stockhammer <martin_s@apache.org>
49 @Service("managedRepositoryContent#mock")
50 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
52 private static final String PATH_SEPARATOR = "/";
53 private static final String GROUP_SEPARATOR = ".";
54 public static final String MAVEN_METADATA = "maven-metadata.xml";
57 private ManagedRepository repository;
58 private RepositoryStorage fsStorage;
60 ManagedRepositoryContentMock(ManagedRepository repo) {
61 this.repository = repo;
62 this.fsStorage = repo;
66 public VersionedReference toVersion( String groupId, String artifactId, String version )
72 public VersionedReference toVersion( ArtifactReference artifactReference )
78 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier )
84 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
90 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
96 public void deleteGroupId( String groupId ) throws ContentNotFoundException
102 public void deleteProject( String namespace, String projectId ) throws RepositoryException
108 public String getId( )
110 return repository.getId();
114 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException, LayoutException
120 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException
126 public List<ArtifactReference> getArtifacts( VersionedReference reference ) throws ContentNotFoundException, LayoutException
132 public String getRepoRoot( )
134 return getRepoRootAsset().getFilePath().toString();
137 private StorageAsset getRepoRootAsset() {
138 if (fsStorage==null) {
140 fsStorage = new FilesystemStorage(Paths.get("", "target", "test-repository", "managed"), new DefaultFileLockManager());
141 } catch (IOException e) {
145 return fsStorage.getAsset("");
149 public ManagedRepository getRepository( )
155 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
161 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
167 public boolean hasContent( ArtifactReference reference )
173 public boolean hasContent( ProjectReference reference )
179 public boolean hasContent( VersionedReference reference )
185 public void setRepository( ManagedRepository repo )
187 this.repository = repo;
190 private Map<ArtifactReference, String> refs = new HashMap<>();
193 public ArtifactReference toArtifactReference( String path ) throws LayoutException
195 if ( StringUtils.isBlank( path ) )
197 throw new LayoutException( "Unable to convert blank path." );
200 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
202 ArtifactReference artifact = new ArtifactReference();
203 artifact.setGroupId( metadata.getNamespace() );
204 artifact.setArtifactId( metadata.getProject() );
205 artifact.setVersion( metadata.getVersion() );
206 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
209 artifact.setClassifier( facet.getClassifier() );
210 artifact.setType( facet.getType() );
212 refs.put(artifact, path);
216 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
218 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
220 int len = parts.length;
223 throw new IllegalArgumentException(
224 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
227 String id = parts[--len];
228 String baseVersion = parts[--len];
229 String artifactId = parts[--len];
230 StringBuilder groupIdBuilder = new StringBuilder();
231 for ( int i = 0; i < len - 1; i++ )
233 groupIdBuilder.append( parts[i] );
234 groupIdBuilder.append( '.' );
236 groupIdBuilder.append( parts[len - 1] );
238 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
241 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
245 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
248 if ( !id.startsWith( projectId + "-" ) )
250 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
251 + "' doesn't start with artifact ID '" + projectId + "'" );
254 MavenArtifactFacet facet = new MavenArtifactFacet();
256 int index = projectId.length() + 1;
258 String idSubStrFromVersion = id.substring( index );
259 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
261 // non-snapshot versions, or non-timestamped snapshot versions
262 version = projectVersion;
264 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
266 // timestamped snapshots
269 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
270 if ( mainVersionLength == 0 )
272 throw new IllegalArgumentException(
273 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
276 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
278 String timestamp = m.group( 1 );
279 String buildNumber = m.group( 2 );
280 facet.setTimestamp( timestamp );
281 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
282 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
284 catch ( IllegalStateException e )
286 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
287 + "' doesn't contain a timestamped version matching snapshot '"
288 + projectVersion + "'", e);
294 throw new IllegalArgumentException(
295 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
296 + projectVersion + "'" );
301 index += version.length();
302 if ( index == id.length() )
304 // no classifier or extension
310 char c = id.charAt( index );
313 // classifier up until '.'
314 int extIndex = id.indexOf( '.', index );
317 classifier = id.substring( index + 1, extIndex );
318 ext = id.substring( extIndex + 1 );
322 classifier = id.substring( index + 1 );
328 // rest is the extension
330 ext = id.substring( index + 1 );
334 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
335 + "' expected classifier or extension but got '"
336 + id.substring( index ) + "'" );
340 ArtifactMetadata metadata = new ArtifactMetadata();
341 metadata.setId( id );
342 metadata.setNamespace( namespace );
343 metadata.setProject( projectId );
344 metadata.setRepositoryId( repoId );
345 metadata.setProjectVersion( projectVersion );
346 metadata.setVersion( version );
348 facet.setClassifier( classifier );
350 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
351 // to select the correct order to apply multiple extensions mappings to a preferred type
352 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
353 // perhaps the plugins could register missing entries in configuration, then we just use configuration
359 // use extension as default
365 // TODO: should we allow this instead?
368 throw new IllegalArgumentException(
369 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
372 facet.setType( type );
373 metadata.addFacet( facet );
380 public StorageAsset toFile( ArtifactReference reference )
382 return getRepoRootAsset().resolve( refs.get(reference));
386 public StorageAsset toFile( ArchivaArtifact reference )
391 private String formatAsDirectory( String directory )
393 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
396 public String toMetadataPath( ProjectReference reference )
398 StringBuilder path = new StringBuilder();
400 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
401 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
402 path.append( MAVEN_METADATA );
404 return path.toString();
407 public String toMetadataPath( VersionedReference reference )
409 StringBuilder path = new StringBuilder();
411 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
412 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
413 if ( reference.getVersion() != null )
415 // add the version only if it is present
416 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
418 path.append( MAVEN_METADATA );
420 return path.toString();
424 public String toPath( ArtifactReference reference )
430 public String toPath( ArchivaArtifact reference )