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.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;
33 import java.nio.file.Paths;
34 import java.util.HashMap;
37 import java.util.regex.Matcher;
38 import java.util.regex.Pattern;
41 * @author Martin Stockhammer <martin_s@apache.org>
43 public class ManagedRepositoryContentMock implements ManagedRepositoryContent
45 private static final String PATH_SEPARATOR = "/";
46 private static final String GROUP_SEPARATOR = ".";
47 public static final String MAVEN_METADATA = "maven-metadata.xml";
50 private ManagedRepository repository;
52 public ManagedRepositoryContentMock(ManagedRepository repo) {
53 this.repository = repo;
57 public void deleteVersion( VersionedReference reference ) throws ContentNotFoundException
63 public void deleteArtifact( ArtifactReference artifactReference ) throws ContentNotFoundException
69 public void deleteGroupId( String groupId ) throws ContentNotFoundException
75 public void deleteProject( String namespace, String projectId ) throws RepositoryException
81 public String getId( )
83 return repository.getId();
87 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference ) throws ContentNotFoundException
93 public String getRepoRoot( )
95 return Paths.get("", "target", "test-repository", "managed").toString();
99 public ManagedRepository getRepository( )
105 public Set<String> getVersions( ProjectReference reference ) throws ContentNotFoundException, LayoutException
111 public Set<String> getVersions( VersionedReference reference ) throws ContentNotFoundException
117 public boolean hasContent( ArtifactReference reference )
123 public boolean hasContent( ProjectReference reference )
129 public boolean hasContent( VersionedReference reference )
135 public void setRepository( ManagedRepository repo )
137 this.repository = repo;
140 private Map<ArtifactReference, String> refs = new HashMap<>();
143 public ArtifactReference toArtifactReference( String path ) throws LayoutException
145 if ( StringUtils.isBlank( path ) )
147 throw new LayoutException( "Unable to convert blank path." );
150 ArtifactMetadata metadata = getArtifactForPath("test-repository", path);
152 ArtifactReference artifact = new ArtifactReference();
153 artifact.setGroupId( metadata.getNamespace() );
154 artifact.setArtifactId( metadata.getProject() );
155 artifact.setVersion( metadata.getVersion() );
156 MavenArtifactFacet facet = (MavenArtifactFacet) metadata.getFacet( MavenArtifactFacet.FACET_ID );
159 artifact.setClassifier( facet.getClassifier() );
160 artifact.setType( facet.getType() );
162 refs.put(artifact, path);
166 public ArtifactMetadata getArtifactForPath( String repoId, String relativePath )
168 String[] parts = relativePath.replace( '\\', '/' ).split( "/" );
170 int len = parts.length;
173 throw new IllegalArgumentException(
174 "Not a valid artifact path in a Maven 2 repository, not enough directories: " + relativePath );
177 String id = parts[--len];
178 String baseVersion = parts[--len];
179 String artifactId = parts[--len];
180 StringBuilder groupIdBuilder = new StringBuilder();
181 for ( int i = 0; i < len - 1; i++ )
183 groupIdBuilder.append( parts[i] );
184 groupIdBuilder.append( '.' );
186 groupIdBuilder.append( parts[len - 1] );
188 return getArtifactFromId( repoId, groupIdBuilder.toString(), artifactId, baseVersion, id );
191 private static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "([0-9]{8}.[0-9]{6})-([0-9]+).*" );
195 public ArtifactMetadata getArtifactFromId( String repoId, String namespace, String projectId, String projectVersion,
198 if ( !id.startsWith( projectId + "-" ) )
200 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
201 + "' doesn't start with artifact ID '" + projectId + "'" );
204 MavenArtifactFacet facet = new MavenArtifactFacet();
206 int index = projectId.length() + 1;
208 String idSubStrFromVersion = id.substring( index );
209 if ( idSubStrFromVersion.startsWith( projectVersion ) && !VersionUtil.isUniqueSnapshot( projectVersion ) )
211 // non-snapshot versions, or non-timestamped snapshot versions
212 version = projectVersion;
214 else if ( VersionUtil.isGenericSnapshot( projectVersion ) )
216 // timestamped snapshots
219 int mainVersionLength = projectVersion.length() - 8; // 8 is length of "SNAPSHOT"
220 if ( mainVersionLength == 0 )
222 throw new IllegalArgumentException(
223 "Timestamped snapshots must contain the main version, filename was '" + id + "'" );
226 Matcher m = TIMESTAMP_PATTERN.matcher( idSubStrFromVersion.substring( mainVersionLength ) );
228 String timestamp = m.group( 1 );
229 String buildNumber = m.group( 2 );
230 facet.setTimestamp( timestamp );
231 facet.setBuildNumber( Integer.parseInt( buildNumber ) );
232 version = idSubStrFromVersion.substring( 0, mainVersionLength ) + timestamp + "-" + buildNumber;
234 catch ( IllegalStateException e )
236 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
237 + "' doesn't contain a timestamped version matching snapshot '"
238 + projectVersion + "'", e);
244 throw new IllegalArgumentException(
245 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' doesn't contain version '"
246 + projectVersion + "'" );
251 index += version.length();
252 if ( index == id.length() )
254 // no classifier or extension
260 char c = id.charAt( index );
263 // classifier up until '.'
264 int extIndex = id.indexOf( '.', index );
267 classifier = id.substring( index + 1, extIndex );
268 ext = id.substring( extIndex + 1 );
272 classifier = id.substring( index + 1 );
278 // rest is the extension
280 ext = id.substring( index + 1 );
284 throw new IllegalArgumentException( "Not a valid artifact path in a Maven 2 repository, filename '" + id
285 + "' expected classifier or extension but got '"
286 + id.substring( index ) + "'" );
290 ArtifactMetadata metadata = new ArtifactMetadata();
291 metadata.setId( id );
292 metadata.setNamespace( namespace );
293 metadata.setProject( projectId );
294 metadata.setRepositoryId( repoId );
295 metadata.setProjectVersion( projectVersion );
296 metadata.setVersion( version );
298 facet.setClassifier( classifier );
300 // we use our own provider here instead of directly accessing Maven's artifact handlers as it has no way
301 // to select the correct order to apply multiple extensions mappings to a preferred type
302 // TODO: this won't allow the user to decide order to apply them if there are conflicts or desired changes -
303 // perhaps the plugins could register missing entries in configuration, then we just use configuration
309 // use extension as default
315 // TODO: should we allow this instead?
318 throw new IllegalArgumentException(
319 "Not a valid artifact path in a Maven 2 repository, filename '" + id + "' does not have a type" );
322 facet.setType( type );
323 metadata.addFacet( facet );
330 public StorageAsset toFile( ArtifactReference reference )
332 return Paths.get(getRepoRoot(), refs.get(reference));
336 public StorageAsset toFile( ArchivaArtifact reference )
341 private String formatAsDirectory( String directory )
343 return directory.replace( GROUP_SEPARATOR, PATH_SEPARATOR );
346 public String toMetadataPath( ProjectReference reference )
348 StringBuilder path = new StringBuilder();
350 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
351 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
352 path.append( MAVEN_METADATA );
354 return path.toString();
357 public String toMetadataPath( VersionedReference reference )
359 StringBuilder path = new StringBuilder();
361 path.append( formatAsDirectory( reference.getGroupId() ) ).append( PATH_SEPARATOR );
362 path.append( reference.getArtifactId() ).append( PATH_SEPARATOR );
363 if ( reference.getVersion() != null )
365 // add the version only if it is present
366 path.append( VersionUtil.getBaseVersion( reference.getVersion() ) ).append( PATH_SEPARATOR );
368 path.append( MAVEN_METADATA );
370 return path.toString();
374 public String toPath( ArtifactReference reference )
380 public String toPath( ArchivaArtifact reference )