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.model.ArchivaArtifact;
26 import org.apache.archiva.model.ArtifactReference;
27 import org.apache.archiva.model.ProjectReference;
28 import org.apache.archiva.model.VersionedReference;
29 import org.apache.archiva.repository.ContentNotFoundException;
30 import org.apache.archiva.repository.ManagedRepositoryContent;
31 import org.apache.archiva.repository.layout.LayoutException;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.springframework.context.annotation.Scope;
35 import org.springframework.stereotype.Service;
37 import javax.inject.Inject;
39 import java.util.HashSet;
43 * ManagedLegacyRepositoryContent
46 * @todo no need to be a component when filetypes, legacy path parser is not
48 @Service( "managedRepositoryContent#legacy" )
50 public class ManagedLegacyRepositoryContent
51 extends AbstractLegacyRepositoryContent
52 implements ManagedRepositoryContent
58 private FileTypes filetypes;
60 private ManagedRepository repository;
62 public void deleteVersion( VersionedReference reference )
63 throws ContentNotFoundException
65 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
67 if ( !groupDir.exists() )
69 throw new ContentNotFoundException(
70 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
73 if ( !groupDir.isDirectory() )
75 throw new ContentNotFoundException(
76 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
79 // First gather up the versions found as artifacts in the managed repository.
80 File typeDirs[] = groupDir.listFiles();
81 for ( File typeDir : typeDirs )
83 if ( !typeDir.isDirectory() )
85 // Skip it, we only care about directories.
89 if ( !typeDir.getName().endsWith( "s" ) )
91 // Skip it, we only care about directories that end in "s".
94 deleteVersions( typeDir, reference );
98 private void deleteVersions( File typeDir, VersionedReference reference )
100 File repoFiles[] = typeDir.listFiles();
101 for ( File repoFile : repoFiles )
103 if ( repoFile.isDirectory() )
105 // Skip it. it's a directory.
109 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
111 if ( filetypes.matchesArtifactPattern( relativePath ) )
115 ArtifactReference artifact = toArtifactReference( relativePath );
116 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
117 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
120 deleteSupportFiles( repoFile );
123 catch ( LayoutException e )
125 /* don't fail the process if there is a bad artifact within the directory. */
131 private void deleteSupportFiles( File repoFile )
133 deleteSupportFile( repoFile, ".sha1" );
134 deleteSupportFile( repoFile, ".md5" );
135 deleteSupportFile( repoFile, ".asc" );
136 deleteSupportFile( repoFile, ".gpg" );
139 private void deleteSupportFile( File repoFile, String supportExtension )
141 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
142 if ( supportFile.exists() && supportFile.isFile() )
144 supportFile.delete();
148 public String getId()
150 return repository.getId();
153 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
154 throws ContentNotFoundException
156 File artifactFile = toFile( reference );
157 File repoDir = artifactFile.getParentFile();
159 if ( !repoDir.exists() )
161 throw new ContentNotFoundException(
162 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
165 if ( !repoDir.isDirectory() )
167 throw new ContentNotFoundException(
168 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
171 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
173 // First gather up the versions found as artifacts in the managed repository.
174 File projectParentDir = repoDir.getParentFile();
175 File typeDirs[] = projectParentDir.listFiles();
176 for ( File typeDir : typeDirs )
178 if ( !typeDir.isDirectory() )
180 // Skip it, we only care about directories.
184 if ( !typeDir.getName().endsWith( "s" ) )
186 // Skip it, we only care about directories that end in "s".
189 getRelatedArtifacts( typeDir, reference, foundArtifacts );
192 return foundArtifacts;
195 public String getRepoRoot()
197 return repository.getLocation();
200 public ManagedRepository getRepository()
205 public Set<String> getVersions( ProjectReference reference )
206 throws ContentNotFoundException
208 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
210 if ( !groupDir.exists() )
212 throw new ContentNotFoundException(
213 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
216 if ( !groupDir.isDirectory() )
218 throw new ContentNotFoundException(
219 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
222 Set<String> foundVersions = new HashSet<String>();
224 // First gather up the versions found as artifacts in the managed repository.
225 File typeDirs[] = groupDir.listFiles();
226 for ( File typeDir : typeDirs )
228 if ( !typeDir.isDirectory() )
230 // Skip it, we only care about directories.
234 if ( !typeDir.getName().endsWith( "s" ) )
236 // Skip it, we only care about directories that end in "s".
239 getProjectVersions( typeDir, reference, foundVersions );
242 return foundVersions;
245 public Set<String> getVersions( VersionedReference reference )
246 throws ContentNotFoundException
248 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
250 if ( !groupDir.exists() )
252 throw new ContentNotFoundException(
253 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
256 if ( !groupDir.isDirectory() )
258 throw new ContentNotFoundException(
259 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
262 Set<String> foundVersions = new HashSet<String>();
264 // First gather up the versions found as artifacts in the managed repository.
265 File typeDirs[] = groupDir.listFiles();
266 for ( File typeDir : typeDirs )
268 if ( !typeDir.isDirectory() )
270 // Skip it, we only care about directories.
274 if ( !typeDir.getName().endsWith( "s" ) )
276 // Skip it, we only care about directories that end in "s".
279 getVersionedVersions( typeDir, reference, foundVersions );
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 CollectionUtils.isNotEmpty( versions );
298 catch ( ContentNotFoundException e )
304 public boolean hasContent( VersionedReference reference )
308 Set<String> versions = getVersions( reference );
309 return CollectionUtils.isNotEmpty( versions );
311 catch ( ContentNotFoundException e )
317 public void setRepository( ManagedRepository repository )
319 this.repository = repository;
323 * Convert a path to an artifact reference.
325 * @param path the path to convert. (relative or full location path)
326 * @throws LayoutException if the path cannot be converted to an artifact reference.
329 public ArtifactReference toArtifactReference( String path )
330 throws LayoutException
332 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
334 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
337 return super.toArtifactReference( path );
340 public File toFile( ArchivaArtifact reference )
342 return new File( repository.getLocation(), toPath( reference ) );
345 public File toFile( ArtifactReference reference )
347 return new File( repository.getLocation(), toPath( reference ) );
350 public String toMetadataPath( ProjectReference reference )
352 // No metadata present in legacy repository.
356 public String toMetadataPath( VersionedReference reference )
358 // No metadata present in legacy repository.
362 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
364 File repoFiles[] = typeDir.listFiles();
365 for ( File repoFile : repoFiles )
367 if ( repoFile.isDirectory() )
369 // Skip it. it's a directory.
373 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
375 if ( filetypes.matchesArtifactPattern( relativePath ) )
379 ArtifactReference artifact = toArtifactReference( relativePath );
380 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
382 foundVersions.add( artifact.getVersion() );
385 catch ( LayoutException e )
387 /* don't fail the process if there is a bad artifact within the directory. */
393 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
395 File repoFiles[] = typeDir.listFiles();
396 for ( int i = 0; i < repoFiles.length; i++ )
398 if ( repoFiles[i].isDirectory() )
400 // Skip it. it's a directory.
404 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
406 if ( filetypes.matchesArtifactPattern( relativePath ) )
410 ArtifactReference artifact = toArtifactReference( relativePath );
411 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
412 && artifact.getVersion().startsWith( reference.getVersion() ) )
414 foundArtifacts.add( artifact );
417 catch ( LayoutException e )
419 /* don't fail the process if there is a bad artifact within the directory. */
425 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
427 File repoFiles[] = typeDir.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 ) )
442 ArtifactReference artifact = toArtifactReference( relativePath );
443 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
444 && artifact.getVersion().startsWith( reference.getVersion() ) )
446 foundVersions.add( artifact.getVersion() );
449 catch ( LayoutException e )
451 /* don't fail the process if there is a bad artifact within the directory. */
457 public void setFileTypes( FileTypes fileTypes )
459 this.filetypes = fileTypes;
462 public void deleteArtifact( ArtifactReference artifactReference )
463 throws ContentNotFoundException
465 // TODO implements for legacy ??
468 public void deleteGroupId( String groupId )
469 throws ContentNotFoundException
471 // TODO implements for legacy ??