use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class ExportWorkflowPropertiesWOH method start.
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.info("Start exporting workflow properties for workflow {}", workflowInstance);
final MediaPackage mediaPackage = workflowInstance.getMediaPackage();
final Set<String> keys = $(getOptConfig(workflowInstance, KEYS_PROPERTY)).bind(Strings.splitCsv).toSet();
final String targetFlavorString = getOptConfig(workflowInstance, TARGET_FLAVOR_PROPERTY).getOr(DEFAULT_TARGET_FLAVOR);
final Stream<String> targetTags = $(getOptConfig(workflowInstance, TARGET_TAGS_PROPERTY)).bind(Strings.splitCsv);
final MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorString);
// Read optional existing workflow properties from mediapackage
Properties workflowProps = new Properties();
Opt<Attachment> existingPropsElem = loadPropertiesElementFromMediaPackage(targetFlavor, workflowInstance);
if (existingPropsElem.isSome()) {
workflowProps = loadPropertiesFromXml(workspace, existingPropsElem.get().getURI());
// Remove specified keys
for (String key : keys) workflowProps.remove(key);
}
// Extend with specified properties
for (String key : workflowInstance.getConfigurationKeys()) {
if (keys.isEmpty() || keys.contains(key))
workflowProps.put(key, workflowInstance.getConfiguration(key));
}
// Store properties as an attachment
Attachment attachment;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
workflowProps.storeToXML(out, null, "UTF-8");
String elementId = UUID.randomUUID().toString();
URI uri = workspace.put(mediaPackage.getIdentifier().compact(), elementId, EXPORTED_PROPERTIES_FILENAME, new ByteArrayInputStream(out.toByteArray()));
MediaPackageElementBuilder builder = MediaPackageElementBuilderFactory.newInstance().newElementBuilder();
attachment = (Attachment) builder.elementFromURI(uri, Attachment.TYPE, targetFlavor);
attachment.setMimeType(MimeTypes.XML);
} catch (IOException e) {
logger.error("Unable to store workflow properties as Attachment with flavor '{}': {}", targetFlavorString, ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException("Unable to store workflow properties as Attachment", e);
}
// Add the target tags
for (String tag : targetTags) {
logger.trace("Tagging with '{}'", tag);
attachment.addTag(tag);
}
// Update attachment
if (existingPropsElem.isSome())
mediaPackage.remove(existingPropsElem.get());
mediaPackage.add(attachment);
logger.info("Added properties from {} as Attachment with flavor {}", workflowInstance, targetFlavorString);
logger.debug("Workflow properties: {}", propertiesAsString(workflowProps));
return createResult(mediaPackage, null, Action.CONTINUE, 0);
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class ZipWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@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;
// Read the target 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);
}
// Read the target tags
String targetTagsOption = StringUtils.trimToEmpty(currentOperation.getConfiguration(TARGET_TAGS_PROPERTY));
String[] targetTags = StringUtils.split(targetTagsOption, ",");
// If the configuration does not specify flavors, just zip them all
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);
// Zip the contents of the mediapackage
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);
}
// Get the collection for storing the archived mediapackage
String configuredCollectionId = currentOperation.getConfiguration(ZIP_COLLECTION_PROPERTY);
String collectionId = configuredCollectionId == null ? DEFAULT_ZIP_COLLECTION : configuredCollectionId;
// Add the zip as an attachment to the mediapackage
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().compact() + ".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);
// Apply the target tags
for (String tag : targetTags) {
attachment.addTag(tag);
logger.trace("Tagging the archive of recording '{}' with '{}'", mediaPackage, tag);
}
attachment.setMimeType(MimeTypes.ZIP);
// The zip file is safely in the archive, so it's now safe to attempt to remove the original zip
try {
FileUtils.forceDelete(zip);
} catch (Exception e) {
throw new WorkflowOperationException(e);
}
mediaPackage.add(attachment);
return createResult(mediaPackage, Action.CONTINUE);
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class AbstractAttachmentBuilderPlugin method newElement.
/**
* @see org.opencastproject.mediapackage.MediaPackageElementBuilder#newElement(org.opencastproject.mediapackage.MediaPackageElement.Type
* , org.opencastproject.mediapackage.MediaPackageElementFlavor)
*/
@Override
public MediaPackageElement newElement(MediaPackageElement.Type type, MediaPackageElementFlavor flavor) {
Attachment attachment = new AttachmentImpl();
attachment.setFlavor(flavor);
return attachment;
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class AttachmentTest method testFromURL.
/**
* Test method for {@link org.opencastproject.mediapackage.attachment.AttachmentImpl#fromURI(java.net.URI)}.
*/
@Test
public void testFromURL() {
MediaPackageElementBuilderFactory factory = MediaPackageElementBuilderFactory.newInstance();
MediaPackageElementBuilder builder = factory.newElementBuilder();
MediaPackageElement packageElement = null;
// Create the element
try {
packageElement = builder.elementFromURI(coverFile.toURI());
} catch (UnsupportedElementException e) {
fail("Attachment is unsupported: " + e.getMessage());
}
// Type test
assertTrue("Type mismatch", packageElement instanceof Attachment);
}
use of org.opencastproject.mediapackage.Attachment in project opencast by opencast.
the class LiveScheduleServiceImpl method replaceAndDistributeAcl.
MediaPackage replaceAndDistributeAcl(MediaPackage previousMp, AccessControlList acl) throws LiveScheduleException {
try {
// This is the mp from the search index
MediaPackage mp = (MediaPackage) previousMp.clone();
// Remove previous Acl from the mp
Attachment[] atts = mp.getAttachments(MediaPackageElements.XACML_POLICY_EPISODE);
if (atts.length > 0)
mp.remove(atts[0]);
// Attach current ACL to mp, acl will be created in the ws/wfr
authService.setAcl(mp, AclScope.Episode, acl);
atts = mp.getAttachments(MediaPackageElements.XACML_POLICY_EPISODE);
if (atts.length > 0) {
String aclId = atts[0].getIdentifier();
// Distribute new acl
Job distributionJob = downloadDistributionService.distribute(CHANNEL_ID, mp, aclId, false);
if (!waitForStatus(distributionJob).isSuccess())
throw new LiveScheduleException("Acl for live media package " + mp.getIdentifier() + " could not be distributed");
MediaPackageElement e = mp.getElementById(aclId);
// Cleanup workspace/wfr
mp.remove(e);
workspace.delete(e.getURI());
// Add distributed acl to mp
mp.add(MediaPackageElementParser.getFromXml(distributionJob.getPayload()));
}
return mp;
} catch (LiveScheduleException e) {
throw e;
} catch (Exception e) {
throw new LiveScheduleException(e);
}
}
Aggregations