From: James William Dumay Date: Tue, 10 Feb 2009 23:22:49 +0000 (+0000) Subject: MRM-799 - Better handling of artifacts with missing poms X-Git-Tag: archiva-1.2~137 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=4719834dfa78ce3b48645968885647b425f30a8e;p=archiva.git MRM-799 - Better handling of artifacts with missing poms * Updated to work with revised model in 1.2 * Changed patch so that Artifacts created for Browsing do not persist to the database (models may not have been proxied) * Big thankyou to Jevica Arianne B. Zurbano for submitting the fixes! git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@743164 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java index bab71b036..01b805b9e 100644 --- a/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java +++ b/archiva-modules/archiva-database/src/main/java/org/apache/maven/archiva/database/browsing/DefaultRepositoryBrowsing.java @@ -20,6 +20,7 @@ package org.apache.maven.archiva.database.browsing; */ import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -33,6 +34,7 @@ import org.apache.maven.archiva.common.utils.VersionUtil; import org.apache.maven.archiva.database.ArchivaDAO; import org.apache.maven.archiva.database.ArchivaDatabaseException; import org.apache.maven.archiva.database.ObjectNotFoundException; +import org.apache.maven.archiva.database.constraints.ArtifactsRelatedConstraint; import org.apache.maven.archiva.database.constraints.ProjectsByArtifactUsageConstraint; import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint; import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint; @@ -65,56 +67,67 @@ public class DefaultRepositoryBrowsing */ private DatabaseUpdater dbUpdater; - public BrowsingResults getRoot( final String principle, final List observableRepositoryIds ) + public BrowsingResults getRoot( final String principal, final List observableRepositoryIds ) { - List groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds ) ); - - BrowsingResults results = new BrowsingResults(); - results.setSelectedRepositoryIds( observableRepositoryIds ); - - results.setGroupIds( GroupIdFilter.filterGroups( groups ) ); + final BrowsingResults results = new BrowsingResults(); + if (!observableRepositoryIds.isEmpty()) + { + final List groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds ) ); + results.setSelectedRepositoryIds( observableRepositoryIds ); + results.setGroupIds( GroupIdFilter.filterGroups( groups ) ); + } return results; } - public BrowsingResults selectArtifactId( final String principle, final List observableRepositoryIds, final String groupId, + public BrowsingResults selectArtifactId( final String principal, final List observableRepositoryIds, final String groupId, final String artifactId ) { - // NOTE: No group Id or artifact Id's should be returned here. - List versions = dao.query( new UniqueVersionConstraint( observableRepositoryIds, groupId, artifactId ) ); + final BrowsingResults results = new BrowsingResults( groupId, artifactId ); - BrowsingResults results = new BrowsingResults( groupId, artifactId ); - results.setSelectedRepositoryIds( observableRepositoryIds ); - - processSnapshots( versions ); + if (!observableRepositoryIds.isEmpty()) + { + // NOTE: No group Id or artifact Id's should be returned here. + final List versions = dao.query( new UniqueVersionConstraint( observableRepositoryIds, groupId, artifactId ) ); + results.setSelectedRepositoryIds( observableRepositoryIds ); - results.setVersions( versions ); + processSnapshots( versions ); + results.setVersions( versions ); + } return results; } - public BrowsingResults selectGroupId( final String principle, final List observableRepositoryIds, final String groupId ) + public BrowsingResults selectGroupId( final String principal, final List observableRepositoryIds, final String groupId ) { - List groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds, groupId ) ); - List artifacts = dao.query( new UniqueArtifactIdConstraint( observableRepositoryIds, groupId ) ); + final BrowsingResults results = new BrowsingResults( groupId ); - BrowsingResults results = new BrowsingResults( groupId ); - - // Remove searched for groupId from groups list. - // Easier to do this here, vs doing it in the SQL query. - CollectionUtils.filter( groups, NotPredicate.getInstance( PredicateUtils.equalPredicate( groupId ) ) ); - - results.setGroupIds( groups ); - results.setArtifacts( artifacts ); + if (!observableRepositoryIds.isEmpty()) + { + final List groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds, groupId ) ); + final List artifacts = dao.query( new UniqueArtifactIdConstraint( observableRepositoryIds, groupId ) ); + + // Remove searched for groupId from groups list. + // Easier to do this here, vs doing it in the SQL query. + CollectionUtils.filter( groups, NotPredicate.getInstance( PredicateUtils.equalPredicate( groupId ) ) ); + + results.setGroupIds( groups ); + results.setArtifacts( artifacts ); + } return results; } - public ArchivaProjectModel selectVersion( final String principle, final List observableRepositoryIds, final String groupId, + public ArchivaProjectModel selectVersion( final String principal, final List observableRepositoryIds, final String groupId, final String artifactId, final String version ) throws ObjectNotFoundException, ArchivaDatabaseException { - ArchivaArtifact pomArtifact = getArtifact( principle, observableRepositoryIds, groupId, artifactId, version ); + if (observableRepositoryIds.isEmpty()) + { + throw new ArchivaDatabaseException("There are no observable repositories for the user " + principal); + } + + ArchivaArtifact pomArtifact = getArtifact( principal, observableRepositoryIds, groupId, artifactId, version ); ArchivaProjectModel model; @@ -129,14 +142,26 @@ public class DefaultRepositoryBrowsing return model; } - public String getRepositoryId( final String principle, final List observableRepositoryIds, final String groupId, + public String getRepositoryId( final String principal, final List observableRepositoryIds, final String groupId, final String artifactId, final String version ) throws ObjectNotFoundException, ArchivaDatabaseException { - ArchivaArtifact pomArchivaArtifact = - getArtifact( principle, observableRepositoryIds, groupId, artifactId, version ); + if (observableRepositoryIds.isEmpty()) + { + throw new ArchivaDatabaseException("There are no observable repositories for the user " + principal); + } - return pomArchivaArtifact.getModel().getRepositoryId(); + try + { + ArchivaArtifact pomArchivaArtifact = + getArtifact( principal, observableRepositoryIds, groupId, artifactId, version ); + + return pomArchivaArtifact.getModel().getRepositoryId(); + } + catch ( ObjectNotFoundException e ) + { + return getNoPomArtifactRepoId( principal, observableRepositoryIds, groupId, artifactId, version, observableRepositoryIds.get(0) ); + } } private ArchivaArtifact getArtifact( final String principal, final List observableRepositoryIds, final String groupId, @@ -150,6 +175,7 @@ public class DefaultRepositoryBrowsing try { pomArtifact = dao.getArtifactDAO().getArtifact( groupId, artifactId, version, null, "pom", repositoryId ); + break; } catch ( ObjectNotFoundException e ) { @@ -157,11 +183,13 @@ public class DefaultRepositoryBrowsing } } - if ( pomArtifact == null ) { - throw new ObjectNotFoundException( "Unable to find artifact [" + Keys.toKey( groupId, artifactId, version ) - + "]" ); + String type = getArtifactType( groupId, artifactId, version ); + + //We dont want these to persist in the database + pomArtifact = new ArchivaArtifact( groupId, artifactId, version, null, type, observableRepositoryIds.get(0) ); + pomArtifact.getModel().setWhenProcessed(new Date()); } // Allowed to see this? @@ -177,7 +205,7 @@ public class DefaultRepositoryBrowsing } } - public List getUsedBy( final String principle, final List observableRepositoryIds, final String groupId, + public List getUsedBy( final String principal, final List observableRepositoryIds, final String groupId, final String artifactId, final String version ) throws ArchivaDatabaseException { @@ -258,7 +286,7 @@ public class DefaultRepositoryBrowsing if ( VersionUtil.isGenericSnapshot( version ) ) { - List versions = dao.query( new UniqueVersionConstraint( groupId, artifactId ) ); + final List versions = dao.query( new UniqueVersionConstraint( groupId, artifactId ) ); Collections.sort( versions ); Collections.reverse( versions ); @@ -286,22 +314,74 @@ public class DefaultRepositoryBrowsing private ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version ) throws ArchivaDatabaseException { + ArchivaProjectModel model = null; + try { - ArchivaProjectModel model = dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version ); + dao.getProjectModelDAO().getProjectModel( groupId, artifactId, version ); + } + catch (ObjectNotFoundException e) + { + log.debug("Unable to find project model for [" + Keys.toKey( groupId, artifactId, version ) + "]", e); + } + + if ( model == null ) + { + model = new ArchivaProjectModel(); + model.setGroupId(groupId); + model.setArtifactId(artifactId); + model.setVersion(version); + } + + return model; + } + + private String getNoPomArtifactRepoId( String principal, List observableRepos, String groupId, String artifactId, String version, String repositoryId ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + ArchivaArtifact artifact = null; + + String type = getArtifactType( groupId, artifactId, version ); + + artifact = dao.getArtifactDAO().createArtifact( groupId, artifactId, version, null, type, repositoryId ); + + if ( artifact == null ) + { + //Lets not persist these + artifact = new ArchivaArtifact( groupId, artifactId, version, null, type, repositoryId ); + } - if ( model == null ) + // Allowed to see this? + if ( !observableRepos.contains( artifact.getModel().getRepositoryId() ) ) + { + throw new ObjectNotFoundException( "Unable to find artifact " + Keys.toKey( groupId, artifactId, version ) + + " in observable repository [" + StringUtils.join( observableRepos.iterator(), ", " ) + + "] for user " + principal ); + } + + return artifact.getModel().getRepositoryId(); + } + + private String getArtifactType( String groupId, String artifactId, String version ) + throws ObjectNotFoundException, ArchivaDatabaseException + { + String type = "jar"; + + try + { + List artifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsRelatedConstraint( groupId, artifactId, version ) ); + + if ( artifacts.size() > 0 ) { - throw new ObjectNotFoundException( "Unable to find project model for [" - + Keys.toKey( groupId, artifactId, version ) + "]" ); + type = artifacts.get( 0 ).getType(); } - - return model; } catch ( ObjectNotFoundException e ) { - throw e; + //swallow exception? } + + return type; } } diff --git a/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java b/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java index ce0dbb8f4..8ac9c80a5 100644 --- a/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java +++ b/archiva-modules/archiva-database/src/test/java/org/apache/maven/archiva/database/browsing/RepositoryBrowsingTest.java @@ -23,6 +23,7 @@ import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase; import org.apache.maven.archiva.database.ArchivaDAO; import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.model.ArchivaArtifact; +import org.apache.maven.archiva.model.ArchivaProjectModel; import java.util.ArrayList; import java.util.Date; @@ -129,6 +130,19 @@ public class RepositoryBrowsingTest assertGroupIds( "Browsing Results (root)", results.getGroupIds(), expectedRootGroupIds ); } + public void testViewArtifact() + throws Exception + { + saveTestData(); + + RepositoryBrowsing browser = lookupBrowser(); + ArchivaProjectModel artifact = browser.selectVersion( USER_GUEST, GUEST_REPO_IDS, "org.apache.commons", "commons-lang", "2.0" ); + assertNotNull( "Artifact should not be null.", artifact ); + assertEquals( "org.apache.commons", artifact.getGroupId() ); + assertEquals( "commons-lang", artifact.getArtifactId() ); + assertEquals( "2.0", artifact.getVersion() ); + } + private void assertGroupIds( String msg, List actualGroupIds, String[] expectedGroupIds ) { assertEquals( msg + ": groupIds.length", expectedGroupIds.length, actualGroupIds.size() );