use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.
the class DefaultFileUploadService method save.
@Override
public Boolean save(String repositoryId, String groupId, String artifactId, String version, String packaging, boolean generatePom) throws ArchivaRestServiceException {
repositoryId = StringUtils.trim(repositoryId);
groupId = StringUtils.trim(groupId);
artifactId = StringUtils.trim(artifactId);
version = StringUtils.trim(version);
packaging = StringUtils.trim(packaging);
List<FileMetadata> fileMetadatas = getSessionFilesList();
if (fileMetadatas == null || fileMetadatas.isEmpty()) {
return Boolean.FALSE;
}
try {
ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repositoryId);
if (managedRepository == null) {
// TODO i18n ?
throw new ArchivaRestServiceException("Cannot find managed repository with id " + repositoryId, Response.Status.BAD_REQUEST.getStatusCode(), null);
}
if (VersionUtil.isSnapshot(version) && !managedRepository.isSnapshots()) {
// TODO i18n ?
throw new ArchivaRestServiceException("Managed repository with id " + repositoryId + " do not accept snapshots", Response.Status.BAD_REQUEST.getStatusCode(), null);
}
} catch (RepositoryAdminException e) {
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
// get from the session file with groupId/artifactId
Iterable<FileMetadata> filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {
public boolean apply(FileMetadata fileMetadata) {
return fileMetadata != null && !fileMetadata.isPomFile();
}
});
Iterator<FileMetadata> iterator = filesToAdd.iterator();
boolean pomGenerated = false;
while (iterator.hasNext()) {
FileMetadata fileMetadata = iterator.next();
log.debug("fileToAdd: {}", fileMetadata);
saveFile(repositoryId, fileMetadata, generatePom && !pomGenerated, groupId, artifactId, version, packaging);
pomGenerated = true;
deleteFile(fileMetadata.getServerFileName());
}
filesToAdd = Iterables.filter(fileMetadatas, new Predicate<FileMetadata>() {
@Override
public boolean apply(FileMetadata fileMetadata) {
return fileMetadata != null && fileMetadata.isPomFile();
}
});
iterator = filesToAdd.iterator();
while (iterator.hasNext()) {
FileMetadata fileMetadata = iterator.next();
log.debug("fileToAdd: {}", fileMetadata);
savePomFile(repositoryId, fileMetadata, groupId, artifactId, version, packaging);
deleteFile(fileMetadata.getServerFileName());
}
return Boolean.TRUE;
}
use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.
the class DefaultFileUploadService method post.
@Override
public FileMetadata post(MultipartBody multipartBody) throws ArchivaRestServiceException {
try {
String classifier = getStringValue(multipartBody, "classifier");
String packaging = getStringValue(multipartBody, "packaging");
// skygo: http header form pomFile was once sending 1 for true and void for false
// leading to permanent false value for pomFile if using toBoolean(); use , "1", ""
boolean pomFile = BooleanUtils.toBoolean(getStringValue(multipartBody, "pomFile"));
Attachment file = multipartBody.getAttachment("files[]");
// Content-Disposition: form-data; name="files[]"; filename="org.apache.karaf.features.command-2.2.2.jar"
String fileName = file.getContentDisposition().getParameter("filename");
Path tmpFile = Files.createTempFile("upload-artifact", ".tmp");
tmpFile.toFile().deleteOnExit();
IOUtils.copy(file.getDataHandler().getInputStream(), new FileOutputStream(tmpFile.toFile()));
FileMetadata fileMetadata = new FileMetadata(fileName, Files.size(tmpFile), "theurl");
fileMetadata.setServerFileName(tmpFile.toString());
fileMetadata.setClassifier(classifier);
fileMetadata.setDeleteUrl(tmpFile.getFileName().toString());
fileMetadata.setPomFile(pomFile);
fileMetadata.setPackaging(packaging);
log.info("uploading file: {}", fileMetadata);
List<FileMetadata> fileMetadatas = getSessionFilesList();
fileMetadatas.add(fileMetadata);
return fileMetadata;
} catch (IOException e) {
throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e);
}
}
use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.
the class DefaultFileUploadService method clearUploadedFiles.
@Override
public Boolean clearUploadedFiles() throws ArchivaRestServiceException {
List<FileMetadata> fileMetadatas = new ArrayList<>(getSessionFileMetadatas());
for (FileMetadata fileMetadata : fileMetadatas) {
deleteFile(Paths.get(fileMetadata.getServerFileName()).toString());
}
getSessionFileMetadatas().clear();
return Boolean.TRUE;
}
use of org.apache.archiva.web.model.FileMetadata in project archiva by apache.
the class DefaultFileUploadService method deleteFile.
@Override
public Boolean deleteFile(String fileName) throws ArchivaRestServiceException {
Path file = SystemUtils.getJavaIoTmpDir().toPath().resolve(fileName);
log.debug("delete file:{},exists:{}", file, Files.exists(file));
boolean removed = getSessionFileMetadatas().remove(new FileMetadata(fileName));
// try with full name as ui only know the file name
if (!removed) {
/* unused */
getSessionFileMetadatas().remove(new FileMetadata(file.toString()));
}
try {
Files.deleteIfExists(file);
} catch (IOException e) {
log.error("Could not delete file {}: {}", file, e.getMessage(), e);
}
return Boolean.FALSE;
}
Aggregations