artifactReference.setGroupId( groupId );
artifactReference.setVersion( version );
artifactReference.setClassifier( classifier );
artifactReference.setType( packaging );
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId );
String artifactPath = repository.toPath( artifactReference );
int lastIndex = artifactPath.lastIndexOf( '/' );
File targetPath = new File( repoConfig.getLocation(), artifactPath.substring( 0, lastIndex ) );
Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
int newBuildNumber = -1;
String timestamp = null;
File metadataFile = getMetadata( targetPath.getAbsolutePath() );
ArchivaRepositoryMetadata metadata = getMetadata( metadataFile );
if ( VersionUtil.isSnapshot( version ) )
{
TimeZone timezone = TimeZone.getTimeZone( "UTC" );
DateFormat fmt = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
fmt.setTimeZone( timezone );
timestamp = fmt.format( lastUpdatedTimestamp );
if ( metadata.getSnapshotVersion() != null )
{
newBuildNumber = metadata.getSnapshotVersion().getBuildNumber() + 1;
}
else
{
metadata.setSnapshotVersion( new SnapshotVersion() );
newBuildNumber = 1;
}
}
if ( !targetPath.exists() )
{
targetPath.mkdirs();
}
String filename = artifactPath.substring( lastIndex + 1 );
if ( VersionUtil.isSnapshot( version ) )
{
filename = filename.replaceAll( "SNAPSHOT", timestamp + "-" + newBuildNumber );
}
boolean fixChecksums = !( config.getRepositoryScanning().getKnownContentConsumers().contains( "create-missing-checksums" ) );
try
{
File targetFile = new File( targetPath, filename );
if( targetFile.exists() && !VersionUtil.isSnapshot( version ) && repoConfig.isBlockRedeployments() )
{
addActionError( "Overwriting released artifacts in repository '" + repoConfig.getId() + "' is not allowed." );
return ERROR;
}
else
{
copyFile( artifactFile, targetPath, filename, fixChecksums );
queueRepositoryTask( repository.getId(), repository.toFile( artifactReference ) );
}
}
catch ( IOException ie )
{
addActionError( "Error encountered while uploading file: " + ie.getMessage() );