1 package org.apache.archiva.repository.content.legacy;
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.RepositoryException;
32 import org.apache.archiva.repository.layout.LayoutException;
33 import org.apache.commons.collections.CollectionUtils;
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;
40 import java.util.HashSet;
44 * ManagedLegacyRepositoryContent
47 * TODO no need to be a component when filetypes, legacy path parser is not
49 @Service( "managedRepositoryContent#legacy" )
51 public class ManagedLegacyRepositoryContent
52 extends AbstractLegacyRepositoryContent
53 implements ManagedRepositoryContent
59 private FileTypes filetypes;
61 private ManagedRepository repository;
64 public void deleteVersion( VersionedReference reference )
65 throws ContentNotFoundException
67 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
69 if ( !groupDir.exists() )
71 throw new ContentNotFoundException(
72 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
75 if ( !groupDir.isDirectory() )
77 throw new ContentNotFoundException(
78 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
81 // First gather up the versions found as artifacts in the managed repository.
82 File typeDirs[] = groupDir.listFiles();
83 for ( File typeDir : typeDirs )
85 if ( !typeDir.isDirectory() )
87 // Skip it, we only care about directories.
91 if ( !typeDir.getName().endsWith( "s" ) )
93 // Skip it, we only care about directories that end in "s".
96 deleteVersions( typeDir, reference );
100 private void deleteVersions( File typeDir, VersionedReference reference )
102 File repoFiles[] = typeDir.listFiles();
103 for ( File repoFile : repoFiles )
105 if ( repoFile.isDirectory() )
107 // Skip it. it's a directory.
111 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
113 if ( filetypes.matchesArtifactPattern( relativePath ) )
117 ArtifactReference artifact = toArtifactReference( relativePath );
118 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
119 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
122 deleteSupportFiles( repoFile );
125 catch ( LayoutException e )
127 /* don't fail the process if there is a bad artifact within the directory. */
134 public void deleteProject( String namespace, String projectId )
135 throws RepositoryException
137 // TODO implements ??
140 private void deleteSupportFiles( File repoFile )
142 deleteSupportFile( repoFile, ".sha1" );
143 deleteSupportFile( repoFile, ".md5" );
144 deleteSupportFile( repoFile, ".asc" );
145 deleteSupportFile( repoFile, ".gpg" );
148 private void deleteSupportFile( File repoFile, String supportExtension )
150 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
151 if ( supportFile.exists() && supportFile.isFile() )
153 supportFile.delete();
158 public String getId()
160 return repository.getId();
164 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
165 throws ContentNotFoundException
167 File artifactFile = toFile( reference );
168 File repoDir = artifactFile.getParentFile();
170 if ( !repoDir.exists() )
172 throw new ContentNotFoundException(
173 "Unable to get related artifacts using a non-existant directory: " + repoDir.getAbsolutePath() );
176 if ( !repoDir.isDirectory() )
178 throw new ContentNotFoundException(
179 "Unable to get related artifacts using a non-directory: " + repoDir.getAbsolutePath() );
182 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
184 // First gather up the versions found as artifacts in the managed repository.
185 File projectParentDir = repoDir.getParentFile();
186 File typeDirs[] = projectParentDir.listFiles();
187 for ( File typeDir : typeDirs )
189 if ( !typeDir.isDirectory() )
191 // Skip it, we only care about directories.
195 if ( !typeDir.getName().endsWith( "s" ) )
197 // Skip it, we only care about directories that end in "s".
200 getRelatedArtifacts( typeDir, reference, foundArtifacts );
203 return foundArtifacts;
207 public String getRepoRoot()
209 return repository.getLocation();
213 public ManagedRepository getRepository()
219 public Set<String> getVersions( ProjectReference reference )
220 throws ContentNotFoundException
222 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
224 if ( !groupDir.exists() )
226 throw new ContentNotFoundException(
227 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
230 if ( !groupDir.isDirectory() )
232 throw new ContentNotFoundException(
233 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
236 Set<String> foundVersions = new HashSet<String>();
238 // First gather up the versions found as artifacts in the managed repository.
239 File typeDirs[] = groupDir.listFiles();
240 for ( File typeDir : typeDirs )
242 if ( !typeDir.isDirectory() )
244 // Skip it, we only care about directories.
248 if ( !typeDir.getName().endsWith( "s" ) )
250 // Skip it, we only care about directories that end in "s".
253 getProjectVersions( typeDir, reference, foundVersions );
256 return foundVersions;
260 public Set<String> getVersions( VersionedReference reference )
261 throws ContentNotFoundException
263 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
265 if ( !groupDir.exists() )
267 throw new ContentNotFoundException(
268 "Unable to get versions using a non-existant groupId directory: " + groupDir.getAbsolutePath() );
271 if ( !groupDir.isDirectory() )
273 throw new ContentNotFoundException(
274 "Unable to get versions using a non-directory: " + groupDir.getAbsolutePath() );
277 Set<String> foundVersions = new HashSet<String>();
279 // First gather up the versions found as artifacts in the managed repository.
280 File typeDirs[] = groupDir.listFiles();
281 for ( File typeDir : typeDirs )
283 if ( !typeDir.isDirectory() )
285 // Skip it, we only care about directories.
289 if ( !typeDir.getName().endsWith( "s" ) )
291 // Skip it, we only care about directories that end in "s".
294 getVersionedVersions( typeDir, reference, foundVersions );
297 return foundVersions;
301 public boolean hasContent( ArtifactReference reference )
303 File artifactFile = toFile( reference );
304 return artifactFile.exists() && artifactFile.isFile();
308 public boolean hasContent( ProjectReference reference )
312 Set<String> versions = getVersions( reference );
313 return CollectionUtils.isNotEmpty( versions );
315 catch ( ContentNotFoundException e )
322 public boolean hasContent( VersionedReference reference )
326 Set<String> versions = getVersions( reference );
327 return CollectionUtils.isNotEmpty( versions );
329 catch ( ContentNotFoundException e )
336 public void setRepository( ManagedRepository repository )
338 this.repository = repository;
342 * Convert a path to an artifact reference.
344 * @param path the path to convert. (relative or full location path)
345 * @throws org.apache.archiva.repository.layout.LayoutException if the path cannot be converted to an artifact reference.
348 public ArtifactReference toArtifactReference( String path )
349 throws LayoutException
351 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
353 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
356 return super.toArtifactReference( path );
360 public File toFile( ArchivaArtifact reference )
362 return new File( repository.getLocation(), toPath( reference ) );
366 public File toFile( ArtifactReference reference )
368 return new File( repository.getLocation(), toPath( reference ) );
372 public String toMetadataPath( ProjectReference reference )
374 // No metadata present in legacy repository.
379 public String toMetadataPath( VersionedReference reference )
381 // No metadata present in legacy repository.
385 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
387 File repoFiles[] = typeDir.listFiles();
388 for ( File repoFile : repoFiles )
390 if ( repoFile.isDirectory() )
392 // Skip it. it's a directory.
396 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
398 if ( filetypes.matchesArtifactPattern( relativePath ) )
402 ArtifactReference artifact = toArtifactReference( relativePath );
403 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
405 foundVersions.add( artifact.getVersion() );
408 catch ( LayoutException e )
410 /* don't fail the process if there is a bad artifact within the directory. */
416 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
418 File repoFiles[] = typeDir.listFiles();
419 for ( int i = 0; i < repoFiles.length; i++ )
421 if ( repoFiles[i].isDirectory() )
423 // Skip it. it's a directory.
427 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
429 if ( filetypes.matchesArtifactPattern( relativePath ) )
433 ArtifactReference artifact = toArtifactReference( relativePath );
434 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
435 && artifact.getVersion().startsWith( reference.getVersion() ) )
437 foundArtifacts.add( artifact );
440 catch ( LayoutException e )
442 /* don't fail the process if there is a bad artifact within the directory. */
448 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
450 File repoFiles[] = typeDir.listFiles();
451 for ( int i = 0; i < repoFiles.length; i++ )
453 if ( repoFiles[i].isDirectory() )
455 // Skip it. it's a directory.
459 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
461 if ( filetypes.matchesArtifactPattern( relativePath ) )
465 ArtifactReference artifact = toArtifactReference( relativePath );
466 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
467 && artifact.getVersion().startsWith( reference.getVersion() ) )
469 foundVersions.add( artifact.getVersion() );
472 catch ( LayoutException e )
474 /* don't fail the process if there is a bad artifact within the directory. */
480 public void setFileTypes( FileTypes fileTypes )
482 this.filetypes = fileTypes;
486 public void deleteArtifact( ArtifactReference artifactReference )
487 throws ContentNotFoundException
489 // TODO implements for legacy ??
493 public void deleteGroupId( String groupId )
494 throws ContentNotFoundException
496 // TODO implements for legacy ??