1 package org.apache.maven.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.commons.collections.CollectionUtils;
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.maven.archiva.common.utils.PathUtil;
25 import org.apache.maven.archiva.configuration.FileTypes;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.model.ProjectReference;
30 import org.apache.maven.archiva.model.VersionedReference;
31 import org.apache.maven.archiva.repository.ContentNotFoundException;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.layout.LayoutException;
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
47 * @todo no need to be a component when filetypes, legacy path parser is not
50 * role="org.apache.maven.archiva.repository.ManagedRepositoryContent"
52 * instantiation-strategy="per-lookup"
54 @Service("managedRepositoryContent#legacy")
56 public class ManagedLegacyRepositoryContent
57 extends AbstractLegacyRepositoryContent
58 implements ManagedRepositoryContent
64 private FileTypes filetypes;
66 private ManagedRepositoryConfiguration repository;
68 public void deleteVersion( VersionedReference reference )
69 throws ContentNotFoundException
71 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
73 if ( !groupDir.exists() )
75 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
76 + groupDir.getAbsolutePath() );
79 if ( !groupDir.isDirectory() )
81 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
82 + groupDir.getAbsolutePath() );
85 // First gather up the versions found as artifacts in the managed repository.
86 File typeDirs[] = groupDir.listFiles();
87 for ( File typeDir : typeDirs )
89 if ( !typeDir.isDirectory() )
91 // Skip it, we only care about directories.
95 if ( !typeDir.getName().endsWith( "s" ) )
97 // Skip it, we only care about directories that end in "s".
100 deleteVersions( typeDir, reference );
104 private void deleteVersions( File typeDir, VersionedReference reference )
106 File repoFiles[] = typeDir.listFiles();
107 for ( File repoFile : repoFiles )
109 if ( repoFile.isDirectory() )
111 // Skip it. it's a directory.
115 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
117 if ( filetypes.matchesArtifactPattern( relativePath ) )
121 ArtifactReference artifact = toArtifactReference( relativePath );
122 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
123 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
126 deleteSupportFiles( repoFile );
129 catch ( LayoutException e )
131 /* don't fail the process if there is a bad artifact within the directory. */
137 private void deleteSupportFiles( File repoFile )
139 deleteSupportFile( repoFile, ".sha1" );
140 deleteSupportFile( repoFile, ".md5" );
141 deleteSupportFile( repoFile, ".asc" );
142 deleteSupportFile( repoFile, ".gpg" );
145 private void deleteSupportFile( File repoFile, String supportExtension )
147 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
148 if ( supportFile.exists() && supportFile.isFile() )
150 supportFile.delete();
154 public String getId()
156 return repository.getId();
159 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
160 throws ContentNotFoundException
162 File artifactFile = toFile( reference );
163 File repoDir = artifactFile.getParentFile();
165 if ( !repoDir.exists() )
167 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
168 + repoDir.getAbsolutePath() );
171 if ( !repoDir.isDirectory() )
173 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
174 + repoDir.getAbsolutePath() );
177 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
179 // First gather up the versions found as artifacts in the managed repository.
180 File projectParentDir = repoDir.getParentFile();
181 File typeDirs[] = projectParentDir.listFiles();
182 for ( File typeDir : typeDirs )
184 if ( !typeDir.isDirectory() )
186 // Skip it, we only care about directories.
190 if ( !typeDir.getName().endsWith( "s" ) )
192 // Skip it, we only care about directories that end in "s".
195 getRelatedArtifacts( typeDir, reference, foundArtifacts );
198 return foundArtifacts;
201 public String getRepoRoot()
203 return repository.getLocation();
206 public ManagedRepositoryConfiguration getRepository()
211 public Set<String> getVersions( ProjectReference reference )
212 throws ContentNotFoundException
214 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
216 if ( !groupDir.exists() )
218 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
219 + groupDir.getAbsolutePath() );
222 if ( !groupDir.isDirectory() )
224 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
225 + groupDir.getAbsolutePath() );
228 Set<String> foundVersions = new HashSet<String>();
230 // First gather up the versions found as artifacts in the managed repository.
231 File typeDirs[] = groupDir.listFiles();
232 for ( File typeDir : typeDirs )
234 if ( !typeDir.isDirectory() )
236 // Skip it, we only care about directories.
240 if ( !typeDir.getName().endsWith( "s" ) )
242 // Skip it, we only care about directories that end in "s".
245 getProjectVersions( typeDir, reference, foundVersions );
248 return foundVersions;
251 public Set<String> getVersions( VersionedReference reference )
252 throws ContentNotFoundException
254 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
256 if ( !groupDir.exists() )
258 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
259 + groupDir.getAbsolutePath() );
262 if ( !groupDir.isDirectory() )
264 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
265 + groupDir.getAbsolutePath() );
268 Set<String> foundVersions = new HashSet<String>();
270 // First gather up the versions found as artifacts in the managed repository.
271 File typeDirs[] = groupDir.listFiles();
272 for ( File typeDir : typeDirs )
274 if ( !typeDir.isDirectory() )
276 // Skip it, we only care about directories.
280 if ( !typeDir.getName().endsWith( "s" ) )
282 // Skip it, we only care about directories that end in "s".
285 getVersionedVersions( typeDir, reference, foundVersions );
288 return foundVersions;
291 public boolean hasContent( ArtifactReference reference )
293 File artifactFile = toFile( reference );
294 return artifactFile.exists() && artifactFile.isFile();
297 public boolean hasContent( ProjectReference reference )
301 Set<String> versions = getVersions( reference );
302 return CollectionUtils.isNotEmpty( versions );
304 catch ( ContentNotFoundException e )
310 public boolean hasContent( VersionedReference reference )
314 Set<String> versions = getVersions( reference );
315 return CollectionUtils.isNotEmpty( versions );
317 catch ( ContentNotFoundException e )
323 public void setRepository( ManagedRepositoryConfiguration repository )
325 this.repository = repository;
329 * Convert a path to an artifact reference.
331 * @param path the path to convert. (relative or full location path)
332 * @throws LayoutException if the path cannot be converted to an artifact reference.
335 public ArtifactReference toArtifactReference( String path )
336 throws LayoutException
338 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
340 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
343 return super.toArtifactReference( path );
346 public File toFile( ArchivaArtifact reference )
348 return new File( repository.getLocation(), toPath( reference ) );
351 public File toFile( ArtifactReference reference )
353 return new File( repository.getLocation(), toPath( reference ) );
356 public String toMetadataPath( ProjectReference reference )
358 // No metadata present in legacy repository.
362 public String toMetadataPath( VersionedReference reference )
364 // No metadata present in legacy repository.
368 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
370 File repoFiles[] = typeDir.listFiles();
371 for ( File repoFile : repoFiles )
373 if ( repoFile.isDirectory() )
375 // Skip it. it's a directory.
379 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
381 if ( filetypes.matchesArtifactPattern( relativePath ) )
385 ArtifactReference artifact = toArtifactReference( relativePath );
386 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
388 foundVersions.add( artifact.getVersion() );
391 catch ( LayoutException e )
393 /* don't fail the process if there is a bad artifact within the directory. */
399 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
401 File repoFiles[] = typeDir.listFiles();
402 for ( int i = 0; i < repoFiles.length; i++ )
404 if ( repoFiles[i].isDirectory() )
406 // Skip it. it's a directory.
410 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
412 if ( filetypes.matchesArtifactPattern( relativePath ) )
416 ArtifactReference artifact = toArtifactReference( relativePath );
417 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
418 && artifact.getVersion().startsWith( reference.getVersion() ) )
420 foundArtifacts.add( artifact );
423 catch ( LayoutException e )
425 /* don't fail the process if there is a bad artifact within the directory. */
431 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
433 File repoFiles[] = typeDir.listFiles();
434 for ( int i = 0; i < repoFiles.length; i++ )
436 if ( repoFiles[i].isDirectory() )
438 // Skip it. it's a directory.
442 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
444 if ( filetypes.matchesArtifactPattern( relativePath ) )
448 ArtifactReference artifact = toArtifactReference( relativePath );
449 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
450 && artifact.getVersion().startsWith( reference.getVersion() ) )
452 foundVersions.add( artifact.getVersion() );
455 catch ( LayoutException e )
457 /* don't fail the process if there is a bad artifact within the directory. */
463 public void setFileTypes( FileTypes fileTypes )
465 this.filetypes = fileTypes;