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 );
144 private StorageAsset getAssetByPath(String assetPath) {
145 return getStorage( ).getAsset( assetPath );
148 private StorageAsset getAsset(String namespace) {
149 String namespacePath = formatAsDirectory( namespace.trim() );
150 if (StringUtils.isEmpty( namespacePath )) {
153 return getAssetByPath(namespacePath);
156 private StorageAsset getAsset(String namespace, String project) {
157 return getAsset( namespace ).resolve( project );
160 private StorageAsset getAsset(String namespace, String project, String version) {
161 return getAsset( namespace, project ).resolve( version );
164 private StorageAsset getAsset(String namespace, String project, String version, String fileName) {
165 return getAsset( namespace, project, version ).resolve( fileName );
169 /// ************* End of new generation interface ******************
171 public void deleteItem( ContentItem item ) throws ItemNotFoundException, ContentAccessException
173 final Path baseDirectory = getRepoDir( );
174 final Path itemPath = item.getAsset( ).getFilePath( );
175 if ( !Files.exists( itemPath ) )
177 throw new ItemNotFoundException( "The item " + item.toString() + "does not exist in the repository " + getId( ) );
179 if ( !itemPath.toAbsolutePath().startsWith( baseDirectory.toAbsolutePath() ) )
181 log.error( "The namespace {} to delete from repository {} is not a subdirectory of the repository base.", item, getId( ) );
182 log.error( "Namespace directory: {}", itemPath );
183 log.error( "Repository directory: {}", baseDirectory );
184 throw new ContentAccessException( "Inconsistent directories found. Could not delete namespace." );
188 if (Files.isDirectory( itemPath ))
190 FileUtils.deleteDirectory( itemPath );
192 Files.deleteIfExists( itemPath );
195 catch ( IOException e )
197 log.error( "Could not delete namespace directory {}: {}", itemPath, e.getMessage( ), e );
198 throw new ContentAccessException( "Error occured while deleting namespace " + item + ": " + e.getMessage( ), e );
203 public ContentItem getItem( ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
205 if (selector.hasVersion() && selector.hasArtifactId()) {
206 return getArtifact( selector );
207 } else if (selector.hasProjectId() && selector.hasVersion()) {
208 return getVersion( selector );
209 } else if (selector.hasProjectId()) {
210 return getProject( selector );
212 return getNamespace( selector );
217 public Namespace getNamespace( final ItemSelector namespaceSelector ) throws ContentAccessException, IllegalArgumentException
219 return namespaceMap.computeIfAbsent( namespaceSelector.getNamespace(),
221 StorageAsset nsPath = getAsset( namespace );
222 return ArchivaNamespace.withRepository( this ).withAsset( nsPath ).
223 withNamespace( namespace ).build( );
229 public Project getProject( final ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
231 if (!selector.hasProjectId()) {
232 throw new IllegalArgumentException( "Project id must be set" );
234 final StorageAsset path = getAsset( selector.getNamespace( ), selector.getProjectId( ) );
235 return projectMap.computeIfAbsent( path, projectPath -> {
236 final Namespace ns = getNamespace( selector );
237 return ArchivaProject.withAsset( projectPath ).withNamespace( ns ).withId( selector.getProjectId( ) ).build( );
244 public Version getVersion( final ItemSelector selector ) throws ContentAccessException, IllegalArgumentException
246 if (!selector.hasProjectId()) {
247 throw new IllegalArgumentException( "Project id must be set" );
249 if (!selector.hasVersion() ) {
250 throw new IllegalArgumentException( "Version must be set" );
252 final StorageAsset path = getAsset(selector.getNamespace(), selector.getProjectId(), selector.getVersion());
253 return versionMap.computeIfAbsent( path, versionPath -> {
254 final Project project = getProject( selector );
255 return ArchivaVersion.withAsset( path )
256 .withProject( project )
257 .withVersion( selector.getVersion( ) ).build();
263 public Artifact createArtifact(final StorageAsset artifactPath, final ItemSelector selector,
264 final String classifier, final String extension) {
265 Version version = getVersion(selector);
266 ArtifactOptBuilder builder = org.apache.archiva.repository.content.base.ArchivaArtifact.withAsset( artifactPath )
267 .withVersion( version )
268 .withId( selector.getArtifactId( ) )
269 .withArtifactVersion( mavenContentHelper.getArtifactVersion( artifactPath, selector ) )
270 .withClassifier( classifier );
271 if (selector.hasType()) {
272 builder.withType( selector.getType( ) );
274 return builder.build( );
277 public Namespace getNamespaceFromArtifactPath( final StorageAsset artifactPath) {
278 final StorageAsset namespacePath = artifactPath.getParent( ).getParent( ).getParent( );
279 final String namespace = MavenContentHelper.getNamespaceFromNamespacePath( namespacePath );
280 return namespaceMap.computeIfAbsent( namespace,
281 myNamespace -> ArchivaNamespace.withRepository( this )
282 .withAsset( namespacePath )
283 .withNamespace( namespace )
287 private Project getProjectFromArtifactPath( final StorageAsset artifactPath) {
288 final StorageAsset projectPath = artifactPath.getParent( ).getParent( );
289 return projectMap.computeIfAbsent( projectPath,
290 myProjectPath -> ArchivaProject.withAsset( projectPath )
291 .withNamespace( getNamespaceFromArtifactPath( artifactPath ) )
292 .withId( projectPath.getName( ) ).build( )
296 private Version getVersionFromArtifactPath( final StorageAsset artifactPath) {
297 final StorageAsset versionPath = artifactPath.getParent( );
298 return versionMap.computeIfAbsent( versionPath,
299 myVersionPath -> ArchivaVersion.withAsset( versionPath )
300 .withProject( getProjectFromArtifactPath( artifactPath ) )
301 .withVersion( versionPath.getName( ) ).build( ) );
304 private Artifact getArtifactFromPath(final StorageAsset artifactPath) {
305 final Version version = getVersionFromArtifactPath( artifactPath );
306 final ArtifactInfo info = getArtifactInfoFromPath( version.getVersion(), artifactPath );
307 return artifactMap.computeIfAbsent( artifactPath, myArtifactPath ->
308 org.apache.archiva.repository.content.base.ArchivaArtifact.withAsset( artifactPath )
309 .withVersion( version )
311 .withClassifier( info.classifier )
312 .withRemainder( info.remainder )
313 .withType( info.type )
314 .withArtifactVersion( info.version )
315 .withContentType( info.contentType )
320 private ContentItem getItemFromPath(final StorageAsset itemPath) {
321 if (itemPath.isLeaf()) {
322 return getArtifactFromPath( itemPath );
324 if (versionMap.containsKey( itemPath )) {
325 return versionMap.get( itemPath );
327 if (projectMap.containsKey( itemPath )) {
328 return projectMap.get( itemPath );
330 String ns = MavenContentHelper.getNamespaceFromNamespacePath( itemPath );
331 if (namespaceMap.containsKey( ns )) {
332 return namespaceMap.get( ns );
334 // No cached item, so we have to gather more information:
335 // Check for version directory (contains at least a pom or metadata file)
336 if (itemPath.list( ).stream( ).map(a -> a.getName().toLowerCase()).anyMatch( n ->
338 || n.startsWith( "maven-metadata" )
340 return versionMap.computeIfAbsent( itemPath,
341 myVersionPath -> ArchivaVersion.withAsset( itemPath )
342 .withProject( (Project)getItemFromPath( itemPath.getParent() ) )
343 .withVersion( itemPath.getName() ).build());
345 // We have to dig further and find the next directory with a pom
346 Optional<StorageAsset> foundFile = StorageUtil.newAssetStream( itemPath )
347 .filter( a -> a.getName().toLowerCase().endsWith( ".pom" )
348 || a.getName().toLowerCase().startsWith( "maven-metadata" ) )
350 if (foundFile.isPresent())
353 StorageAsset current = foundFile.get( );
354 while (current.hasParent() && !current.equals(itemPath)) {
356 current = current.getParent( );
358 // Project path if it is one level up from the found file
360 return projectMap.computeIfAbsent( itemPath,
361 myItemPath -> getProjectFromArtifactPath( foundFile.get( ) ) );
363 // All other paths are treated as namespace
364 return namespaceMap.computeIfAbsent( ns,
365 myNamespace -> ArchivaNamespace.withRepository( this )
366 .withAsset( itemPath )
371 // Don't know what to do with it, so we treat it as namespace path
372 return namespaceMap.computeIfAbsent( ns,
373 myNamespace -> ArchivaNamespace.withRepository( this )
374 .withAsset( itemPath )
383 // Simple object to hold artifact information
384 private class ArtifactInfo {
386 private String version;
387 private String extension;
388 private String remainder;
390 private String classifier;
391 private String contentType;
394 private ArtifactInfo getArtifactInfoFromPath(String genericVersion, StorageAsset path) {
395 final ArtifactInfo info = new ArtifactInfo( );
396 info.id = path.getParent( ).getParent( ).getName( );
397 final String fileName = path.getName( );
398 if ( genericVersion.endsWith( "-" + SNAPSHOT ) )
400 String baseVersion = StringUtils.substringBeforeLast( genericVersion, "-" + SNAPSHOT );
401 String prefix = info.id+"-"+baseVersion+"-";
402 if (fileName.startsWith( prefix ))
404 String versionPostfix = StringUtils.removeStart( fileName, prefix );
405 Matcher matcher = UNIQUE_SNAPSHOT_PATTERN.matcher( versionPostfix );
406 if (matcher.matches()) {
407 info.version = baseVersion + "-" + matcher.group( 1 );
408 String newPrefix = prefix + info.version;
409 if (fileName.startsWith( newPrefix ))
411 String classPostfix = StringUtils.removeStart( fileName, newPrefix );
412 Matcher cMatch = CLASSIFIER_PATTERN.matcher( classPostfix );
413 if (cMatch.matches()) {
414 info.classifier = cMatch.group( 1 );
415 info.remainder = cMatch.group( 2 );
417 info.classifier = "";
418 info.remainder = classPostfix;
421 log.error( "Artifact does not match the maven name pattern {}", path );
422 info.classifier = "";
423 info.remainder = StringUtils.substringAfter( fileName, prefix );
426 log.error( "Artifact does not match the snapshot version pattern {}", path );
428 info.classifier = "";
429 info.remainder = StringUtils.substringAfter( fileName, prefix );
432 log.error( "Artifact does not match the maven name pattern: {}", path );
434 info.classifier = "";
435 info.remainder = StringUtils.substringAfterLast( fileName, "." );
438 String prefix = info.id+"-"+genericVersion;
439 if (fileName.startsWith( prefix ))
441 info.version=genericVersion;
442 String classPostfix = StringUtils.removeStart( fileName, prefix );
443 Matcher cMatch = CLASSIFIER_PATTERN.matcher( classPostfix );
444 if (cMatch.matches()) {
445 info.classifier = cMatch.group( 1 );
446 info.remainder = cMatch.group( 2 );
448 info.classifier = "";
449 info.remainder = classPostfix;
452 log.error( "Artifact does not match the version pattern {}", path );
454 info.classifier = "";
455 info.remainder = StringUtils.substringAfterLast( fileName, "." );
458 info.extension = StringUtils.substringAfterLast( fileName, "." );
459 info.type = MavenContentHelper.getTypeFromClassifierAndExtension( info.classifier, info.extension );
461 info.contentType = Files.probeContentType( path.getFilePath( ) );
462 } catch (IOException e) {
463 info.contentType = "";
471 public Artifact getArtifact( final ItemSelector selector ) throws ContentAccessException
473 if (!selector.hasProjectId( )) {
474 throw new IllegalArgumentException( "Project id must be set" );
476 if (!selector.hasVersion( )) {
477 throw new IllegalArgumentException( "Version must be set" );
479 if (!selector.hasArtifactId( )) {
480 throw new IllegalArgumentException( "Artifact Id must be set" );
482 final StorageAsset artifactDir = getAsset(selector.getNamespace(), selector.getProjectId(),
483 selector.getVersion());
484 final String artifactVersion = mavenContentHelper.getArtifactVersion( artifactDir, selector );
485 final String classifier = MavenContentHelper.getClassifier( selector );
486 final String extension = MavenContentHelper.getArtifactExtension( selector );
487 final String artifactId = StringUtils.isEmpty( selector.getArtifactId( ) ) ? selector.getProjectId( ) : selector.getArtifactId( );
488 final String fileName = MavenContentHelper.getArtifactFileName( artifactId, artifactVersion, classifier, extension );
489 final StorageAsset path = getAsset( selector.getNamespace( ), selector.getProjectId( ),
490 selector.getVersion( ), fileName );
491 return artifactMap.computeIfAbsent( path, artifactPath -> createArtifact( path, selector, classifier, extension ) );
494 private StorageAsset getBasePathFromSelector(ItemSelector selector) {
495 StringBuilder path = new StringBuilder( );
496 if (selector.hasNamespace()) {
497 path.append(String.join( "/", getNamespace( selector ).getNamespacePath( ) ));
499 if (selector.hasProjectId()) {
500 path.append( "/" ).append( selector.getProjectId( ) );
502 if (selector.hasVersion()) {
503 path.append( "/" ).append( selector.getVersion( ) );
505 return getStorage( ).getAsset( path.toString( ) );
509 * File filter to select certain artifacts using the selector data.
511 private Predicate<StorageAsset> getFileFilterFromSelector(final ItemSelector selector) {
512 Predicate<StorageAsset> p = a -> a.isLeaf( );
513 if (selector.hasArtifactId()) {
514 final String pattern = selector.getArtifactId( );
515 p = p.and( a -> StringUtils.startsWithIgnoreCase( a.getName( ), pattern ) );
517 if (selector.hasArtifactVersion()) {
518 final String pattern = selector.getArtifactVersion( );
519 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ), pattern ) );
521 if (selector.hasExtension()) {
522 final String pattern = "."+selector.getExtension( );
523 p = p.and( a -> StringUtils.endsWithIgnoreCase( a.getName( ), pattern ) );
524 } else if (selector.hasType()) {
525 final String pattern = "."+ MavenContentHelper.getArtifactExtension( selector );
526 p = p.and( a -> StringUtils.endsWithIgnoreCase( a.getName( ), pattern ) );
528 if (selector.hasClassifier()) {
529 final String pattern = "-" + selector.getClassifier( ) + ".";
530 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ), pattern ) );
531 } else if (selector.hasType()) {
532 final String pattern = "-" + MavenContentHelper.getClassifierFromType( selector.getType( ) ) + ".";
533 p = p.and( a -> StringUtils.containsIgnoreCase( a.getName( ).toLowerCase( ), pattern ) );
542 public List<? extends Artifact> getAllArtifacts( ItemSelector selector ) throws ContentAccessException
551 public Stream<? extends Artifact> getAllArtifactStream( ItemSelector selector ) throws ContentAccessException
560 public List<? extends Project> getProjects( Namespace namespace )
569 public List<? extends Version> getVersions( Project project )
578 public List<? extends Artifact> getArtifacts( ContentItem item )
587 public List<? extends Artifact> getArtifacts( Namespace namespace, boolean recurse )
596 public Stream<? extends Artifact> getArtifactStream( ContentItem item )
605 public Stream<? extends Artifact> getArtifactStream( Namespace namespace, boolean recurse )
614 public boolean hasContent( ItemSelector selector )
623 public void copyArtifact( Path sourceFile, ContentItem destination ) throws IllegalArgumentException
630 * @param path the path string that points to the item
632 * @throws LayoutException
635 public ContentItem toItem( String path ) throws LayoutException
637 ItemSelector selector = getPathParser( ).toItemSelector( path );
638 return getItem( selector );
642 public ContentItem toItem( StorageAsset assetPath ) throws LayoutException
644 return toItem( assetPath.getPath( ) );
647 /// ************* End of new generation interface ******************
650 * Returns a version reference from the coordinates
651 * @param groupId the group id
652 * @param artifactId the artifact id
653 * @param version the version
654 * @return the versioned reference object
657 public VersionedReference toVersion( String groupId, String artifactId, String version ) {
658 return new VersionedReference().groupId( groupId ).artifactId( artifactId ).version( version );
662 public VersionedReference toGenericVersion( ArtifactReference artifactReference )
664 return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), VersionUtil.getBaseVersion( artifactReference.getVersion( ) ));
668 * Return the version the artifact is part of
669 * @param artifactReference
672 public VersionedReference toVersion( ArtifactReference artifactReference) {
673 return toVersion( artifactReference.getGroupId( ), artifactReference.getArtifactId( ), artifactReference.getVersion( ) );
677 public ArtifactReference toArtifact( String groupId, String artifactId, String version, String type, String classifier) {
678 return new ArtifactReference( ).groupId( groupId ).artifactId( artifactId ).version( version ).type( type ).classifier( classifier );
683 public void deleteVersion( VersionedReference ref ) throws ContentNotFoundException, ContentAccessException
685 final String path = toPath( ref );
686 final Path deleteTarget = getRepoDir().resolve(path);
687 if ( !Files.exists(deleteTarget) )
689 log.warn( "Version path for repository {} does not exist: {}", getId(), deleteTarget );
690 throw new ContentNotFoundException( "Version not found for repository "+getId()+": "+path );
692 if ( Files.isDirectory(deleteTarget) )
696 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
698 catch ( IOException e )
700 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
701 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
704 log.warn( "Version path for repository {} is not a directory {}", getId(), deleteTarget );
705 throw new ContentNotFoundException( "Version path for repository "+getId()+" is not directory: " + path );
710 public void deleteProject( ProjectReference ref )
711 throws ContentNotFoundException, ContentAccessException
713 final String path = toPath( ref );
714 final Path deleteTarget = getRepoDir( ).resolve( path );
715 if ( !Files.exists(deleteTarget) )
717 log.warn( "Project path for repository {} does not exist: {}", getId(), deleteTarget );
718 throw new ContentNotFoundException( "Project not found for repository "+getId()+": "+path );
720 if ( Files.isDirectory(deleteTarget) )
724 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
726 catch ( IOException e )
728 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
729 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
734 log.warn( "Project path for repository {} is not a directory {}", getId(), deleteTarget );
735 throw new ContentNotFoundException( "Project path for repository "+getId()+" is not directory: " + path );
741 public void deleteProject( String namespace, String projectId ) throws ContentNotFoundException, ContentAccessException
743 this.deleteProject( new ProjectReference().groupId( namespace ).artifactId( projectId ) );
747 public void deleteArtifact( ArtifactReference ref ) throws ContentNotFoundException, ContentAccessException
749 final String path = toPath( ref );
750 final Path repoDir = getRepoDir( );
751 Path deleteTarget = repoDir.resolve( path );
752 if ( Files.exists(deleteTarget) )
756 if (Files.isDirectory( deleteTarget ))
758 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
760 Files.delete( deleteTarget );
763 catch ( IOException e )
765 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
766 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
769 log.warn( "Artifact path for repository {} does not exist: {}", getId(), deleteTarget );
770 throw new ContentNotFoundException( "Artifact not found for repository "+getId()+": "+path );
776 public void deleteGroupId( String groupId )
777 throws ContentNotFoundException, ContentAccessException
779 final String path = toPath( groupId );
780 final Path deleteTarget = getRepoDir( ).resolve( path );
781 if (!Files.exists(deleteTarget)) {
782 log.warn( "Namespace path for repository {} does not exist: {}", getId(), deleteTarget );
783 throw new ContentNotFoundException( "Namespace not found for repository "+getId()+": "+path );
785 if ( Files.isDirectory(deleteTarget) )
789 org.apache.archiva.common.utils.FileUtils.deleteDirectory( deleteTarget );
791 catch ( IOException e )
793 log.error( "Could not delete file path {}: {}", deleteTarget, e.getMessage( ), e );
794 throw new ContentAccessException( "Error while trying to delete path "+path+" from repository "+getId()+": "+e.getMessage( ), e );
797 log.warn( "Namespace path for repository {} is not a directory {}", getId(), deleteTarget );
798 throw new ContentNotFoundException( "Namespace path for repository "+getId()+" is not directory: " + path );
804 public String getId()
806 return repository.getId();
810 public List<ArtifactReference> getRelatedArtifacts( VersionedReference reference )
811 throws ContentNotFoundException, LayoutException, ContentAccessException
813 StorageAsset artifactDir = toFile( reference );
814 if ( !artifactDir.exists())
816 throw new ContentNotFoundException(
817 "Unable to get related artifacts using a non-existant directory: " + artifactDir.getPath() );
820 if ( !artifactDir.isContainer() )
822 throw new ContentNotFoundException(
823 "Unable to get related artifacts using a non-directory: " + artifactDir.getPath() );
826 // First gather up the versions found as artifacts in the managed repository.
828 try (Stream<? extends StorageAsset> stream = artifactDir.list().stream() ) {
829 return stream.filter(asset -> !asset.isContainer()).map(path -> {
831 ArtifactReference artifact = toArtifactReference(path.getPath());
832 if( artifact.getGroupId().equals( reference.getGroupId() ) && artifact.getArtifactId().equals(
833 reference.getArtifactId() ) && artifact.getVersion().equals( reference.getVersion() )) {
838 } catch (LayoutException e) {
839 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
842 }).filter(Objects::nonNull).collect(Collectors.toList());
843 } catch (RuntimeException e) {
844 Throwable cause = e.getCause( );
846 if (cause instanceof LayoutException) {
847 throw (LayoutException)cause;
850 throw new ContentAccessException( cause.getMessage( ), cause );
853 throw new ContentAccessException( e.getMessage( ), e );
859 * Create the filter for various combinations of classifier and type
861 private Predicate<ArtifactReference> getChecker(ArtifactReference referenceObject, String extension) {
862 // TODO: Check, if extension is the correct parameter here
863 // We compare type with extension which works for artifacts like .jar.md5 but may
864 // be not the best way.
866 if (referenceObject.getClassifier()!=null && referenceObject.getType()!=null) {
867 return ((ArtifactReference a) ->
868 referenceObject.getGroupId().equals( a.getGroupId() )
869 && referenceObject.getArtifactId().equals( a.getArtifactId() )
870 && referenceObject.getVersion( ).equals( a.getVersion( ) )
871 && ( (a.getType()==null)
872 || referenceObject.getType().equals( a.getType() )
873 || a.getType().startsWith(extension) )
874 && referenceObject.getClassifier().equals( a.getClassifier() )
876 } else if (referenceObject.getClassifier()!=null && referenceObject.getType()==null){
877 return ((ArtifactReference a) ->
878 referenceObject.getGroupId().equals( a.getGroupId() )
879 && referenceObject.getArtifactId().equals( a.getArtifactId() )
880 && referenceObject.getVersion( ).equals( a.getVersion( ) )
881 && referenceObject.getClassifier().equals( a.getClassifier() )
883 } else if (referenceObject.getClassifier()==null && referenceObject.getType()!=null){
884 return ((ArtifactReference a) ->
885 referenceObject.getGroupId().equals( a.getGroupId() )
886 && referenceObject.getArtifactId().equals( a.getArtifactId() )
887 && referenceObject.getVersion( ).equals( a.getVersion( ) )
888 && ( (a.getType()==null)
889 || referenceObject.getType().equals( a.getType() )
890 || a.getType().startsWith(extension) )
893 return ((ArtifactReference a) ->
894 referenceObject.getGroupId().equals( a.getGroupId() )
895 && referenceObject.getArtifactId().equals( a.getArtifactId() )
896 && referenceObject.getVersion( ).equals( a.getVersion( ) )
904 public List<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
905 throws ContentNotFoundException, LayoutException, ContentAccessException
907 if ( StringUtils.isEmpty( reference.getType() ) && StringUtils.isEmpty( reference.getClassifier() ) ) {
908 return getRelatedArtifacts( toVersion( reference ) );
911 StorageAsset artifactFile = toFile( reference );
912 StorageAsset repoDir = artifactFile.getParent();
914 if (!artifactFile.isContainer()) {
915 ext = StringUtils.substringAfterLast( artifactFile.getName(), ".");
920 if ( !repoDir.exists())
922 throw new ContentNotFoundException(
923 "Unable to get related artifacts using a non-existant directory: " + repoDir.getPath() );
926 if ( !repoDir.isContainer() )
928 throw new ContentNotFoundException(
929 "Unable to get related artifacts using a non-directory: " + repoDir.getPath() );
932 // First gather up the versions found as artifacts in the managed repository.
934 try (Stream<? extends StorageAsset> stream = repoDir.list().stream() ) {
935 return stream.filter(
936 asset -> !asset.isContainer())
939 return toArtifactReference(path.getPath());
940 } catch (LayoutException e) {
941 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
944 }).filter(Objects::nonNull).filter(getChecker( reference, ext )).collect(Collectors.toList());
945 } catch (RuntimeException e) {
946 Throwable cause = e.getCause( );
948 if (cause instanceof LayoutException) {
949 throw (LayoutException)cause;
952 throw new ContentAccessException( cause.getMessage( ), cause );
955 throw new ContentAccessException( e.getMessage( ), e );
961 public List<StorageAsset> getRelatedAssets( ArtifactReference reference ) throws ContentNotFoundException, LayoutException, ContentAccessException
967 public String getRepoRoot()
969 return convertUriToPath( repository.getLocation() );
972 private String convertUriToPath( URI uri ) {
973 if (uri.getScheme()==null) {
974 return Paths.get(uri.getPath()).toString();
975 } else if ("file".equals(uri.getScheme())) {
976 return Paths.get(uri).toString();
978 return uri.toString();
983 public ManagedRepository getRepository()
989 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
992 * @return the Set of available versions, based on the project reference.
993 * @throws LayoutException
996 public Set<String> getVersions( ProjectReference reference )
997 throws ContentNotFoundException, LayoutException, ContentAccessException
999 final String path = toPath( reference );
1000 final Path projDir = getRepoDir().resolve(toPath(reference));
1001 if ( !Files.exists(projDir) )
1003 throw new ContentNotFoundException(
1004 "Unable to get Versions on a non-existant directory for repository "+getId()+": " + path );
1007 if ( !Files.isDirectory(projDir) )
1009 throw new ContentNotFoundException(
1010 "Unable to get Versions on a non-directory for repository "+getId()+": " + path );
1013 final String groupId = reference.getGroupId();
1014 final String artifactId = reference.getArtifactId();
1015 try(Stream<Path> stream = Files.list(projDir)) {
1016 return stream.filter(Files::isDirectory).map(
1017 p -> toVersion(groupId, artifactId, p.getFileName().toString())
1018 ).filter(this::hasArtifact).map(ref -> ref.getVersion())
1019 .collect(Collectors.toSet());
1020 } catch (IOException e) {
1021 log.error("Could not read directory {}: {}", projDir, e.getMessage(), e);
1022 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, e );
1023 } catch (RuntimeException e) {
1024 Throwable cause = e.getCause( );
1027 if ( cause instanceof LayoutException )
1029 throw (LayoutException) cause;
1031 log.error("Could not read directory {}: {}", projDir, cause.getMessage(), cause);
1032 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, cause );
1035 log.error("Could not read directory {}: {}", projDir, e.getMessage(), e);
1036 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, cause );
1042 public Set<String> getVersions( VersionedReference reference )
1043 throws ContentNotFoundException, ContentAccessException, LayoutException
1045 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1047 return stream.filter( Objects::nonNull )
1048 .map( ar -> ar.getVersion( ) )
1049 .collect( Collectors.toSet( ) );
1050 } catch (IOException e) {
1051 final String path = toPath( reference );
1052 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1053 throw new ContentAccessException( "Could not read path for repository "+getId()+": "+ path, e );
1058 public boolean hasContent( ArtifactReference reference ) throws ContentAccessException
1060 StorageAsset artifactFile = toFile( reference );
1061 return artifactFile.exists() && !artifactFile.isContainer();
1065 public boolean hasContent( ProjectReference reference ) throws ContentAccessException
1069 Set<String> versions = getVersions( reference );
1070 return !versions.isEmpty();
1072 catch ( ContentNotFoundException | LayoutException e )
1079 public boolean hasContent( VersionedReference reference ) throws ContentAccessException
1083 return ( getFirstArtifact( reference ) != null );
1085 catch ( LayoutException | ContentNotFoundException e )
1089 catch ( IOException e )
1091 String path = toPath( reference );
1092 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1093 throw new ContentAccessException( "Could not read path from repository " + getId( ) + ": " + path, e );
1098 public void setRepository( final ManagedRepository repo )
1100 this.repository = repo;
1102 if (repository instanceof EditableManagedRepository) {
1103 ((EditableManagedRepository) repository).setContent(this);
1108 private Path getRepoDir() {
1109 return repository.getAsset( "" ).getFilePath( );
1112 private RepositoryStorage getStorage() {
1113 return repository.getAsset( "" ).getStorage( );
1117 * Convert a path to an artifact reference.
1119 * @param path the path to convert. (relative or full location path)
1120 * @throws LayoutException if the path cannot be converted to an artifact reference.
1123 public ArtifactReference toArtifactReference( String path )
1124 throws LayoutException
1126 String repoPath = convertUriToPath( repository.getLocation() );
1127 if ( ( path != null ) && path.startsWith( repoPath ) && repoPath.length() > 0 )
1129 return super.toArtifactReference( path.substring( repoPath.length() + 1 ) );
1132 if (repoPath!=null) {
1133 while (repoPath.startsWith("/")) {
1134 repoPath = repoPath.substring(1);
1137 return super.toArtifactReference( repoPath );
1142 // The variant with runtime exception for stream usage
1143 private ArtifactReference toArtifactRef(String path) {
1145 return toArtifactReference(path);
1146 } catch (LayoutException e) {
1147 throw new RuntimeException(e);
1154 public StorageAsset toFile( ArtifactReference reference )
1156 return repository.getAsset(toPath(reference));
1160 public StorageAsset toFile( ArchivaArtifact reference )
1162 return repository.getAsset( toPath( reference ) );
1166 public StorageAsset toFile( VersionedReference reference )
1168 return repository.getAsset( toPath( reference ) );
1172 * Get the first Artifact found in the provided VersionedReference location.
1174 * @param reference the reference to the versioned reference to search within
1175 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
1176 * no artifact was found within the versioned reference.
1177 * @throws java.io.IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
1178 * @throws LayoutException
1180 private ArtifactReference getFirstArtifact( VersionedReference reference )
1181 throws ContentNotFoundException, LayoutException, IOException
1183 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1185 return stream.findFirst( ).orElse( null );
1186 } catch (RuntimeException e) {
1187 throw new ContentNotFoundException( e.getMessage( ), e.getCause( ) );
1191 private Stream<ArtifactReference> getArtifactStream(VersionedReference reference) throws ContentNotFoundException, LayoutException, IOException {
1192 final Path repoBase = getRepoDir( );
1193 String path = toMetadataPath( reference );
1194 Path versionDir = repoBase.resolve( path ).getParent();
1195 if ( !Files.exists(versionDir) )
1197 throw new ContentNotFoundException( "Unable to gather the list of artifacts on a non-existant directory: "
1198 + versionDir.toAbsolutePath() );
1201 if ( !Files.isDirectory(versionDir) )
1203 throw new ContentNotFoundException(
1204 "Unable to gather the list of snapshot versions on a non-directory: " + versionDir.toAbsolutePath() );
1206 return Files.list(versionDir).filter(Files::isRegularFile)
1207 .map(p -> repoBase.relativize(p).toString())
1208 .filter(p -> !filetypes.matchesDefaultExclusions(p))
1209 .filter(filetypes::matchesArtifactPattern)
1210 .map(this::toArtifactRef);
1213 public List<ArtifactReference> getArtifacts(VersionedReference reference) throws ContentNotFoundException, LayoutException, ContentAccessException
1215 try (Stream<ArtifactReference> stream = getArtifactStream( reference ))
1217 return stream.collect( Collectors.toList( ) );
1218 } catch ( IOException e )
1220 String path = toPath( reference );
1221 log.error("Could not read directory from repository {} - {}: ", getId(), path, e.getMessage(), e);
1222 throw new ContentAccessException( "Could not read path from repository " + getId( ) + ": " + path, e );
1227 private boolean hasArtifact( VersionedReference reference )
1230 try(Stream<ArtifactReference> stream = getArtifactStream( reference ))
1232 return stream.anyMatch( e -> true );
1233 } catch (ContentNotFoundException e) {
1235 } catch ( LayoutException | IOException e) {
1236 // We throw the runtime exception for better stream handling
1237 throw new RuntimeException(e);
1241 public void setFiletypes( FileTypes filetypes )
1243 this.filetypes = filetypes;
1246 public void setMavenContentHelper( MavenContentHelper contentHelper) {
1247 this.mavenContentHelper = contentHelper;