ManifestManager { @Transactional public void writeStoredExportToResponse(String exportId, String exportedConsumerUuid, HttpServletResponse response) throws ManifestFileServiceException, NotFoundException, BadRequestException, IseException { Consumer exportedConsumer = consumerCurator.verifyAndLookupConsumer(exportedConsumerUuid); ManifestFile manifest = manifestFileService.get(exportId); if (manifest == null) { throw new NotFoundException( i18n.tr("Unable to find specified manifest by id: {0}", exportId)); } if (!exportedConsumer.getUuid().equals(manifest.getTargetId())) { throw new BadRequestException( i18n.tr("Could not validate export against specifed consumer: {0}", exportedConsumer.getUuid())); } BufferedOutputStream output = null; InputStream input = null; try { response.setContentType("application/zip"); response.setHeader("Content-Disposition", "attachment; filename=" + manifest.getName()); input = manifest.getInputStream(); output = new BufferedOutputStream(response.getOutputStream()); int data = input.read(); while (data != -1) { output.write(data); data = input.read(); } output.flush(); } catch (Exception e) { response.setContentType("text/json"); response.setHeader("Content-Disposition", ""); throw new IseException(i18n.tr("Unable to download manifest: {0}", exportId), e); } } @Inject  ManifestManager(ManifestFileService manifestFileService, Exporter exporter, Importer importer,
        ConsumerCurator consumerCurator, ConsumerTypeCurator consumerTypeCurator,
        EntitlementCurator entitlementCurator, CdnCurator cdnCurator, PoolManager poolManager,
        PrincipalProvider principalProvider, I18n i18n, EventSink eventSink, EventFactory eventFactory); JobConfig generateManifestAsync(String consumerUuid, Owner owner, String cdnLabel,
        String webUrl, String apiUrl, Map<String, String> extensionData); File generateManifest(String consumerUuid, String cdnLabel, String webUrl, String apiUrl,
        Map<String, String> extensionData); JobConfig importManifestAsync(Owner owner, File archive, String uploadedFileName,
        ConflictOverrides overrides); ImportRecord importManifest(Owner owner, File archive, String uploadedFileName,
        ConflictOverrides overrides); void recordImportFailure(Owner owner, Throwable error, String filename); @Transactional ImportRecord importStoredManifest(Owner targetOwner, String fileId, ConflictOverrides overrides,
        String uploadedFileName); @Transactional int cleanup(int maxAgeInMinutes); @Transactional void writeStoredExportToResponse(String exportId, String exportedConsumerUuid,
        HttpServletResponse response); ExportResult generateAndStoreManifest(String consumerUuid, String cdnLabel, String webUrl,
        String apiUrl, Map<String, String> extensionData); @Transactional void deleteStoredManifest(String manifestFileId); File generateEntitlementArchive(Consumer consumer, Set<Long> serials);  }
@Test public void testWriteStoredExportToResponse() throws Exception { HttpServletResponse response = mock(HttpServletResponse.class); ServletOutputStream responseOutputStream = mock(ServletOutputStream.class); when(response.getOutputStream()).thenReturn(responseOutputStream); Consumer exportedConsumer = this.createMockConsumer(true); when(consumerCurator.verifyAndLookupConsumer(eq(exportedConsumer.getUuid()))) .thenReturn(exportedConsumer); String manifestId = "124"; String manifestFilename = "manifest.zip"; ManifestFile manifest = mock(ManifestFile.class); InputStream mockManifestInputStream = mock(InputStream.class); when(manifest.getId()).thenReturn(manifestId); when(manifest.getName()).thenReturn(manifestFilename); when(manifest.getTargetId()).thenReturn(exportedConsumer.getUuid()); when(manifest.getInputStream()).thenReturn(mockManifestInputStream); when(fileService.get(eq(manifestId))).thenReturn(manifest); when(mockManifestInputStream.read()).thenReturn(-1); manager.writeStoredExportToResponse(manifestId, exportedConsumer.getUuid(), response); verify(fileService).get(eq(manifestId)); verify(response).setContentType("application/zip"); verify(response).setHeader(eq("Content-Disposition"), eq("attachment; filename=" + manifestFilename)); verify(responseOutputStream).flush(); }
@Test public void testWriteStoredExportToResponseFailsWhenManifestFileNotFound() throws Exception { HttpServletResponse response = mock(HttpServletResponse.class); Consumer exportedConsumer = this.createMockConsumer(true); String manifestId = "124"; ManifestFile manifest = mock(ManifestFile.class); when(manifest.getTargetId()).thenReturn(exportedConsumer.getUuid()); when(fileService.get(eq(manifestId))).thenReturn(null); assertThrows(NotFoundException.class, () -> manager.writeStoredExportToResponse(manifestId, exportedConsumer.getUuid(), response) ); }
@Test public void testWriteStoredExportToResponseFailsWhenConsumerIdDoesntMatchManifest() throws Exception { HttpServletResponse response = mock(HttpServletResponse.class); Consumer exportedConsumer = this.createMockConsumer(true); String manifestId = "124"; when(consumerCurator.verifyAndLookupConsumer(eq(exportedConsumer.getUuid()))) .thenReturn(exportedConsumer); ManifestFile manifest = mock(ManifestFile.class); when(manifest.getTargetId()).thenReturn("another-consumer-uuid"); when(fileService.get(eq(manifestId))).thenReturn(manifest); assertThrows(BadRequestException.class, () -> manager.writeStoredExportToResponse(manifestId, exportedConsumer.getUuid(), response) ); }