use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler 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 engage publication workflow operation");
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
// Check which tags have been configured
String downloadSourceTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_TAGS));
String downloadTargetTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_TARGET_TAGS));
String downloadSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_FLAVORS));
String downloadTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(DOWNLOAD_TARGET_SUBFLAVOR));
String streamingSourceTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_TAGS));
String streamingTargetTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_TARGET_TAGS));
String streamingSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_FLAVORS));
String streamingTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(STREAMING_TARGET_SUBFLAVOR));
String republishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
boolean checkAvailability = option(op.getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
String[] sourceDownloadTags = StringUtils.split(downloadSourceTags, ",");
String[] targetDownloadTags = StringUtils.split(downloadTargetTags, ",");
String[] sourceDownloadFlavors = StringUtils.split(downloadSourceFlavors, ",");
String[] sourceStreamingTags = StringUtils.split(streamingSourceTags, ",");
String[] targetStreamingTags = StringUtils.split(streamingTargetTags, ",");
String[] sourceStreamingFlavors = StringUtils.split(streamingSourceFlavors, ",");
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 publication channel");
return createResult(mediaPackage, Action.CONTINUE);
}
// Parse the download target flavor
MediaPackageElementFlavor downloadSubflavor = null;
if (downloadTargetSubflavor != null) {
try {
downloadSubflavor = MediaPackageElementFlavor.parseFlavor(downloadTargetSubflavor);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException(e);
}
}
// Parse the streaming target flavor
MediaPackageElementFlavor streamingSubflavor = null;
if (streamingTargetSubflavor != null) {
try {
streamingSubflavor = MediaPackageElementFlavor.parseFlavor(streamingTargetSubflavor);
} catch (IllegalArgumentException e) {
throw new WorkflowOperationException(e);
}
}
// Configure the download element selector
SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
for (String flavor : sourceDownloadFlavors) {
downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceDownloadTags) {
downloadElementSelector.addTag(tag);
}
// Configure the streaming element selector
SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
for (String flavor : sourceStreamingFlavors) {
streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
}
for (String tag : sourceStreamingTags) {
streamingElementSelector.addTag(tag);
}
// Select the appropriate elements for download and streaming
Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
Collection<MediaPackageElement> streamingElements = streamingElementSelector.select(mediaPackage, false);
try {
Set<String> downloadElementIds = new HashSet<String>();
Set<String> streamingElementIds = new HashSet<String>();
// 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());
}
}
removePublicationElement(mediaPackage);
switch(republishStrategy) {
case ("merge"):
// nothing to do here. other publication strategies can be added to this list later on
break;
default:
retractFromEngage(mediaPackage);
}
List<Job> jobs = new ArrayList<Job>();
// distribute Elements
try {
if (downloadElementIds.size() > 0) {
Job job = downloadDistributionService.distribute(CHANNEL_ID, mediaPackage, downloadElementIds, checkAvailability);
if (job != null) {
jobs.add(job);
}
}
if (distributeStreaming) {
for (String elementId : streamingElementIds) {
Job job = streamingDistributionService.distribute(CHANNEL_ID, mediaPackage, elementId);
if (job != null) {
jobs.add(job);
}
}
}
} catch (DistributionException e) {
throw new WorkflowOperationException(e);
}
if (jobs.size() < 1) {
logger.info("No mediapackage element was found for distribution to engage");
return createResult(mediaPackage, Action.CONTINUE);
}
// Wait until all distribution jobs have returned
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess())
throw new WorkflowOperationException("One of the distribution jobs did not complete successfully");
logger.debug("Distribute of mediapackage {} completed", mediaPackage);
String engageUrlString = null;
try {
MediaPackage mediaPackageForSearch = getMediaPackageForSearchIndex(mediaPackage, jobs, downloadSubflavor, targetDownloadTags, downloadElementIds, streamingSubflavor, streamingElementIds, targetStreamingTags);
// MH-10216, check if only merging into existing mediapackage
removePublicationElement(mediaPackage);
switch(republishStrategy) {
case ("merge"):
// merge() returns merged mediapackage or null mediaPackage is not published
mediaPackageForSearch = merge(mediaPackageForSearch);
if (mediaPackageForSearch == null) {
logger.info("Skipping republish for {} since it is not currently published", mediaPackage.getIdentifier().toString());
return createResult(mediaPackage, Action.SKIP);
}
break;
default:
}
if (!isPublishable(mediaPackageForSearch))
throw new WorkflowOperationException("Media package does not meet criteria for publication");
logger.info("Publishing media package {} to search index", mediaPackageForSearch);
URL engageBaseUrl = null;
engageUrlString = StringUtils.trimToNull(workflowInstance.getOrganization().getProperties().get(ENGAGE_URL_PROPERTY));
if (engageUrlString != null) {
engageBaseUrl = new URL(engageUrlString);
} else {
engageBaseUrl = serverUrl;
logger.info("Using 'server.url' as a fallback for the non-existing organization level key '{}' for the publication url", ENGAGE_URL_PROPERTY);
}
// create the publication URI (used by Admin UI for event details link)
URI engageUri = this.createEngageUri(engageBaseUrl.toURI(), mediaPackage);
// Create new distribution element
Publication publicationElement = PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_ID, engageUri, MimeTypes.parseMimeType("text/html"));
mediaPackage.add(publicationElement);
// Adding media package to the search index
Job publishJob = null;
try {
publishJob = searchService.add(mediaPackageForSearch);
if (!waitForStatus(publishJob).isSuccess()) {
throw new WorkflowOperationException("Mediapackage " + mediaPackageForSearch.getIdentifier() + " could not be published");
}
} catch (SearchException e) {
throw new WorkflowOperationException("Error publishing media package", e);
} catch (MediaPackageException e) {
throw new WorkflowOperationException("Error parsing media package", e);
}
logger.debug("Publishing of mediapackage {} completed", mediaPackage);
return createResult(mediaPackage, Action.CONTINUE);
} catch (MalformedURLException e) {
logger.error("{} is malformed: {}", ENGAGE_URL_PROPERTY, engageUrlString);
throw new WorkflowOperationException(e);
} catch (Throwable t) {
if (t instanceof WorkflowOperationException)
throw (WorkflowOperationException) t;
else
throw new WorkflowOperationException(t);
}
} catch (Exception e) {
if (e instanceof WorkflowOperationException) {
throw (WorkflowOperationException) e;
} else {
throw new WorkflowOperationException(e);
}
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler method retractFromEngage.
/**
* Removes every Publication for Searchindex from Mediapackage
* Removes Mediapackage from Searchindex
* @param mediaPackage Mediapackage
* @param mediaPackageForSearch Mediapackage prepared for searchIndex
* @throws WorkflowOperationException
*/
private void retractFromEngage(MediaPackage mediaPackage) throws WorkflowOperationException {
List<Job> jobs = new ArrayList<Job>();
Set<String> elementIds = new HashSet<String>();
try {
MediaPackage distributedMediaPackage = getDistributedMediapackage(mediaPackage.toString());
if (distributedMediaPackage != null) {
for (MediaPackageElement element : distributedMediaPackage.getElements()) {
elementIds.add(element.getIdentifier());
}
// bulk retraction
if (elementIds.size() > 0) {
Job retractDownloadDistributionJob = downloadDistributionService.retract(CHANNEL_ID, distributedMediaPackage, elementIds);
if (retractDownloadDistributionJob != null) {
jobs.add(retractDownloadDistributionJob);
}
}
if (distributeStreaming) {
for (MediaPackageElement element : distributedMediaPackage.getElements()) {
Job retractStreamingJob = streamingDistributionService.retract(CHANNEL_ID, distributedMediaPackage, element.getIdentifier());
if (retractStreamingJob != null) {
jobs.add(retractStreamingJob);
}
}
}
Job deleteSearchJob = null;
logger.info("Retracting already published Elements for Mediapackage: {}", mediaPackage.getIdentifier().toString());
deleteSearchJob = searchService.delete(mediaPackage.getIdentifier().toString());
if (deleteSearchJob != null) {
jobs.add(deleteSearchJob);
}
}
// Wait until all retraction jobs have returned
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException("One of the retraction jobs did not complete successfully");
}
} catch (DistributionException e) {
throw new WorkflowOperationException(e);
} catch (SearchException e) {
throw new WorkflowOperationException("Error retracting media package", e);
} catch (UnauthorizedException | NotFoundException ex) {
logger.error("Retraction failed of Mediapackage: { }", mediaPackage.getIdentifier().toString(), ex);
}
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class PublishEngageWorkflowOperationHandler method getDistributedMediapackage.
protected MediaPackage getDistributedMediapackage(String mediaPackageID) throws WorkflowOperationException {
MediaPackage mediaPackage = null;
SearchQuery query = new SearchQuery().withId(mediaPackageID);
query.includeEpisodes(true);
query.includeSeries(false);
SearchResult result = searchService.getByQuery(query);
if (result.size() == 0) {
logger.info("The search service doesn't know mediapackage {}.", mediaPackageID);
// i.e. null
return mediaPackage;
} else if (result.size() > 1) {
logger.warn("More than one mediapackage with id {} returned from search service", mediaPackageID);
throw new WorkflowOperationException("More than one mediapackage with id " + mediaPackageID + " found");
} else {
// else, merge the new with the existing (new elements will overwrite existing elements)
mediaPackage = result.getItems()[0].getMediaPackage();
}
return mediaPackage;
}
use of org.opencastproject.workflow.api.WorkflowOperationException 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);
}
use of org.opencastproject.workflow.api.WorkflowOperationException in project opencast by opencast.
the class RetractEngageWorkflowOperationHandler method start.
/**
* {@inheritDoc}
*
* @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(WorkflowInstance, JobContext)
*/
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
MediaPackage mediaPackage = workflowInstance.getMediaPackage();
try {
List<Job> jobs = new ArrayList<Job>();
SearchQuery query = new SearchQuery().withId(mediaPackage.getIdentifier().toString());
SearchResult result = searchService.getByQuery(query);
if (result.size() == 0) {
logger.info("The search service doesn't know mediapackage {}", mediaPackage);
return createResult(mediaPackage, Action.SKIP);
} else if (result.size() > 1) {
logger.warn("More than one mediapackage with id {} returned from search service", mediaPackage.getIdentifier());
throw new WorkflowOperationException("More than one mediapackage with id " + mediaPackage.getIdentifier() + " found");
} else {
Set<String> retractElementIds = new HashSet<String>();
MediaPackage searchMediaPackage = result.getItems()[0].getMediaPackage();
logger.info("Retracting media package {} from download/streaming distribution channel", searchMediaPackage);
for (MediaPackageElement element : searchMediaPackage.getElements()) {
retractElementIds.add(element.getIdentifier());
}
if (retractElementIds.size() > 0) {
Job retractDownloadDistributionJob = downloadDistributionService.retract(CHANNEL_ID, searchMediaPackage, retractElementIds);
if (retractDownloadDistributionJob != null) {
jobs.add(retractDownloadDistributionJob);
}
}
if (distributeStreaming) {
for (MediaPackageElement element : searchMediaPackage.getElements()) {
if (distributeStreaming) {
Job retractStreamingJob = streamingDistributionService.retract(CHANNEL_ID, searchMediaPackage, element.getIdentifier());
if (retractStreamingJob != null) {
jobs.add(retractStreamingJob);
}
}
}
}
}
// Wait for retraction to finish
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException("One of the download/streaming retract job did not complete successfully");
}
logger.debug("Retraction operation complete");
logger.info("Removing media package {} from the search index", mediaPackage);
Job deleteFromSearch = searchService.delete(mediaPackage.getIdentifier().toString());
if (!waitForStatus(deleteFromSearch).isSuccess())
throw new WorkflowOperationException("Removing media package from search did not complete successfully");
logger.debug("Remove from search operation complete");
// Remove publication element
logger.info("Removing engage publication element from media package {}", mediaPackage);
Publication[] publications = mediaPackage.getPublications();
for (Publication publication : publications) {
if (CHANNEL_ID.equals(publication.getChannel())) {
mediaPackage.remove(publication);
logger.debug("Remove engage publication element '{}' complete", publication);
}
}
return createResult(mediaPackage, Action.CONTINUE);
} catch (Throwable t) {
throw new WorkflowOperationException(t);
}
}
Aggregations