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
This commit is contained in:
James William Dumay 2009-02-10 23:22:49 +00:00
parent f3e319b018
commit 4719834dfa
2 changed files with 137 additions and 43 deletions

View File

@ -20,6 +20,7 @@ package org.apache.maven.archiva.database.browsing;
*/ */
import java.util.Collections; import java.util.Collections;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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.ArchivaDAO;
import org.apache.maven.archiva.database.ArchivaDatabaseException; import org.apache.maven.archiva.database.ArchivaDatabaseException;
import org.apache.maven.archiva.database.ObjectNotFoundException; 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.ProjectsByArtifactUsageConstraint;
import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint; import org.apache.maven.archiva.database.constraints.UniqueArtifactIdConstraint;
import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint; import org.apache.maven.archiva.database.constraints.UniqueGroupIdConstraint;
@ -65,56 +67,67 @@ public class DefaultRepositoryBrowsing
*/ */
private DatabaseUpdater dbUpdater; private DatabaseUpdater dbUpdater;
public BrowsingResults getRoot( final String principle, final List<String> observableRepositoryIds ) public BrowsingResults getRoot( final String principal, final List<String> observableRepositoryIds )
{ {
List<String> groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds ) ); final BrowsingResults results = new BrowsingResults();
BrowsingResults results = new BrowsingResults();
results.setSelectedRepositoryIds( observableRepositoryIds );
results.setGroupIds( GroupIdFilter.filterGroups( groups ) );
if (!observableRepositoryIds.isEmpty())
{
final List<String> groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds ) );
results.setSelectedRepositoryIds( observableRepositoryIds );
results.setGroupIds( GroupIdFilter.filterGroups( groups ) );
}
return results; return results;
} }
public BrowsingResults selectArtifactId( final String principle, final List<String> observableRepositoryIds, final String groupId, public BrowsingResults selectArtifactId( final String principal, final List<String> observableRepositoryIds, final String groupId,
final String artifactId ) final String artifactId )
{ {
// NOTE: No group Id or artifact Id's should be returned here. final BrowsingResults results = new BrowsingResults( groupId, artifactId );
List<String> versions = dao.query( new UniqueVersionConstraint( observableRepositoryIds, groupId, artifactId ) );
BrowsingResults results = new BrowsingResults( groupId, artifactId ); if (!observableRepositoryIds.isEmpty())
results.setSelectedRepositoryIds( observableRepositoryIds ); {
// NOTE: No group Id or artifact Id's should be returned here.
final List<String> versions = dao.query( new UniqueVersionConstraint( observableRepositoryIds, groupId, artifactId ) );
results.setSelectedRepositoryIds( observableRepositoryIds );
processSnapshots( versions ); processSnapshots( versions );
results.setVersions( versions );
results.setVersions( versions );
}
return results; return results;
} }
public BrowsingResults selectGroupId( final String principle, final List<String> observableRepositoryIds, final String groupId ) public BrowsingResults selectGroupId( final String principal, final List<String> observableRepositoryIds, final String groupId )
{ {
List<String> groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds, groupId ) ); final BrowsingResults results = new BrowsingResults( groupId );
List<String> artifacts = dao.query( new UniqueArtifactIdConstraint( observableRepositoryIds, groupId ) );
BrowsingResults results = new BrowsingResults( groupId ); if (!observableRepositoryIds.isEmpty())
{
final List<String> groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds, groupId ) );
final List<String> 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 ) ) );
// Remove searched for groupId from groups list. results.setGroupIds( groups );
// Easier to do this here, vs doing it in the SQL query. results.setArtifacts( artifacts );
CollectionUtils.filter( groups, NotPredicate.getInstance( PredicateUtils.equalPredicate( groupId ) ) ); }
results.setGroupIds( groups );
results.setArtifacts( artifacts );
return results; return results;
} }
public ArchivaProjectModel selectVersion( final String principle, final List<String> observableRepositoryIds, final String groupId, public ArchivaProjectModel selectVersion( final String principal, final List<String> observableRepositoryIds, final String groupId,
final String artifactId, final String version ) final String artifactId, final String version )
throws ObjectNotFoundException, ArchivaDatabaseException 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; ArchivaProjectModel model;
@ -129,14 +142,26 @@ public class DefaultRepositoryBrowsing
return model; return model;
} }
public String getRepositoryId( final String principle, final List<String> observableRepositoryIds, final String groupId, public String getRepositoryId( final String principal, final List<String> observableRepositoryIds, final String groupId,
final String artifactId, final String version ) final String artifactId, final String version )
throws ObjectNotFoundException, ArchivaDatabaseException throws ObjectNotFoundException, ArchivaDatabaseException
{ {
ArchivaArtifact pomArchivaArtifact = if (observableRepositoryIds.isEmpty())
getArtifact( principle, observableRepositoryIds, groupId, artifactId, version ); {
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<String> observableRepositoryIds, final String groupId, private ArchivaArtifact getArtifact( final String principal, final List<String> observableRepositoryIds, final String groupId,
@ -150,6 +175,7 @@ public class DefaultRepositoryBrowsing
try try
{ {
pomArtifact = dao.getArtifactDAO().getArtifact( groupId, artifactId, version, null, "pom", repositoryId ); pomArtifact = dao.getArtifactDAO().getArtifact( groupId, artifactId, version, null, "pom", repositoryId );
break;
} }
catch ( ObjectNotFoundException e ) catch ( ObjectNotFoundException e )
{ {
@ -157,11 +183,13 @@ public class DefaultRepositoryBrowsing
} }
} }
if ( pomArtifact == null ) 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? // Allowed to see this?
@ -177,7 +205,7 @@ public class DefaultRepositoryBrowsing
} }
} }
public List<ArchivaProjectModel> getUsedBy( final String principle, final List<String> observableRepositoryIds, final String groupId, public List<ArchivaProjectModel> getUsedBy( final String principal, final List<String> observableRepositoryIds, final String groupId,
final String artifactId, final String version ) final String artifactId, final String version )
throws ArchivaDatabaseException throws ArchivaDatabaseException
{ {
@ -258,7 +286,7 @@ public class DefaultRepositoryBrowsing
if ( VersionUtil.isGenericSnapshot( version ) ) if ( VersionUtil.isGenericSnapshot( version ) )
{ {
List<String> versions = dao.query( new UniqueVersionConstraint( groupId, artifactId ) ); final List<String> versions = dao.query( new UniqueVersionConstraint( groupId, artifactId ) );
Collections.sort( versions ); Collections.sort( versions );
Collections.reverse( versions ); Collections.reverse( versions );
@ -286,22 +314,74 @@ public class DefaultRepositoryBrowsing
private ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version ) private ArchivaProjectModel getProjectModel( String groupId, String artifactId, String version )
throws ArchivaDatabaseException throws ArchivaDatabaseException
{ {
ArchivaProjectModel model = null;
try 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 ) if ( model == null )
{
model = new ArchivaProjectModel();
model.setGroupId(groupId);
model.setArtifactId(artifactId);
model.setVersion(version);
}
return model;
}
private String getNoPomArtifactRepoId( String principal, List<String> 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 );
}
// 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<ArchivaArtifact> artifacts = dao.getArtifactDAO().queryArtifacts( new ArtifactsRelatedConstraint( groupId, artifactId, version ) );
if ( artifacts.size() > 0 )
{ {
throw new ObjectNotFoundException( "Unable to find project model for [" type = artifacts.get( 0 ).getType();
+ Keys.toKey( groupId, artifactId, version ) + "]" );
} }
return model;
} }
catch ( ObjectNotFoundException e ) catch ( ObjectNotFoundException e )
{ {
throw e; //swallow exception?
} }
return type;
} }
} }

View File

@ -23,6 +23,7 @@ import org.apache.maven.archiva.database.AbstractArchivaDatabaseTestCase;
import org.apache.maven.archiva.database.ArchivaDAO; import org.apache.maven.archiva.database.ArchivaDAO;
import org.apache.maven.archiva.database.ArtifactDAO; import org.apache.maven.archiva.database.ArtifactDAO;
import org.apache.maven.archiva.model.ArchivaArtifact; import org.apache.maven.archiva.model.ArchivaArtifact;
import org.apache.maven.archiva.model.ArchivaProjectModel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
@ -129,6 +130,19 @@ public class RepositoryBrowsingTest
assertGroupIds( "Browsing Results (root)", results.getGroupIds(), expectedRootGroupIds ); 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 ) private void assertGroupIds( String msg, List actualGroupIds, String[] expectedGroupIds )
{ {
assertEquals( msg + ": groupIds.length", expectedGroupIds.length, actualGroupIds.size() ); assertEquals( msg + ": groupIds.length", expectedGroupIds.length, actualGroupIds.size() );