use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.
the class ManifestManager method importStoredManifest.
/**
* Imports a stored manifest file into the target {@link Owner}. The stored file is deleted
* as soon as the import is complete.
*
* @param targetOwner the target owner.
* @param fileId the manifest file ID.
* @param overrides the {@link ConflictOverrides} to apply to the import process.
* @param uploadedFileName the originally uploaded file name.
* @return the result of the import.
* @throws BadRequestException if the file is not found in the {@link ManifestFileService}
* @throws ImporterException if there is an issue importing the file.
*/
@Transactional
public ImportRecord importStoredManifest(Owner targetOwner, String fileId, ConflictOverrides overrides, String uploadedFileName) throws BadRequestException, ImporterException {
ManifestFile manifest = manifestFileService.get(fileId);
if (manifest == null) {
throw new BadRequestException(i18n.tr("The requested manifest file was not found: {0}", fileId));
}
ImportRecord importResult = importer.loadStoredExport(manifest, targetOwner, overrides, uploadedFileName);
deleteStoredManifest(manifest.getId());
return importResult;
}
use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.
the class ManifestManager method writeStoredExportToResponse.
/**
* Write the stored manifest file to the specified response output stream and update
* the appropriate response data.
*
* @param exportId the id of the manifest file to find.
* @param exportedConsumerUuid the UUID of the consumer the export was generated for.
* @param response the response to write the file to.
* @throws ManifestFileServiceException if there was an issue getting the file from the service
* @throws NotFoundException if the manifest file is not found
* @throws BadRequestException if the manifests target consumer does not match the specified
* consumer.
* @throws IseException if there was an issue writing the file to the response.
*/
@Transactional
public void writeStoredExportToResponse(String exportId, String exportedConsumerUuid, HttpServletResponse response) throws ManifestFileServiceException, NotFoundException, BadRequestException, IseException {
Consumer exportedConsumer = consumerCurator.verifyAndLookupConsumer(exportedConsumerUuid);
// In order to stream the results from the DB to the client
// we write the file contents directly to the response output stream.
//
// NOTE: Passing the database input stream to the response builder seems
// like it would be a correct approach here, but large object streaming
// can only be done inside a single transaction, so we have to stream it
// manually.
ManifestFile manifest = manifestFileService.get(exportId);
if (manifest == null) {
throw new NotFoundException(i18n.tr("Unable to find specified manifest by id: {0}", exportId));
}
// The specified consumer must match that of the manifest.
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());
// NOTE: Input and output streams are expected to be closed by their creators.
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) {
// Reset the response data so that a json response can be returned,
// by RestEasy.
response.setContentType("text/json");
response.setHeader("Content-Disposition", "");
throw new IseException(i18n.tr("Unable to download manifest: {0}", exportId), e);
}
}
use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.
the class ManifestManagerTest method testWriteStoredExportToResponse.
@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);
// Simulate no data for test.
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();
}
use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.
the class ManifestManagerTest method testGenerateManifest.
@Test
public void testGenerateManifest() throws Exception {
Consumer consumer = this.createMockConsumer(true);
Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
String webAppPrefix = "webapp-prefix";
String apiUrl = "api-url";
Map<String, String> extData = new HashMap<>();
Event event = mock(Event.class);
when(eventFactory.exportCreated(eq(consumer))).thenReturn(event);
List<Entitlement> ents = new ArrayList<>();
when(entitlementCurator.listByConsumer(eq(consumer))).thenReturn(ents);
when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
File manifestFile = mock(File.class);
when(exporter.getFullExport(eq(consumer), eq(cdn.getLabel()), eq(webAppPrefix), eq(apiUrl), eq(extData))).thenReturn(manifestFile);
File result = manager.generateManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, extData);
assertEquals(manifestFile, result);
verify(entitlementCurator).listByConsumer(eq(consumer));
verify(exporter).getFullExport(eq(consumer), eq(cdn.getLabel()), eq(webAppPrefix), eq(apiUrl), eq(extData));
verify(eventFactory).exportCreated(eq(consumer));
verify(eventSink).queueEvent(eq(event));
verifyZeroInteractions(fileService);
}
use of org.candlepin.sync.file.ManifestFile in project candlepin by candlepin.
the class ManifestManagerTest method ensureEventSentWhenManifestGeneratedAndStored.
@Test
public void ensureEventSentWhenManifestGeneratedAndStored() throws Exception {
Consumer consumer = this.createMockConsumer(true);
Cdn cdn = new Cdn("test-cdn", "Test CDN", "");
String webAppPrefix = "webapp-prefix";
String apiUrl = "api-url";
Event event = mock(Event.class);
when(eventFactory.exportCreated(eq(consumer))).thenReturn(event);
UserPrincipal principal = TestUtil.createOwnerPrincipal();
when(principalProvider.get()).thenReturn(principal);
ManifestFile manifest = mock(ManifestFile.class);
when(fileService.store(eq(ManifestFileType.EXPORT), any(File.class), eq(principal.getName()), any(String.class))).thenReturn(manifest);
when(consumerCurator.verifyAndLookupConsumer(eq(consumer.getUuid()))).thenReturn(consumer);
when(cdnCurator.lookupByLabel(eq(cdn.getLabel()))).thenReturn(cdn);
manager.generateAndStoreManifest(consumer.getUuid(), cdn.getLabel(), webAppPrefix, apiUrl, new HashMap<>());
verify(eventFactory).exportCreated(eq(consumer));
verify(eventSink).queueEvent(eq(event));
}
Aggregations