cleanup logic to upload artifacts groupId,artifactId,version,packaging can be changed at save time

git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1308255 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2012-04-02 07:17:50 +00:00
parent 5284e5c312
commit 4fb3387944
4 changed files with 52 additions and 107 deletions

View File

@ -207,7 +207,8 @@ public class DefaultFileUploadService
return fileMetadatas == null ? Collections.<FileMetadata>emptyList() : fileMetadatas; return fileMetadatas == null ? Collections.<FileMetadata>emptyList() : fileMetadatas;
} }
public Boolean save( String repositoryId, final String groupId, final String artifactId, final boolean generatePom ) public Boolean save( String repositoryId, final String groupId, final String artifactId, String version,
String packaging, final boolean generatePom )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
List<FileMetadata> fileMetadatas = List<FileMetadata> fileMetadatas =
@ -231,7 +232,8 @@ public class DefaultFileUploadService
{ {
FileMetadata fileMetadata = iterator.next(); FileMetadata fileMetadata = iterator.next();
log.debug( "fileToAdd: {}", fileMetadata ); log.debug( "fileToAdd: {}", fileMetadata );
saveFile( repositoryId, fileMetadata, generatePom && !pomGenerated ); saveFile( repositoryId, fileMetadata, generatePom && !pomGenerated, groupId, artifactId, version,
packaging );
pomGenerated = true; pomGenerated = true;
} }
@ -248,13 +250,14 @@ public class DefaultFileUploadService
{ {
FileMetadata fileMetadata = iterator.next(); FileMetadata fileMetadata = iterator.next();
log.debug( "fileToAdd: {}", fileMetadata ); log.debug( "fileToAdd: {}", fileMetadata );
savePomFile( repositoryId, fileMetadata ); savePomFile( repositoryId, fileMetadata, groupId, artifactId, version, packaging );
} }
return Boolean.TRUE; return Boolean.TRUE;
} }
protected void savePomFile( String repositoryId, FileMetadata fileMetadata ) protected void savePomFile( String repositoryId, FileMetadata fileMetadata, String groupId, String artifactId,
String version, String packaging )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
@ -266,11 +269,11 @@ public class DefaultFileUploadService
ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId ); ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
ArtifactReference artifactReference = new ArtifactReference(); ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setArtifactId( fileMetadata.getArtifactId() ); artifactReference.setArtifactId( artifactId );
artifactReference.setGroupId( fileMetadata.getGroupId() ); artifactReference.setGroupId( groupId );
artifactReference.setVersion( fileMetadata.getVersion() ); artifactReference.setVersion( version );
artifactReference.setClassifier( fileMetadata.getClassifier() ); artifactReference.setClassifier( fileMetadata.getClassifier() );
artifactReference.setType( fileMetadata.getPackaging() ); artifactReference.setType( packaging );
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId ); ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
@ -309,7 +312,8 @@ public class DefaultFileUploadService
} }
} }
protected void saveFile( String repositoryId, FileMetadata fileMetadata, boolean generatePom ) protected void saveFile( String repositoryId, FileMetadata fileMetadata, boolean generatePom, String groupId,
String artifactId, String version, String packaging )
throws ArchivaRestServiceException throws ArchivaRestServiceException
{ {
try try
@ -318,11 +322,11 @@ public class DefaultFileUploadService
ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId ); ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId );
ArtifactReference artifactReference = new ArtifactReference(); ArtifactReference artifactReference = new ArtifactReference();
artifactReference.setArtifactId( fileMetadata.getArtifactId() ); artifactReference.setArtifactId( artifactId );
artifactReference.setGroupId( fileMetadata.getGroupId() ); artifactReference.setGroupId( groupId );
artifactReference.setVersion( fileMetadata.getVersion() ); artifactReference.setVersion( version );
artifactReference.setClassifier( fileMetadata.getClassifier() ); artifactReference.setClassifier( fileMetadata.getClassifier() );
artifactReference.setType( fileMetadata.getPackaging() ); artifactReference.setType( packaging );
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId ); ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
@ -342,7 +346,7 @@ public class DefaultFileUploadService
File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA ); File versionMetadataFile = new File( targetPath, MetadataTools.MAVEN_METADATA );
ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetadataFile ); ArchivaRepositoryMetadata versionMetadata = getMetadata( versionMetadataFile );
if ( VersionUtil.isSnapshot( fileMetadata.getVersion() ) ) if ( VersionUtil.isSnapshot( version ) )
{ {
TimeZone timezone = TimeZone.getTimeZone( "UTC" ); TimeZone timezone = TimeZone.getTimeZone( "UTC" );
DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" ); DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
@ -364,7 +368,7 @@ public class DefaultFileUploadService
} }
String filename = artifactPath.substring( lastIndex + 1 ); String filename = artifactPath.substring( lastIndex + 1 );
if ( VersionUtil.isSnapshot( fileMetadata.getVersion() ) ) if ( VersionUtil.isSnapshot( version ) )
{ {
filename = filename.replaceAll( "SNAPSHOT", timestamp + "-" + newBuildNumber ); filename = filename.replaceAll( "SNAPSHOT", timestamp + "-" + newBuildNumber );
} }
@ -375,8 +379,7 @@ public class DefaultFileUploadService
try try
{ {
File targetFile = new File( targetPath, filename ); File targetFile = new File( targetPath, filename );
if ( targetFile.exists() && !VersionUtil.isSnapshot( fileMetadata.getVersion() ) if ( targetFile.exists() && !VersionUtil.isSnapshot( version ) && repoConfig.isBlockRedeployments() )
&& repoConfig.isBlockRedeployments() )
{ {
throw new ArchivaRestServiceException( throw new ArchivaRestServiceException(
"Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed.", "Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed.",
@ -407,7 +410,8 @@ public class DefaultFileUploadService
try try
{ {
File generatedPomFile = createPom( targetPath, pomFilename, fileMetadata ); File generatedPomFile =
createPom( targetPath, pomFilename, fileMetadata, groupId, artifactId, version, packaging );
triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE );
if ( fixChecksums ) if ( fixChecksums )
{ {
@ -427,12 +431,13 @@ public class DefaultFileUploadService
if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) ) if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) )
{ {
updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber, updateProjectMetadata( targetPath.getAbsolutePath(), lastUpdatedTimestamp, timestamp, newBuildNumber,
fixChecksums, fileMetadata ); fixChecksums, fileMetadata, groupId, artifactId, version, packaging );
if ( VersionUtil.isSnapshot( fileMetadata.getVersion() ) ) if ( VersionUtil.isSnapshot( version ) )
{ {
updateVersionMetadata( versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp, updateVersionMetadata( versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp,
newBuildNumber, fixChecksums, fileMetadata ); newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version,
packaging );
} }
} }
} }
@ -471,15 +476,16 @@ public class DefaultFileUploadService
return metadata; return metadata;
} }
private File createPom( File targetPath, String filename, FileMetadata fileMetadata ) private File createPom( File targetPath, String filename, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging )
throws IOException throws IOException
{ {
Model projectModel = new Model(); Model projectModel = new Model();
projectModel.setModelVersion( "4.0.0" ); projectModel.setModelVersion( "4.0.0" );
projectModel.setGroupId( fileMetadata.getGroupId() ); projectModel.setGroupId( groupId );
projectModel.setArtifactId( fileMetadata.getArtifactId() ); projectModel.setArtifactId( artifactId );
projectModel.setVersion( fileMetadata.getVersion() ); projectModel.setVersion( version );
projectModel.setPackaging( fileMetadata.getPackaging() ); projectModel.setPackaging( packaging );
File pomFile = new File( targetPath, filename ); File pomFile = new File( targetPath, filename );
MavenXpp3Writer writer = new MavenXpp3Writer(); MavenXpp3Writer writer = new MavenXpp3Writer();
@ -547,11 +553,12 @@ public class DefaultFileUploadService
* Update artifact level metadata. If it does not exist, create the metadata and fix checksums if necessary. * Update artifact level metadata. If it does not exist, create the metadata and fix checksums if necessary.
*/ */
private void updateProjectMetadata( String targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber, private void updateProjectMetadata( String targetPath, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
boolean fixChecksums, FileMetadata fileMetadata ) boolean fixChecksums, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging )
throws RepositoryMetadataException throws RepositoryMetadataException
{ {
List<String> availableVersions = new ArrayList<String>(); List<String> availableVersions = new ArrayList<String>();
String latestVersion = fileMetadata.getVersion(); String latestVersion = version;
File projectDir = new File( targetPath ).getParentFile(); File projectDir = new File( targetPath ).getParentFile();
File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA ); File projectMetadataFile = new File( projectDir, MetadataTools.MAVEN_METADATA );
@ -564,36 +571,36 @@ public class DefaultFileUploadService
Collections.sort( availableVersions, VersionComparator.getInstance() ); Collections.sort( availableVersions, VersionComparator.getInstance() );
if ( !availableVersions.contains( fileMetadata.getVersion() ) ) if ( !availableVersions.contains( version ) )
{ {
availableVersions.add( fileMetadata.getVersion() ); availableVersions.add( version );
} }
latestVersion = availableVersions.get( availableVersions.size() - 1 ); latestVersion = availableVersions.get( availableVersions.size() - 1 );
} }
else else
{ {
availableVersions.add( fileMetadata.getVersion() ); availableVersions.add( version );
projectMetadata.setGroupId( fileMetadata.getGroupId() ); projectMetadata.setGroupId( groupId );
projectMetadata.setArtifactId( fileMetadata.getArtifactId() ); projectMetadata.setArtifactId( artifactId );
} }
if ( projectMetadata.getGroupId() == null ) if ( projectMetadata.getGroupId() == null )
{ {
projectMetadata.setGroupId( fileMetadata.getGroupId() ); projectMetadata.setGroupId( groupId );
} }
if ( projectMetadata.getArtifactId() == null ) if ( projectMetadata.getArtifactId() == null )
{ {
projectMetadata.setArtifactId( fileMetadata.getArtifactId() ); projectMetadata.setArtifactId( artifactId );
} }
projectMetadata.setLatestVersion( latestVersion ); projectMetadata.setLatestVersion( latestVersion );
projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp ); projectMetadata.setLastUpdatedTimestamp( lastUpdatedTimestamp );
projectMetadata.setAvailableVersions( availableVersions ); projectMetadata.setAvailableVersions( availableVersions );
if ( !VersionUtil.isSnapshot( fileMetadata.getVersion() ) ) if ( !VersionUtil.isSnapshot( version ) )
{ {
projectMetadata.setReleasedVersion( latestVersion ); projectMetadata.setReleasedVersion( latestVersion );
} }
@ -612,14 +619,15 @@ public class DefaultFileUploadService
*/ */
private void updateVersionMetadata( ArchivaRepositoryMetadata metadata, File metadataFile, private void updateVersionMetadata( ArchivaRepositoryMetadata metadata, File metadataFile,
Date lastUpdatedTimestamp, String timestamp, int buildNumber, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
boolean fixChecksums, FileMetadata fileMetadata ) boolean fixChecksums, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging )
throws RepositoryMetadataException throws RepositoryMetadataException
{ {
if ( !metadataFile.exists() ) if ( !metadataFile.exists() )
{ {
metadata.setGroupId( fileMetadata.getGroupId() ); metadata.setGroupId( groupId );
metadata.setArtifactId( fileMetadata.getArtifactId() ); metadata.setArtifactId( artifactId );
metadata.setVersion( fileMetadata.getVersion() ); metadata.setVersion( version );
} }
if ( metadata.getSnapshotVersion() == null ) if ( metadata.getSnapshotVersion() == null )

View File

@ -67,12 +67,13 @@ public interface FileUploadService
List<FileMetadata> getSessionFileMetadatas() List<FileMetadata> getSessionFileMetadatas()
throws ArchivaRestServiceException; throws ArchivaRestServiceException;
@Path( "save/{repositoryId}/{groupId}/{artifactId}" ) @Path( "save/{repositoryId}/{groupId}/{artifactId}/{version}/{packaging}" )
@GET @GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } ) @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML } )
@RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) @RedbackAuthorization( permissions = ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )
Boolean save( @PathParam( "repositoryId" ) String repositoryId, @PathParam( "groupId" ) String groupId, Boolean save( @PathParam( "repositoryId" ) String repositoryId, @PathParam( "groupId" ) String groupId,
@PathParam( "artifactId" ) String artifactId, @QueryParam( "generatePom" ) boolean generatePom ) @PathParam( "artifactId" ) String artifactId, @PathParam( "version" ) String version,
@PathParam( "packaging" ) String packaging, @QueryParam( "generatePom" ) boolean generatePom )
throws ArchivaRestServiceException; throws ArchivaRestServiceException;

