Search in sources :

Example 1 with ManifestFile

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;
}
Also used : BadRequestException(org.candlepin.common.exceptions.BadRequestException) ImportRecord(org.candlepin.model.ImportRecord) ManifestFile(org.candlepin.sync.file.ManifestFile) Transactional(com.google.inject.persist.Transactional)

Example 2 with ManifestFile

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);
    }
}
Also used : Consumer(org.candlepin.model.Consumer) IseException(org.candlepin.common.exceptions.IseException) InputStream(java.io.InputStream) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) BufferedOutputStream(java.io.BufferedOutputStream) ManifestFile(org.candlepin.sync.file.ManifestFile) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ImporterException(org.candlepin.sync.ImporterException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) ExportCreationException(org.candlepin.sync.ExportCreationException) IOException(java.io.IOException) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) IseException(org.candlepin.common.exceptions.IseException) Transactional(com.google.inject.persist.Transactional)

Example 3 with ManifestFile

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();
}
Also used : Consumer(org.candlepin.model.Consumer) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Example 4 with ManifestFile

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);
}
Also used : Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Event(org.candlepin.audit.Event) Cdn(org.candlepin.model.Cdn) Entitlement(org.candlepin.model.Entitlement) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) Test(org.junit.Test)

Example 5 with ManifestFile

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));
}
Also used : Consumer(org.candlepin.model.Consumer) Event(org.candlepin.audit.Event) Cdn(org.candlepin.model.Cdn) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) UserPrincipal(org.candlepin.auth.UserPrincipal) ManifestFile(org.candlepin.sync.file.ManifestFile) Test(org.junit.Test)

Aggregations

ManifestFile (org.candlepin.sync.file.ManifestFile)11 Consumer (org.candlepin.model.Consumer)8 Test (org.junit.Test)8 File (java.io.File)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Event (org.candlepin.audit.Event)3 UserPrincipal (org.candlepin.auth.UserPrincipal)3 Cdn (org.candlepin.model.Cdn)3 Transactional (com.google.inject.persist.Transactional)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 BadRequestException (org.candlepin.common.exceptions.BadRequestException)2 Entitlement (org.candlepin.model.Entitlement)2 Owner (org.candlepin.model.Owner)2 ConflictOverrides (org.candlepin.sync.ConflictOverrides)2 ExportCreationException (org.candlepin.sync.ExportCreationException)2 ExportResult (org.candlepin.sync.ExportResult)2 ManifestFileServiceException (org.candlepin.sync.file.ManifestFileServiceException)2