use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class DownloadDistributionServiceImpl method retract.
@Override
public Job retract(String channelId, MediaPackage mediapackage, Set<String> elementIds) throws DistributionException {
notNull(mediapackage, "mediapackage");
notNull(elementIds, "elementIds");
notNull(channelId, "channelId");
try {
return serviceRegistry.createJob(JOB_TYPE, Operation.Retract.toString(), Arrays.asList(channelId, MediaPackageParser.getAsXml(mediapackage), gson.toJson(elementIds)), retractJobLoad);
} catch (ServiceRegistryException e) {
throw new DistributionException("Unable to create a job", e);
}
}
use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class DownloadDistributionServiceImpl method retractElement.
/**
* Retract a media package element from the distribution channel. The retracted element must not necessarily be the
* one given as parameter <code>elementId</code>. Instead, the element's distribution URI will be calculated. This way
* you are able to retract elements by providing the "original" element here.
*
* @param channelId
* the channel id
* @param mediapackage
* the mediapackage
* @param element
* the element
* @return the retracted element or <code>null</code> if the element was not retracted
* @throws org.opencastproject.distribution.api.DistributionException
* in case of an error
*/
protected MediaPackageElement retractElement(String channelId, MediaPackage mediapackage, MediaPackageElement element) throws DistributionException {
notNull(mediapackage, "mediapackage");
notNull(element, "element");
notNull(channelId, "channelId");
String mediapackageId = mediapackage.getIdentifier().compact();
String elementId = element.getIdentifier();
try {
final File elementFile = getDistributionFile(channelId, mediapackage, element);
final File mediapackageDir = getMediaPackageDirectory(channelId, mediapackage);
// or has been removed otherwise
if (!elementFile.exists()) {
logger.info("Element {} from media package {} has already been removed or has never been distributed to " + "publication channel {}", elementId, mediapackageId, channelId);
return element;
}
logger.debug("Retracting element {} ({})", element, elementFile);
// Try to remove the file and its parent folder representing the mediapackage element id
if (!FileUtils.deleteQuietly(elementFile.getParentFile())) {
// TODO Removing a folder containing deleted files may fail on NFS volumes. This needs a cleanup strategy.
logger.debug("Unable to delete folder {}", elementFile.getParentFile().getAbsolutePath());
}
if (mediapackageDir.isDirectory() && mediapackageDir.list().length == 0)
FileSupport.delete(mediapackageDir);
logger.debug("Finished retracting element {} of media package {} from publication channel {}", elementId, mediapackageId, channelId);
return element;
} catch (Exception e) {
logger.warn("Error retracting element {} of media package {} from publication channel {}", elementId, mediapackageId, channelId, e);
if (e instanceof DistributionException) {
throw (DistributionException) e;
} else {
throw new DistributionException(e);
}
}
}
use of org.opencastproject.distribution.api.DistributionException 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.distribution.api.DistributionException 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.distribution.api.DistributionException in project opencast by opencast.
the class OaiPmhPublicationServiceImpl method publish.
protected Publication publish(Job job, MediaPackage mediaPackage, String repository, Set<String> downloadElementIds, Set<String> streamingElementIds, boolean checkAvailability) throws PublicationException, MediaPackageException {
String mpId = mediaPackage.getIdentifier().compact();
SearchResult searchResult = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mpId).isDeleted(false).build());
// retract oai-pmh if published
if (searchResult.size() > 0) {
try {
Publication p = retract(job, mediaPackage, repository);
if (mediaPackage.contains(p))
mediaPackage.remove(p);
} catch (NotFoundException e) {
logger.debug("No OAI-PMH publication found for media package {}.", mpId, e);
// this is ok
}
}
List<Job> distributionJobs = new ArrayList<>(2);
if (downloadElementIds != null && !downloadElementIds.isEmpty()) {
// select elements for download distribution
MediaPackage mpDownloadDist = (MediaPackage) mediaPackage.clone();
for (MediaPackageElement mpe : mpDownloadDist.getElements()) {
if (downloadElementIds.contains(mpe.getIdentifier()))
continue;
mpDownloadDist.remove(mpe);
}
// publish to download
if (mpDownloadDist.getElements().length > 0) {
try {
Job downloadDistributionJob = downloadDistributionService.distribute(getPublicationChannelName(repository), mpDownloadDist, downloadElementIds, checkAvailability);
if (downloadDistributionJob != null) {
distributionJobs.add(downloadDistributionJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to distribute media package %s to download distribution.", mpId), e);
}
}
}
if (streamingElementIds != null && !streamingElementIds.isEmpty()) {
// select elements for streaming distribution
MediaPackage mpStreamingDist = (MediaPackage) mediaPackage.clone();
for (MediaPackageElement mpe : mpStreamingDist.getElements()) {
if (streamingElementIds.contains(mpe.getIdentifier()))
continue;
mpStreamingDist.remove(mpe);
}
// publish to streaming
if (mpStreamingDist.getElements().length > 0) {
try {
Job streamingDistributionJob = streamingDistributionService.distribute(getPublicationChannelName(repository), mpStreamingDist, streamingElementIds);
if (streamingDistributionJob != null) {
distributionJobs.add(streamingDistributionJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to distribute media package %s to streaming distribution.", mpId), e);
}
}
}
if (distributionJobs.size() < 0) {
throw new IllegalStateException(format("The media package %s does not contain any elements for publishing to OAI-PMH", mpId));
}
// wait for distribution jobs
if (!waitForJobs(job, serviceRegistry, distributionJobs).isSuccess()) {
throw new PublicationException(format("Unable to distribute elements of media package %s to distribution channels.", mpId));
}
List<MediaPackageElement> distributedElements = new ArrayList<>();
for (Job distributionJob : distributionJobs) {
String distributedElementsXml = distributionJob.getPayload();
if (StringUtils.isNotBlank(distributedElementsXml)) {
for (MediaPackageElement distributedElement : MediaPackageElementParser.getArrayFromXml(distributedElementsXml)) {
distributedElements.add(distributedElement);
}
}
}
MediaPackage oaiPmhDistMp = (MediaPackage) mediaPackage.clone();
// cleanup media package elements
for (MediaPackageElement mpe : oaiPmhDistMp.getElements()) {
// keep publications
if (MediaPackageElement.Type.Publication == mpe.getElementType())
continue;
oaiPmhDistMp.remove(mpe);
}
// ...add the distributed elements
for (MediaPackageElement mpe : distributedElements) {
oaiPmhDistMp.add(mpe);
}
// publish to oai-pmh
try {
oaiPmhDatabase.store(oaiPmhDistMp, repository);
} catch (OaiPmhDatabaseException e) {
// todo: should we retract the elements from download and streaming here?
throw new PublicationException(format("Unable to distribute media package %s to OAI-PMH repository %s", mpId, repository), e);
}
return createPublicationElement(mpId, repository);
}
Aggregations