mirror of https://github.com/apache/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
This commit is contained in:
parent
f3e319b018
commit
4719834dfa
|
@ -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,40 +67,45 @@ public class DefaultRepositoryBrowsing
|
|||
*/
|
||||
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();
|
||||
if (!observableRepositoryIds.isEmpty())
|
||||
{
|
||||
final List<String> groups = dao.query( new UniqueGroupIdConstraint( observableRepositoryIds ) );
|
||||
results.setSelectedRepositoryIds( observableRepositoryIds );
|
||||
|
||||
results.setGroupIds( GroupIdFilter.filterGroups( groups ) );
|
||||
|
||||
}
|
||||
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 )
|
||||
{
|
||||
// NOTE: No group Id or artifact Id's should be returned here.
|
||||
List<String> versions = dao.query( new UniqueVersionConstraint( observableRepositoryIds, groupId, artifactId ) );
|
||||
final BrowsingResults results = new BrowsingResults( groupId, artifactId );
|
||||
|
||||
BrowsingResults results = new BrowsingResults( groupId, artifactId );
|
||||
if (!observableRepositoryIds.isEmpty())
|
||||
{
|
||||
// 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 );
|
||||
|
||||
results.setVersions( versions );
|
||||
|
||||
}
|
||||
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 ) );
|
||||
List<String> artifacts = dao.query( new UniqueArtifactIdConstraint( observableRepositoryIds, groupId ) );
|
||||
final BrowsingResults results = new BrowsingResults( 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.
|
||||
|
@ -106,15 +113,21 @@ public class DefaultRepositoryBrowsing
|
|||
|
||||
results.setGroupIds( groups );
|
||||
results.setArtifacts( artifacts );
|
||||
}
|
||||
|
||||
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 )
|
||||
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,15 +142,27 @@ public class DefaultRepositoryBrowsing
|
|||
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 )
|
||||
throws ObjectNotFoundException, ArchivaDatabaseException
|
||||
{
|
||||
if (observableRepositoryIds.isEmpty())
|
||||
{
|
||||
throw new ArchivaDatabaseException("There are no observable repositories for the user " + principal);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ArchivaArtifact pomArchivaArtifact =
|
||||
getArtifact( principle, observableRepositoryIds, groupId, artifactId, version );
|
||||
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,
|
||||
final String artifactId, final String version )
|
||||
|
@ -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<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 )
|
||||
throws ArchivaDatabaseException
|
||||
{
|
||||
|
@ -258,7 +286,7 @@ public class DefaultRepositoryBrowsing
|
|||
|
||||
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.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 )
|
||||
{
|
||||
throw new ObjectNotFoundException( "Unable to find project model for ["
|
||||
+ Keys.toKey( groupId, artifactId, version ) + "]" );
|
||||
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 )
|
||||
{
|
||||
type = artifacts.get( 0 ).getType();
|
||||
}
|
||||
}
|
||||
catch ( ObjectNotFoundException e )
|
||||
{
|
||||
throw e;
|
||||
//swallow exception?
|
||||
}
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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() );
|
||||
|
|
Loading…
Reference in New Issue