1 package org.apache.archiva.repository.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
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.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
24 import org.apache.commons.io.FileUtils;
25 import org.apache.archiva.common.utils.PathUtil;
26 import org.apache.archiva.configuration.FileTypes;
27 import org.apache.archiva.model.ArchivaArtifact;
28 import org.apache.archiva.model.ArtifactReference;
29 import org.apache.archiva.model.ProjectReference;
30 import org.apache.archiva.model.VersionedReference;
31 import org.apache.archiva.repository.ContentNotFoundException;
32 import org.apache.archiva.repository.ManagedRepositoryContent;
33 import org.apache.archiva.repository.layout.LayoutException;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
38 import javax.inject.Named;
40 import java.io.IOException;
41 import java.util.Collections;
42 import java.util.HashSet;
46 * ManagedDefaultRepositoryContent
50 @Service("managedRepositoryContent#default")
52 public class ManagedDefaultRepositoryContent
53 extends AbstractDefaultRepositoryContent
54 implements ManagedRepositoryContent
57 @Named(value = "fileTypes" )
58 private FileTypes filetypes;
60 private ManagedRepository repository;
62 public ManagedDefaultRepositoryContent()
64 // default to use if there are none supplied as components
65 this.artifactMappingProviders = Collections.singletonList( new DefaultArtifactMappingProvider() );
68 public void deleteVersion( VersionedReference reference )
69 throws ContentNotFoundException
71 String path = toMetadataPath( reference );
72 File projectPath = new File( getRepoRoot(), path );
74 File projectDir = projectPath.getParentFile();
75 if( projectDir.exists() && projectDir.isDirectory() )
77 FileUtils.deleteQuietly( projectDir );
81 throw new ContentNotFoundException( "Unable to delete non-existing project directory: " + projectDir );
87 return repository.getId();
90 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
91 throws ContentNotFoundException
93 File artifactFile = toFile( reference );
94 File repoDir = artifactFile.getParentFile();
96 if ( !repoDir.exists() )
98 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
99 + repoDir.getAbsolutePath() );
102 if ( !repoDir.isDirectory() )
104 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
105 + repoDir.getAbsolutePath() );
108 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
110 // First gather up the versions found as artifacts in the managed repository.
111 File repoFiles[] = repoDir.listFiles();
112 for ( int i = 0; i < repoFiles.length; i++ )
114 if ( repoFiles[i].isDirectory() )
116 // Skip it. it's a directory.
120 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
122 if ( filetypes.matchesArtifactPattern( relativePath ) )
126 ArtifactReference artifact = toArtifactReference( relativePath );
128 // Test for related, groupId / artifactId / version must match.
129 if ( artifact.getGroupId().equals( reference.getGroupId() )
130 && artifact.getArtifactId().equals( reference.getArtifactId() )
131 && artifact.getVersion().equals( reference.getVersion() ) )
133 foundArtifacts.add( artifact );
136 catch ( LayoutException e )
138 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
143 return foundArtifacts;
146 public String getRepoRoot()
148 return repository.getLocation();
151 public ManagedRepository getRepository()
157 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
160 * @return the Set of available versions, based on the project reference.
161 * @throws LayoutException
162 * @throws LayoutException
164 public Set<String> getVersions( ProjectReference reference )
165 throws ContentNotFoundException, LayoutException
167 String path = toMetadataPath( reference );
169 int idx = path.lastIndexOf( '/' );
172 path = path.substring( 0, idx );
175 File repoDir = new File( repository.getLocation(), path );
177 if ( !repoDir.exists() )
179 throw new ContentNotFoundException( "Unable to get Versions on a non-existant directory: "
180 + repoDir.getAbsolutePath() );
183 if ( !repoDir.isDirectory() )
185 throw new ContentNotFoundException( "Unable to get Versions on a non-directory: "
186 + repoDir.getAbsolutePath() );
189 Set<String> foundVersions = new HashSet<String>();
190 VersionedReference versionRef = new VersionedReference();
191 versionRef.setGroupId( reference.getGroupId() );
192 versionRef.setArtifactId( reference.getArtifactId() );
194 File repoFiles[] = repoDir.listFiles();
195 for ( int i = 0; i < repoFiles.length; i++ )
197 if ( !repoFiles[i].isDirectory() )
199 // Skip it. not a directory.
203 // Test if dir has an artifact, which proves to us that it is a valid version directory.
204 String version = repoFiles[i].getName();
205 versionRef.setVersion( version );
207 if ( hasArtifact( versionRef ) )
209 // Found an artifact, must be a valid version.
210 foundVersions.add( version );
214 return foundVersions;
217 public Set<String> getVersions( VersionedReference reference )
218 throws ContentNotFoundException
220 String path = toMetadataPath( reference );
222 int idx = path.lastIndexOf( '/' );
225 path = path.substring( 0, idx );
228 File repoDir = new File( repository.getLocation(), path );
230 if ( !repoDir.exists() )
232 throw new ContentNotFoundException( "Unable to get versions on a non-existant directory: "
233 + repoDir.getAbsolutePath() );
236 if ( !repoDir.isDirectory() )
238 throw new ContentNotFoundException( "Unable to get versions on a non-directory: "
239 + repoDir.getAbsolutePath() );
242 Set<String> foundVersions = new HashSet<String>();
244 // First gather up the versions found as artifacts in the managed repository.
245 File repoFiles[] = repoDir.listFiles();
246 for ( int i = 0; i < repoFiles.length; i++ )
248 if ( repoFiles[i].isDirectory() )
250 // Skip it. it's a directory.
254 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
256 if ( filetypes.matchesDefaultExclusions( relativePath ) )
258 // Skip it, it's metadata or similar
262 if ( filetypes.matchesArtifactPattern( relativePath ) )
266 ArtifactReference artifact = toArtifactReference( relativePath );
268 foundVersions.add( artifact.getVersion() );
270 catch ( LayoutException e )
272 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
277 return foundVersions;
280 public boolean hasContent( ArtifactReference reference )
282 File artifactFile = toFile( reference );
283 return artifactFile.exists() && artifactFile.isFile();
286 public boolean hasContent( ProjectReference reference )
290 Set<String> versions = getVersions( reference );
291 return !versions.isEmpty();
293 catch ( ContentNotFoundException e )
297 catch ( LayoutException e )
303 public boolean hasContent( VersionedReference reference )
307 return ( getFirstArtifact( reference ) != null );
309 catch ( IOException e )
313 catch ( LayoutException e )
319 public void setRepository( ManagedRepository repository )
321 this.repository = repository;
325 * Convert a path to an artifact reference.
327 * @param path the path to convert. (relative or full location path)
328 * @throws LayoutException if the path cannot be converted to an artifact reference.
331 public ArtifactReference toArtifactReference( String path )
332 throws LayoutException
334 if ( ( path != null ) && path.startsWith( repository.getLocation() ) && repository.getLocation().length() > 0 )
336 return super.toArtifactReference( path.substring( repository.getLocation().length() + 1 ) );
339 return super.toArtifactReference( path );
342 public File toFile( ArtifactReference reference )
344 return new File( repository.getLocation(), toPath( reference ) );
347 public File toFile( ArchivaArtifact reference )
349 return new File( repository.getLocation(), toPath( reference ) );
353 * Get the first Artifact found in the provided VersionedReference location.
355 * @param reference the reference to the versioned reference to search within
356 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
357 * no artifact was found within the versioned reference.
358 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
359 * @throws LayoutException
361 private ArtifactReference getFirstArtifact( VersionedReference reference )
362 throws LayoutException, IOException
364 String path = toMetadataPath( reference );
366 int idx = path.lastIndexOf( '/' );
369 path = path.substring( 0, idx );
372 File repoDir = new File( repository.getLocation(), path );
374 if ( !repoDir.exists() )
376 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
377 + repoDir.getAbsolutePath() );
380 if ( !repoDir.isDirectory() )
382 throw new IOException( "Unable to gather the list of snapshot versions on a non-directory: "
383 + repoDir.getAbsolutePath() );
386 File repoFiles[] = repoDir.listFiles();
387 for ( int i = 0; i < repoFiles.length; i++ )
389 if ( repoFiles[i].isDirectory() )
391 // Skip it. it's a directory.
395 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
397 if ( filetypes.matchesArtifactPattern( relativePath ) )
399 ArtifactReference artifact = toArtifactReference( relativePath );
405 // No artifact was found.
409 private boolean hasArtifact( VersionedReference reference )
410 throws LayoutException
414 return ( getFirstArtifact( reference ) != null );
416 catch ( IOException e )
422 public void setFiletypes( FileTypes filetypes )
424 this.filetypes = filetypes;