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.utils.VersionUtil;
23 import org.apache.archiva.metadata.model.ArtifactMetadata;
24 import org.apache.archiva.metadata.model.maven2.MavenArtifactFacet;
25 import org.apache.archiva.model.ArchivaArtifact;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.*;
30 import org.apache.archiva.repository.content.StorageAsset;
31 import org.apache.commons.lang.StringUtils;
32 import org.springframework.stereotype.Service;
34 import java.nio.file.Paths;
35 import java.util.HashMap;
38 import java.util.regex.Matcher;
39 import java.util.regex.Pattern;
42 * @author Martin Stockhammer <martin_s@apache.org>
44 @Service("managedRepositoryContent#mock")
45 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
47 private static final String PATH_SEPARATOR = "/";
48 private static final String GROUP_SEPARATOR = ".";
49 public static final String MAVEN_METADATA = "maven-metadata.xml";
52 private ManagedRepository repository;
54 ManagedRepositoryContentMock(ManagedRepository repo) {
55 this.repository = repo;
59 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
65 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
71 public void deleteGroupId( String groupId ) throws ContentNotFoundException
77 public void deleteProject( String namespace, String projectId ) throws RepositoryException
83 public String getId( )
85 return repository.getId();
89 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException
95 public String getRepoRoot( )
97 return Paths.get("", "target", "test-repository", "managed").toString();
101 public ManagedRepository getRepository( )
107 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
113 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
119 public boolean hasContent( ArtifactReference reference )
125 public boolean hasContent( ProjectReference reference )
131 public boolean hasContent( VersionedReference reference )
137 public void setRepository( ManagedRepository repo )
139 this.repository = repo;
142 private Map<ArtifactReference, String> refs = new HashMap<>();
145 public ArtifactReference toArtifactReference( String path ) throws LayoutException
147 if ( StringUtils.isBlank( path ) )
149 throw new LayoutException( "Unable to convert blank path." );
152 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
154 ArtifactReference artifact = new ArtifactReference();
155 artifact.setGroupId( metadata.getNamespace() );
156 artifact.setArtifactId( metadata.getProject() );
157 artifact.setVersion( metadata.getVersion() );
158 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
161 artifact.setClassifier( facet.getClassifier() );
162 artifact.setType( facet.getType() );
164 refs.put(artifact, path);
168 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
170 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
172 int len = parts.length;
175 throw new IllegalArgumentException(
176 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
179 String id = parts[--len];
180 String baseVersion = parts[--len];
181 String artifactId = parts[--len];
182 StringBuilder groupIdBuilder = new StringBuilder();
183 for ( int i = 0; i < len - 1; i++ )
185 groupIdBuilder.append( parts[i] );
186 groupIdBuilder.append( '.' );
188 groupIdBuilder.append( parts[len - 1] );
190 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
193 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
197 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
200 if ( !id.startsWith( projectId + "-" ) )
202 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
203 + "' doesn't start with artifact ID '" + projectId + "'" );
206 MavenArtifactFacet facet = new MavenArtifactFacet();
208 int index = projectId.length() + 1;
210 String idSubStrFromVersion = id.substring( index );
211 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
213 // non-snapshot versions, or non-timestamped snapshot versions
214 version = projectVersion;
216 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
218 // timestamped snapshots
221 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
222 if ( mainVersionLength == 0 )
224 throw new IllegalArgumentException(
225 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
228 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
230 String timestamp = m.group( 1 );
231 String buildNumber = m.group( 2 );
232 facet.setTimestamp( timestamp );
233 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
234 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
236 catch ( IllegalStateException e )
238 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
239 + "' doesn't contain a timestamped version matching snapshot '"
240 + projectVersion + "'", e);
246 throw new IllegalArgumentException(
247 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
248 + projectVersion + "'" );
253 index += version.length();
254 if ( index == id.length() )
256 // no classifier or extension
262 char c = id.charAt( index );
265 // classifier up until '.'
266 int extIndex = id.indexOf( '.', index );
269 classifier = id.substring( index + 1, extIndex );
270 ext = id.substring( extIndex + 1 );
274 classifier = id.substring( index + 1 );
280 // rest is the extension
282 ext = id.substring( index + 1 );
286 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
287 + "' expected classifier or extension but got '"
288 + id.substring( index ) + "'" );
292 ArtifactMetadata metadata = new ArtifactMetadata();
293 metadata.setId( id );
294 metadata.setNamespace( namespace );
295 metadata.setProject( projectId );
296 metadata.setRepositoryId( repoId );
297 metadata.setProjectVersion( projectVersion );
298 metadata.setVersion( version );
300 facet.setClassifier( classifier );
302 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
303 // to select the correct order to apply multiple extensions mappings to a preferred type
304 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
305 // perhaps the plugins could register missing entries in configuration, then we just use configuration
311 // use extension as default
317 // TODO: should we allow this instead?
320 throw new IllegalArgumentException(
321 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
324 facet.setType( type );
325 metadata.addFacet( facet );
332 public StorageAsset toFile( ArtifactReference reference )
334 return Paths.get(getRepoRoot(), refs.get(reference));
338 public StorageAsset toFile( ArchivaArtifact reference )
343 private String formatAsDirectory( String directory )
345 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
348 public String toMetadataPath( ProjectReference reference )
350 StringBuilder path = new StringBuilder();
352 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
353 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
354 path.append( MAVEN_METADATA );
356 return path.toString();
359 public String toMetadataPath( VersionedReference reference )
361 StringBuilder path = new StringBuilder();
363 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
364 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
365 if ( reference.getVersion() != null )
367 // add the version only if it is present
368 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
370 path.append( MAVEN_METADATA );
372 return path.toString();
376 public String toPath( ArtifactReference reference )
382 public String toPath( ArchivaArtifact reference )