ZipWorkflowOperationHandler extends AbstractWorkflowOperationHandler { @Override public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { if (workflowInstance == null) { throw new WorkflowOperationException("Invalid workflow instance"); } final MediaPackage mediaPackage = workflowInstance.getMediaPackage(); final WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation(); if (currentOperation == null) { throw new WorkflowOperationException("Cannot get current workflow operation"); } String flavors = currentOperation.getConfiguration(INCLUDE_FLAVORS_PROPERTY); final List<MediaPackageElementFlavor> flavorsToZip = new ArrayList<MediaPackageElementFlavor>(); MediaPackageElementFlavor targetFlavor = DEFAULT_ARCHIVE_FLAVOR; String targetFlavorOption = currentOperation.getConfiguration(TARGET_FLAVOR_PROPERTY); try { targetFlavor = targetFlavorOption == null ? DEFAULT_ARCHIVE_FLAVOR : MediaPackageElementFlavor.parseFlavor(targetFlavorOption); logger.trace("Using '{}' as the target flavor for the zip archive of recording {}", targetFlavor, mediaPackage); } catch (IllegalArgumentException e) { throw new WorkflowOperationException("Flavor '" + targetFlavorOption + "' is not valid", e); } String targetTagsOption = StringUtils.trimToEmpty(currentOperation.getConfiguration(TARGET_TAGS_PROPERTY)); String[] targetTags = StringUtils.split(targetTagsOption, ","); if (flavors == null) { flavorsToZip.add(MediaPackageElementFlavor.parseFlavor("*/*")); } else { for (String flavor : asList(flavors)) { flavorsToZip.add(MediaPackageElementFlavor.parseFlavor(flavor)); } } logger.info("Archiving mediapackage {} in workflow {}", mediaPackage, workflowInstance); String compressProperty = currentOperation.getConfiguration(COMPRESS_PROPERTY); boolean compress = compressProperty == null ? false : Boolean.valueOf(compressProperty); File zip = null; try { logger.info("Creating zipped archive of recording {}", mediaPackage); zip = zip(mediaPackage, flavorsToZip, compress); } catch (Exception e) { throw new WorkflowOperationException("Unable to create a zip archive from mediapackage " + mediaPackage, e); } String configuredCollectionId = currentOperation.getConfiguration(ZIP_COLLECTION_PROPERTY); String collectionId = configuredCollectionId == null ? DEFAULT_ZIP_COLLECTION : configuredCollectionId; logger.info("Moving zipped archive of recording {} to the working file repository collection '{}'", mediaPackage, collectionId); InputStream in = null; URI uri = null; try { in = new FileInputStream(zip); uri = workspace.putInCollection(collectionId, mediaPackage.getIdentifier().toString() + ".zip", in); logger.info("Zipped archive of recording {} is available from {}", mediaPackage, uri); } catch (FileNotFoundException e) { throw new WorkflowOperationException("zip file " + zip + " not found", e); } catch (IOException e) { throw new WorkflowOperationException(e); } finally { IOUtils.closeQuietly(in); } Attachment attachment = (Attachment) MediaPackageElementBuilderFactory.newInstance().newElementBuilder() .elementFromURI(uri, Type.Attachment, targetFlavor); try { attachment.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, zip)); } catch (IOException e) { throw new WorkflowOperationException(e); } attachment.setMimeType(MimeTypes.ZIP); for (String tag : targetTags) { attachment.addTag(tag); logger.trace("Tagging the archive of recording '{}' with '{}'", mediaPackage, tag); } attachment.setMimeType(MimeTypes.ZIP); try { FileUtils.forceDelete(zip); } catch (Exception e) { throw new WorkflowOperationException(e); } mediaPackage.add(attachment); return createResult(mediaPackage, Action.CONTINUE); }  void setWorkspace(Workspace workspace); @Override WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context); static final String ZIP_COLLECTION_PROPERTY; static final String INCLUDE_FLAVORS_PROPERTY; static final String TARGET_FLAVOR_PROPERTY; static final String TARGET_TAGS_PROPERTY; static final String COMPRESS_PROPERTY; static final String DEFAULT_ZIP_COLLECTION; static final String DEFAULT_ZIP_ARCHIVE_TEMP_DIR; static final String ZIP_ARCHIVE_TEMP_DIR_CFG_KEY; static final MediaPackageElementFlavor DEFAULT_ARCHIVE_FLAVOR; }
@Test public void testInvalidWorkflow() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); operation.setConfiguration(ZipWorkflowOperationHandler.ZIP_COLLECTION_PROPERTY, "failed-zips"); operation.setConfiguration(ZipWorkflowOperationHandler.INCLUDE_FLAVORS_PROPERTY, "*/source,dublincore/*"); operation.setConfiguration(ZipWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "archive/zip"); operation.setConfiguration(ZipWorkflowOperationHandler.COMPRESS_PROPERTY, "false"); try { operationHandler.start(null, null); Assert.fail("A null workflow is passed so an exception should be thrown"); } catch (WorkflowOperationException e) { } }
@Test public void testInvalidMediaPackage() throws Exception { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(null); operation.setConfiguration(ZipWorkflowOperationHandler.ZIP_COLLECTION_PROPERTY, "failed-zips"); operation.setConfiguration(ZipWorkflowOperationHandler.INCLUDE_FLAVORS_PROPERTY, "*/source,dublincore/*"); operation.setConfiguration(ZipWorkflowOperationHandler.TARGET_FLAVOR_PROPERTY, "archive/zip"); operation.setConfiguration(ZipWorkflowOperationHandler.COMPRESS_PROPERTY, "false"); try { operationHandler.start(instance, null); Assert.fail("A null mediapackage is passed so an exception should be thrown"); } catch (WorkflowOperationException e) { } }
@Test public void testConfigKeyTargetFlavorDefaultValue() { WorkflowInstanceImpl instance = new WorkflowInstanceImpl(); List<WorkflowOperationInstance> ops = new ArrayList<WorkflowOperationInstance>(); WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("test", OperationState.INSTANTIATED); ops.add(operation); instance.setOperations(ops); instance.setMediaPackage(mp); operation.setConfiguration(ZipWorkflowOperationHandler.ZIP_COLLECTION_PROPERTY, "failed-zips"); operation.setConfiguration(ZipWorkflowOperationHandler.INCLUDE_FLAVORS_PROPERTY, "*/source,dublincore/*"); operation.setConfiguration(ZipWorkflowOperationHandler.COMPRESS_PROPERTY, "false"); try { WorkflowOperationResult result = operationHandler.start(instance, null); Assert.assertEquals("workflow result action not CONTINUE: " + result.getAction(), WorkflowOperationResult.Action.CONTINUE, result.getAction()); } catch (WorkflowOperationException e) { Assert.fail("missing target-flavor and no default value kicked in: " + e); } }