use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class DuplicateEventWorkflowOperationHandler method copyDublinCore.
private MediaPackage copyDublinCore(MediaPackage source, MediaPackageElement sourceDublinCore, MediaPackage destination, List<String> removeTags, List<String> addTags, List<String> overrideTags, List<URI> temporaryFiles) throws WorkflowOperationException {
final DublinCoreCatalog destinationDublinCore = DublinCoreUtil.loadEpisodeDublinCore(workspace, source).get();
destinationDublinCore.setIdentifier(null);
destinationDublinCore.setURI(sourceDublinCore.getURI());
destinationDublinCore.set(DublinCore.PROPERTY_TITLE, destination.getTitle());
try (InputStream inputStream = IOUtils.toInputStream(destinationDublinCore.toXmlString(), "UTF-8")) {
final String elementId = UUID.randomUUID().toString();
final URI newUrl = workspace.put(destination.getIdentifier().compact(), elementId, "dublincore.xml", inputStream);
temporaryFiles.add(newUrl);
final MediaPackageElement mpe = destination.add(newUrl, MediaPackageElement.Type.Catalog, MediaPackageElements.EPISODE);
mpe.setIdentifier(elementId);
for (String tag : sourceDublinCore.getTags()) {
mpe.addTag(tag);
}
updateTags(mpe, removeTags, addTags, overrideTags);
} catch (IOException e) {
throw new WorkflowOperationException(e);
}
return destination;
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class SeriesWorkflowOperationHandler method addDublinCoreCatalog.
private MediaPackage addDublinCoreCatalog(DublinCoreCatalog catalog, MediaPackageElementFlavor flavor, MediaPackage mediaPackage) throws WorkflowOperationException {
try (InputStream in = IOUtils.toInputStream(catalog.toXmlString(), "UTF-8")) {
String elementId = UUID.randomUUID().toString();
URI catalogUrl = workspace.put(mediaPackage.getIdentifier().compact(), elementId, "dublincore.xml", in);
logger.info("Adding catalog with flavor {} to mediapackage {}", flavor, mediaPackage);
MediaPackageElement mpe = mediaPackage.add(catalogUrl, MediaPackageElement.Type.Catalog, flavor);
mpe.setIdentifier(elementId);
mpe.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, workspace.get(catalogUrl)));
return mediaPackage;
} catch (IOException | NotFoundException e) {
throw new WorkflowOperationException(e);
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class TagByDublinCoreTermWOH 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 configuredCatalog = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCCATALOG_PROPERTY));
String configuredDCTerm = StringUtils.trimToEmpty(currentOperation.getConfiguration(DCTERM_PROPERTY));
String configuredDefaultValue = StringUtils.trimToNull(currentOperation.getConfiguration(DEFAULT_VALUE_PROPERTY));
String configuredMatchValue = StringUtils.trimToEmpty(currentOperation.getConfiguration(MATCH_VALUE_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));
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));
}
for (String tag : sourceTags) {
elementSelector.addTag(tag);
}
List<String> removeTags = new ArrayList<>();
List<String> addTags = new ArrayList<>();
List<String> overrideTags = new ArrayList<>();
for (String tag : targetTags) {
if (tag.startsWith(MINUS)) {
removeTags.add(tag);
} else if (tag.startsWith(PLUS)) {
addTags.add(tag);
} else {
overrideTags.add(tag);
}
}
// Find Catalog
Catalog[] catalogs = mediaPackage.getCatalogs(new MediaPackageElementFlavor("dublincore", StringUtils.lowerCase(configuredCatalog)));
if (catalogs != null && catalogs.length > 0) {
Boolean foundValue = false;
EName dcterm = new EName(TERMS_NS_URI, configuredDCTerm);
// Find DCTerm
for (Catalog catalog : catalogs) {
DublinCoreCatalog dc = DublinCoreUtil.loadDublinCore(workspace, catalog);
// Match Value
List<DublinCoreValue> values = dc.get(dcterm);
if (values.isEmpty()) {
// Use default
if (configuredDefaultValue != null) {
foundValue = configuredDefaultValue.equals(configuredMatchValue);
}
} else {
foundValue = values.contains(DublinCoreValue.mk(configuredMatchValue));
}
}
if (foundValue) {
if (copy) {
logger.info("Retagging mediapackage elements as a copy");
} else {
logger.info("Retagging mediapackage elements");
}
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);
}
}
}
// if foundValue
}
return createResult(mediaPackage, Action.CONTINUE);
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class CleanupWorkflowOperationHandlerTest method testCreanupWOHwithPreservedFlavorAndCollectionPathPrefix.
@Test
public void testCreanupWOHwithPreservedFlavorAndCollectionPathPrefix() 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.COLLECTION_PATH_PREFIX, "asset", 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.COLLECTION_PATH_PREFIX, "compose", 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.COLLECTION_PATH_PREFIX, "compose", 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.COLLECTION_PATH_PREFIX, "silence", 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.COLLECTION_PATH_PREFIX, "asset", 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.COLLECTION_PATH_PREFIX, "security", 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());
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class CleanupWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
cleanUpJobArgument(workflowInstance);
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
String flavors = currentOperation.getConfiguration(PRESERVE_FLAVOR_PROPERTY);
final List<MediaPackageElementFlavor> flavorsToPreserve = new ArrayList<MediaPackageElementFlavor>();
boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
String delayStr = currentOperation.getConfiguration(DELAY);
int delay = 1;
if (delayStr != null) {
try {
delay = Integer.parseInt(delayStr);
} catch (NumberFormatException e) {
logger.warn("Invalid value '{}' for delay in workflow operation configuration (should be integer)", delayStr);
}
}
if (delay > 0) {
try {
logger.debug("Sleeping {}s before removing workflow files", delay);
Thread.sleep(delay * 1000);
} catch (InterruptedException e) {
// ignore
}
}
// If the configuration does not specify flavors, remove them all
for (String flavor : asList(flavors)) flavorsToPreserve.add(MediaPackageElementFlavor.parseFlavor(flavor));
List<MediaPackageElement> elementsToRemove = new ArrayList<>();
for (MediaPackageElement element : mediaPackage.getElements()) {
if (element.getURI() == null)
continue;
if (!isPreserved(element, flavorsToPreserve))
elementsToRemove.add(element);
}
List<String> externalBaseUrls = null;
if (deleteExternal) {
externalBaseUrls = getAllWorkingFileRepositoryUrls();
externalBaseUrls.remove(workspace.getBaseUri().toString());
}
for (MediaPackageElement elementToRemove : elementsToRemove) {
if (deleteExternal) {
// cleanup external working file repositories
for (String repository : externalBaseUrls) {
logger.debug("Removing {} from repository {}", elementToRemove.getURI(), repository);
try {
removeElementFromRepository(elementToRemove, repository);
} catch (TrustedHttpClientException ex) {
logger.debug("Removing media package element {} from repository {} failed: {}", elementToRemove.getURI(), repository, ex.getMessage());
}
}
}
// cleanup workspace and also the internal working file repository
logger.debug("Removing {} from the workspace", elementToRemove.getURI());
try {
mediaPackage.remove(elementToRemove);
workspace.delete(elementToRemove.getURI());
} catch (NotFoundException ex) {
logger.debug("Workspace doesn't contain element with Id '{}' from media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
} catch (IOException ex) {
logger.warn("Unable to remove element with Id '{}' from the media package '{}': {}", elementToRemove.getIdentifier(), mediaPackage.getIdentifier().compact(), ex.getMessage());
}
}
return createResult(mediaPackage, Action.CONTINUE);
}
Aggregations