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
49 @Service("managedRepositoryContent#legacy")
51 public class ManagedLegacyRepositoryContent
52 extends AbstractLegacyRepositoryContent
53 implements ManagedRepositoryContent
59 private FileTypes filetypes;
61 private ManagedRepositoryConfiguration repository;
63 public void deleteVersion( VersionedReference reference )
64 throws ContentNotFoundException
66 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
68 if ( !groupDir.exists() )
70 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
71 + groupDir.getAbsolutePath() );
74 if ( !groupDir.isDirectory() )
76 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
77 + groupDir.getAbsolutePath() );
80 // First gather up the versions found as artifacts in the managed repository.
81 File typeDirs[] = groupDir.listFiles();
82 for ( File typeDir : typeDirs )
84 if ( !typeDir.isDirectory() )
86 // Skip it, we only care about directories.
90 if ( !typeDir.getName().endsWith( "s" ) )
92 // Skip it, we only care about directories that end in "s".
95 deleteVersions( typeDir, reference );
99 private void deleteVersions( File typeDir, VersionedReference reference )
101 File repoFiles[] = typeDir.listFiles();
102 for ( File repoFile : repoFiles )
104 if ( repoFile.isDirectory() )
106 // Skip it. it's a directory.
110 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
112 if ( filetypes.matchesArtifactPattern( relativePath ) )
116 ArtifactReference artifact = toArtifactReference( relativePath );
117 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
118 && StringUtils.equals( artifact.getVersion(), reference.getVersion() ) )
121 deleteSupportFiles( repoFile );
124 catch ( LayoutException e )
126 /* don't fail the process if there is a bad artifact within the directory. */
132 private void deleteSupportFiles( File repoFile )
134 deleteSupportFile( repoFile, ".sha1" );
135 deleteSupportFile( repoFile, ".md5" );
136 deleteSupportFile( repoFile, ".asc" );
137 deleteSupportFile( repoFile, ".gpg" );
140 private void deleteSupportFile( File repoFile, String supportExtension )
142 File supportFile = new File( repoFile.getAbsolutePath() + supportExtension );
143 if ( supportFile.exists() && supportFile.isFile() )
145 supportFile.delete();
149 public String getId()
151 return repository.getId();
154 public Set<ArtifactReference> getRelatedArtifacts( ArtifactReference reference )
155 throws ContentNotFoundException
157 File artifactFile = toFile( reference );
158 File repoDir = artifactFile.getParentFile();
160 if ( !repoDir.exists() )
162 throw new ContentNotFoundException( "Unable to get related artifacts using a non-existant directory: "
163 + repoDir.getAbsolutePath() );
166 if ( !repoDir.isDirectory() )
168 throw new ContentNotFoundException( "Unable to get related artifacts using a non-directory: "
169 + repoDir.getAbsolutePath() );
172 Set<ArtifactReference> foundArtifacts = new HashSet<ArtifactReference>();
174 // First gather up the versions found as artifacts in the managed repository.
175 File projectParentDir = repoDir.getParentFile();
176 File typeDirs[] = projectParentDir.listFiles();
177 for ( File typeDir : typeDirs )
179 if ( !typeDir.isDirectory() )
181 // Skip it, we only care about directories.
185 if ( !typeDir.getName().endsWith( "s" ) )
187 // Skip it, we only care about directories that end in "s".
190 getRelatedArtifacts( typeDir, reference, foundArtifacts );
193 return foundArtifacts;
196 public String getRepoRoot()
198 return repository.getLocation();
201 public ManagedRepositoryConfiguration getRepository()
206 public Set<String> getVersions( ProjectReference reference )
207 throws ContentNotFoundException
209 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
211 if ( !groupDir.exists() )
213 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
214 + groupDir.getAbsolutePath() );
217 if ( !groupDir.isDirectory() )
219 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
220 + groupDir.getAbsolutePath() );
223 Set<String> foundVersions = new HashSet<String>();
225 // First gather up the versions found as artifacts in the managed repository.
226 File typeDirs[] = groupDir.listFiles();
227 for ( File typeDir : typeDirs )
229 if ( !typeDir.isDirectory() )
231 // Skip it, we only care about directories.
235 if ( !typeDir.getName().endsWith( "s" ) )
237 // Skip it, we only care about directories that end in "s".
240 getProjectVersions( typeDir, reference, foundVersions );
243 return foundVersions;
246 public Set<String> getVersions( VersionedReference reference )
247 throws ContentNotFoundException
249 File groupDir = new File( repository.getLocation(), reference.getGroupId() );
251 if ( !groupDir.exists() )
253 throw new ContentNotFoundException( "Unable to get versions using a non-existant groupId directory: "
254 + groupDir.getAbsolutePath() );
257 if ( !groupDir.isDirectory() )
259 throw new ContentNotFoundException( "Unable to get versions using a non-directory: "
260 + groupDir.getAbsolutePath() );
263 Set<String> foundVersions = new HashSet<String>();
265 // First gather up the versions found as artifacts in the managed repository.
266 File typeDirs[] = groupDir.listFiles();
267 for ( File typeDir : typeDirs )
269 if ( !typeDir.isDirectory() )
271 // Skip it, we only care about directories.
275 if ( !typeDir.getName().endsWith( "s" ) )
277 // Skip it, we only care about directories that end in "s".
280 getVersionedVersions( typeDir, reference, foundVersions );
283 return foundVersions;
286 public boolean hasContent( ArtifactReference reference )
288 File artifactFile = toFile( reference );
289 return artifactFile.exists() && artifactFile.isFile();
292 public boolean hasContent( ProjectReference reference )
296 Set<String> versions = getVersions( reference );
297 return CollectionUtils.isNotEmpty( versions );
299 catch ( ContentNotFoundException e )
305 public boolean hasContent( VersionedReference reference )
309 Set<String> versions = getVersions( reference );
310 return CollectionUtils.isNotEmpty( versions );
312 catch ( ContentNotFoundException e )
318 public void setRepository( ManagedRepositoryConfiguration repository )
320 this.repository = repository;
324 * Convert a path to an artifact reference.
326 * @param path the path to convert. (relative or full location path)
327 * @throws LayoutException if the path cannot be converted to an artifact reference.
330 public ArtifactReference toArtifactReference( String path )
331 throws LayoutException
333 if ( ( path != null ) && path.startsWith( repository.getLocation() ) )
335 return super.toArtifactReference( path.substring( repository.getLocation().length() ) );
338 return super.toArtifactReference( path );
341 public File toFile( ArchivaArtifact reference )
343 return new File( repository.getLocation(), toPath( reference ) );
346 public File toFile( ArtifactReference reference )
348 return new File( repository.getLocation(), toPath( reference ) );
351 public String toMetadataPath( ProjectReference reference )
353 // No metadata present in legacy repository.
357 public String toMetadataPath( VersionedReference reference )
359 // No metadata present in legacy repository.
363 private void getProjectVersions( File typeDir, ProjectReference reference, Set<String> foundVersions )
365 File repoFiles[] = typeDir.listFiles();
366 for ( File repoFile : repoFiles )
368 if ( repoFile.isDirectory() )
370 // Skip it. it's a directory.
374 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFile );
376 if ( filetypes.matchesArtifactPattern( relativePath ) )
380 ArtifactReference artifact = toArtifactReference( relativePath );
381 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() ) )
383 foundVersions.add( artifact.getVersion() );
386 catch ( LayoutException e )
388 /* don't fail the process if there is a bad artifact within the directory. */
394 private void getRelatedArtifacts( File typeDir, ArtifactReference reference, Set<ArtifactReference> foundArtifacts )
396 File repoFiles[] = typeDir.listFiles();
397 for ( int i = 0; i < repoFiles.length; i++ )
399 if ( repoFiles[i].isDirectory() )
401 // Skip it. it's a directory.
405 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
407 if ( filetypes.matchesArtifactPattern( relativePath ) )
411 ArtifactReference artifact = toArtifactReference( relativePath );
412 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
413 && artifact.getVersion().startsWith( reference.getVersion() ) )
415 foundArtifacts.add( artifact );
418 catch ( LayoutException e )
420 /* don't fail the process if there is a bad artifact within the directory. */
426 private void getVersionedVersions( File typeDir, VersionedReference reference, Set<String> foundVersions )
428 File repoFiles[] = typeDir.listFiles();
429 for ( int i = 0; i < repoFiles.length; i++ )
431 if ( repoFiles[i].isDirectory() )
433 // Skip it. it's a directory.
437 String relativePath = PathUtil.getRelative( repository.getLocation(), repoFiles[i] );
439 if ( filetypes.matchesArtifactPattern( relativePath ) )
443 ArtifactReference artifact = toArtifactReference( relativePath );
444 if ( StringUtils.equals( artifact.getArtifactId(), reference.getArtifactId() )
445 && artifact.getVersion().startsWith( reference.getVersion() ) )
447 foundVersions.add( artifact.getVersion() );
450 catch ( LayoutException e )
452 /* don't fail the process if there is a bad artifact within the directory. */
458 public void setFileTypes( FileTypes fileTypes )
460 this.filetypes = fileTypes;