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.common.utils.PathUtil;
24 import org.apache.archiva.configuration.FileTypes;
25 import org.apache.archiva.metadata.repository.storage.maven2.DefaultArtifactMappingProvider;
26 import org.apache.archiva.model.ArchivaArtifact;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.ContentNotFoundException;
31 import org.apache.archiva.repository.ManagedRepositoryContent;
32 import org.apache.archiva.repository.layout.LayoutException;
33 import org.apache.commons.io.FileUtils;
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 )
70 String path = toMetadataPath( reference );
71 File projectPath = new File( getRepoRoot(), path );
73 File projectDir = projectPath.getParentFile();
74 if ( projectDir.exists() && projectDir.isDirectory() )
76 FileUtils.deleteQuietly( projectDir );
80 public void deleteArtifact( ArtifactReference artifactReference )
82 String path = toPath( artifactReference );
83 File filePath = new File( getRepoRoot(), path );
85 if ( filePath.exists() )
87 FileUtils.deleteQuietly( filePath );
93 return repository.getId();
96 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
97 throws ContentNotFoundException
99 File artifactFile = toFile( reference );
100 File repoDir = artifactFile.getParentFile();
102 if ( !repoDir.exists() )
104 throw new ContentNotFoundException(
105 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
108 if ( !repoDir.isDirectory() )
110 throw new ContentNotFoundException(
111 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
114 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
116 // First gather up the versions found as artifacts in the managed repository.
117 File repoFiles[] = repoDir.listFiles();
118 for ( int i = 0; i < repoFiles.length; i++ )
120 if ( repoFiles[i].isDirectory() )
122 // Skip it. it's a directory.
126 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
128 if ( filetypes.matchesArtifactPattern( relativePath ) )
132 ArtifactReference artifact = toArtifactReference( relativePath );
134 // Test for related, groupId / artifactId / version must match.
135 if ( artifact.getGroupId().equals( reference.getGroupId() ) && artifact.getArtifactId().equals(
136 reference.getArtifactId() ) && artifact.getVersion().equals( reference.getVersion() ) )
138 foundArtifacts.add( artifact );
141 catch ( LayoutException e )
143 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
148 return foundArtifacts;
151 public String getRepoRoot()
153 return repository.getLocation();
156 public ManagedRepository getRepository()
162 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
165 * @return the Set of available versions, based on the project reference.
166 * @throws LayoutException
167 * @throws LayoutException
169 public Set<String> getVersions( ProjectReference reference )
170 throws ContentNotFoundException, LayoutException
172 String path = toMetadataPath( reference );
174 int idx = path.lastIndexOf( '/' );
177 path = path.substring( 0, idx );
180 File repoDir = new File( repository.getLocation(), path );
182 if ( !repoDir.exists() )
184 throw new ContentNotFoundException(
185 "Unable to get Versions on a non-existant directory: " + repoDir.getAbsolutePath() );
188 if ( !repoDir.isDirectory() )
190 throw new ContentNotFoundException(
191 "Unable to get Versions on a non-directory: " + repoDir.getAbsolutePath() );
194 Set<String> foundVersions = new HashSet<String>();
195 VersionedReference versionRef = new VersionedReference();
196 versionRef.setGroupId( reference.getGroupId() );
197 versionRef.setArtifactId( reference.getArtifactId() );
199 File repoFiles[] = repoDir.listFiles();
200 for ( int i = 0; i < repoFiles.length; i++ )
202 if ( !repoFiles[i].isDirectory() )
204 // Skip it. not a directory.
208 // Test if dir has an artifact, which proves to us that it is a valid version directory.
209 String version = repoFiles[i].getName();
210 versionRef.setVersion( version );
212 if ( hasArtifact( versionRef ) )
214 // Found an artifact, must be a valid version.
215 foundVersions.add( version );
219 return foundVersions;
222 public Set<String> getVersions( VersionedReference reference )
223 throws ContentNotFoundException
225 String path = toMetadataPath( reference );
227 int idx = path.lastIndexOf( '/' );
230 path = path.substring( 0, idx );
233 File repoDir = new File( repository.getLocation(), path );
235 if ( !repoDir.exists() )
237 throw new ContentNotFoundException(
238 "Unable to get versions on a non-existant directory: " + repoDir.getAbsolutePath() );
241 if ( !repoDir.isDirectory() )
243 throw new ContentNotFoundException(
244 "Unable to get versions on a non-directory: " + repoDir.getAbsolutePath() );
247 Set<String> foundVersions = new HashSet<String>();
249 // First gather up the versions found as artifacts in the managed repository.
250 File repoFiles[] = repoDir.listFiles();
251 for ( int i = 0; i < repoFiles.length; i++ )
253 if ( repoFiles[i].isDirectory() )
255 // Skip it. it's a directory.
259 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
261 if ( filetypes.matchesDefaultExclusions( relativePath ) )
263 // Skip it, it's metadata or similar
267 if ( filetypes.matchesArtifactPattern( relativePath ) )
271 ArtifactReference artifact = toArtifactReference( relativePath );
273 foundVersions.add( artifact.getVersion() );
275 catch ( LayoutException e )
277 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
282 return foundVersions;
285 public boolean hasContent( ArtifactReference reference )
287 File artifactFile = toFile( reference );
288 return artifactFile.exists() && artifactFile.isFile();
291 public boolean hasContent( ProjectReference reference )
295 Set<String> versions = getVersions( reference );
296 return !versions.isEmpty();
298 catch ( ContentNotFoundException e )
302 catch ( LayoutException e )
308 public boolean hasContent( VersionedReference reference )
312 return ( getFirstArtifact( reference ) != null );
314 catch ( IOException e )
318 catch ( LayoutException e )
324 public void setRepository( ManagedRepository repository )
326 this.repository = repository;
330 * Convert a path to an artifact reference.
332 * @param path the path to convert. (relative or full location path)
333 * @throws LayoutException if the path cannot be converted to an artifact reference.
336 public ArtifactReference toArtifactReference( String path )
337 throws LayoutException
339 if ( ( path != null ) && path.startsWith( repository.getLocation() ) && repository.getLocation().length() > 0 )
341 return super.toArtifactReference( path.substring( repository.getLocation().length() + 1 ) );
344 return super.toArtifactReference( path );
347 public File toFile( ArtifactReference reference )
349 return new File( repository.getLocation(), toPath( reference ) );
352 public File toFile( ArchivaArtifact reference )
354 return new File( repository.getLocation(), toPath( reference ) );
358 * Get the first Artifact found in the provided VersionedReference location.
360 * @param reference the reference to the versioned reference to search within
361 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
362 * no artifact was found within the versioned reference.
363 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
364 * @throws LayoutException
366 private ArtifactReference getFirstArtifact( VersionedReference reference )
367 throws LayoutException, IOException
369 String path = toMetadataPath( reference );
371 int idx = path.lastIndexOf( '/' );
374 path = path.substring( 0, idx );
377 File repoDir = new File( repository.getLocation(), path );
379 if ( !repoDir.exists() )
381 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
382 + repoDir.getAbsolutePath() );
385 if ( !repoDir.isDirectory() )
387 throw new IOException(
388 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
391 File repoFiles[] = repoDir.listFiles();
392 for ( int i = 0; i < repoFiles.length; i++ )
394 if ( repoFiles[i].isDirectory() )
396 // Skip it. it's a directory.
400 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
402 if ( filetypes.matchesArtifactPattern( relativePath ) )
404 ArtifactReference artifact = toArtifactReference( relativePath );
410 // No artifact was found.
414 private boolean hasArtifact( VersionedReference reference )
415 throws LayoutException
419 return ( getFirstArtifact( reference ) != null );
421 catch ( IOException e )
427 public void setFiletypes( FileTypes filetypes )
429 this.filetypes = filetypes;