Search in sources :

Example 11 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class PublishYouTubeWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running youtube publication workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    // Check which tags have been configured
    String sourceTags = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-tags"));
    String sourceFlavors = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration("source-flavors"));
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector;
    if (sourceTags == null && sourceFlavors == null) {
        logger.warn("No tags or flavor have been specified");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    elementSelector = new SimpleElementSelector();
    if (sourceFlavors != null) {
        for (String flavor : asList(sourceFlavors)) {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        }
    }
    if (sourceTags != null) {
        for (String tag : asList(sourceTags)) {
            elementSelector.addTag(tag);
        }
    }
    try {
        // Look for elements matching the tag
        final Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
        if (elements.size() > 1) {
            logger.warn("More than one element has been found for publishing to youtube: {}", elements);
            return createResult(mediaPackage, Action.SKIP);
        }
        if (elements.size() < 1) {
            logger.info("No mediapackage element was found for publishing");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        Job youtubeJob;
        try {
            Track track = mediaPackage.getTrack(elements.iterator().next().getIdentifier());
            youtubeJob = publicationService.publish(mediaPackage, track);
        } catch (PublicationException e) {
            throw new WorkflowOperationException(e);
        }
        // Wait until the youtube publication job has returned
        if (!waitForStatus(youtubeJob).isSuccess())
            throw new WorkflowOperationException("The youtube publication jobs did not complete successfully");
        // All the jobs have passed
        Job job = serviceRegistry.getJob(youtubeJob.getId());
        // If there is no payload, then the item has not been published.
        if (job.getPayload() == null) {
            logger.warn("Publish to youtube failed, no payload from publication job: {}", job);
            return createResult(mediaPackage, Action.CONTINUE);
        }
        Publication newElement = null;
        try {
            newElement = (Publication) MediaPackageElementParser.getFromXml(job.getPayload());
        } catch (MediaPackageException e) {
            throw new WorkflowOperationException(e);
        }
        if (newElement == null) {
            logger.warn("Publication to youtube failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", job.getPayload(), job);
            return createResult(mediaPackage, Action.CONTINUE);
        }
        mediaPackage.add(newElement);
        logger.debug("Publication to youtube operation completed");
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) Publication(org.opencastproject.mediapackage.Publication) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Example 12 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class CloneWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running clone workflow operation on workflow {}", workflowInstance.getId());
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_TAGS));
    String sourceFlavorOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_FLAVOR));
    String targetFlavorOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_FLAVOR));
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector = new SimpleElementSelector();
    // Make sure either one of tags or flavors are provided
    if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorOption)) {
        logger.info("No source tags or flavors have been specified, not matching anything. Operation will be skipped.");
        return createResult(mediaPackage, Action.SKIP);
    }
    // if no source-favor is specified, all flavors will be checked for given tags
    if (sourceFlavorOption == null) {
        sourceFlavorOption = "*/*";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("Parameters passed to clone workflow operation:");
    sb.append("\n source-tags: ").append(sourceTagsOption);
    sb.append("\n source-flavor: ").append(sourceFlavorOption);
    sb.append("\n target-flavor: ").append(targetFlavorOption);
    logger.debug(sb.toString());
    // Select the source flavors
    MediaPackageElementFlavor sourceFlavor = MediaPackageElementFlavor.parseFlavor(sourceFlavorOption);
    elementSelector.addFlavor(sourceFlavor);
    // Select the source tags
    for (String tag : asList(sourceTagsOption)) {
        elementSelector.addTag(tag);
    }
    // Look for elements matching the tags and the flavor
    Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
    // Check the the number of element returned
    if (elements.size() == 0) {
        // If no one found, we skip the operation
        logger.debug("No matching elements found, skipping operation.");
        return createResult(workflowInstance.getMediaPackage(), Action.SKIP);
    } else {
        logger.debug("Copy " + elements.size() + " elements to new flavor: {}", targetFlavorOption);
        MediaPackageElementFlavor targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
        for (MediaPackageElement element : elements) {
            MediaPackageElementFlavor flavor;
            // Take subtype either from option or from element (if option is *)
            String subtype;
            if ("*".equals(targetFlavor.getSubtype()))
                subtype = element.getFlavor().getSubtype();
            else
                subtype = targetFlavor.getSubtype();
            // Take type either from option or from element (if option is *)
            if ("*".equals(targetFlavor.getType()))
                flavor = new MediaPackageElementFlavor(element.getFlavor().getType(), subtype);
            else
                flavor = new MediaPackageElementFlavor(targetFlavor.getType(), subtype);
            // Copy element and set new flavor
            MediaPackageElement newElement = copyElement(element);
            newElement.setFlavor(flavor);
            mediaPackage.add(newElement);
        }
    }
    return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 13 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class CopyWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    logger.debug("Running copy workflow operation on workflow {}", workflowInstance.getId());
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String sourceTagsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_TAGS));
    String sourceFlavorsOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_SOURCE_FLAVORS));
    String targetDirectoryOption = StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_DIRECTORY));
    Option<String> targetFilenameOption = Option.option(StringUtils.trimToNull(currentOperation.getConfiguration(OPT_TARGET_FILENAME)));
    StringBuilder sb = new StringBuilder();
    sb.append("Parameters passed to copy workflow operation:");
    sb.append("\n source-tags: ").append(sourceTagsOption);
    sb.append("\n source-flavors: ").append(sourceFlavorsOption);
    sb.append("\n target-directory: ").append(targetDirectoryOption);
    sb.append("\n target-filename: ").append(targetFilenameOption);
    logger.debug(sb.toString());
    AbstractMediaPackageElementSelector<MediaPackageElement> elementSelector = new SimpleElementSelector();
    // Make sure either one of tags or flavors are provided
    if (StringUtils.isBlank(sourceTagsOption) && StringUtils.isBlank(sourceFlavorsOption)) {
        logger.info("No source tags or flavors have been specified, not matching anything");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Make the target filename and directory are provided
    if (StringUtils.isBlank(targetDirectoryOption))
        throw new WorkflowOperationException("No target directory has been set for the copy operation!");
    // Select the source flavors
    for (String flavor : asList(sourceFlavorsOption)) {
        try {
            elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException("Source flavor '" + flavor + "' is malformed");
        }
    }
    // Select the source tags
    for (String tag : asList(sourceTagsOption)) {
        elementSelector.addTag(tag);
    }
    // Look for elements matching the tag
    Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, true);
    // Check the the number of element returned
    if (elements.size() == 0) {
        // If no one found, we skip the operation
        return createResult(workflowInstance.getMediaPackage(), Action.SKIP);
    } else if (elements.size() == 1) {
        for (MediaPackageElement element : elements) {
            logger.debug("Copy single element to: {}", targetDirectoryOption);
            final String fileName;
            if (targetFilenameOption.isSome()) {
                fileName = targetFilenameOption.get();
            } else {
                fileName = FilenameUtils.getBaseName(element.getURI().toString());
            }
            String ext = FilenameUtils.getExtension(element.getURI().toString());
            ext = ext.length() > 0 ? ".".concat(ext) : "";
            File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName.concat(ext)));
            copyElement(element, targetFile);
        }
    } else {
        logger.debug("Copy multiple elements to: {}", targetDirectoryOption);
        int i = 1;
        for (MediaPackageElement element : elements) {
            final String fileName;
            if (targetFilenameOption.isSome()) {
                fileName = String.format(targetFilenameOption.get(), i);
            } else {
                fileName = FilenameUtils.getBaseName(element.getURI().toString());
            }
            String ext = FilenameUtils.getExtension(element.getURI().toString());
            ext = ext.length() > 0 ? ".".concat(ext) : "";
            File targetFile = new File(UrlSupport.concat(targetDirectoryOption, fileName + ext));
            copyElement(element, targetFile);
            i++;
        }
    }
    return createResult(workflowInstance.getMediaPackage(), Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) File(java.io.File)