View File

@ -43,14 +43,6 @@ public class FileMetadata
private String errorKey; private String errorKey;
private String groupId;
private String artifactId;
private String version;
private String packaging;
private String classifier; private String classifier;
private boolean pomFile; private boolean pomFile;
@ -134,46 +126,6 @@ public class FileMetadata
this.errorKey = errorKey; this.errorKey = errorKey;
} }
public String getGroupId()
{
return groupId;
}
public void setGroupId( String groupId )
{
this.groupId = groupId;
}
public String getArtifactId()
{
return artifactId;
}
public void setArtifactId( String artifactId )
{
this.artifactId = artifactId;
}
public String getVersion()
{
return version;
}
public void setVersion( String version )
{
this.version = version;
}
public String getPackaging()
{
return packaging;
}
public void setPackaging( String packaging )
{
this.packaging = packaging;
}
public String getClassifier() public String getClassifier()
{ {
return classifier; return classifier;
@ -245,10 +197,6 @@ public class FileMetadata
sb.append( ", deleteUrl='" ).append( deleteUrl ).append( '\'' ); sb.append( ", deleteUrl='" ).append( deleteUrl ).append( '\'' );
sb.append( ", deleteType='" ).append( deleteType ).append( '\'' ); sb.append( ", deleteType='" ).append( deleteType ).append( '\'' );
sb.append( ", errorKey='" ).append( errorKey ).append( '\'' ); sb.append( ", errorKey='" ).append( errorKey ).append( '\'' );
sb.append( ", groupId='" ).append( groupId ).append( '\'' );
sb.append( ", artifactId='" ).append( artifactId ).append( '\'' );
sb.append( ", version='" ).append( version ).append( '\'' );
sb.append( ", packaging='" ).append( packaging ).append( '\'' );
sb.append( ", classifier='" ).append( classifier ).append( '\'' ); sb.append( ", classifier='" ).append( classifier ).append( '\'' );
sb.append( ", pomFile=" ).append( pomFile ); sb.append( ", pomFile=" ).append( pomFile );
sb.append( '}' ); sb.append( '}' );

View File

@ -47,6 +47,7 @@ define("archiva.artifacts-management",["jquery","i18n","order!utils","order!jque
return; return;
} }
var url="restServices/archivaUiServices/fileUploadService/save/"+this.repositoryId()+"/"+this.groupId()+"/"+this.artifactId(); var url="restServices/archivaUiServices/fileUploadService/save/"+this.repositoryId()+"/"+this.groupId()+"/"+this.artifactId();
url+="/"+this.version()+"/"+this.packaging();
if (this.generatePom()){ if (this.generatePom()){
url+="?generatePom=true"; url+="?generatePom=true";
} }
@ -89,21 +90,8 @@ define("archiva.artifacts-management",["jquery","i18n","order!utils","order!jque
} }
}); });
$('#fileupload').fileupload({ $('#fileupload').fileupload({
add: function (e, data) {
if(!mainContent.find("#fileupload" ).valid()){
return;
}
data.formData = {
groupId: artifactUploadViewModel.groupId(),
artifactId: artifactUploadViewModel.artifactId(),
version: artifactUploadViewModel.version(),
packaging: artifactUploadViewModel.packaging()
};
$.blueimpUI.fileupload.prototype.options.add.call(this, e, data);
},
submit: function (e, data) { submit: function (e, data) {
var $this = $(this); var $this = $(this);
$this.fileupload('send', data); $this.fileupload('send', data);
artifactUploadViewModel.artifactUploads.push(new ArtifactUpload(data.formData.classifier,data.formData.pomFile)); artifactUploadViewModel.artifactUploads.push(new ArtifactUpload(data.formData.classifier,data.formData.pomFile));
return false; return false;