Using new artifact util in upload service

This commit is contained in:
Martin Stockhammer 2019-05-04 10:49:34 +02:00
parent a3c149327e
commit 0e09883158
1 changed files with 301 additions and 406 deletions

View File

@ -34,10 +34,9 @@ import org.apache.archiva.model.ArchivaRepositoryMetadata;
import org.apache.archiva.model.ArtifactReference; import org.apache.archiva.model.ArtifactReference;
import org.apache.archiva.model.SnapshotVersion; import org.apache.archiva.model.SnapshotVersion;
import org.apache.archiva.redback.components.taskqueue.TaskQueueException; import org.apache.archiva.redback.components.taskqueue.TaskQueueException;
import org.apache.archiva.repository.ManagedRepositoryContent;
import org.apache.archiva.repository.RepositoryContentFactory;
import org.apache.archiva.repository.RepositoryException; import org.apache.archiva.repository.RepositoryException;
import org.apache.archiva.repository.RepositoryNotFoundException; import org.apache.archiva.repository.RepositoryNotFoundException;
import org.apache.archiva.repository.content.ArtifactUtil;
import org.apache.archiva.repository.metadata.MetadataTools; import org.apache.archiva.repository.metadata.MetadataTools;
import org.apache.archiva.repository.metadata.RepositoryMetadataException; import org.apache.archiva.repository.metadata.RepositoryMetadataException;
import org.apache.archiva.repository.metadata.RepositoryMetadataWriter; import org.apache.archiva.repository.metadata.RepositoryMetadataWriter;
@ -63,6 +62,7 @@ import org.springframework.stereotype.Service;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -72,14 +72,7 @@ import java.net.URLDecoder;
import java.nio.file.*; import java.nio.file.*;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
/** /**
@ -88,8 +81,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
@Service("fileUploadService#rest") @Service("fileUploadService#rest")
public class DefaultFileUploadService public class DefaultFileUploadService
extends AbstractRestService extends AbstractRestService
implements FileUploadService implements FileUploadService {
{
private Logger log = LoggerFactory.getLogger(getClass()); private Logger log = LoggerFactory.getLogger(getClass());
@Context @Context
@ -99,7 +91,7 @@ public class DefaultFileUploadService
private ManagedRepositoryAdmin managedRepositoryAdmin; private ManagedRepositoryAdmin managedRepositoryAdmin;
@Inject @Inject
private RepositoryContentFactory repositoryFactory; private ArtifactUtil artifactUtil;
@Inject @Inject
private ArchivaAdministration archivaAdministration; private ArchivaAdministration archivaAdministration;
@ -113,8 +105,7 @@ public class DefaultFileUploadService
private ArchivaTaskScheduler<RepositoryTask> scheduler; private ArchivaTaskScheduler<RepositoryTask> scheduler;
private String getStringValue(MultipartBody multipartBody, String attachmentId) private String getStringValue(MultipartBody multipartBody, String attachmentId)
throws IOException throws IOException {
{
Attachment attachment = multipartBody.getAttachment(attachmentId); Attachment attachment = multipartBody.getAttachment(attachmentId);
return attachment == null ? "" : return attachment == null ? "" :
StringUtils.trim(URLDecoder.decode(IOUtils.toString(attachment.getDataHandler().getInputStream(), "UTF-8"), "UTF-8")); StringUtils.trim(URLDecoder.decode(IOUtils.toString(attachment.getDataHandler().getInputStream(), "UTF-8"), "UTF-8"));
@ -122,11 +113,9 @@ public class DefaultFileUploadService
@Override @Override
public FileMetadata post(MultipartBody multipartBody) public FileMetadata post(MultipartBody multipartBody)
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
try try {
{
String classifier = getStringValue(multipartBody, "classifier"); String classifier = getStringValue(multipartBody, "classifier");
String packaging = getStringValue(multipartBody, "packaging"); String packaging = getStringValue(multipartBody, "packaging");
@ -138,12 +127,9 @@ public class DefaultFileUploadService
// leading to permanent false value for pomFile if using toBoolean(); use , "1", "" // leading to permanent false value for pomFile if using toBoolean(); use , "1", ""
boolean pomFile = false; boolean pomFile = false;
try try {
{
pomFile = BooleanUtils.toBoolean(getStringValue(multipartBody, "pomFile")); pomFile = BooleanUtils.toBoolean(getStringValue(multipartBody, "pomFile"));
} } catch (IllegalArgumentException ex) {
catch ( IllegalArgumentException ex )
{
ArchivaRestServiceException e = new ArchivaRestServiceException("Bad value for boolean pomFile field.", null); ArchivaRestServiceException e = new ArchivaRestServiceException("Bad value for boolean pomFile field.", null);
e.setHttpErrorCode(422); e.setHttpErrorCode(422);
e.setFieldName("pomFile"); e.setFieldName("pomFile");
@ -181,9 +167,7 @@ public class DefaultFileUploadService
fileMetadatas.add(fileMetadata); fileMetadatas.add(fileMetadata);
return fileMetadata; return fileMetadata;
} } catch (IOException e) {
catch ( IOException e )
{
throw new ArchivaRestServiceException(e.getMessage(), throw new ArchivaRestServiceException(e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} }
@ -191,25 +175,29 @@ public class DefaultFileUploadService
} }
/** /**
* FIXME must be per session synchronized not globally * @return The file list from the session.
*
* @return
*/ */
protected synchronized List<FileMetadata> getSessionFilesList() @SuppressWarnings("unchecked")
{ protected List<FileMetadata> getSessionFilesList() {
@SuppressWarnings("unchecked") List<FileMetadata> fileMetadatas = (List<FileMetadata>) httpServletRequest.getSession().getAttribute( FILES_SESSION_KEY ); final HttpSession session = httpServletRequest.getSession();
if ( fileMetadatas == null ) List<FileMetadata> fileMetadata = (List<FileMetadata>) session.getAttribute(FILES_SESSION_KEY);
{ // Double check with synchronization, we assume, that httpServletRequest is
fileMetadatas = new CopyOnWriteArrayList<>(); // fully initialized (no volatile)
httpServletRequest.getSession().setAttribute( FILES_SESSION_KEY, fileMetadatas ); if (fileMetadata == null) {
synchronized (session) {
fileMetadata = (List<FileMetadata>) session.getAttribute(FILES_SESSION_KEY);
if (fileMetadata == null) {
fileMetadata = new CopyOnWriteArrayList<>();
session.setAttribute(FILES_SESSION_KEY, fileMetadata);
} }
return fileMetadatas; }
}
return fileMetadata;
} }
@Override @Override
public Boolean deleteFile(String fileName) public Boolean deleteFile(String fileName)
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
log.debug("Deleting file {}", fileName); log.debug("Deleting file {}", fileName);
// we make sure, that there are no other path components in the filename: // we make sure, that there are no other path components in the filename:
String checkedFileName = Paths.get(fileName).getFileName().toString(); String checkedFileName = Paths.get(fileName).getFileName().toString();
@ -233,11 +221,9 @@ public class DefaultFileUploadService
@Override @Override
public Boolean clearUploadedFiles() public Boolean clearUploadedFiles()
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
List<FileMetadata> fileMetadatas = new ArrayList<>(getSessionFileMetadatas()); List<FileMetadata> fileMetadatas = new ArrayList<>(getSessionFileMetadatas());
for ( FileMetadata fileMetadata : fileMetadatas ) for (FileMetadata fileMetadata : fileMetadatas) {
{
deleteFile(Paths.get(fileMetadata.getServerFileName()).toString()); deleteFile(Paths.get(fileMetadata.getServerFileName()).toString());
} }
getSessionFileMetadatas().clear(); getSessionFileMetadatas().clear();
@ -246,8 +232,7 @@ public class DefaultFileUploadService
@Override @Override
public List<FileMetadata> getSessionFileMetadatas() public List<FileMetadata> getSessionFileMetadatas()
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
@SuppressWarnings("unchecked") List<FileMetadata> fileMetadatas = @SuppressWarnings("unchecked") List<FileMetadata> fileMetadatas =
(List<FileMetadata>) httpServletRequest.getSession().getAttribute(FILES_SESSION_KEY); (List<FileMetadata>) httpServletRequest.getSession().getAttribute(FILES_SESSION_KEY);
@ -280,8 +265,7 @@ public class DefaultFileUploadService
@Override @Override
public Boolean save(String repositoryId, String groupId, String artifactId, String version, String packaging, public Boolean save(String repositoryId, String groupId, String artifactId, String version, String packaging,
boolean generatePom) boolean generatePom)
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
repositoryId = StringUtils.trim(repositoryId); repositoryId = StringUtils.trim(repositoryId);
groupId = StringUtils.trim(groupId); groupId = StringUtils.trim(groupId);
artifactId = StringUtils.trim(artifactId); artifactId = StringUtils.trim(artifactId);
@ -296,49 +280,40 @@ public class DefaultFileUploadService
List<FileMetadata> fileMetadatas = getSessionFilesList(); List<FileMetadata> fileMetadatas = getSessionFilesList();
if ( fileMetadatas == null || fileMetadatas.isEmpty() ) if (fileMetadatas == null || fileMetadatas.isEmpty()) {
{
return Boolean.FALSE; return Boolean.FALSE;
} }
try try {
{
ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repositoryId); ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repositoryId);
if ( managedRepository == null ) if (managedRepository == null) {
{
// TODO i18n ? // TODO i18n ?
throw new ArchivaRestServiceException("Cannot find managed repository with id " + repositoryId, throw new ArchivaRestServiceException("Cannot find managed repository with id " + repositoryId,
Response.Status.BAD_REQUEST.getStatusCode(), null); Response.Status.BAD_REQUEST.getStatusCode(), null);
} }
if ( VersionUtil.isSnapshot( version ) && !managedRepository.isSnapshots() ) if (VersionUtil.isSnapshot(version) && !managedRepository.isSnapshots()) {
{
// TODO i18n ? // TODO i18n ?
throw new ArchivaRestServiceException( throw new ArchivaRestServiceException(
"Managed repository with id " + repositoryId + " do not accept snapshots", "Managed repository with id " + repositoryId + " do not accept snapshots",
Response.Status.BAD_REQUEST.getStatusCode(), null); Response.Status.BAD_REQUEST.getStatusCode(), null);
} }
} } catch (RepositoryAdminException e) {
catch ( RepositoryAdminException e )
{
throw new ArchivaRestServiceException(e.getMessage(), throw new ArchivaRestServiceException(e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} }
// get from the session file with groupId/artifactId // get from the session file with groupId/artifactId
Iterable<FileMetadata> filesToAdd = Iterables.filter( fileMetadatas, new Predicate<FileMetadata>() Iterable<FileMetadata> filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {
{ public boolean apply(FileMetadata fileMetadata) {
public boolean apply( FileMetadata fileMetadata )
{
return fileMetadata != null && !fileMetadata.isPomFile(); return fileMetadata != null && !fileMetadata.isPomFile();
} }
}); });
Iterator<FileMetadata> iterator = filesToAdd.iterator(); Iterator<FileMetadata> iterator = filesToAdd.iterator();
boolean pomGenerated = false; boolean pomGenerated = false;
while ( iterator.hasNext() ) while (iterator.hasNext()) {
{
FileMetadata fileMetadata = iterator.next(); FileMetadata fileMetadata = iterator.next();
log.debug("fileToAdd: {}", fileMetadata); log.debug("fileToAdd: {}", fileMetadata);
saveFile(repositoryId, fileMetadata, generatePom && !pomGenerated, groupId, artifactId, version, saveFile(repositoryId, fileMetadata, generatePom && !pomGenerated, groupId, artifactId, version,
@ -347,18 +322,15 @@ public class DefaultFileUploadService
deleteFile(fileMetadata.getServerFileName()); deleteFile(fileMetadata.getServerFileName());
} }
filesToAdd = Iterables.filter( fileMetadatas, new Predicate<FileMetadata>() filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {
{
@Override @Override
public boolean apply( FileMetadata fileMetadata ) public boolean apply(FileMetadata fileMetadata) {
{
return fileMetadata != null && fileMetadata.isPomFile(); return fileMetadata != null && fileMetadata.isPomFile();
} }
}); });
iterator = filesToAdd.iterator(); iterator = filesToAdd.iterator();
while ( iterator.hasNext() ) while (iterator.hasNext()) {
{
FileMetadata fileMetadata = iterator.next(); FileMetadata fileMetadata = iterator.next();
log.debug("fileToAdd: {}", fileMetadata); log.debug("fileToAdd: {}", fileMetadata);
savePomFile(repositoryId, fileMetadata, groupId, artifactId, version, packaging); savePomFile(repositoryId, fileMetadata, groupId, artifactId, version, packaging);
@ -370,59 +342,40 @@ public class DefaultFileUploadService
protected void savePomFile(String repositoryId, FileMetadata fileMetadata, String groupId, String artifactId, protected void savePomFile(String repositoryId, FileMetadata fileMetadata, String groupId, String artifactId,
String version, String packaging) String version, String packaging)
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
log.debug("Saving POM"); log.debug("Saving POM");
try try {
{
boolean fixChecksums = boolean fixChecksums =
!(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums")); !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
ManagedRepository repoConfig = managedRepositoryAdmin.getManagedRepository( repositoryId ); org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(repositoryId);
ArtifactReference artifactReference = new ArtifactReference(); ArtifactReference artifactReference = createArtifactRef(fileMetadata, groupId, artifactId, version);
artifactReference.setArtifactId( artifactId );
artifactReference.setGroupId( groupId );
artifactReference.setVersion( version );
artifactReference.setClassifier( fileMetadata.getClassifier() );
artifactReference.setType(packaging); artifactReference.setType(packaging);
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repositoryId ); Path pomPath = artifactUtil.getArtifactPath(repoConfig, artifactReference);
Path targetPath = pomPath.getParent();
String artifactPath = repository.toPath( artifactReference ); String pomFilename = pomPath.getFileName().toString();
if (StringUtils.isNotEmpty(fileMetadata.getClassifier())) {
int lastIndex = artifactPath.lastIndexOf( '/' );
String path = artifactPath.substring( 0, lastIndex );
Path targetPath = Paths.get( repoConfig.getLocation(), path );
String pomFilename = artifactPath.substring( lastIndex + 1 );
if ( StringUtils.isNotEmpty( fileMetadata.getClassifier() ) )
{
pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier()); pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier());
} }
pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom"; pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, pomFilename, fixChecksums); copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, pomFilename, fixChecksums);
triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); triggerAuditEvent(repoConfig.getId(), targetPath.resolve(pomFilename).toString(), AuditEvent.UPLOAD_FILE);
queueRepositoryTask(repoConfig.getId(), targetPath.resolve(pomFilename)); queueRepositoryTask(repoConfig.getId(), targetPath.resolve(pomFilename));
log.debug("Finished Saving POM"); log.debug("Finished Saving POM");
} } catch (IOException ie) {
catch ( IOException ie )
{
log.error("IOException for POM {}", ie.getMessage()); log.error("IOException for POM {}", ie.getMessage());
throw new ArchivaRestServiceException("Error encountered while uploading pom file: " + ie.getMessage(), throw new ArchivaRestServiceException("Error encountered while uploading pom file: " + ie.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
} } catch (RepositoryException rep) {
catch ( RepositoryException rep )
{
log.error("RepositoryException for POM {}", rep.getMessage()); log.error("RepositoryException for POM {}", rep.getMessage());
throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(), throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep);
} } catch (RepositoryAdminException e) {
catch ( RepositoryAdminException e )
{
log.error("RepositoryAdminException for POM {}", e.getMessage()); log.error("RepositoryAdminException for POM {}", e.getMessage());
throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
@ -431,30 +384,18 @@ public class DefaultFileUploadService
protected void saveFile(String repositoryId, FileMetadata fileMetadata, boolean generatePom, String groupId, protected void saveFile(String repositoryId, FileMetadata fileMetadata, boolean generatePom, String groupId,
String artifactId, String version, String packaging) String artifactId, String version, String packaging)
throws ArchivaRestServiceException throws ArchivaRestServiceException {
{
log.debug("Saving file"); log.debug("Saving file");
try try {
{
org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(repositoryId); org.apache.archiva.repository.ManagedRepository repoConfig = repositoryRegistry.getManagedRepository(repositoryId);
ArtifactReference artifactReference = new ArtifactReference(); ArtifactReference artifactReference = createArtifactRef(fileMetadata, groupId, artifactId, version);
artifactReference.setArtifactId( artifactId );
artifactReference.setGroupId( groupId );
artifactReference.setVersion( version );
artifactReference.setClassifier( fileMetadata.getClassifier() );
artifactReference.setType( artifactReference.setType(
StringUtils.isEmpty(fileMetadata.getPackaging()) ? packaging : fileMetadata.getPackaging()); StringUtils.isEmpty(fileMetadata.getPackaging()) ? packaging : fileMetadata.getPackaging());
ManagedRepositoryContent repository = repositoryFactory.getManagedRepositoryContent( repoConfig ); Path artifactPath = artifactUtil.getArtifactPath(repoConfig, artifactReference);
Path targetPath = artifactPath.getParent();
String artifactPath = repository.toPath( artifactReference );
int lastIndex = artifactPath.lastIndexOf( '/' );
String path = artifactPath.substring( 0, lastIndex );
Path targetPath = Paths.get(repoConfig.getLocation()).resolve(path);
log.debug("artifactPath: {} found targetPath: {}", artifactPath, targetPath); log.debug("artifactPath: {} found targetPath: {}", artifactPath, targetPath);
@ -465,82 +406,64 @@ public class DefaultFileUploadService
Path versionMetadataFile = targetPath.resolve(MetadataTools.MAVEN_METADATA); Path versionMetadataFile = targetPath.resolve(MetadataTools.MAVEN_METADATA);
ArchivaRepositoryMetadata versionMetadata = getMetadata(versionMetadataFile); ArchivaRepositoryMetadata versionMetadata = getMetadata(versionMetadataFile);
if ( VersionUtil.isSnapshot( version ) ) 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");
fmt.setTimeZone(timezone); fmt.setTimeZone(timezone);
timestamp = fmt.format(lastUpdatedTimestamp); timestamp = fmt.format(lastUpdatedTimestamp);
if ( versionMetadata.getSnapshotVersion() != null ) if (versionMetadata.getSnapshotVersion() != null) {
{
newBuildNumber = versionMetadata.getSnapshotVersion().getBuildNumber() + 1; newBuildNumber = versionMetadata.getSnapshotVersion().getBuildNumber() + 1;
} } else {
else
{
newBuildNumber = 1; newBuildNumber = 1;
} }
} }
if ( !Files.exists(targetPath) ) if (!Files.exists(targetPath)) {
{
Files.createDirectories(targetPath); Files.createDirectories(targetPath);
} }
String filename = artifactPath.substring( lastIndex + 1 ); String filename = artifactPath.getFileName().toString();
if ( VersionUtil.isSnapshot( version ) ) if (VersionUtil.isSnapshot(version)) {
{
filename = filename.replaceAll(VersionUtil.SNAPSHOT, timestamp + "-" + newBuildNumber); filename = filename.replaceAll(VersionUtil.SNAPSHOT, timestamp + "-" + newBuildNumber);
} }
boolean fixChecksums = boolean fixChecksums =
!(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums")); !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
try try {
{
Path targetFile = targetPath.resolve(filename); Path targetFile = targetPath.resolve(filename);
if ( Files.exists(targetFile) && !VersionUtil.isSnapshot( version ) && repoConfig.blocksRedeployments()) if (Files.exists(targetFile) && !VersionUtil.isSnapshot(version) && repoConfig.blocksRedeployments()) {
{
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.",
Response.Status.BAD_REQUEST.getStatusCode(), null); Response.Status.BAD_REQUEST.getStatusCode(), null);
} } else {
else
{
copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, filename, fixChecksums); copyFile(Paths.get(fileMetadata.getServerFileName()), targetPath, filename, fixChecksums);
triggerAuditEvent( repository.getId(), path + "/" + filename, AuditEvent.UPLOAD_FILE ); triggerAuditEvent(repoConfig.getId(), artifactPath.toString(), AuditEvent.UPLOAD_FILE);
queueRepositoryTask( repository.getId(), targetFile ); queueRepositoryTask(repoConfig.getId(), targetFile);
} }
} } catch (IOException ie) {
catch ( IOException ie )
{
log.error("IOException copying file: {}", ie.getMessage(), ie); log.error("IOException copying file: {}", ie.getMessage(), ie);
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.",
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
} }
if ( generatePom ) if (generatePom) {
{
String pomFilename = filename; String pomFilename = filename;
if ( StringUtils.isNotEmpty( fileMetadata.getClassifier() ) ) if (StringUtils.isNotEmpty(fileMetadata.getClassifier())) {
{
pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier()); pomFilename = StringUtils.remove(pomFilename, "-" + fileMetadata.getClassifier());
} }
pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom"; pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
try try {
{
Path generatedPomFile = Path generatedPomFile =
createPom(targetPath, pomFilename, fileMetadata, groupId, artifactId, version, packaging); createPom(targetPath, pomFilename, fileMetadata, groupId, artifactId, version, packaging);
triggerAuditEvent( repoConfig.getId(), path + "/" + pomFilename, AuditEvent.UPLOAD_FILE ); triggerAuditEvent(repoConfig.getId(), targetPath.resolve(pomFilename).toString(), AuditEvent.UPLOAD_FILE);
if ( fixChecksums ) if (fixChecksums) {
{
fixChecksums(generatedPomFile); fixChecksums(generatedPomFile);
} }
queueRepositoryTask(repoConfig.getId(), generatedPomFile); queueRepositoryTask(repoConfig.getId(), generatedPomFile);
} } catch (IOException ie) {
catch ( IOException ie )
{
throw new ArchivaRestServiceException( throw new ArchivaRestServiceException(
"Error encountered while writing pom file: " + ie.getMessage(), "Error encountered while writing pom file: " + ie.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ie);
@ -548,58 +471,52 @@ public class DefaultFileUploadService
} }
// explicitly update only if metadata-updater consumer is not enabled! // explicitly update only if metadata-updater consumer is not enabled!
if ( !archivaAdministration.getKnownContentConsumers().contains( "metadata-updater" ) ) if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
{
updateProjectMetadata(targetPath.toAbsolutePath().toString(), lastUpdatedTimestamp, timestamp, newBuildNumber, updateProjectMetadata(targetPath.toAbsolutePath().toString(), lastUpdatedTimestamp, timestamp, newBuildNumber,
fixChecksums, fileMetadata, groupId, artifactId, version, packaging); fixChecksums, fileMetadata, groupId, artifactId, version, packaging);
if ( VersionUtil.isSnapshot( version ) ) if (VersionUtil.isSnapshot(version)) {
{
updateVersionMetadata(versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp, updateVersionMetadata(versionMetadata, versionMetadataFile, lastUpdatedTimestamp, timestamp,
newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version, newBuildNumber, fixChecksums, fileMetadata, groupId, artifactId, version,
packaging); packaging);
} }
} }
} } catch (RepositoryNotFoundException re) {
catch ( RepositoryNotFoundException re )
{
log.error("RepositoryNotFoundException during save {}", re.getMessage()); log.error("RepositoryNotFoundException during save {}", re.getMessage());
re.printStackTrace(); re.printStackTrace();
throw new ArchivaRestServiceException("Target repository cannot be found: " + re.getMessage(), throw new ArchivaRestServiceException("Target repository cannot be found: " + re.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), re); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), re);
} } catch (RepositoryException rep) {
catch ( RepositoryException rep )
{
log.error("RepositoryException during save {}", rep.getMessage()); log.error("RepositoryException during save {}", rep.getMessage());
throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(), throw new ArchivaRestServiceException("Repository exception: " + rep.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), rep);
} } catch (RepositoryAdminException e) {
catch ( RepositoryAdminException e )
{
log.error("RepositoryAdminException during save {}", e.getMessage()); log.error("RepositoryAdminException during save {}", e.getMessage());
throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(), throw new ArchivaRestServiceException("RepositoryAdmin exception: " + e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} } catch (IOException e) {
catch ( IOException e )
{
log.error("IOException during save {}", e.getMessage()); log.error("IOException during save {}", e.getMessage());
throw new ArchivaRestServiceException("Repository exception " + e.getMessage(), throw new ArchivaRestServiceException("Repository exception " + e.getMessage(),
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
} }
} }
private ArchivaRepositoryMetadata getMetadata( Path metadataFile ) private ArtifactReference createArtifactRef(FileMetadata fileMetadata, String groupId, String artifactId, String version) {
throws RepositoryMetadataException ArtifactReference artifactReference = new ArtifactReference();
{ artifactReference.setArtifactId(artifactId);
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata(); artifactReference.setGroupId(groupId);
if ( Files.exists(metadataFile) ) artifactReference.setVersion(version);
{ artifactReference.setClassifier(fileMetadata.getClassifier());
try return artifactReference;
{
metadata = MavenMetadataReader.read( metadataFile );
} }
catch ( XMLException e )
{ private ArchivaRepositoryMetadata getMetadata(Path metadataFile)
throws RepositoryMetadataException {
ArchivaRepositoryMetadata metadata = new ArchivaRepositoryMetadata();
if (Files.exists(metadataFile)) {
try {
metadata = MavenMetadataReader.read(metadataFile);
} catch (XMLException e) {
throw new RepositoryMetadataException(e.getMessage(), e); throw new RepositoryMetadataException(e.getMessage(), e);
} }
} }
@ -608,8 +525,7 @@ public class DefaultFileUploadService
private Path createPom(Path targetPath, String filename, FileMetadata fileMetadata, String groupId, private Path createPom(Path targetPath, String filename, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging) 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(groupId); projectModel.setGroupId(groupId);
@ -620,48 +536,40 @@ public class DefaultFileUploadService
Path pomFile = targetPath.resolve(filename); Path pomFile = targetPath.resolve(filename);
MavenXpp3Writer writer = new MavenXpp3Writer(); MavenXpp3Writer writer = new MavenXpp3Writer();
try (FileWriter w = new FileWriter( pomFile.toFile() )) try (FileWriter w = new FileWriter(pomFile.toFile())) {
{
writer.write(w, projectModel); writer.write(w, projectModel);
} }
return pomFile; return pomFile;
} }
private void fixChecksums( Path file ) private void fixChecksums(Path file) {
{
ChecksummedFile checksum = new ChecksummedFile(file); ChecksummedFile checksum = new ChecksummedFile(file);
checksum.fixChecksums(algorithms); checksum.fixChecksums(algorithms);
} }
private void queueRepositoryTask( String repositoryId, Path localFile ) private void queueRepositoryTask(String repositoryId, Path localFile) {
{
RepositoryTask task = new RepositoryTask(); RepositoryTask task = new RepositoryTask();
task.setRepositoryId(repositoryId); task.setRepositoryId(repositoryId);
task.setResourceFile(localFile); task.setResourceFile(localFile);
task.setUpdateRelatedArtifacts(true); task.setUpdateRelatedArtifacts(true);
task.setScanAll(false); task.setScanAll(false);
try try {
{
scheduler.queueTask(task); scheduler.queueTask(task);
} } catch (TaskQueueException e) {
catch ( TaskQueueException e )
{
log.error("Unable to queue repository task to execute consumers on resource file ['{}" log.error("Unable to queue repository task to execute consumers on resource file ['{}"
+ "'].", localFile.getFileName()); + "'].", localFile.getFileName());
} }
} }
private void copyFile(Path sourceFile, Path targetPath, String targetFilename, boolean fixChecksums) private void copyFile(Path sourceFile, Path targetPath, String targetFilename, boolean fixChecksums)
throws IOException throws IOException {
{
Files.copy(sourceFile, targetPath.resolve(targetFilename), StandardCopyOption.REPLACE_EXISTING, Files.copy(sourceFile, targetPath.resolve(targetFilename), StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES); StandardCopyOption.COPY_ATTRIBUTES);
if ( fixChecksums ) if (fixChecksums) {
{
fixChecksums(targetPath.resolve(targetFilename)); fixChecksums(targetPath.resolve(targetFilename));
} }
} }
@ -672,8 +580,7 @@ public class DefaultFileUploadService
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, String groupId, boolean fixChecksums, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging) String artifactId, String version, String packaging)
throws RepositoryMetadataException throws RepositoryMetadataException {
{
List<String> availableVersions = new ArrayList<>(); List<String> availableVersions = new ArrayList<>();
String latestVersion = version; String latestVersion = version;
@ -682,34 +589,28 @@ public class DefaultFileUploadService
ArchivaRepositoryMetadata projectMetadata = getMetadata(projectMetadataFile); ArchivaRepositoryMetadata projectMetadata = getMetadata(projectMetadataFile);
if ( Files.exists(projectMetadataFile) ) if (Files.exists(projectMetadataFile)) {
{
availableVersions = projectMetadata.getAvailableVersions(); availableVersions = projectMetadata.getAvailableVersions();
Collections.sort(availableVersions, VersionComparator.getInstance()); Collections.sort(availableVersions, VersionComparator.getInstance());
if ( !availableVersions.contains( version ) ) if (!availableVersions.contains(version)) {
{
availableVersions.add(version); availableVersions.add(version);
} }
latestVersion = availableVersions.get(availableVersions.size() - 1); latestVersion = availableVersions.get(availableVersions.size() - 1);
} } else {
else
{
availableVersions.add(version); availableVersions.add(version);
projectMetadata.setGroupId(groupId); projectMetadata.setGroupId(groupId);
projectMetadata.setArtifactId(artifactId); projectMetadata.setArtifactId(artifactId);
} }
if ( projectMetadata.getGroupId() == null ) if (projectMetadata.getGroupId() == null) {
{
projectMetadata.setGroupId(groupId); projectMetadata.setGroupId(groupId);
} }
if ( projectMetadata.getArtifactId() == null ) if (projectMetadata.getArtifactId() == null) {
{
projectMetadata.setArtifactId(artifactId); projectMetadata.setArtifactId(artifactId);
} }
@ -717,15 +618,13 @@ public class DefaultFileUploadService
projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp); projectMetadata.setLastUpdatedTimestamp(lastUpdatedTimestamp);
projectMetadata.setAvailableVersions(availableVersions); projectMetadata.setAvailableVersions(availableVersions);
if ( !VersionUtil.isSnapshot( version ) ) if (!VersionUtil.isSnapshot(version)) {
{
projectMetadata.setReleasedVersion(latestVersion); projectMetadata.setReleasedVersion(latestVersion);
} }
RepositoryMetadataWriter.write(projectMetadata, projectMetadataFile); RepositoryMetadataWriter.write(projectMetadata, projectMetadataFile);
if ( fixChecksums ) if (fixChecksums) {
{
fixChecksums(projectMetadataFile); fixChecksums(projectMetadataFile);
} }
} }
@ -738,17 +637,14 @@ public class DefaultFileUploadService
Date lastUpdatedTimestamp, String timestamp, int buildNumber, Date lastUpdatedTimestamp, String timestamp, int buildNumber,
boolean fixChecksums, FileMetadata fileMetadata, String groupId, boolean fixChecksums, FileMetadata fileMetadata, String groupId,
String artifactId, String version, String packaging) String artifactId, String version, String packaging)
throws RepositoryMetadataException throws RepositoryMetadataException {
{ if (!Files.exists(metadataFile)) {
if ( !Files.exists(metadataFile) )
{
metadata.setGroupId(groupId); metadata.setGroupId(groupId);
metadata.setArtifactId(artifactId); metadata.setArtifactId(artifactId);
metadata.setVersion(version); metadata.setVersion(version);
} }
if ( metadata.getSnapshotVersion() == null ) if (metadata.getSnapshotVersion() == null) {
{
metadata.setSnapshotVersion(new SnapshotVersion()); metadata.setSnapshotVersion(new SnapshotVersion());
} }
@ -758,8 +654,7 @@ public class DefaultFileUploadService
RepositoryMetadataWriter.write(metadata, metadataFile); RepositoryMetadataWriter.write(metadata, metadataFile);
if ( fixChecksums ) if (fixChecksums) {
{
fixChecksums(metadataFile); fixChecksums(metadataFile);
} }
} }