Search in sources :

Example 26 with Attachment

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);
}
Also used : Attachment(org.opencastproject.mediapackage.Attachment) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ImportWorkflowPropertiesWOH.loadPropertiesElementFromMediaPackage(org.opencastproject.workflow.handler.workflow.ImportWorkflowPropertiesWOH.loadPropertiesElementFromMediaPackage) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 27 with Attachment

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);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) URISyntaxException(java.net.URISyntaxException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) File(java.io.File)

Example 28 with Attachment

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;
}
Also used : Attachment(org.opencastproject.mediapackage.Attachment) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl)

Example 29 with 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);
}
Also used : MediaPackageElementBuilder(org.opencastproject.mediapackage.MediaPackageElementBuilder) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaPackageElementBuilderFactory(org.opencastproject.mediapackage.MediaPackageElementBuilderFactory) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Attachment(org.opencastproject.mediapackage.Attachment) MediaPackageBuilderTest(org.opencastproject.mediapackage.MediaPackageBuilderTest) Test(org.junit.Test)

Example 30 with 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);
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Attachment(org.opencastproject.mediapackage.Attachment) LiveScheduleException(org.opencastproject.liveschedule.api.LiveScheduleException) Job(org.opencastproject.job.api.Job) URISyntaxException(java.net.URISyntaxException) LiveScheduleException(org.opencastproject.liveschedule.api.LiveScheduleException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException)

Aggregations

Attachment (org.opencastproject.mediapackage.Attachment)64 MediaPackage (org.opencastproject.mediapackage.MediaPackage)28 URI (java.net.URI)24 IOException (java.io.IOException)20 Job (org.opencastproject.job.api.Job)20 NotFoundException (org.opencastproject.util.NotFoundException)19 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)18 Track (org.opencastproject.mediapackage.Track)16 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)16 Test (org.junit.Test)15 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)15 Catalog (org.opencastproject.mediapackage.Catalog)14 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)12 InputStream (java.io.InputStream)11 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)10 File (java.io.File)9 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)8 AttachmentImpl (org.opencastproject.mediapackage.attachment.AttachmentImpl)8 Workspace (org.opencastproject.workspace.api.Workspace)8