1 package org.apache.archiva.repository.maven.content;
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
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.common.filelock.FileLockManager;
22 import org.apache.archiva.common.utils.FileUtils;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
26 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
27 import org.apache.archiva.repository.maven.metadata.storage.ArtifactMappingProvider;
28 import org.apache.archiva.repository.maven.metadata.storage.DefaultArtifactMappingProvider;
29 import org.apache.archiva.model.ArchivaArtifact;
30 import org.apache.archiva.model.ArtifactReference;
31 import org.apache.archiva.model.ProjectReference;
32 import org.apache.archiva.model.VersionedReference;
33 import org.apache.archiva.repository.ContentAccessException;
34 import org.apache.archiva.repository.ContentNotFoundException;
35 import org.apache.archiva.repository.EditableManagedRepository;
36 import org.apache.archiva.repository.LayoutException;
37 import org.apache.archiva.repository.ManagedRepository;
38 import org.apache.archiva.repository.ManagedRepositoryContent;
39 import org.apache.archiva.repository.content.Artifact;
40 import org.apache.archiva.repository.content.ContentItem;
41 import org.apache.archiva.repository.content.ItemNotFoundException;
42 import org.apache.archiva.repository.content.ItemSelector;
43 import org.apache.archiva.repository.content.Namespace;
44 import org.apache.archiva.repository.content.Project;
45 import org.apache.archiva.repository.content.Version;
46 import org.apache.archiva.repository.content.base.ArchivaNamespace;
47 import org.apache.archiva.repository.content.base.ArchivaProject;
48 import org.apache.archiva.repository.content.base.ArchivaVersion;
49 import org.apache.archiva.repository.content.base.builder.ArtifactOptBuilder;
50 import org.apache.archiva.repository.storage.RepositoryStorage;
51 import org.apache.archiva.repository.storage.StorageAsset;
52 import org.apache.archiva.repository.storage.util.StorageUtil;
53 import org.apache.commons.collections4.map.ReferenceMap;
54 import org.apache.commons.lang3.StringUtils;
56 import javax.inject.Inject;
57 import javax.inject.Named;
58 import java.io.IOException;
60 import java.nio.file.Files;
61 import java.nio.file.Path;
62 import java.nio.file.Paths;
63 import java.util.Collections;
64 import java.util.List;
65 import java.util.Objects;
66 import java.util.Optional;
68 import java.util.function.Predicate;
69 import java.util.regex.Matcher;
70 import java.util.regex.Pattern;
71 import java.util.stream.Collectors;
72 import java.util.stream.Stream;
75 * ManagedDefaultRepositoryContent
77 public class ManagedDefaultRepositoryContent
78 extends AbstractDefaultRepositoryContent
79 implements ManagedRepositoryContent
82 public static final String METADATA_FILENAME = "maven-metadata.xml";
83 private FileTypes filetypes;
85 public void setFileTypes(FileTypes fileTypes) {
86 this.filetypes = fileTypes;
89 private ManagedRepository repository;
91 private FileLockManager lockManager;
94 @Named("repositoryPathTranslator#maven2")
95 private RepositoryPathTranslator pathTranslator;
98 @Named( "metadataReader#maven" )
99 MavenMetadataReader metadataReader;
102 @Named( "MavenContentHelper" )
103 MavenContentHelper mavenContentHelper;
105 public static final String SNAPSHOT = "SNAPSHOT";
107 public static final Pattern UNIQUE_SNAPSHOT_PATTERN = Pattern.compile( "^(SNAPSHOT|[0-9]{8}\\.[0-9]{6}-[0-9]+)(.*)" );
108 public static final Pattern CLASSIFIER_PATTERN = Pattern.compile( "^-([^.]+)(\\..*)" );
110 public static final Pattern TIMESTAMP_PATTERN = Pattern.compile( "^([0-9]{8})\\.([0-9]{6})$" );
112 public static final Pattern GENERIC_SNAPSHOT_PATTERN = Pattern.compile( "^(.*)-" + SNAPSHOT );
115 * We are caching content items in a weak reference map. To avoid always recreating the
116 * the hierarchical structure.
117 * TODO: Better use a object cache? E.g. our spring cache implementation?
119 private ReferenceMap<String, Namespace> namespaceMap = new ReferenceMap<>( );
120 private ReferenceMap<StorageAsset, Project> projectMap = new ReferenceMap<>( );
121 private ReferenceMap<StorageAsset, Version> versionMap = new ReferenceMap<>( );
122 private ReferenceMap<StorageAsset, Artifact> artifactMap = new ReferenceMap<>( );
124 public ManagedDefaultRepositoryContent() {
125 super(Collections.singletonList( new DefaultArtifactMappingProvider() ));
128 public ManagedDefaultRepositoryContent(ManagedRepository repository, FileTypes fileTypes, FileLockManager lockManager) {
129 super(Collections.singletonList( new DefaultArtifactMappingProvider() ));
130 setFileTypes( fileTypes );
131 this.lockManager = lockManager;
132 setRepository( repository );
135 public ManagedDefaultRepositoryContent( ManagedRepository repository, List<? extends ArtifactMappingProvider> artifactMappingProviders, FileTypes fileTypes, FileLockManager lockManager )
137 super(artifactMappingProviders==null ? Collections.singletonList( new DefaultArtifactMappingProvider() ) : artifactMappingProviders);
138 setFileTypes( fileTypes );
139 this.lockManager = lockManager;
140 setRepository( repository );
145 * Returns a version reference from the coordinates
146 * @param groupId the group id
147 * @param artifactId the artifact id
148 * @param version the version
149 * @return the versioned reference object
152 public VersionedReference toVersion( String groupId, String artifactId, String version ) {
153 return new VersionedReference().groupId( groupId ).artifactId( artifactId ).version( version );
157 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
159 return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), VersionUtil.getBaseVersion( artifactReference.getVersion( ) ));
163 * Return the version the artifact is part of
164 * @param artifactReference
167 public VersionedReference toVersion( ArtifactReference artifactReference) {
168 return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), artifactReference.getVersion( ) );
172 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier) {
173 return new ArtifactReference( ).groupId( groupId ).artifactId( artifactId ).version( version ).type( type ).classifier( classifier );
177 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
179 final Path baseDirectory = getRepoDir( );
180 final Path itemPath = item.getAsset( ).getFilePath( );
181 if ( !Files.exists( itemPath ) )
183 throw new ItemNotFoundException( "The item " + item.toString() + "does not exist in the repository " + getId( ) );
185 if ( !itemPath.toAbsolutePath().startsWith( baseDirectory.toAbsolutePath() ) )
187 log.error( "The namespace {} to delete from repository {} is not a subdirectory of the repository base.", item, getId( ) );
188 log.error( "Namespace directory: {}", itemPath );
189 log.error( "Repository directory: {}", baseDirectory );
190 throw new ContentAccessException( "Inconsistent directories found. Could not delete namespace." );
194 if (Files.isDirectory( itemPath ))
196 FileUtils.deleteDirectory( itemPath );
198 Files.deleteIfExists( itemPath );
201 catch ( IOException e )
203 log.error( "Could not delete namespace directory {}: {}", itemPath, e.getMessage( ), e );
204 throw new ContentAccessException( "Error occured while deleting namespace " + item + ": " + e.getMessage( ), e );
208 private StorageAsset getAssetByPath(String assetPath) {
209 return getStorage( ).getAsset( assetPath );
212 private StorageAsset getAsset(String namespace) {
213 String namespacePath = formatAsDirectory( namespace.trim() );
214 if (StringUtils.isEmpty( namespacePath )) {
217 return getAssetByPath(namespacePath);
220 private StorageAsset getAsset(String namespace, String project) {
221 return getAsset( namespace ).resolve( project );
224 private StorageAsset getAsset(String namespace, String project, String version) {
225 return getAsset( namespace, project ).resolve( version );
228 private StorageAsset getAsset(String namespace, String project, String version, String fileName) {
229 return getAsset( namespace, project, version ).resolve( fileName );
234 public Namespace getNamespace( final ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
236 return namespaceMap.computeIfAbsent( namespaceSelector.getNamespace(),
238 StorageAsset nsPath = getAsset( namespace );
239 return ArchivaNamespace.withRepository( this ).withAsset( nsPath ).
240 withNamespace( namespace ).build( );
246 public Project getProject( final ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
248 if (!selector.hasProjectId()) {
249 throw new IllegalArgumentException( "Project id must be set" );
251 final StorageAsset path = getAsset( selector.getNamespace( ), selector.getProjectId( ) );
252 return projectMap.computeIfAbsent( path, projectPath -> {
253 final Namespace ns = getNamespace( selector );
254 return ArchivaProject.withAsset( projectPath ).withNamespace( ns ).withId( selector.getProjectId( ) ).build( );
261 public Version getVersion( final ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
263 if (!selector.hasProjectId()) {
264 throw new IllegalArgumentException( "Project id must be set" );
266 if (!selector.hasVersion() ) {
267 throw new IllegalArgumentException( "Version must be set" );
269 final StorageAsset path = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
270 return versionMap.computeIfAbsent( path, versionPath -> {
271 final Project project = getProject( selector );
272 return ArchivaVersion.withAsset( path )
273 .withProject( project )
274 .withVersion( selector.getVersion( ) ).build();
280 public Artifact createArtifact(final StorageAsset artifactPath, final ItemSelector selector,
281 final String classifier, final String extension) {
282 Version version = getVersion(selector);
283 ArtifactOptBuilder builder = org.apache.archiva.repository.content.base.ArchivaArtifact.withAsset( artifactPath )
284 .withVersion( version )
285 .withId( selector.getArtifactId( ) )
286 .withArtifactVersion( mavenContentHelper.getArtifactVersion( artifactPath, selector ) )
287 .withClassifier( classifier );
288 if (selector.hasType()) {
289 builder.withType( selector.getType( ) );
291 return builder.build( );
294 public Namespace getNamespaceFromArtifactPath( final StorageAsset artifactPath) {
295 final StorageAsset namespacePath = artifactPath.getParent( ).getParent( ).getParent( );
296 final String namespace = MavenContentHelper.getNamespaceFromNamespacePath( namespacePath );
297 return namespaceMap.computeIfAbsent( namespace,
298 myNamespace -> ArchivaNamespace.withRepository( this )
299 .withAsset( namespacePath )
300 .withNamespace( namespace )
304 private Project getProjectFromArtifactPath( final StorageAsset artifactPath) {
305 final StorageAsset projectPath = artifactPath.getParent( ).getParent( );
306 return projectMap.computeIfAbsent( projectPath,
307 myProjectPath -> ArchivaProject.withAsset( projectPath )
308 .withNamespace( getNamespaceFromArtifactPath( artifactPath ) )
309 .withId( projectPath.getName( ) ).build( )
313 private Version getVersionFromArtifactPath( final StorageAsset artifactPath) {
314 final StorageAsset versionPath = artifactPath.getParent( );
315 return versionMap.computeIfAbsent( versionPath,
316 myVersionPath -> ArchivaVersion.withAsset( versionPath )
317 .withProject( getProjectFromArtifactPath( artifactPath ) )
318 .withVersion( versionPath.getName( ) ).build( ) );
321 private Artifact getArtifactFromPath(final StorageAsset artifactPath) {
322 final Version version = getVersionFromArtifactPath( artifactPath );
323 final ArtifactInfo info = getArtifactInfoFromPath( version.getVersion(), artifactPath );
324 return artifactMap.computeIfAbsent( artifactPath, myArtifactPath ->
325 org.apache.archiva.repository.content.base.ArchivaArtifact.withAsset( artifactPath )
326 .withVersion( version )
328 .withClassifier( info.classifier )
329 .withRemainder( info.remainder )
330 .withType( info.type )
331 .withArtifactVersion( info.version )
332 .withContentType( info.contentType )
337 private ContentItem getItemFromPath(final StorageAsset itemPath) {
338 if (itemPath.isLeaf()) {
339 return getArtifactFromPath( itemPath );
341 if (versionMap.containsKey( itemPath )) {
342 return versionMap.get( itemPath );
344 if (projectMap.containsKey( itemPath )) {
345 return projectMap.get( itemPath );
347 String ns = MavenContentHelper.getNamespaceFromNamespacePath( itemPath );
348 if (namespaceMap.containsKey( ns )) {
349 return namespaceMap.get( ns );
351 // No cached item, so we have to gather more information:
352 // Check for version directory (contains at least a pom or metadata file)
353 if (itemPath.list( ).stream( ).map(a -> a.getName().toLowerCase()).anyMatch( n ->
355 || n.startsWith( "maven-metadata" )
357 return versionMap.computeIfAbsent( itemPath,
358 myVersionPath -> ArchivaVersion.withAsset( itemPath )
359 .withProject( (Project)getItemFromPath( itemPath.getParent() ) )
360 .withVersion( itemPath.getName() ).build());
362 // We have to dig further and find the next directory with a pom
363 Optional<StorageAsset> foundFile = StorageUtil.newAssetStream( itemPath )
364 .filter( a -> a.getName().toLowerCase().endsWith( ".pom" )
365 || a.getName().toLowerCase().startsWith( "maven-metadata" ) )
367 if (foundFile.isPresent())
370 StorageAsset current = foundFile.get( );
371 while (current.hasParent() && !current.equals(itemPath)) {
373 current = current.getParent( );
375 // Project path if it is one level up from the found file
377 return projectMap.computeIfAbsent( itemPath,
378 myItemPath -> getProjectFromArtifactPath( foundFile.get( ) ) );
380 // All other paths are treated as namespace
381 return namespaceMap.computeIfAbsent( ns,
382 myNamespace -> ArchivaNamespace.withRepository( this )
383 .withAsset( itemPath )
388 // Don't know what to do with it, so we treat it as namespace path
389 return namespaceMap.computeIfAbsent( ns,
390 myNamespace -> ArchivaNamespace.withRepository( this )
391 .withAsset( itemPath )
400 // Simple object to hold artifact information
401 private class ArtifactInfo {
403 private String version;
404 private String extension;
405 private String remainder;
407 private String classifier;
408 private String contentType;
411 private ArtifactInfo getArtifactInfoFromPath(String genericVersion, StorageAsset path) {
412 final ArtifactInfo info = new ArtifactInfo( );
413 info.id = path.getParent( ).getParent( ).getName( );
414 final String fileName = path.getName( );
415 if ( genericVersion.endsWith( "-" + SNAPSHOT ) )
417 String baseVersion = StringUtils.substringBeforeLast( genericVersion, "-" + SNAPSHOT );
418 String prefix = info.id+"-"+baseVersion+"-";
419 if (fileName.startsWith( prefix ))
421 String versionPostfix = StringUtils.removeStart( fileName, prefix );
422 Matcher matcher = UNIQUE_SNAPSHOT_PATTERN.matcher( versionPostfix );
423 if (matcher.matches()) {
424 info.version = baseVersion + "-" + matcher.group( 1 );
425 String newPrefix = prefix + info.version;
426 if (fileName.startsWith( newPrefix ))
428 String classPostfix = StringUtils.removeStart( fileName, newPrefix );
429 Matcher cMatch = CLASSIFIER_PATTERN.matcher( classPostfix );
430 if (cMatch.matches()) {
431 info.classifier = cMatch.group( 1 );
432 info.remainder = cMatch.group( 2 );
434 info.classifier = "";
435 info.remainder = classPostfix;
438 log.error( "Artifact does not match the maven name pattern {}", path );
439 info.classifier = "";
440 info.remainder = StringUtils.substringAfter( fileName, prefix );
443 log.error( "Artifact does not match the snapshot version pattern {}", path );
445 info.classifier = "";
446 info.remainder = StringUtils.substringAfter( fileName, prefix );
449 log.error( "Artifact does not match the maven name pattern: {}", path );
451 info.classifier = "";
452 info.remainder = StringUtils.substringAfterLast( fileName, "." );
455 String prefix = info.id+"-"+genericVersion;
456 if (fileName.startsWith( prefix ))
458 info.version=genericVersion;
459 String classPostfix = StringUtils.removeStart( fileName, prefix );
460 Matcher cMatch = CLASSIFIER_PATTERN.matcher( classPostfix );
461 if (cMatch.matches()) {
462 info.classifier = cMatch.group( 1 );
463 info.remainder = cMatch.group( 2 );
465 info.classifier = "";
466 info.remainder = classPostfix;
469 log.error( "Artifact does not match the version pattern {}", path );
471 info.classifier = "";
472 info.remainder = StringUtils.substringAfterLast( fileName, "." );
475 info.extension = StringUtils.substringAfterLast( fileName, "." );
476 info.type = MavenContentHelper.getTypeFromClassifierAndExtension( info.classifier, info.extension );
478 info.contentType = Files.probeContentType( path.getFilePath( ) );
479 } catch (IOException e) {
480 info.contentType = "";
488 public Artifact getArtifact( final ItemSelector selector ) throws ContentAccessException
490 if (!selector.hasProjectId( )) {
491 throw new IllegalArgumentException( "Project id must be set" );
493 if (!selector.hasVersion( )) {
494 throw new IllegalArgumentException( "Version must be set" );
496 if (!selector.hasArtifactId( )) {
497 throw new IllegalArgumentException( "Artifact Id must be set" );
499 final StorageAsset artifactDir = getAsset(selector.getNamespace(), selector.getProjectId(),
500 selector.getVersion());
501 final String artifactVersion = mavenContentHelper.getArtifactVersion( artifactDir, selector );
502 final String classifier = MavenContentHelper.getClassifier( selector );
503 final String extension = MavenContentHelper.getArtifactExtension( selector );
504 final String fileName = MavenContentHelper.getArtifactFileName( selector, artifactVersion, classifier, extension );
505 final StorageAsset path = getAsset( selector.getNamespace( ), selector.getProjectId( ),
506 selector.getVersion( ), fileName );
507 return artifactMap.computeIfAbsent( path, artifactPath -> createArtifact( path, selector, classifier, extension ) );
510 private StorageAsset getBasePathFromSelector(ItemSelector selector) {
511 StringBuilder path = new StringBuilder( );
512 if (selector.hasNamespace()) {
513 path.append(String.join( "/", getNamespace( selector ).getNamespacePath( ) ));
515 if (selector.hasProjectId()) {
516 path.append( "/" ).append( selector.getProjectId( ) );
518 if (selector.hasVersion()) {
519 path.append( "/" ).append( selector.getVersion( ) );
521 return getStorage( ).getAsset( path.toString( ) );
525 * File filter to select certain artifacts using the selector data.
527 private Predicate<StorageAsset> getFileFilterFromSelector(final ItemSelector selector) {
528 Predicate<StorageAsset> p = a -> a.isLeaf( );
529 if (selector.hasArtifactId()) {
530 final String pattern = selector.getArtifactId( );
531 p = p.and( a -> StringUtils.startsWithIgnoreCase( a.getName( ), pattern ) );
533 if (selector.hasArtifactVersion()) {
534 final String pattern = selector.getArtifactVersion( );
535 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ), pattern ) );
537 if (selector.hasExtension()) {
538 final String pattern = "."+selector.getExtension( );
539 p = p.and( a -> StringUtils.endsWithIgnoreCase( a.getName( ), pattern ) );
540 } else if (selector.hasType()) {
541 final String pattern = "."+ MavenContentHelper.getArtifactExtension( selector );
542 p = p.and( a -> StringUtils.endsWithIgnoreCase( a.getName( ), pattern ) );
544 if (selector.hasClassifier()) {
545 final String pattern = "-" + selector.getClassifier( ) + ".";
546 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ), pattern ) );
547 } else if (selector.hasType()) {
548 final String pattern = "-" + MavenContentHelper.getClassifierFromType( selector.getType( ) ) + ".";
549 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ).toLowerCase( ), pattern ) );
558 public List<? extends Artifact> getAllArtifacts( ItemSelector selector ) throws ContentAccessException
567 public Stream<? extends Artifact> getAllArtifactStream( ItemSelector selector ) throws ContentAccessException
576 public List<? extends Project> getProjects( Namespace namespace )
585 public List<? extends Version> getVersions( Project project )
594 public List<? extends Artifact> getArtifacts( ContentItem item )
603 public List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse )
612 public Stream<? extends Artifact> getArtifactStream( ContentItem item )
621 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse )
630 public boolean hasContent( ItemSelector selector )
639 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
646 * @param path the path string that points to the item
648 * @throws LayoutException
651 public ContentItem toItem( String path ) throws LayoutException
653 return getItemFromPath( getAssetByPath( path ) );
657 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
659 return getItemFromPath( assetPath );
663 public void deleteVersion( VersionedReference ref ) throws ContentNotFoundException, ContentAccessException
665 final String path = toPath( ref );
666 final Path deleteTarget = getRepoDir().resolve(path);
667 if ( !Files.exists(deleteTarget) )
669 log.warn( "Version path for repository {} does not exist: {}", getId(), deleteTarget );
670 throw new ContentNotFoundException( "Version not found for repository "+getId()+": "+path );
672 if ( Files.isDirectory(deleteTarget) )
676 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
678 catch ( IOException e )
680 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
681 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
684 log.warn( "Version path for repository {} is not a directory {}", getId(), deleteTarget );
685 throw new ContentNotFoundException( "Version path for repository "+getId()+" is not directory: " + path );
690 public void deleteProject( ProjectReference ref )
691 throws ContentNotFoundException, ContentAccessException
693 final String path = toPath( ref );
694 final Path deleteTarget = getRepoDir( ).resolve( path );
695 if ( !Files.exists(deleteTarget) )
697 log.warn( "Project path for repository {} does not exist: {}", getId(), deleteTarget );
698 throw new ContentNotFoundException( "Project not found for repository "+getId()+": "+path );
700 if ( Files.isDirectory(deleteTarget) )
704 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
706 catch ( IOException e )
708 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
709 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
714 log.warn( "Project path for repository {} is not a directory {}", getId(), deleteTarget );
715 throw new ContentNotFoundException( "Project path for repository "+getId()+" is not directory: " + path );
721 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
723 this.deleteProject( new ProjectReference().groupId( namespace ).artifactId( projectId ) );
727 public void deleteArtifact( ArtifactReference ref ) throws ContentNotFoundException, ContentAccessException
729 final String path = toPath( ref );
730 final Path repoDir = getRepoDir( );
731 Path deleteTarget = repoDir.resolve( path );
732 if ( Files.exists(deleteTarget) )
736 if (Files.isDirectory( deleteTarget ))
738 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
740 Files.delete( deleteTarget );
743 catch ( IOException e )
745 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
746 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
749 log.warn( "Artifact path for repository {} does not exist: {}", getId(), deleteTarget );
750 throw new ContentNotFoundException( "Artifact not found for repository "+getId()+": "+path );
756 public void deleteGroupId( String groupId )
757 throws ContentNotFoundException, ContentAccessException
759 final String path = toPath( groupId );
760 final Path deleteTarget = getRepoDir( ).resolve( path );
761 if (!Files.exists(deleteTarget)) {
762 log.warn( "Namespace path for repository {} does not exist: {}", getId(), deleteTarget );
763 throw new ContentNotFoundException( "Namespace not found for repository "+getId()+": "+path );
765 if ( Files.isDirectory(deleteTarget) )
769 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
771 catch ( IOException e )
773 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
774 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
777 log.warn( "Namespace path for repository {} is not a directory {}", getId(), deleteTarget );
778 throw new ContentNotFoundException( "Namespace path for repository "+getId()+" is not directory: " + path );
784 public String getId()
786 return repository.getId();
790 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference )
791 throws ContentNotFoundException, LayoutException, ContentAccessException
793 StorageAsset artifactDir = toFile( reference );
794 if ( !artifactDir.exists())
796 throw new ContentNotFoundException(
797 "Unable to get related artifacts using a non-existant directory: " + artifactDir.getPath() );
800 if ( !artifactDir.isContainer() )
802 throw new ContentNotFoundException(
803 "Unable to get related artifacts using a non-directory: " + artifactDir.getPath() );
806 // First gather up the versions found as artifacts in the managed repository.
808 try (Stream<? extends StorageAsset> stream = artifactDir.list().stream() ) {
809 return stream.filter(asset -> !asset.isContainer()).map(path -> {
811 ArtifactReference artifact = toArtifactReference(path.getPath());
812 if( artifact.getGroupId().equals( reference.getGroupId() ) && artifact.getArtifactId().equals(
813 reference.getArtifactId() ) && artifact.getVersion().equals( reference.getVersion() )) {
818 } catch (LayoutException e) {
819 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
822 }).filter(Objects::nonNull).collect(Collectors.toList());
823 } catch (RuntimeException e) {
824 Throwable cause = e.getCause( );
826 if (cause instanceof LayoutException) {
827 throw (LayoutException)cause;
830 throw new ContentAccessException( cause.getMessage( ), cause );
833 throw new ContentAccessException( e.getMessage( ), e );
839 * Create the filter for various combinations of classifier and type
841 private Predicate<ArtifactReference> getChecker(ArtifactReference referenceObject, String extension) {
842 // TODO: Check, if extension is the correct parameter here
843 // We compare type with extension which works for artifacts like .jar.md5 but may
844 // be not the best way.
846 if (referenceObject.getClassifier()!=null && referenceObject.getType()!=null) {
847 return ((ArtifactReference a) ->
848 referenceObject.getGroupId().equals( a.getGroupId() )
849 && referenceObject.getArtifactId().equals( a.getArtifactId() )
850 && referenceObject.getVersion( ).equals( a.getVersion( ) )
851 && ( (a.getType()==null)
852 || referenceObject.getType().equals( a.getType() )
853 || a.getType().startsWith(extension) )
854 && referenceObject.getClassifier().equals( a.getClassifier() )
856 } else if (referenceObject.getClassifier()!=null && referenceObject.getType()==null){
857 return ((ArtifactReference a) ->
858 referenceObject.getGroupId().equals( a.getGroupId() )
859 && referenceObject.getArtifactId().equals( a.getArtifactId() )
860 && referenceObject.getVersion( ).equals( a.getVersion( ) )
861 && referenceObject.getClassifier().equals( a.getClassifier() )
863 } else if (referenceObject.getClassifier()==null && referenceObject.getType()!=null){
864 return ((ArtifactReference a) ->
865 referenceObject.getGroupId().equals( a.getGroupId() )
866 && referenceObject.getArtifactId().equals( a.getArtifactId() )
867 && referenceObject.getVersion( ).equals( a.getVersion( ) )
868 && ( (a.getType()==null)
869 || referenceObject.getType().equals( a.getType() )
870 || a.getType().startsWith(extension) )
873 return ((ArtifactReference a) ->
874 referenceObject.getGroupId().equals( a.getGroupId() )
875 && referenceObject.getArtifactId().equals( a.getArtifactId() )
876 && referenceObject.getVersion( ).equals( a.getVersion( ) )
884 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
885 throws ContentNotFoundException, LayoutException, ContentAccessException
887 if ( StringUtils.isEmpty( reference.getType() ) && StringUtils.isEmpty( reference.getClassifier() ) ) {
888 return getRelatedArtifacts( toVersion( reference ) );
891 StorageAsset artifactFile = toFile( reference );
892 StorageAsset repoDir = artifactFile.getParent();
894 if (!artifactFile.isContainer()) {
895 ext = StringUtils.substringAfterLast( artifactFile.getName(), ".");
900 if ( !repoDir.exists())
902 throw new ContentNotFoundException(
903 "Unable to get related artifacts using a non-existant directory: " + repoDir.getPath() );
906 if ( !repoDir.isContainer() )
908 throw new ContentNotFoundException(
909 "Unable to get related artifacts using a non-directory: " + repoDir.getPath() );
912 // First gather up the versions found as artifacts in the managed repository.
914 try (Stream<? extends StorageAsset> stream = repoDir.list().stream() ) {
915 return stream.filter(
916 asset -> !asset.isContainer())
919 return toArtifactReference(path.getPath());
920 } catch (LayoutException e) {
921 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
924 }).filter(Objects::nonNull).filter(getChecker( reference, ext )).collect(Collectors.toList());
925 } catch (RuntimeException e) {
926 Throwable cause = e.getCause( );
928 if (cause instanceof LayoutException) {
929 throw (LayoutException)cause;
932 throw new ContentAccessException( cause.getMessage( ), cause );
935 throw new ContentAccessException( e.getMessage( ), e );
941 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
947 public String getRepoRoot()
949 return convertUriToPath( repository.getLocation() );
952 private String convertUriToPath( URI uri ) {
953 if (uri.getScheme()==null) {
954 return Paths.get(uri.getPath()).toString();
955 } else if ("file".equals(uri.getScheme())) {
956 return Paths.get(uri).toString();
958 return uri.toString();
963 public ManagedRepository getRepository()
969 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
972 * @return the Set of available versions, based on the project reference.
973 * @throws LayoutException
976 public Set<String> getVersions( ProjectReference reference )
977 throws ContentNotFoundException, LayoutException, ContentAccessException
979 final String path = toPath( reference );
980 final Path projDir = getRepoDir().resolve(toPath(reference));
981 if ( !Files.exists(projDir) )
983 throw new ContentNotFoundException(
984 "Unable to get Versions on a non-existant directory for repository "+getId()+": " + path );
987 if ( !Files.isDirectory(projDir) )
989 throw new ContentNotFoundException(
990 "Unable to get Versions on a non-directory for repository "+getId()+": " + path );
993 final String groupId = reference.getGroupId();
994 final String artifactId = reference.getArtifactId();
995 try(Stream<Path> stream = Files.list(projDir)) {
996 return stream.filter(Files::isDirectory).map(
997 p -> toVersion(groupId, artifactId, p.getFileName().toString())
998 ).filter(this::hasArtifact).map(ref -> ref.getVersion())
999 .collect(Collectors.toSet());
1000 } catch (IOException e) {
1001 log.error("Could not read directory {}: {}", projDir, e.getMessage(), e);
1002 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, e );
1003 } catch (RuntimeException e) {
1004 Throwable cause = e.getCause( );
1007 if ( cause instanceof LayoutException )
1009 throw (LayoutException) cause;
1011 log.error("Could not read directory {}: {}", projDir, cause.getMessage(), cause);
1012 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, cause );
1015 log.error("Could not read directory {}: {}", projDir, e.getMessage(), e);
1016 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, cause );
1022 public Set<String> getVersions( VersionedReference reference )
1023 throws ContentNotFoundException, ContentAccessException, LayoutException
1025 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1027 return stream.filter( Objects::nonNull )
1028 .map( ar -> ar.getVersion( ) )
1029 .collect( Collectors.toSet( ) );
1030 } catch (IOException e) {
1031 final String path = toPath( reference );
1032 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1033 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, e );
1038 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
1040 StorageAsset artifactFile = toFile( reference );
1041 return artifactFile.exists() && !artifactFile.isContainer();
1045 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
1049 Set<String> versions = getVersions( reference );
1050 return !versions.isEmpty();
1052 catch ( ContentNotFoundException | LayoutException e )
1059 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
1063 return ( getFirstArtifact( reference ) != null );
1065 catch ( LayoutException | ContentNotFoundException e )
1069 catch ( IOException e )
1071 String path = toPath( reference );
1072 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1073 throw new ContentAccessException( "Could not read path from repository " + getId( ) + ": " + path, e );
1078 public void setRepository( final ManagedRepository repo )
1080 this.repository = repo;
1082 if (repository instanceof EditableManagedRepository) {
1083 ((EditableManagedRepository) repository).setContent(this);
1088 private Path getRepoDir() {
1089 return repository.getAsset( "" ).getFilePath( );
1092 private RepositoryStorage getStorage() {
1093 return repository.getAsset( "" ).getStorage( );
1097 * Convert a path to an artifact reference.
1099 * @param path the path to convert. (relative or full location path)
1100 * @throws LayoutException if the path cannot be converted to an artifact reference.
1103 public ArtifactReference toArtifactReference( String path )
1104 throws LayoutException
1106 String repoPath = convertUriToPath( repository.getLocation() );
1107 if ( ( path != null ) && path.startsWith( repoPath ) && repoPath.length() > 0 )
1109 return super.toArtifactReference( path.substring( repoPath.length() + 1 ) );
1112 if (repoPath!=null) {
1113 while (repoPath.startsWith("/")) {
1114 repoPath = repoPath.substring(1);
1117 return super.toArtifactReference( repoPath );
1122 // The variant with runtime exception for stream usage
1123 private ArtifactReference toArtifactRef(String path) {
1125 return toArtifactReference(path);
1126 } catch (LayoutException e) {
1127 throw new RuntimeException(e);
1134 public StorageAsset toFile( ArtifactReference reference )
1136 return repository.getAsset(toPath(reference));
1140 public StorageAsset toFile( ArchivaArtifact reference )
1142 return repository.getAsset( toPath( reference ) );
1146 public StorageAsset toFile( VersionedReference reference )
1148 return repository.getAsset( toPath( reference ) );
1152 * Get the first Artifact found in the provided VersionedReference location.
1154 * @param reference the reference to the versioned reference to search within
1155 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
1156 * no artifact was found within the versioned reference.
1157 * @throws java.io.IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
1158 * @throws LayoutException
1160 private ArtifactReference getFirstArtifact( VersionedReference reference )
1161 throws ContentNotFoundException, LayoutException, IOException
1163 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1165 return stream.findFirst( ).orElse( null );
1166 } catch (RuntimeException e) {
1167 throw new ContentNotFoundException( e.getMessage( ), e.getCause( ) );
1171 private Stream<ArtifactReference> getArtifactStream(VersionedReference reference) throws ContentNotFoundException, LayoutException, IOException {
1172 final Path repoBase = getRepoDir( );
1173 String path = toMetadataPath( reference );
1174 Path versionDir = repoBase.resolve( path ).getParent();
1175 if ( !Files.exists(versionDir) )
1177 throw new ContentNotFoundException( "Unable to gather the list of artifacts on a non-existant directory: "
1178 + versionDir.toAbsolutePath() );
1181 if ( !Files.isDirectory(versionDir) )
1183 throw new ContentNotFoundException(
1184 "Unable to gather the list of snapshot versions on a non-directory: " + versionDir.toAbsolutePath() );
1186 return Files.list(versionDir).filter(Files::isRegularFile)
1187 .map(p -> repoBase.relativize(p).toString())
1188 .filter(p -> !filetypes.matchesDefaultExclusions(p))
1189 .filter(filetypes::matchesArtifactPattern)
1190 .map(this::toArtifactRef);
1193 public List<ArtifactReference> getArtifacts(VersionedReference reference) throws ContentNotFoundException, LayoutException, ContentAccessException
1195 try (Stream<ArtifactReference> stream = getArtifactStream( reference ))
1197 return stream.collect( Collectors.toList( ) );
1198 } catch ( IOException e )
1200 String path = toPath( reference );
1201 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1202 throw new ContentAccessException( "Could not read path from repository " + getId( ) + ": " + path, e );
1207 private boolean hasArtifact( VersionedReference reference )
1210 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1212 return stream.anyMatch( e -> true );
1213 } catch (ContentNotFoundException e) {
1215 } catch ( LayoutException | IOException e) {
1216 // We throw the runtime exception for better stream handling
1217 throw new RuntimeException(e);
1221 public void setFiletypes( FileTypes filetypes )
1223 this.filetypes = filetypes;
1226 public void setMavenContentHelper( MavenContentHelper contentHelper) {
1227 this.mavenContentHelper = contentHelper;