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);
}
}
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;
}
}
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.
}
}
}
}
Aggregations