use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class MediaPackagePostOperationHandlerTest method testHTTPPostJSON.
@Test
public void testHTTPPostJSON() throws Exception {
// create a dummy mediapackage
MediaPackageBuilderFactory factory = MediaPackageBuilderFactory.newInstance();
MediaPackageBuilder builder = factory.newMediaPackageBuilder();
MediaPackage mp = builder.createNew(new IdImpl("xyz"));
mp.setTitle("test");
mp.addContributor("lkiesow");
mp.addContributor("lkiesow");
/* Sending stuff to port 9 shound never return anything as the Discard
* Protocol uses port 9 */
InstanceAndHandler tuple = createWorkflow("http://127.0.0.1:9", "json");
MediaPackagePostOperationHandler handler = (MediaPackagePostOperationHandler) tuple.workflowHandler;
tuple.workflowInstance.setMediaPackage(mp);
try {
tuple.workflowHandler.start(tuple.workflowInstance, null);
/* This should raise an exception. Something is wrong if not. */
Assert.fail();
} catch (WorkflowOperationException e) {
logger.info(e.toString());
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ConfigurablePublishWorkflowOperationHandler method distribute.
private Set<MediaPackageElement> distribute(Collection<MediaPackageElement> elements, MediaPackage mediapackage, String channelId, String mode, boolean checkAvailability) throws WorkflowOperationException {
Set<MediaPackageElement> result = new HashSet<>();
Set<String> bulkElementIds = new HashSet<>();
Set<String> singleElementIds = new HashSet<>();
for (MediaPackageElement element : elements) {
if (MODE_BULK.equals(mode) || (MODE_MIXED.equals(mode) && (element.getElementType() != MediaPackageElement.Type.Track))) {
bulkElementIds.add(element.getIdentifier());
} else {
singleElementIds.add(element.getIdentifier());
}
}
Set<Job> jobs = new HashSet<>();
if (bulkElementIds.size() > 0) {
logger.info("Start bulk publishing of {} elements of media package '{}' to publication channel '{}'", bulkElementIds.size(), mediapackage, channelId);
try {
Job job = distributionService.distribute(channelId, mediapackage, bulkElementIds, checkAvailability);
jobs.add(job);
} catch (DistributionException | MediaPackageException e) {
logger.error("Creating the distribution job for {} elements of media package '{}' failed: {}", bulkElementIds.size(), mediapackage, getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
if (singleElementIds.size() > 0) {
logger.info("Start single publishing of {} elements of media package '{}' to publication channel '{}'", singleElementIds.size(), mediapackage, channelId);
for (String elementId : singleElementIds) {
try {
Job job = distributionService.distribute(channelId, mediapackage, elementId, checkAvailability);
jobs.add(job);
} catch (DistributionException | MediaPackageException e) {
logger.error("Creating the distribution job for element '{}' of media package '{}' failed: {}", elementId, mediapackage, getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
}
if (jobs.size() > 0) {
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException("At least one of the distribution jobs did not complete successfully");
}
for (Job job : jobs) {
try {
List<? extends MediaPackageElement> elems = MediaPackageElementParser.getArrayFromXml(job.getPayload());
result.addAll(elems);
} catch (MediaPackageException e) {
logger.error("Job '{}' returned payload ({}) that could not be parsed to media package elements: {}", job, job.getPayload(), ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
logger.info("Published {} elements of media package {} to publication channel {}", bulkElementIds.size() + singleElementIds.size(), mediapackage, channelId);
}
return result;
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class ConfigurableWorkflowOperationHandlerBase method retractPublicationElements.
/**
* Remove the {@link Publication}'s {@link MediaPackageElement}s from a given channel.
*
* @param channelId
* The channel to remove the {@link MediaPackageElement}s from.
* @param publication
* The {@link Publication} that is being removed.
* @param mp
* The {@link MediaPackage} that the {@link Publication} is part of.
* @return the number of {@link MediaPackageElement}s that have been retracted
* @throws WorkflowOperationException
* Thrown if unable to retract the {@link MediaPackageElement}s.
*/
private int retractPublicationElements(String channelId, Publication publication, MediaPackage mp) throws WorkflowOperationException {
assert ((channelId != null) && (publication != null) && (mp != null));
MediaPackage mediapackageWithPublicationElements = (MediaPackage) mp.clone();
// Add the publications to the mediapackage so that we can use the standard retract
addPublicationElementsToMediaPackage(publication, mediapackageWithPublicationElements);
Set<String> elementIds = new HashSet<>();
for (Attachment attachment : publication.getAttachments()) {
elementIds.add(attachment.getIdentifier());
}
for (Catalog catalog : publication.getCatalogs()) {
elementIds.add(catalog.getIdentifier());
}
for (Track track : publication.getTracks()) {
elementIds.add(track.getIdentifier());
}
if (elementIds.size() > 0) {
logger.info("Retracting {} elements of media package {} from publication channel {}", elementIds.size(), mp, channelId);
Job job = null;
try {
job = getDistributionService().retract(channelId, mediapackageWithPublicationElements, elementIds);
} catch (DistributionException e) {
logger.error("Error while retracting '{}' elements from channel '{}' of distribution '{}': {}", elementIds.size(), channelId, getDistributionService(), getStackTrace(e));
throw new WorkflowOperationException("The retraction job did not complete successfully");
}
if (!waitForStatus(job).isSuccess()) {
throw new WorkflowOperationException("The retraction job did not complete successfully");
}
} else {
logger.debug("No publication elements were found for retraction");
}
return elementIds.size();
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PublishOaiPmhWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
* JobContext)
*/
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
logger.debug("Running distribution workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
// Check which tags have been configured
String downloadTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_TAGS));
String downloadFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(DOWNLOAD_FLAVORS));
String streamingTags = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_TAGS));
String streamingFlavors = StringUtils.trimToEmpty(workflowInstance.getCurrentOperation().getConfiguration(STREAMING_FLAVORS));
boolean checkAvailability = option(workflowInstance.getCurrentOperation().getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
String repository = StringUtils.trimToNull(workflowInstance.getCurrentOperation().getConfiguration(REPOSITORY));
Opt<String> externalChannel = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_CHANNEL_NAME);
Opt<String> externalTempalte = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_TEMPLATE);
Opt<MimeType> externalMimetype = getOptConfig(workflowInstance.getCurrentOperation(), EXTERNAL_MIME_TYPE).bind(MimeTypes.toMimeType);
if (repository == null)
throw new IllegalArgumentException("No repository has been specified");
String[] sourceDownloadTags = StringUtils.split(downloadTags, ",");
String[] sourceDownloadFlavors = StringUtils.split(downloadFlavors, ",");
String[] sourceStreamingTags = StringUtils.split(streamingTags, ",");
String[] sourceStreamingFlavors = StringUtils.split(streamingFlavors, ",");
if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
logger.warn("No tags or flavors have been specified, so nothing will be published to the engage");
return createResult(mediaPackage, Action.CONTINUE);
}
final SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
for (String flavor : sourceDownloadFlavors) {
downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceDownloadTags) {
downloadElementSelector.addTag(tag);
}
final Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
final Collection<MediaPackageElement> streamingElements;
if (distributeStreaming) {
final SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
for (String flavor : sourceStreamingFlavors) {
streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceStreamingTags) {
streamingElementSelector.addTag(tag);
}
streamingElements = streamingElementSelector.select(mediaPackage, false);
} else {
streamingElements = list();
}
try {
Set<String> downloadElementIds = new HashSet<>();
Set<String> streamingElementIds = new HashSet<>();
// Look for elements matching the tag
for (MediaPackageElement elem : downloadElements) {
downloadElementIds.add(elem.getIdentifier());
}
for (MediaPackageElement elem : streamingElements) {
streamingElementIds.add(elem.getIdentifier());
}
// Also distribute the security configuration
// -----
// This was removed in the meantime by a fix for MH-8515, but could now be used again.
// -----
Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
if (securityAttachments != null && securityAttachments.length > 0) {
for (Attachment a : securityAttachments) {
downloadElementIds.add(a.getIdentifier());
streamingElementIds.add(a.getIdentifier());
}
}
Job publishJob = null;
try {
publishJob = publicationService.publish(mediaPackage, repository, downloadElementIds, streamingElementIds, checkAvailability);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Error parsing media package", e);
} catch (PublicationException e) {
throw new WorkflowOperationException("Error parsing media package", e);
}
// Wait until the publication job has returned
if (!waitForStatus(publishJob).isSuccess())
throw new WorkflowOperationException("Mediapackage " + mediaPackage.getIdentifier() + " could not be published to OAI-PMH repository " + repository);
// The job has passed
Job job = serviceRegistry.getJob(publishJob.getId());
// If there is no payload, then the item has not been published.
if (job.getPayload() == null) {
logger.warn("Publish to OAI-PMH repository '{}' failed, no payload from publication job: {}", repository, 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 OAI-PMH repository '{}' failed, unable to parse the payload '{}' from job '{}' to a mediapackage element", repository, job.getPayload(), job.toString());
return createResult(mediaPackage, Action.CONTINUE);
}
for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(newElement.getChannel()).toFn())) {
mediaPackage.remove(existingPublication);
}
mediaPackage.add(newElement);
if (externalChannel.isSome() && externalMimetype.isSome() && externalTempalte.isSome()) {
String template = externalTempalte.get().replace("{event}", mediaPackage.getIdentifier().compact());
if (StringUtils.isNotBlank(mediaPackage.getSeries()))
template = template.replace("{series}", mediaPackage.getSeries());
Publication externalElement = PublicationImpl.publication(UUID.randomUUID().toString(), externalChannel.get(), URI.create(template), externalMimetype.get());
for (Publication existingPublication : $(mediaPackage.getPublications()).find(ofChannel(externalChannel.get()).toFn())) {
mediaPackage.remove(existingPublication);
}
mediaPackage.add(externalElement);
}
logger.debug("Publication to OAI-PMH repository '{}' operation completed", repository);
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
throw new WorkflowOperationException(e);
}
}
return createResult(mediaPackage, Action.CONTINUE);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler method getMediaPackageForSearchIndex.
/**
* Returns a mediapackage that only contains elements that are marked for distribution.
*
* @param current
* the current mediapackage
* @param jobs
* the distribution jobs
* @param downloadSubflavor
* flavor to be applied to elements distributed to download
* @param downloadTargetTags
* tags to be applied to elements distributed to downloads
* @param downloadElementIds
* identifiers for elements that have been distributed to downloads
* @param streamingSubflavor
* flavor to be applied to elements distributed to streaming
* @param streamingElementIds
* identifiers for elements that have been distributed to streaming
* @param streamingTargetTags
* tags to be applied to elements distributed to streaming
* @return the new mediapackage
*/
protected MediaPackage getMediaPackageForSearchIndex(MediaPackage current, List<Job> jobs, MediaPackageElementFlavor downloadSubflavor, String[] downloadTargetTags, Set<String> downloadElementIds, MediaPackageElementFlavor streamingSubflavor, Set<String> streamingElementIds, String[] streamingTargetTags) throws MediaPackageException, NotFoundException, ServiceRegistryException, WorkflowOperationException {
MediaPackage mp = (MediaPackage) current.clone();
// All the jobs have passed, let's update the mediapackage with references to the distributed elements
List<String> elementsToPublish = new ArrayList<String>();
Map<String, String> distributedElementIds = new HashMap<String, String>();
for (Job entry : jobs) {
Job job = serviceRegistry.getJob(entry.getId());
// If there is no payload, then the item has not been distributed.
if (job.getPayload() == null)
continue;
List<MediaPackageElement> distributedElements = null;
try {
distributedElements = (List<MediaPackageElement>) MediaPackageElementParser.getArrayFromXml(job.getPayload());
} catch (MediaPackageException e) {
throw new WorkflowOperationException(e);
}
// kind of element. So we just keep on looping.
if (distributedElements == null || distributedElements.size() < 1)
continue;
for (MediaPackageElement distributedElement : distributedElements) {
String sourceElementId = distributedElement.getIdentifier();
if (sourceElementId != null) {
MediaPackageElement sourceElement = mp.getElementById(sourceElementId);
// Make sure the mediapackage is prompted to create a new identifier for this element
distributedElement.setIdentifier(null);
if (sourceElement != null) {
// Adjust the flavor and tags for downloadable elements
if (downloadElementIds.contains(sourceElementId)) {
if (downloadSubflavor != null) {
MediaPackageElementFlavor flavor = sourceElement.getFlavor();
if (flavor != null) {
MediaPackageElementFlavor newFlavor = new MediaPackageElementFlavor(flavor.getType(), downloadSubflavor.getSubtype());
distributedElement.setFlavor(newFlavor);
}
}
} else // Adjust the flavor and tags for streaming elements
if (streamingElementIds.contains(sourceElementId)) {
if (streamingSubflavor != null && streamingElementIds.contains(sourceElementId)) {
MediaPackageElementFlavor flavor = sourceElement.getFlavor();
if (flavor != null) {
MediaPackageElementFlavor newFlavor = new MediaPackageElementFlavor(flavor.getType(), streamingSubflavor.getSubtype());
distributedElement.setFlavor(newFlavor);
}
}
}
// Copy references from the source elements to the distributed elements
MediaPackageReference ref = sourceElement.getReference();
if (ref != null && mp.getElementByReference(ref) != null) {
MediaPackageReference newReference = (MediaPackageReference) ref.clone();
distributedElement.setReference(newReference);
}
}
}
if (isStreamingFormat(distributedElement))
applyTags(distributedElement, streamingTargetTags);
else
applyTags(distributedElement, downloadTargetTags);
// Add the new element to the mediapackage
mp.add(distributedElement);
elementsToPublish.add(distributedElement.getIdentifier());
distributedElementIds.put(sourceElementId, distributedElement.getIdentifier());
}
}
// Mark everything that is set for removal
List<MediaPackageElement> removals = new ArrayList<MediaPackageElement>();
for (MediaPackageElement element : mp.getElements()) {
if (!elementsToPublish.contains(element.getIdentifier())) {
removals.add(element);
}
}
// Translate references to the distributed artifacts
for (MediaPackageElement element : mp.getElements()) {
if (removals.contains(element))
continue;
// Is the element referencing anything?
MediaPackageReference reference = element.getReference();
if (reference == null)
continue;
// See if the element has been distributed
String distributedElementId = distributedElementIds.get(reference.getIdentifier());
if (distributedElementId == null)
continue;
MediaPackageReference translatedReference = new MediaPackageReferenceImpl(mp.getElementById(distributedElementId));
if (reference.getProperties() != null) {
translatedReference.getProperties().putAll(reference.getProperties());
}
// Set the new reference
element.setReference(translatedReference);
}
// Remove everything we don't want to add to publish
for (MediaPackageElement element : removals) {
mp.remove(element);
}
return mp;
}
Aggregations