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.apache.commons.lang.StringUtils;
35 import org.springframework.context.annotation.Scope;
36 import org.springframework.stereotype.Service;
38 import javax.inject.Inject;
39 import javax.inject.Named;
41 import java.io.IOException;
42 import java.util.Collections;
43 import java.util.HashSet;
47 * ManagedDefaultRepositoryContent
51 @Service( "managedRepositoryContent#default" )
53 public class ManagedDefaultRepositoryContent
54 extends AbstractDefaultRepositoryContent
55 implements ManagedRepositoryContent
58 @Named( value = "fileTypes" )
59 private FileTypes filetypes;
61 private ManagedRepository repository;
63 public ManagedDefaultRepositoryContent()
65 // default to use if there are none supplied as components
66 this.artifactMappingProviders = Collections.singletonList( new DefaultArtifactMappingProvider() );
69 public void deleteVersion( VersionedReference reference )
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 public void deleteArtifact( ArtifactReference artifactReference )
83 String path = toPath( artifactReference );
84 File filePath = new File( getRepoRoot(), path );
86 if ( filePath.exists() )
88 FileUtils.deleteQuietly( filePath );
91 File filePathmd5 = new File( getRepoRoot(), path + ".md5" );
93 if ( filePathmd5.exists() )
95 FileUtils.deleteQuietly( filePathmd5 );
98 File filePathsha1 = new File( getRepoRoot(), path + ".sha1" );
100 if ( filePathsha1.exists() )
102 FileUtils.deleteQuietly( filePathsha1 );
106 public void deleteGroupId( String groupId )
107 throws ContentNotFoundException
110 String path = StringUtils.replaceChars( groupId, '.', '/' );
112 File directory = new File( getRepoRoot(), path );
114 if ( directory.exists() )
118 FileUtils.deleteDirectory( directory );
120 catch ( IOException e )
122 log.warn( "skip error deleting directory {}:", directory.getPath(), e );
127 public String getId()
129 return repository.getId();
132 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
133 throws ContentNotFoundException
135 File artifactFile = toFile( reference );
136 File repoDir = artifactFile.getParentFile();
138 if ( !repoDir.exists() )
140 throw new ContentNotFoundException(
141 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
144 if ( !repoDir.isDirectory() )
146 throw new ContentNotFoundException(
147 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
150 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
152 // First gather up the versions found as artifacts in the managed repository.
153 File repoFiles[] = repoDir.listFiles();
154 for ( int i = 0; i < repoFiles.length; i++ )
156 if ( repoFiles[i].isDirectory() )
158 // Skip it. it's a directory.
162 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
164 if ( filetypes.matchesArtifactPattern( relativePath ) )
168 ArtifactReference artifact = toArtifactReference( relativePath );
170 // Test for related, groupId / artifactId / version must match.
171 if ( artifact.getGroupId().equals( reference.getGroupId() ) && artifact.getArtifactId().equals(
172 reference.getArtifactId() ) && artifact.getVersion().equals( reference.getVersion() ) )
174 foundArtifacts.add( artifact );
177 catch ( LayoutException e )
179 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
184 return foundArtifacts;
187 public String getRepoRoot()
189 return repository.getLocation();
192 public ManagedRepository getRepository()
198 * Gather the Available Versions (on disk) for a specific Project Reference, based on filesystem
201 * @return the Set of available versions, based on the project reference.
202 * @throws LayoutException
203 * @throws LayoutException
205 public Set<String> getVersions( ProjectReference reference )
206 throws ContentNotFoundException, LayoutException
208 String path = toMetadataPath( reference );
210 int idx = path.lastIndexOf( '/' );
213 path = path.substring( 0, idx );
216 File repoDir = new File( repository.getLocation(), path );
218 if ( !repoDir.exists() )
220 throw new ContentNotFoundException(
221 "Unable to get Versions on a non-existant directory: " + repoDir.getAbsolutePath() );
224 if ( !repoDir.isDirectory() )
226 throw new ContentNotFoundException(
227 "Unable to get Versions on a non-directory: " + repoDir.getAbsolutePath() );
230 Set<String> foundVersions = new HashSet<String>();
231 VersionedReference versionRef = new VersionedReference();
232 versionRef.setGroupId( reference.getGroupId() );
233 versionRef.setArtifactId( reference.getArtifactId() );
235 File repoFiles[] = repoDir.listFiles();
236 for ( int i = 0; i < repoFiles.length; i++ )
238 if ( !repoFiles[i].isDirectory() )
240 // Skip it. not a directory.
244 // Test if dir has an artifact, which proves to us that it is a valid version directory.
245 String version = repoFiles[i].getName();
246 versionRef.setVersion( version );
248 if ( hasArtifact( versionRef ) )
250 // Found an artifact, must be a valid version.
251 foundVersions.add( version );
255 return foundVersions;
258 public Set<String> getVersions( VersionedReference reference )
259 throws ContentNotFoundException
261 String path = toMetadataPath( reference );
263 int idx = path.lastIndexOf( '/' );
266 path = path.substring( 0, idx );
269 File repoDir = new File( repository.getLocation(), path );
271 if ( !repoDir.exists() )
273 throw new ContentNotFoundException(
274 "Unable to get versions on a non-existant directory: " + repoDir.getAbsolutePath() );
277 if ( !repoDir.isDirectory() )
279 throw new ContentNotFoundException(
280 "Unable to get versions on a non-directory: " + repoDir.getAbsolutePath() );
283 Set<String> foundVersions = new HashSet<String>();
285 // First gather up the versions found as artifacts in the managed repository.
286 File repoFiles[] = repoDir.listFiles();
287 for ( int i = 0; i < repoFiles.length; i++ )
289 if ( repoFiles[i].isDirectory() )
291 // Skip it. it's a directory.
295 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
297 if ( filetypes.matchesDefaultExclusions( relativePath ) )
299 // Skip it, it's metadata or similar
303 if ( filetypes.matchesArtifactPattern( relativePath ) )
307 ArtifactReference artifact = toArtifactReference( relativePath );
309 foundVersions.add( artifact.getVersion() );
311 catch ( LayoutException e )
313 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
318 return foundVersions;
321 public boolean hasContent( ArtifactReference reference )
323 File artifactFile = toFile( reference );
324 return artifactFile.exists() && artifactFile.isFile();
327 public boolean hasContent( ProjectReference reference )
331 Set<String> versions = getVersions( reference );
332 return !versions.isEmpty();
334 catch ( ContentNotFoundException e )
338 catch ( LayoutException e )
344 public boolean hasContent( VersionedReference reference )
348 return ( getFirstArtifact( reference ) != null );
350 catch ( IOException e )
354 catch ( LayoutException e )
360 public void setRepository( ManagedRepository repository )
362 this.repository = repository;
366 * Convert a path to an artifact reference.
368 * @param path the path to convert. (relative or full location path)
369 * @throws LayoutException if the path cannot be converted to an artifact reference.
372 public ArtifactReference toArtifactReference( String path )
373 throws LayoutException
375 if ( ( path != null ) && path.startsWith( repository.getLocation() ) && repository.getLocation().length() > 0 )
377 return super.toArtifactReference( path.substring( repository.getLocation().length() + 1 ) );
380 return super.toArtifactReference( path );
383 public File toFile( ArtifactReference reference )
385 return new File( repository.getLocation(), toPath( reference ) );
388 public File toFile( ArchivaArtifact reference )
390 return new File( repository.getLocation(), toPath( reference ) );
394 * Get the first Artifact found in the provided VersionedReference location.
396 * @param reference the reference to the versioned reference to search within
397 * @return the ArtifactReference to the first artifact located within the versioned reference. or null if
398 * no artifact was found within the versioned reference.
399 * @throws IOException if the versioned reference is invalid (example: doesn't exist, or isn't a directory)
400 * @throws LayoutException
402 private ArtifactReference getFirstArtifact( VersionedReference reference )
403 throws LayoutException, IOException
405 String path = toMetadataPath( reference );
407 int idx = path.lastIndexOf( '/' );
410 path = path.substring( 0, idx );
413 File repoDir = new File( repository.getLocation(), path );
415 if ( !repoDir.exists() )
417 throw new IOException( "Unable to gather the list of snapshot versions on a non-existant directory: "
418 + repoDir.getAbsolutePath() );
421 if ( !repoDir.isDirectory() )
423 throw new IOException(
424 "Unable to gather the list of snapshot versions on a non-directory: " + repoDir.getAbsolutePath() );
427 File repoFiles[] = repoDir.listFiles();
428 for ( int i = 0; i < repoFiles.length; i++ )
430 if ( repoFiles[i].isDirectory() )
432 // Skip it. it's a directory.
436 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
438 if ( filetypes.matchesArtifactPattern( relativePath ) )
440 ArtifactReference artifact = toArtifactReference( relativePath );
446 // No artifact was found.
450 private boolean hasArtifact( VersionedReference reference )
451 throws LayoutException
455 return ( getFirstArtifact( reference ) != null );
457 catch ( IOException e )
463 public void setFiletypes( FileTypes filetypes )
465 this.filetypes = filetypes;