Search in sources :

Example 1 with ManifestFileServiceException

use of org.candlepin.sync.file.ManifestFileServiceException 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 2 with ManifestFileServiceException

use of org.candlepin.sync.file.ManifestFileServiceException in project candlepin by candlepin.

the class OwnerResource method importManifestAsync.

/**
 * Initiates an asynchronous manifest import for the given organization. The details of
 * the started job can be obtained via the {@link JobResource}.
 *
 * This will bring in any products, content, and subscriptions that were assigned to
 * the distributor who generated the manifest.
 *
 * @return a JobDetail object representing the newly started {@link ImportJob}.
 * @httpcode 400
 * @httpcode 404
 * @httpcode 500
 * @httpcode 200
 * @httpcode 409
 */
@POST
@Path("{owner_key}/imports/async")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@ApiOperation(notes = "Initiates an asynchronous manifest import for the given organization. " + "This will bring in any products, content, and subscriptions that were " + "assigned to the distributor who generated the manifest.", value = "Import Manifest Asynchronously")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "Owner not found"), @ApiResponse(code = 500, message = ""), @ApiResponse(code = 409, message = "") })
public JobDetail importManifestAsync(@PathParam("owner_key") @Verify(Owner.class) String ownerKey, @QueryParam("force") String[] overrideConflicts, MultipartInput input) {
    ConflictOverrides overrides = processConflictOverrideParams(overrideConflicts);
    UploadMetadata fileData = new UploadMetadata();
    Owner owner = findOwnerByKey(ownerKey);
    try {
        fileData = getArchiveFromResponse(input);
        String archivePath = fileData.getData().getAbsolutePath();
        log.info("Running async import of archive {} for owner {}", archivePath, owner.getDisplayName());
        return manifestManager.importManifestAsync(owner, fileData.getData(), fileData.getUploadedFilename(), overrides);
    } catch (IOException e) {
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw new IseException(i18n.tr("Error reading export archive"), e);
    } catch (ManifestFileServiceException e) {
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw new IseException(i18n.tr("Error storing uploaded archive for asynchronous processing."), e);
    } catch (CandlepinException e) {
        manifestManager.recordImportFailure(owner, e, fileData.getUploadedFilename());
        throw e;
    }
}
Also used : ConflictOverrides(org.candlepin.sync.ConflictOverrides) CandlepinException(org.candlepin.common.exceptions.CandlepinException) Owner(org.candlepin.model.Owner) IseException(org.candlepin.common.exceptions.IseException) IOException(java.io.IOException) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with ManifestFileServiceException

use of org.candlepin.sync.file.ManifestFileServiceException in project candlepin by candlepin.

the class ManifestManager method generateAndStoreManifest.

/**
 * Generates a manifest for the specifed consumer and stores the resulting file via the
 * {@link ManifestFileService}.
 *
 * @param consumerUuid the target consumer's UUID.
 * @param cdnLabel the CDN label to store in the meta file.
 * @param webUrl the URL pointing to the manifest's originating web application.
 * @param apiUrl the API URL pointing to the manifest's originating candlepin API.
 * @param extensionData data to be passed to the {@link ExportExtensionAdapter} when creating
 *                      a new export of the target consumer.
 * @return an {@link ExportResult} containing the details of the stored file.
 * @throws ExportCreationException if there are any issues generating the manifest.
 */
public ExportResult generateAndStoreManifest(String consumerUuid, String cdnLabel, String webUrl, String apiUrl, Map<String, String> extensionData) throws ExportCreationException {
    Consumer consumer = validateConsumerForExport(consumerUuid, cdnLabel);
    File export = null;
    try {
        poolManager.regenerateDirtyEntitlements(entitlementCurator.listByConsumer(consumer));
        export = exporter.getFullExport(consumer, cdnLabel, webUrl, apiUrl, extensionData);
        ManifestFile manifestFile = storeExport(export, consumer);
        sink.queueEvent(eventFactory.exportCreated(consumer));
        return new ExportResult(consumer.getUuid(), manifestFile.getId());
    } catch (ManifestFileServiceException e) {
        throw new ExportCreationException("Unable to create export archive", e);
    } finally {
        // We no longer need the export work directory since the archive has been saved in the DB.
        if (export != null) {
            File workDir = export.getParentFile();
            try {
                FileUtils.deleteDirectory(workDir);
            } catch (IOException ioe) {
            // It'll get cleaned up by the ManifestCleanerJob if it couldn't
            // be deleted for some reason.
            }
        }
    }
}
Also used : Consumer(org.candlepin.model.Consumer) ExportCreationException(org.candlepin.sync.ExportCreationException) IOException(java.io.IOException) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ManifestFile(org.candlepin.sync.file.ManifestFile) ManifestFileServiceException(org.candlepin.sync.file.ManifestFileServiceException) ExportResult(org.candlepin.sync.ExportResult)

Aggregations

IOException (java.io.IOException)3 ManifestFileServiceException (org.candlepin.sync.file.ManifestFileServiceException)3 IseException (org.candlepin.common.exceptions.IseException)2 Consumer (org.candlepin.model.Consumer)2 ExportCreationException (org.candlepin.sync.ExportCreationException)2 ManifestFile (org.candlepin.sync.file.ManifestFile)2 Transactional (com.google.inject.persist.Transactional)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 BadRequestException (org.candlepin.common.exceptions.BadRequestException)1 CandlepinException (org.candlepin.common.exceptions.CandlepinException)1 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)1 NotFoundException (org.candlepin.common.exceptions.NotFoundException)1