1 package org.apache.archiva.metadata.repository.storage.maven2;
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.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.beans.NetworkProxy;
25 import org.apache.archiva.admin.model.beans.ProxyConnector;
26 import org.apache.archiva.admin.model.beans.RemoteRepository;
27 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
28 import org.apache.archiva.admin.model.networkproxy.NetworkProxyAdmin;
29 import org.apache.archiva.admin.model.proxyconnector.ProxyConnectorAdmin;
30 import org.apache.archiva.admin.model.remote.RemoteRepositoryAdmin;
31 import org.apache.archiva.checksum.ChecksumAlgorithm;
32 import org.apache.archiva.checksum.ChecksummedFile;
33 import org.apache.archiva.common.utils.VersionUtil;
34 import org.apache.archiva.maven2.metadata.MavenMetadataReader;
35 import org.apache.archiva.metadata.model.ArtifactMetadata;
36 import org.apache.archiva.metadata.model.ProjectMetadata;
37 import org.apache.archiva.metadata.model.ProjectVersionMetadata;
38 import org.apache.archiva.metadata.repository.filter.Filter;
39 import org.apache.archiva.metadata.repository.storage.ReadMetadataRequest;
40 import org.apache.archiva.metadata.repository.storage.RelocationException;
41 import org.apache.archiva.metadata.repository.storage.RepositoryPathTranslator;
42 import org.apache.archiva.metadata.repository.storage.RepositoryStorage;
43 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataInvalidException;
44 import org.apache.archiva.metadata.repository.storage.RepositoryStorageMetadataNotFoundException;
45 import org.apache.archiva.metadata.repository.storage.RepositoryStorageRuntimeException;
46 import org.apache.archiva.model.ArchivaRepositoryMetadata;
47 import org.apache.archiva.model.ArtifactReference;
48 import org.apache.archiva.model.SnapshotVersion;
49 import org.apache.archiva.policies.ProxyDownloadException;
50 import org.apache.archiva.proxy.common.WagonFactory;
51 import org.apache.archiva.proxy.model.RepositoryProxyConnectors;
52 import org.apache.archiva.reports.RepositoryProblemFacet;
53 import org.apache.archiva.repository.ManagedRepositoryContent;
54 import org.apache.archiva.repository.content.PathParser;
55 import org.apache.archiva.repository.layout.LayoutException;
56 import org.apache.archiva.xml.XMLException;
57 import org.apache.commons.lang.ArrayUtils;
58 import org.apache.commons.lang.StringUtils;
59 import org.apache.maven.model.CiManagement;
60 import org.apache.maven.model.Dependency;
61 import org.apache.maven.model.DistributionManagement;
62 import org.apache.maven.model.IssueManagement;
63 import org.apache.maven.model.License;
64 import org.apache.maven.model.MailingList;
65 import org.apache.maven.model.Model;
66 import org.apache.maven.model.Organization;
67 import org.apache.maven.model.Relocation;
68 import org.apache.maven.model.Scm;
69 import org.apache.maven.model.building.DefaultModelBuilderFactory;
70 import org.apache.maven.model.building.DefaultModelBuildingRequest;
71 import org.apache.maven.model.building.ModelBuilder;
72 import org.apache.maven.model.building.ModelBuildingException;
73 import org.apache.maven.model.building.ModelBuildingRequest;
74 import org.apache.maven.model.building.ModelProblem;
75 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
76 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
77 import org.slf4j.Logger;
78 import org.slf4j.LoggerFactory;
79 import org.springframework.context.ApplicationContext;
80 import org.springframework.stereotype.Service;
82 import javax.annotation.PostConstruct;
83 import javax.inject.Inject;
84 import javax.inject.Named;
86 import java.io.FileNotFoundException;
87 import java.io.FileReader;
88 import java.io.FilenameFilter;
89 import java.io.IOException;
90 import java.nio.file.Files;
91 import java.util.ArrayList;
92 import java.util.Arrays;
93 import java.util.Collection;
94 import java.util.Collections;
95 import java.util.Date;
96 import java.util.HashMap;
97 import java.util.List;
101 * Maven 2 repository format storage implementation. This class currently takes parameters to indicate the repository to
102 * deal with rather than being instantiated per-repository.
103 * FIXME: instantiate one per repository and allocate permanently from a factory (which can be obtained within the session).
105 * The session is passed in as an argument to obtain any necessary resources, rather than the class being instantiated
106 * within the session in the context of a single managed repository's resolution needs.
109 @Service( "repositoryStorage#maven2" )
110 public class Maven2RepositoryStorage
111 implements RepositoryStorage
114 private static final Logger LOGGER = LoggerFactory.getLogger( Maven2RepositoryStorage.class );
116 private ModelBuilder builder;
119 private RemoteRepositoryAdmin remoteRepositoryAdmin;
122 private ManagedRepositoryAdmin managedRepositoryAdmin;
125 private ProxyConnectorAdmin proxyConnectorAdmin;
128 private NetworkProxyAdmin networkProxyAdmin;
131 @Named( value = "repositoryPathTranslator#maven2" )
132 private RepositoryPathTranslator pathTranslator;
135 private WagonFactory wagonFactory;
138 private ApplicationContext applicationContext;
141 @Named( value = "pathParser#default" )
142 private PathParser pathParser;
144 private static final String METADATA_FILENAME_START = "maven-metadata";
146 private static final String METADATA_FILENAME = METADATA_FILENAME_START + ".xml";
148 private static final MavenXpp3Reader MAVEN_XPP_3_READER = new MavenXpp3Reader();
152 public void initialize()
154 builder = new DefaultModelBuilderFactory().newInstance();
159 public ProjectMetadata readProjectMetadata( String repoId, String namespace, String projectId )
161 // TODO: could natively implement the "shared model" concept from the browse action to avoid needing it there?
166 public ProjectVersionMetadata readProjectVersionMetadata( ReadMetadataRequest readMetadataRequest )
167 throws RepositoryStorageMetadataNotFoundException, RepositoryStorageMetadataInvalidException,
168 RepositoryStorageRuntimeException
172 ManagedRepository managedRepository =
173 managedRepositoryAdmin.getManagedRepository( readMetadataRequest.getRepositoryId() );
174 String artifactVersion = readMetadataRequest.getProjectVersion();
175 // olamy: in case of browsing via the ui we can mix repos (parent of a SNAPSHOT can come from release repo)
176 if ( !readMetadataRequest.isBrowsingRequest() )
178 if ( VersionUtil.isSnapshot( artifactVersion ) )
180 // skygo trying to improve speed by honoring managed configuration MRM-1658
181 if ( managedRepository.isReleases() && !managedRepository.isSnapshots() )
183 throw new RepositoryStorageRuntimeException( "lookforsnaponreleaseonly",
184 "managed repo is configured for release only" );
189 if ( !managedRepository.isReleases() && managedRepository.isSnapshots() )
191 throw new RepositoryStorageRuntimeException( "lookforsreleaseonsneponly",
192 "managed repo is configured for snapshot only" );
196 File basedir = new File( managedRepository.getLocation() );
197 if ( VersionUtil.isSnapshot( artifactVersion ) )
199 File metadataFile = pathTranslator.toFile( basedir, readMetadataRequest.getNamespace(),
200 readMetadataRequest.getProjectId(), artifactVersion,
204 ArchivaRepositoryMetadata metadata = MavenMetadataReader.read( metadataFile );
206 // re-adjust to timestamp if present, otherwise retain the original -SNAPSHOT filename
207 SnapshotVersion snapshotVersion = metadata.getSnapshotVersion();
208 if ( snapshotVersion != null )
211 artifactVersion.substring( 0, artifactVersion.length() - 8 ); // remove SNAPSHOT from end
213 artifactVersion + snapshotVersion.getTimestamp() + "-" + snapshotVersion.getBuildNumber();
216 catch ( XMLException e )
218 // unable to parse metadata - LOGGER it, and continue with the version as the original SNAPSHOT version
219 LOGGER.warn( "Invalid metadata: {} - {}", metadataFile, e.getMessage() );
223 // TODO: won't work well with some other layouts, might need to convert artifact parts to ID by path translator
224 String id = readMetadataRequest.getProjectId() + "-" + artifactVersion + ".pom";
226 pathTranslator.toFile( basedir, readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(),
227 readMetadataRequest.getProjectVersion(), id );
229 if ( !file.exists() )
231 // metadata could not be resolved
232 throw new RepositoryStorageMetadataNotFoundException(
233 "The artifact's POM file '" + file.getAbsolutePath() + "' was missing" );
236 // TODO: this is a workaround until we can properly resolve using proxies as well - this doesn't cache
238 List<RemoteRepository> remoteRepositories = new ArrayList<>();
239 Map<String, NetworkProxy> networkProxies = new HashMap<>();
241 Map<String, List<ProxyConnector>> proxyConnectorsMap = proxyConnectorAdmin.getProxyConnectorAsMap();
242 List<ProxyConnector> proxyConnectors = proxyConnectorsMap.get( readMetadataRequest.getRepositoryId() );
243 if ( proxyConnectors != null )
245 for ( ProxyConnector proxyConnector : proxyConnectors )
247 RemoteRepository remoteRepoConfig =
248 remoteRepositoryAdmin.getRemoteRepository( proxyConnector.getTargetRepoId() );
250 if ( remoteRepoConfig != null )
252 remoteRepositories.add( remoteRepoConfig );
254 NetworkProxy networkProxyConfig =
255 networkProxyAdmin.getNetworkProxy( proxyConnector.getProxyId() );
257 if ( networkProxyConfig != null )
259 // key/value: remote repo ID/proxy info
260 networkProxies.put( proxyConnector.getTargetRepoId(), networkProxyConfig );
266 // That's a browsing request so we can a mix of SNAPSHOT and release artifacts (especially with snapshots which
267 // can have released parent pom
268 if ( readMetadataRequest.isBrowsingRequest() )
270 remoteRepositories.addAll( remoteRepositoryAdmin.getRemoteRepositories() );
273 ModelBuildingRequest req =
274 new DefaultModelBuildingRequest().setProcessPlugins( false ).setPomFile( file ).setTwoPhaseBuilding(
275 false ).setValidationLevel( ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL );
277 //MRM-1607. olamy this will resolve jdk profiles on the current running archiva jvm
278 req.setSystemProperties( System.getProperties() );
281 req.setModelResolver(
282 new RepositoryModelResolver( managedRepository, pathTranslator, wagonFactory, remoteRepositories,
283 networkProxies, managedRepository ) );
288 model = builder.build( req ).getEffectiveModel();
290 catch ( ModelBuildingException e )
292 String msg = "The artifact's POM file '" + file + "' was invalid: " + e.getMessage();
294 List<ModelProblem> modelProblems = e.getProblems();
295 for ( ModelProblem problem : modelProblems )
297 // MRM-1411, related to MRM-1335
298 // this means that the problem was that the parent wasn't resolved!
299 // olamy really hackhish but fail with java profile so use error message
300 // || ( StringUtils.startsWith( problem.getMessage(), "Failed to determine Java version for profile" ) )
301 // but setTwoPhaseBuilding(true) fix that
302 if ( ( problem.getException() instanceof FileNotFoundException && e.getModelId() != null &&
303 !e.getModelId().equals( problem.getModelId() ) ) )
305 LOGGER.warn( "The artifact's parent POM file '{}' cannot be resolved. "
306 + "Using defaults for project version metadata..", file );
308 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
309 metadata.setId( readMetadataRequest.getProjectVersion() );
311 MavenProjectFacet facet = new MavenProjectFacet();
312 facet.setGroupId( readMetadataRequest.getNamespace() );
313 facet.setArtifactId( readMetadataRequest.getProjectId() );
314 facet.setPackaging( "jar" );
315 metadata.addFacet( facet );
318 "Error in resolving artifact's parent POM file. " + ( problem.getException() == null
319 ? problem.getMessage()
320 : problem.getException().getMessage() );
321 RepositoryProblemFacet repoProblemFacet = new RepositoryProblemFacet();
322 repoProblemFacet.setRepositoryId( readMetadataRequest.getRepositoryId() );
323 repoProblemFacet.setId( readMetadataRequest.getRepositoryId() );
324 repoProblemFacet.setMessage( errMsg );
325 repoProblemFacet.setProblem( errMsg );
326 repoProblemFacet.setProject( readMetadataRequest.getProjectId() );
327 repoProblemFacet.setVersion( readMetadataRequest.getProjectVersion() );
328 repoProblemFacet.setNamespace( readMetadataRequest.getNamespace() );
330 metadata.addFacet( repoProblemFacet );
336 throw new RepositoryStorageMetadataInvalidException( "invalid-pom", msg, e );
339 // Check if the POM is in the correct location
340 boolean correctGroupId = readMetadataRequest.getNamespace().equals( model.getGroupId() );
341 boolean correctArtifactId = readMetadataRequest.getProjectId().equals( model.getArtifactId() );
342 boolean correctVersion = readMetadataRequest.getProjectVersion().equals( model.getVersion() );
343 if ( !correctGroupId || !correctArtifactId || !correctVersion )
345 StringBuilder message = new StringBuilder( "Incorrect POM coordinates in '" + file + "':" );
346 if ( !correctGroupId )
348 message.append( "\nIncorrect group ID: " ).append( model.getGroupId() );
350 if ( !correctArtifactId )
352 message.append( "\nIncorrect artifact ID: " ).append( model.getArtifactId() );
354 if ( !correctVersion )
356 message.append( "\nIncorrect version: " ).append( model.getVersion() );
359 throw new RepositoryStorageMetadataInvalidException( "mislocated-pom", message.toString() );
362 ProjectVersionMetadata metadata = new ProjectVersionMetadata();
363 metadata.setCiManagement( convertCiManagement( model.getCiManagement() ) );
364 metadata.setDescription( model.getDescription() );
365 metadata.setId( readMetadataRequest.getProjectVersion() );
366 metadata.setIssueManagement( convertIssueManagement( model.getIssueManagement() ) );
367 metadata.setLicenses( convertLicenses( model.getLicenses() ) );
368 metadata.setMailingLists( convertMailingLists( model.getMailingLists() ) );
369 metadata.setDependencies( convertDependencies( model.getDependencies() ) );
370 metadata.setName( model.getName() );
371 metadata.setOrganization( convertOrganization( model.getOrganization() ) );
372 metadata.setScm( convertScm( model.getScm() ) );
373 metadata.setUrl( model.getUrl() );
375 MavenProjectFacet facet = new MavenProjectFacet();
376 facet.setGroupId( model.getGroupId() != null ? model.getGroupId() : model.getParent().getGroupId() );
377 facet.setArtifactId( model.getArtifactId() );
378 facet.setPackaging( model.getPackaging() );
379 if ( model.getParent() != null )
381 MavenProjectParent parent = new MavenProjectParent();
382 parent.setGroupId( model.getParent().getGroupId() );
383 parent.setArtifactId( model.getParent().getArtifactId() );
384 parent.setVersion( model.getParent().getVersion() );
385 facet.setParent( parent );
387 metadata.addFacet( facet );
391 catch ( RepositoryAdminException e )
393 throw new RepositoryStorageRuntimeException( "repo-admin", e.getMessage(), e );
397 public void setWagonFactory( WagonFactory wagonFactory )
399 this.wagonFactory = wagonFactory;
402 private List<org.apache.archiva.metadata.model.Dependency> convertDependencies( List<Dependency> dependencies )
404 List<org.apache.archiva.metadata.model.Dependency> l =
406 for ( Dependency dependency : dependencies )
408 org.apache.archiva.metadata.model.Dependency newDependency =
409 new org.apache.archiva.metadata.model.Dependency();
410 newDependency.setArtifactId( dependency.getArtifactId() );
411 newDependency.setClassifier( dependency.getClassifier() );
412 newDependency.setGroupId( dependency.getGroupId() );
413 newDependency.setOptional( dependency.isOptional() );
414 newDependency.setScope( dependency.getScope() );
415 newDependency.setSystemPath( dependency.getSystemPath() );
416 newDependency.setType( dependency.getType() );
417 newDependency.setVersion( dependency.getVersion() );
418 l.add( newDependency );
423 private org.apache.archiva.metadata.model.Scm convertScm( Scm scm )
425 org.apache.archiva.metadata.model.Scm newScm = null;
428 newScm = new org.apache.archiva.metadata.model.Scm();
429 newScm.setConnection( scm.getConnection() );
430 newScm.setDeveloperConnection( scm.getDeveloperConnection() );
431 newScm.setUrl( scm.getUrl() );
436 private org.apache.archiva.metadata.model.Organization convertOrganization( Organization organization )
438 org.apache.archiva.metadata.model.Organization org = null;
439 if ( organization != null )
441 org = new org.apache.archiva.metadata.model.Organization();
442 org.setName( organization.getName() );
443 org.setUrl( organization.getUrl() );
448 private List<org.apache.archiva.metadata.model.License> convertLicenses( List<License> licenses )
450 List<org.apache.archiva.metadata.model.License> l = new ArrayList<>();
451 for ( License license : licenses )
453 org.apache.archiva.metadata.model.License newLicense = new org.apache.archiva.metadata.model.License();
454 newLicense.setName( license.getName() );
455 newLicense.setUrl( license.getUrl() );
461 private List<org.apache.archiva.metadata.model.MailingList> convertMailingLists( List<MailingList> mailingLists )
463 List<org.apache.archiva.metadata.model.MailingList> l =
465 for ( MailingList mailingList : mailingLists )
467 org.apache.archiva.metadata.model.MailingList newMailingList =
468 new org.apache.archiva.metadata.model.MailingList();
469 newMailingList.setName( mailingList.getName() );
470 newMailingList.setMainArchiveUrl( mailingList.getArchive() );
471 newMailingList.setPostAddress( mailingList.getPost() );
472 newMailingList.setSubscribeAddress( mailingList.getSubscribe() );
473 newMailingList.setUnsubscribeAddress( mailingList.getUnsubscribe() );
474 newMailingList.setOtherArchives( mailingList.getOtherArchives() );
475 l.add( newMailingList );
480 private org.apache.archiva.metadata.model.IssueManagement convertIssueManagement( IssueManagement issueManagement )
482 org.apache.archiva.metadata.model.IssueManagement im = null;
483 if ( issueManagement != null )
485 im = new org.apache.archiva.metadata.model.IssueManagement();
486 im.setSystem( issueManagement.getSystem() );
487 im.setUrl( issueManagement.getUrl() );
492 private org.apache.archiva.metadata.model.CiManagement convertCiManagement( CiManagement ciManagement )
494 org.apache.archiva.metadata.model.CiManagement ci = null;
495 if ( ciManagement != null )
497 ci = new org.apache.archiva.metadata.model.CiManagement();
498 ci.setSystem( ciManagement.getSystem() );
499 ci.setUrl( ciManagement.getUrl() );
505 public Collection<String> listRootNamespaces( String repoId, Filter<String> filter )
506 throws RepositoryStorageRuntimeException
508 File dir = getRepositoryBasedir( repoId );
510 return getSortedFiles( dir, filter );
513 private static Collection<String> getSortedFiles( File dir, Filter<String> filter )
515 List<String> fileNames;
516 String[] files = dir.list( new DirectoryFilter( filter ) );
519 fileNames = new ArrayList<>( Arrays.asList( files ) );
520 Collections.sort( fileNames );
524 fileNames = Collections.emptyList();
529 private File getRepositoryBasedir( String repoId )
530 throws RepositoryStorageRuntimeException
534 ManagedRepository repositoryConfiguration = managedRepositoryAdmin.getManagedRepository( repoId );
536 return new File( repositoryConfiguration.getLocation() );
538 catch ( RepositoryAdminException e )
540 throw new RepositoryStorageRuntimeException( "repo-admin", e.getMessage(), e );
545 public Collection<String> listNamespaces( String repoId, String namespace, Filter<String> filter )
546 throws RepositoryStorageRuntimeException
548 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
550 // scan all the directories which are potential namespaces. Any directories known to be projects are excluded
551 List<String> namespaces = new ArrayList<>();
552 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
555 for ( File file : files )
557 if ( !isProject( file, filter ) )
559 namespaces.add( file.getName() );
563 Collections.sort( namespaces );
568 public Collection<String> listProjects( String repoId, String namespace, Filter<String> filter )
569 throws RepositoryStorageRuntimeException
571 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace );
573 // scan all directories in the namespace, and only include those that are known to be projects
574 List<String> projects = new ArrayList<>();
576 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
579 for ( File file : files )
581 if ( isProject( file, filter ) )
583 projects.add( file.getName() );
587 Collections.sort( projects );
592 public Collection<String> listProjectVersions( String repoId, String namespace, String projectId,
593 Filter<String> filter )
594 throws RepositoryStorageRuntimeException
596 File dir = pathTranslator.toFile( getRepositoryBasedir( repoId ), namespace, projectId );
598 // all directories in a project directory can be considered a version
599 return getSortedFiles( dir, filter );
603 public Collection<ArtifactMetadata> readArtifactsMetadata( ReadMetadataRequest readMetadataRequest )
604 throws RepositoryStorageRuntimeException
606 File dir = pathTranslator.toFile( getRepositoryBasedir( readMetadataRequest.getRepositoryId() ),
607 readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(),
608 readMetadataRequest.getProjectVersion() );
610 // all files that are not metadata and not a checksum / signature are considered artifacts
611 File[] files = dir.listFiles( new ArtifactDirectoryFilter( readMetadataRequest.getFilter() ) );
613 List<ArtifactMetadata> artifacts = new ArrayList<>();
616 for ( File file : files )
618 ArtifactMetadata metadata =
619 getArtifactFromFile( readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(),
620 readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(),
622 artifacts.add( metadata );
629 public ArtifactMetadata readArtifactMetadataFromPath( String repoId, String path )
630 throws RepositoryStorageRuntimeException
632 ArtifactMetadata metadata = pathTranslator.getArtifactForPath( repoId, path );
634 populateArtifactMetadataFromFile( metadata, new File( getRepositoryBasedir( repoId ), path ) );
639 private ArtifactMetadata getArtifactFromFile( String repoId, String namespace, String projectId,
640 String projectVersion, File file )
642 ArtifactMetadata metadata =
643 pathTranslator.getArtifactFromId( repoId, namespace, projectId, projectVersion, file.getName() );
645 populateArtifactMetadataFromFile( metadata, file );
651 * A relocation capable client will request the POM prior to the artifact, and will then read meta-data and do
652 * client side relocation. A simplier client (like maven 1) will only request the artifact and not use the
655 * For such clients, archiva does server-side relocation by reading itself the <relocation> element in
656 * metadatas and serving the expected artifact.
659 public void applyServerSideRelocation( ManagedRepositoryContent managedRepository, ArtifactReference artifact )
660 throws ProxyDownloadException
662 if ( "pom".equals( artifact.getType() ) )
667 // Build the artifact POM reference
668 ArtifactReference pomReference = new ArtifactReference();
669 pomReference.setGroupId( artifact.getGroupId() );
670 pomReference.setArtifactId( artifact.getArtifactId() );
671 pomReference.setVersion( artifact.getVersion() );
672 pomReference.setType( "pom" );
674 RepositoryProxyConnectors connectors =
675 applicationContext.getBean( "repositoryProxyConnectors#default", RepositoryProxyConnectors.class );
677 // Get the artifact POM from proxied repositories if needed
678 connectors.fetchFromProxies( managedRepository, pomReference );
680 // Open and read the POM from the managed repo
681 File pom = managedRepository.toFile( pomReference );
690 // MavenXpp3Reader leaves the file open, so we need to close it ourselves.
691 FileReader reader = new FileReader( pom );
695 model = MAVEN_XPP_3_READER.read( reader );
699 if ( reader != null )
705 DistributionManagement dist = model.getDistributionManagement();
708 Relocation relocation = dist.getRelocation();
709 if ( relocation != null )
711 // artifact is relocated : update the repositoryPath
712 if ( relocation.getGroupId() != null )
714 artifact.setGroupId( relocation.getGroupId() );
716 if ( relocation.getArtifactId() != null )
718 artifact.setArtifactId( relocation.getArtifactId() );
720 if ( relocation.getVersion() != null )
722 artifact.setVersion( relocation.getVersion() );
727 catch ( FileNotFoundException e )
729 // Artifact has no POM in repo : ignore
731 catch ( IOException e )
733 // Unable to read POM : ignore.
735 catch ( XmlPullParserException e )
737 // Invalid POM : ignore
743 public String getFilePath( String requestPath, ManagedRepository managedRepository )
745 // managedRepository can be null
746 // extract artifact reference from url
747 // groupId:artifactId:version:packaging:classifier
748 //org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar
749 String logicalResource = null;
750 String requestPathInfo = StringUtils.defaultString( requestPath );
752 //remove prefix ie /repository/blah becomes /blah
753 requestPathInfo = removePrefix( requestPathInfo );
755 // Remove prefixing slash as the repository id doesn't contain it;
756 if ( requestPathInfo.startsWith( "/" ) )
758 requestPathInfo = requestPathInfo.substring( 1 );
761 int slash = requestPathInfo.indexOf( '/' );
764 logicalResource = requestPathInfo.substring( slash );
766 if ( logicalResource.endsWith( "/.." ) )
768 logicalResource += "/";
771 if ( logicalResource != null && logicalResource.startsWith( "//" ) )
773 logicalResource = logicalResource.substring( 1 );
776 if ( logicalResource == null )
778 logicalResource = "/";
783 logicalResource = "/";
785 return logicalResource;
790 public String getFilePathWithVersion( final String requestPath, ManagedRepositoryContent managedRepositoryContent )
791 throws XMLException, RelocationException
794 if ( StringUtils.endsWith( requestPath, METADATA_FILENAME ) )
796 return getFilePath( requestPath, managedRepositoryContent.getRepository() );
799 String filePath = getFilePath( requestPath, managedRepositoryContent.getRepository() );
801 ArtifactReference artifactReference = null;
804 artifactReference = pathParser.toArtifactReference( filePath );
806 catch ( LayoutException e )
811 if ( StringUtils.endsWith( artifactReference.getVersion(), VersionUtil.SNAPSHOT ) )
813 // read maven metadata to get last timestamp
814 File metadataDir = new File( managedRepositoryContent.getRepoRoot(), filePath ).getParentFile();
815 if ( !metadataDir.exists() )
819 File metadataFile = new File( metadataDir, METADATA_FILENAME );
820 if ( !metadataFile.exists() )
824 ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read( metadataFile );
825 int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber();
826 String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp();
828 // org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar
829 // -> archiva-checksum-1.4-M4-20130425.081822-1.jar
832 StringUtils.replace( filePath, artifactReference.getArtifactId() + "-" + artifactReference.getVersion(),
833 artifactReference.getArtifactId() + "-" + StringUtils.remove(
834 artifactReference.getVersion(), "-" + VersionUtil.SNAPSHOT ) + "-" + timestamp
835 + "-" + buildNumber );
837 throw new RelocationException( "/repository/" + managedRepositoryContent.getRepository().getId() +
838 ( StringUtils.startsWith( filePath, "/" ) ? "" : "/" ) + filePath,
839 RelocationException.RelocationType.TEMPORARY );
846 //-----------------------------
848 //-----------------------------
856 private static String removePrefix( final String href )
858 String[] parts = StringUtils.split( href, '/' );
859 parts = (String[]) ArrayUtils.subarray( parts, 1, parts.length );
860 if ( parts == null || parts.length == 0 )
865 String joinedString = StringUtils.join( parts, '/' );
866 if ( href.endsWith( "/" ) )
868 joinedString = joinedString + "/";
874 private static void populateArtifactMetadataFromFile( ArtifactMetadata metadata, File file )
876 metadata.setWhenGathered( new Date() );
877 metadata.setFileLastModified( file.lastModified() );
878 ChecksummedFile checksummedFile = new ChecksummedFile( file );
881 metadata.setMd5( checksummedFile.calculateChecksum( ChecksumAlgorithm.MD5 ) );
883 catch ( IOException e )
885 LOGGER.error( "Unable to checksum file {}: {},MD5", file, e.getMessage() );
889 metadata.setSha1( checksummedFile.calculateChecksum( ChecksumAlgorithm.SHA1 ) );
891 catch ( IOException e )
893 LOGGER.error( "Unable to checksum file {}: {},SHA1", file, e.getMessage() );
895 metadata.setSize( file.length() );
898 private boolean isProject( File dir, Filter<String> filter )
900 // scan directories for a valid project version subdirectory, meaning this must be a project directory
901 File[] files = dir.listFiles( new DirectoryFilter( filter ) );
904 for ( File file : files )
906 if ( isProjectVersion( file ) )
913 // if a metadata file is present, check if this is the "artifactId" directory, marking it as a project
914 ArchivaRepositoryMetadata metadata = readMetadata( dir );
915 if ( metadata != null && dir.getName().equals( metadata.getArtifactId() ) )
923 private boolean isProjectVersion( File dir )
925 final String artifactId = dir.getParentFile().getName();
926 final String projectVersion = dir.getName();
928 // check if there is a POM artifact file to ensure it is a version directory
930 if ( VersionUtil.isSnapshot( projectVersion ) )
932 files = dir.listFiles( new PomFilenameFilter( artifactId, projectVersion ) );
936 final String pomFile = artifactId + "-" + projectVersion + ".pom";
937 files = dir.listFiles( new PomFileFilter( pomFile ) );
939 if ( files != null && files.length > 0 )
944 // if a metadata file is present, check if this is the "version" directory, marking it as a project version
945 ArchivaRepositoryMetadata metadata = readMetadata( dir );
946 if ( metadata != null && projectVersion.equals( metadata.getVersion() ) )
954 private ArchivaRepositoryMetadata readMetadata( File directory )
956 ArchivaRepositoryMetadata metadata = null;
957 File metadataFile = new File( directory, METADATA_FILENAME );
958 if ( metadataFile.exists() )
962 metadata = MavenMetadataReader.read( metadataFile );
964 catch ( XMLException e )
966 // ignore missing or invalid metadata
972 private static class DirectoryFilter
973 implements FilenameFilter
975 private final Filter<String> filter;
977 public DirectoryFilter( Filter<String> filter )
979 this.filter = filter;
983 public boolean accept( File dir, String name )
985 if ( !filter.accept( name ) )
989 else if ( name.startsWith( "." ) )
993 else if ( !new File( dir, name ).isDirectory() )
1001 private static class ArtifactDirectoryFilter
1002 implements FilenameFilter
1004 private final Filter<String> filter;
1006 private ArtifactDirectoryFilter( Filter<String> filter )
1008 this.filter = filter;
1012 public boolean accept( File dir, String name )
1014 // TODO compare to logic in maven-repository-layer
1015 if ( !filter.accept( name ) )
1019 else if ( name.startsWith( "." ) )
1023 else if ( name.endsWith( ".md5" ) || name.endsWith( ".sha1" ) || name.endsWith( ".asc" ) )
1027 else if ( name.equals( METADATA_FILENAME ) )
1031 else if ( new File( dir, name ).isDirectory() )
1035 // some files from remote repositories can have name like maven-metadata-archiva-vm-all-public.xml
1036 else if ( StringUtils.startsWith( name, METADATA_FILENAME_START ) && StringUtils.endsWith( name, ".xml" ) )
1047 private static final class PomFilenameFilter
1048 implements FilenameFilter
1051 private final String artifactId, projectVersion;
1053 private PomFilenameFilter( String artifactId, String projectVersion )
1055 this.artifactId = artifactId;
1056 this.projectVersion = projectVersion;
1060 public boolean accept( File dir, String name )
1062 if ( name.startsWith( artifactId + "-" ) && name.endsWith( ".pom" ) )
1064 String v = name.substring( artifactId.length() + 1, name.length() - 4 );
1065 v = VersionUtil.getBaseVersion( v );
1066 if ( v.equals( projectVersion ) )
1075 private static class PomFileFilter
1076 implements FilenameFilter
1078 private final String pomFile;
1080 private PomFileFilter( String pomFile )
1082 this.pomFile = pomFile;
1086 public boolean accept( File dir, String name )
1088 return pomFile.equals( name );
1093 public PathParser getPathParser()
1098 public void setPathParser( PathParser pathParser )
1100 this.pathParser = pathParser;