Example 14 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class TagWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    String configuredSourceFlavors = StringUtils.trimToEmpty(currentOperation.getConfiguration(SOURCE_FLAVORS_PROPERTY));
    String configuredSourceTags = StringUtils.trimToEmpty(currentOperation.getConfiguration(SOURCE_TAGS_PROPERTY));
    String configuredTargetFlavor = StringUtils.trimToNull(currentOperation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String configuredTargetTags = StringUtils.trimToEmpty(currentOperation.getConfiguration(TARGET_TAGS_PROPERTY));
    boolean copy = BooleanUtils.toBoolean(currentOperation.getConfiguration(COPY_PROPERTY));
    if (copy) {
        logger.info("Retagging mediapackage elements as a copy");
    } else {
        logger.info("Retagging mediapackage elements");
    }
    String[] sourceTags = StringUtils.split(configuredSourceTags, ",");
    String[] targetTags = StringUtils.split(configuredTargetTags, ",");
    String[] sourceFlavors = StringUtils.split(configuredSourceFlavors, ",");
    SimpleElementSelector elementSelector = new SimpleElementSelector();
    for (String flavor : sourceFlavors) {
        elementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    List<String> removeTags = new ArrayList<String>();
    List<String> addTags = new ArrayList<String>();
    List<String> overrideTags = new ArrayList<String>();
    for (String tag : targetTags) {
        if (tag.startsWith(MINUS)) {
            removeTags.add(tag);
        } else if (tag.startsWith(PLUS)) {
            addTags.add(tag);
        } else {
            overrideTags.add(tag);
        }
    }
    for (String tag : sourceTags) {
        elementSelector.addTag(tag);
    }
    Collection<MediaPackageElement> elements = elementSelector.select(mediaPackage, false);
    for (MediaPackageElement e : elements) {
        MediaPackageElement element = e;
        if (copy) {
            element = (MediaPackageElement) e.clone();
            element.setIdentifier(null);
            // use the same URI as the original
            element.setURI(e.getURI());
        }
        if (configuredTargetFlavor != null)
            element.setFlavor(MediaPackageElementFlavor.parseFlavor(configuredTargetFlavor));
        if (overrideTags.size() > 0) {
            element.clearTags();
            for (String tag : overrideTags) {
                element.addTag(tag);
            }
        } else {
            for (String tag : removeTags) {
                element.removeTag(tag.substring(MINUS.length()));
            }
            for (String tag : addTags) {
                element.addTag(tag.substring(PLUS.length()));
            }
        }
        if (copy)
            mediaPackage.addDerived(element, e);
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ArrayList(java.util.ArrayList) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector)

Example 15 with SimpleElementSelector

use of org.opencastproject.mediapackage.selector.SimpleElementSelector in project opencast by opencast.

the class CleanupWorkflowOperationHandlerTest method testCreanupWOHwithPreservedFlavorAndMediaPackagePathPrefix.

@Test
public void testCreanupWOHwithPreservedFlavorAndMediaPackagePathPrefix() throws WorkflowOperationException, MediaPackageException {
    Map<String, String> wfInstConfig = new Hashtable<>();
    wfInstConfig.put(CleanupWorkflowOperationHandler.PRESERVE_FLAVOR_PROPERTY, "*/source,smil/trimmed,security/*");
    wfInstConfig.put(CleanupWorkflowOperationHandler.DELETE_EXTERNAL, "true");
    MediaPackageBuilder mpBuilder = MediaPackageBuilderFactory.newInstance().newMediaPackageBuilder();
    MediaPackage mp = mpBuilder.createNew();
    MediaPackageElement track1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Track, "presenter", "source", null);
    track1.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), track1.getIdentifier(), "track.mp4"));
    MediaPackageElement track2 = addElementToMediaPackage(mp, MediaPackageElement.Type.Track, "presentation", "work", null);
    track2.setURI(UrlSupport.uri(HOSTNAME_NODE2, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), track2.getIdentifier(), "track.mp4"));
    MediaPackageElement att1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Attachment, "presentation", "preview", null);
    att1.setURI(UrlSupport.uri(HOSTNAME_NODE2, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), att1.getIdentifier(), "preview.png"));
    MediaPackageElement att2 = addElementToMediaPackage(mp, MediaPackageElement.Type.Attachment, "smil", "trimmed", null);
    att2.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), att2.getIdentifier(), "trimmed.smil"));
    MediaPackageElement cat1 = addElementToMediaPackage(mp, MediaPackageElement.Type.Catalog, "dublincore", "episode", null);
    cat1.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), cat1.getIdentifier(), "dublincore.xml"));
    MediaPackageElement cat2 = addElementToMediaPackage(mp, MediaPackageElement.Type.Catalog, "security", "xaml", null);
    cat2.setURI(UrlSupport.uri(HOSTNAME_NODE1, WFR_URL_PREFIX, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX, mp.getIdentifier().compact(), cat2.getIdentifier(), "security.xml"));
    cleanupWOH.start(createWorkflowInstance(wfInstConfig, mp), null);
    Assert.assertEquals("Media package should contain at least tree elements", 3, mp.getElements().length);
    SimpleElementSelector elementSelector = new SimpleElementSelector();
    elementSelector.addFlavor("*/source");
    Assert.assertFalse("Media package doesn't contain an element with a preserved flavor '*/source'", elementSelector.select(mp, false).isEmpty());
    elementSelector = new SimpleElementSelector();
    elementSelector.addFlavor("smil/trimmed");
    Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'smil/trimmed'", elementSelector.select(mp, false).isEmpty());
    elementSelector = new SimpleElementSelector();
    elementSelector.addFlavor("security/*");
    Assert.assertFalse("Media package doesn't contain an element with a preserved flavor 'security/*'", elementSelector.select(mp, false).isEmpty());
    Assert.assertEquals("At least one file wasn't deleted on remote repository", 3, deletedFilesURIs.size());
}
Also used : MediaPackageBuilder(org.opencastproject.mediapackage.MediaPackageBuilder) Hashtable(java.util.Hashtable) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) Test(org.junit.Test)

Aggregations

SimpleElementSelector (org.opencastproject.mediapackage.selector.SimpleElementSelector)16 MediaPackage (org.opencastproject.mediapackage.MediaPackage)15 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)15 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)8 ArrayList (java.util.ArrayList)7 Publication (org.opencastproject.mediapackage.Publication)7 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)7 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)5 Job (org.opencastproject.job.api.Job)4 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)4 URI (java.net.URI)3 HashSet (java.util.HashSet)3 MalformedURLException (java.net.MalformedURLException)2 Hashtable (java.util.Hashtable)2 Test (org.junit.Test)2 Attachment (org.opencastproject.mediapackage.Attachment)2 Catalog (org.opencastproject.mediapackage.Catalog)2 MediaPackageBuilder (org.opencastproject.mediapackage.MediaPackageBuilder)2 MediaPackageReference (org.opencastproject.mediapackage.MediaPackageReference)2 Track (org.opencastproject.mediapackage.Track)2