private Optional<Artifact> uploadArtifact(final String filename) { try { return Optional.ofNullable(artifactManagement.create(inputStream, fileUploadId.getSoftwareModuleId(), filename, null, null, true, mimeType, -1)); } catch (final ArtifactUploadFailedException | InvalidSHA1HashException | InvalidMD5HashException e) { LOG.error("Failed to transfer file to repository", e); return Optional.empty(); } }
private void handleUploadFailure(final Artifact artifact) { Exception exception; int tries = 0; do { try { artifactManagement.delete(artifact.getId()); return; } catch (final RuntimeException e) { exception = e; tries++; } } while (tries < 5); LOG.error("Failed to delete artifact from repository after upload was interrupted", exception); } }
private void deleteGridFsArtifacts(final JpaSoftwareModule swModule) { for (final Artifact localArtifact : swModule.getArtifacts()) { artifactManagement.clearArtifactBinary(localArtifact.getSha1Hash(), swModule.getId()); } }
private Optional<Artifact> findArtifactByFileResource(final FileResource fileResource) { if (fileResource == null) { return Optional.empty(); } if (fileResource.getSha1() != null) { return artifactManagement.findFirstBySHA1(fileResource.getSha1()); } if (fileResource.getFilename() != null) { return artifactManagement.getByFilename(fileResource.getFilename()); } if (fileResource.getArtifactId() != null) { return artifactManagement.get(fileResource.getArtifactId()); } if (fileResource.getSoftwareModuleFilenameResource() != null) { return artifactManagement.getByFilenameAndSoftwareModule( fileResource.getSoftwareModuleFilenameResource().getFilename(), fileResource.getSoftwareModuleFilenameResource().getSoftwareModuleId()); } return Optional.empty(); }
/** * Handles the GET request for downloading an artifact. * * @param softwareModuleId * of the parent SoftwareModule * @param artifactId * of the related Artifact * * @return responseEntity with status ok if successful */ @Override public ResponseEntity<InputStream> downloadArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("artifactId") final Long artifactId) { final SoftwareModule module = softwareModuleManagement.get(softwareModuleId) .orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, softwareModuleId)); final Artifact artifact = module.getArtifact(artifactId) .orElseThrow(() -> new EntityNotFoundException(Artifact.class, artifactId)); final AbstractDbArtifact file = artifactManagement.loadArtifactBinary(artifact.getSha1Hash()) .orElseThrow(() -> new ArtifactBinaryNotFoundException(artifact.getSha1Hash())); final HttpServletRequest request = requestResponseContextHolder.getHttpServletRequest(); final String ifMatch = request.getHeader(HttpHeaders.IF_MATCH); if (ifMatch != null && !HttpUtil.matchesHttpHeader(ifMatch, artifact.getSha1Hash())) { return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED); } return FileStreamingUtil.writeFileResponse(file, artifact.getFilename(), artifact.getCreatedAt(), requestResponseContextHolder.getHttpServletResponse(), request, null); }
@Override protected List<Artifact> loadBeans(final int startIndex, final int count) { Page<Artifact> artifactBeans; if (startIndex == 0 && firstPagetArtifacts != null) { artifactBeans = firstPagetArtifacts; } else { artifactBeans = getArtifactManagement() .findBySoftwareModule(new OffsetBasedPageRequest(startIndex, count, sort), baseSwModuleId); } return artifactBeans.getContent(); }
final Artifact artifact = module.getArtifactByFilename(fileName).get(); final AbstractDbArtifact file = artifactManagement.loadArtifactBinary(artifact.getSha1Hash()) .orElseThrow(() -> new ArtifactBinaryNotFoundException(artifact.getSha1Hash()));
@Override public int size() { long size = 0; if (baseSwModuleId != null) { firstPagetArtifacts = getArtifactManagement() .findBySoftwareModule(PageRequest.of(0, SPUIDefinitions.PAGE_SIZE, sort), baseSwModuleId); size = firstPagetArtifacts.getTotalElements(); } if (size > Integer.MAX_VALUE) { size = Integer.MAX_VALUE; } return (int) size; }
/** * Creates {@link Artifact}s for given {@link SoftwareModule} with a small * text payload. * * @param moduleId * the {@link Artifact}s belong to. * * @return {@link Artifact} entity. */ public List<Artifact> createArtifacts(final Long moduleId) { final List<Artifact> artifacts = new ArrayList<>(); for (int i = 0; i < 3; i++) { final String artifactData = "some test data" + i; final InputStream stubInputStream = IOUtils.toInputStream(artifactData, Charset.forName("UTF-8")); artifacts.add( artifactManagement.create(stubInputStream, moduleId, "filename" + i, false, artifactData.length())); } return artifacts; }
private void handleUploadFailure(final Artifact artifact) { Exception exception; int tries = 0; do { try { artifactManagement.delete(artifact.getId()); return; } catch (final RuntimeException e) { exception = e; tries++; } } while (tries < 5); LOG.error("Failed to delete artifact from repository after upload was interrupted", exception); } }
final Artifact artifact = module.getArtifactByFilename(fileName).get(); final AbstractDbArtifact file = artifactManagement.loadArtifactBinary(artifact.getSha1Hash()) .orElseThrow(() -> new ArtifactBinaryNotFoundException(artifact.getSha1Hash()));
@Override protected List<Artifact> loadBeans(final int startIndex, final int count) { Page<Artifact> artifactBeans; if (startIndex == 0 && firstPagetArtifacts != null) { artifactBeans = firstPagetArtifacts; } else { artifactBeans = getArtifactManagement() .findBySoftwareModule(new OffsetBasedPageRequest(startIndex, count, sort), baseSwModuleId); } return artifactBeans.getContent(); }
private void deleteGridFsArtifacts(final JpaSoftwareModule swModule) { for (final Artifact localArtifact : swModule.getArtifacts()) { artifactManagement.clearArtifactBinary(localArtifact.getSha1Hash(), swModule.getId()); } }
private Optional<Artifact> uploadArtifact(final String filename) { try { return Optional.ofNullable(artifactManagement.create(new ArtifactUpload(inputStream, fileUploadId.getSoftwareModuleId(), filename, null, null, true, mimeType, -1))); } catch (final ArtifactUploadFailedException | InvalidSHA1HashException | InvalidMD5HashException e) { LOG.error("Failed to transfer file to repository", e); return Optional.empty(); } }
@Override @ResponseBody public ResponseEntity<Void> deleteArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId, @PathVariable("artifactId") final Long artifactId) { findSoftwareModuleWithExceptionIfNotFound(softwareModuleId, artifactId); artifactManagement.delete(artifactId); return ResponseEntity.ok().build(); }
@Override public int size() { long size = 0; if (baseSwModuleId != null) { firstPagetArtifacts = getArtifactManagement() .findBySoftwareModule(new PageRequest(0, SPUIDefinitions.PAGE_SIZE, sort), baseSwModuleId); size = firstPagetArtifacts.getTotalElements(); } if (size > Integer.MAX_VALUE) { size = Integer.MAX_VALUE; } return (int) size; }
/** * Creates {@link Artifact}s for given {@link SoftwareModule} with a small * text payload. * * @param moduleId * the {@link Artifact}s belong to. * * @return {@link Artifact} entity. */ public List<Artifact> createArtifacts(final Long moduleId) { final List<Artifact> artifacts = new ArrayList<>(); for (int i = 0; i < 3; i++) { final String artifactData = "some test data" + i; final InputStream stubInputStream = IOUtils.toInputStream(artifactData, Charset.forName("UTF-8")); artifacts.add( artifactManagement.create(new ArtifactUpload(stubInputStream, moduleId, "filename" + i, false, artifactData.length()))); } return artifacts; }
private void confirmAndDeleteArtifact(final Long id, final String fileName) { final ConfirmationDialog confirmDialog = new ConfirmationDialog( i18n.getMessage("caption.delete.artifact.confirmbox"), i18n.getMessage("message.delete.artifact", fileName), i18n.getMessage(UIMessageIdProvider.BUTTON_OK), i18n.getMessage(UIMessageIdProvider.BUTTON_CANCEL), ok -> { if (ok) { artifactManagement.delete(id); uINotification.displaySuccess(i18n.getMessage("message.artifact.deleted", fileName)); final Optional<SoftwareModule> softwareModule = findSelectedSoftwareModule(); populateArtifactDetails(softwareModule.orElse(null)); } }); UI.getCurrent().addWindow(confirmDialog.getWindow()); confirmDialog.getWindow().bringToFront(); }
@Override public ResponseEntity<MgmtArtifact> uploadArtifact(@PathVariable("softwareModuleId") final Long softwareModuleId, @RequestPart("file") final MultipartFile file, @RequestParam(value = "filename", required = false) final String optionalFileName, @RequestParam(value = "md5sum", required = false) final String md5Sum, @RequestParam(value = "sha1sum", required = false) final String sha1Sum) { if (file.isEmpty()) { return ResponseEntity.badRequest().build(); } String fileName = optionalFileName; if (fileName == null) { fileName = file.getOriginalFilename(); } try (InputStream in = file.getInputStream()) { final Artifact result = artifactManagement.create(new ArtifactUpload(in, softwareModuleId, fileName, md5Sum == null ? null : md5Sum.toLowerCase(), sha1Sum == null ? null : sha1Sum.toLowerCase(), false, file.getContentType(), file.getSize())); final MgmtArtifact reponse = MgmtSoftwareModuleMapper.toResponse(result); MgmtSoftwareModuleMapper.addLinks(result, reponse); return ResponseEntity.status(HttpStatus.CREATED).body(reponse); } catch (final IOException e) { LOG.error("Failed to store artifact", e); return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } }
private void confirmAndDeleteArtifact(final Long id, final String fileName) { final ConfirmationDialog confirmDialog = new ConfirmationDialog( i18n.getMessage("caption.delete.artifact.confirmbox"), i18n.getMessage("message.delete.artifact", new Object[] { fileName }), i18n.getMessage(UIMessageIdProvider.BUTTON_OK), i18n.getMessage(UIMessageIdProvider.BUTTON_CANCEL), ok -> { if (ok) { artifactManagement.delete(id); uINotification.displaySuccess(i18n.getMessage("message.artifact.deleted", fileName)); final Optional<SoftwareModule> softwareModule = findSelectedSoftwareModule(); populateArtifactDetails(softwareModule.orElse(null)); } }); UI.getCurrent().addWindow(confirmDialog.getWindow()); confirmDialog.getWindow().bringToFront(); }