private Artifact storeArtifactMetadata( final SoftwareModule softwareModule, final String providedFilename, final DbArtifact result, final Artifact existing) { JpaArtifact artifact = (JpaArtifact) existing; if (existing == null) { artifact = new JpaArtifact(result.getHashes().getSha1(), providedFilename, softwareModule); } artifact.setMd5Hash(result.getHashes().getMd5()); artifact.setSha1Hash(result.getHashes().getSha1()); artifact.setSize(result.getSize()); LOG.debug("storing new artifact into repository {}", artifact); return localArtifactRepository.save(artifact); }
/** * Handles the GET request for downloading an artifact. * * @param downloadId the generated download id * @param response of the servlet * @return {@link ResponseEntity} with status {@link HttpStatus#OK} if successful */ @RequestMapping(method = RequestMethod.GET, value = RestConstants.DOWNLOAD_ID_V1_REQUEST_MAPPING) @ResponseBody public ResponseEntity<Void> downloadArtifactByDownloadId( @PathVariable final String downloadId, final HttpServletResponse response) { try { final ValueWrapper cacheWrapper = cache.get(downloadId); if (cacheWrapper == null) { LOGGER.warn("Download Id {} could not be found", downloadId); return new ResponseEntity<>(HttpStatus.NOT_FOUND); } final DownloadArtifactCache artifactCache = (DownloadArtifactCache) cacheWrapper.get(); DbArtifact artifact = null; switch (artifactCache.getDownloadType()) { case BY_SHA1: artifact = artifactRepository.getArtifactBySha1(artifactCache.getId()); break; default: LOGGER.warn("Download Type {} not supported", artifactCache.getDownloadType()); break; } if (artifact == null) { LOGGER.warn( "Artifact with cached id {} and download type {} could not be found.", artifactCache.getId(), artifactCache.getDownloadType()); return new ResponseEntity<>(HttpStatus.NOT_FOUND); } try { IOUtils.copy(artifact.getFileInputStream(), response.getOutputStream()); } catch (final IOException e) { LOGGER.error("Cannot copy streams", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } finally { cache.evict(downloadId); } return ResponseEntity.ok().build(); }