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(); } }
ConcurrencyFailureException.class }, maxAttempts = Constants.TX_RT_MAX, backoff = @Backoff(delay = Constants.TX_RT_DELAY)) public Artifact create(final ArtifactUpload artifactUpload) { final String filename = artifactUpload.getFilename(); final long moduleId = artifactUpload.getModuleId(); AbstractDbArtifact result = null; final Artifact existing = checkForExistingArtifact(filename, artifactUpload.overrideExisting(), softwareModule); assertMaxArtifactSizeQuota(filename, moduleId, artifactUpload.getFilesize()); assertMaxArtifactStorageQuota(filename, artifactUpload.getFilesize()); result = artifactRepository.store(tenantAware.getCurrentTenant(), artifactUpload.getInputStream(), filename, artifactUpload.getContentType(), new DbArtifactHash(artifactUpload.getProvidedSha1Sum(), artifactUpload.getProvidedMd5Sum())); } catch (final ArtifactStoreException e) { throw new ArtifactUploadFailedException(e);
/** * 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; }
@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); } }
protected DistributionSet createDistributionSet() { DistributionSet distributionSet = testdataFactory.createDistributionSet(""); distributionSet = distributionSetManagement.update(entityFactory.distributionSet() .update(distributionSet.getId()).description("The descption of the distribution set.")); distributionSet.getModules().forEach(module -> { final byte[] random = RandomStringUtils.random(5).getBytes(); artifactManagement .create(new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "file1", false, 0)); softwareModuleManagement.update(entityFactory.softwareModule().update(module.getId()) .description("Description of the software module")); }); return distributionSet; }
@Test @Description("Handles the DELETE request for a single SoftwareModule within SP. Required Permission: " + SpPermission.DELETE_REPOSITORY) public void deleteArtifact() throws Exception { final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); final byte random[] = RandomStringUtils.random(5).getBytes(); final Artifact artifact = artifactManagement .create(new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, 0)); mockMvc.perform(delete( MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts/{artifactId}", sm.getId(), artifact.getId())).andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) .andDo(this.document.document(pathParameters( parameterWithName("softwareModuleId").description(ApiModelPropertiesGeneric.ITEM_ID), parameterWithName("artifactId").description(ApiModelPropertiesGeneric.ITEM_ID)))); }
@Test @Description("Handles the GET request for downloading an artifact. Required Permission: " + SpPermission.READ_REPOSITORY) public void getDownloadArtifact() throws Exception { final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); final byte random[] = RandomStringUtils.random(5).getBytes(); final Artifact artifact = artifactManagement .create(new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, 0)); mockMvc.perform(get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts/{artifactId}/download", sm.getId(), artifact.getId()) .accept(MediaType.APPLICATION_OCTET_STREAM)) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM)) .andDo(this.document.document(pathParameters( parameterWithName("softwareModuleId").description(ApiModelPropertiesGeneric.ITEM_ID), parameterWithName("artifactId").description(ApiModelPropertiesGeneric.ITEM_ID)))); }
new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "binary.tgz", false, 0)); artifactManagement.create( new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "file.signature", false, 0)); });
@Test @Description("Handles the GET request of retrieving all meta data of artifacts assigned to a software module. Required Permission: " + SpPermission.READ_REPOSITORY) public void getArtifacts() throws Exception { final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); final byte random[] = RandomStringUtils.random(5).getBytes(); artifactManagement.create(new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, 0)); mockMvc.perform( get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts", sm.getId())) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON_UTF8)) .andDo(this.document.document( pathParameters( parameterWithName("softwareModuleId").description(ApiModelPropertiesGeneric.ITEM_ID)), responseFields(fieldWithPath("[].id").description(ApiModelPropertiesGeneric.ITEM_ID), fieldWithPath("[].size").description(MgmtApiModelProperties.ARTIFACT_SIZE), fieldWithPath("[].createdBy").description(ApiModelPropertiesGeneric.CREATED_BY), fieldWithPath("[].createdAt").description(ApiModelPropertiesGeneric.CREATED_AT), fieldWithPath("[].lastModifiedBy") .description(ApiModelPropertiesGeneric.LAST_MODIFIED_BY), fieldWithPath("[].lastModifiedAt") .description(ApiModelPropertiesGeneric.LAST_MODIFIED_AT), fieldWithPath("[].hashes.md5").description(MgmtApiModelProperties.ARTIFACT_HASHES_MD5), fieldWithPath("[].hashes.sha1") .description(MgmtApiModelProperties.ARTIFACT_HASHES_SHA1), fieldWithPath("[].providedFilename") .description(MgmtApiModelProperties.ARTIFACT_PROVIDED_FILENAME), fieldWithPath("[]._links.self").ignored()))); }
.create(new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "binaryFile", false, 0));
@Test @Description("Handles the GET request of retrieving a single Artifact meta data request. Required Permission: " + SpPermission.READ_REPOSITORY) public void getArtifact() throws Exception { final SoftwareModule sm = testdataFactory.createSoftwareModuleOs(); final byte random[] = RandomStringUtils.random(5).getBytes(); final Artifact artifact = artifactManagement .create(new ArtifactUpload(new ByteArrayInputStream(random), sm.getId(), "file1", false, 0)); mockMvc.perform( get(MgmtRestConstants.SOFTWAREMODULE_V1_REQUEST_MAPPING + "/{softwareModuleId}/artifacts/{artifactId}", sm.getId(), artifact.getId())) .andDo(MockMvcResultPrinter.print()).andExpect(status().isOk()) .andExpect(content().contentType(MediaTypes.HAL_JSON_UTF8)) .andDo(this.document.document( pathParameters( parameterWithName("softwareModuleId").description(ApiModelPropertiesGeneric.ITEM_ID), parameterWithName("artifactId").description(ApiModelPropertiesGeneric.ITEM_ID)), responseFields(fieldWithPath("id").description(ApiModelPropertiesGeneric.ITEM_ID), fieldWithPath("size").description(MgmtApiModelProperties.ARTIFACT_SIZE), fieldWithPath("createdBy").description(ApiModelPropertiesGeneric.CREATED_BY), fieldWithPath("createdAt").description(ApiModelPropertiesGeneric.CREATED_AT), fieldWithPath("lastModifiedBy").description(ApiModelPropertiesGeneric.LAST_MODIFIED_BY), fieldWithPath("lastModifiedAt").description(ApiModelPropertiesGeneric.LAST_MODIFIED_AT), fieldWithPath("hashes.md5").description(MgmtApiModelProperties.ARTIFACT_HASHES_MD5), fieldWithPath("hashes.sha1").description(MgmtApiModelProperties.ARTIFACT_HASHES_SHA1), fieldWithPath("providedFilename") .description(MgmtApiModelProperties.ARTIFACT_PROVIDED_FILENAME), fieldWithPath("_links.self").ignored(), fieldWithPath("_links.download") .description(MgmtApiModelProperties.ARTIFACT_DOWNLOAD_LINK)))); }
new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "binary.tgz", false, 0)); artifactManagement.create( new ArtifactUpload(new ByteArrayInputStream(random), module.getId(), "file.signature", false, 0)); });