use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class AwsS3DistributionServiceRemoteImpl method retract.
@Override
public Job retract(String channelId, MediaPackage mediaPackage, Set<String> elementIds) throws DistributionException {
logger.info(format("Retracting %s elements from %s@%s", elementIds.size(), channelId, distributionChannel));
final HttpPost req = post("/retract", param(PARAM_MEDIAPACKAGE, MediaPackageParser.getAsXml(mediaPackage)), param(PARAM_ELEMENT_ID, gson.toJson(elementIds)), param(PARAM_CHANNEL_ID, channelId));
for (Job job : join(runRequest(req, jobFromHttpResponse))) {
return job;
}
throw new DistributionException(format("Unable to retract '%s' elements of " + "mediapackage '%s' using a remote destribution service proxy", elementIds.size(), mediaPackage.getIdentifier().toString()));
}
use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class LiveScheduleServiceImpl method retractPreviousElements.
void retractPreviousElements(MediaPackage previousMp, MediaPackage newMp) throws LiveScheduleException {
try {
// Now can retract elements from previous publish. Before creating a retraction
// job, check if the element url is still used by the new media package.
Set<String> elementIds = new HashSet<String>();
for (MediaPackageElement element : previousMp.getElements()) {
// We don't retract tracks because they are just live links
if (!Track.TYPE.equals(element.getElementType())) {
boolean canBeDeleted = true;
for (MediaPackageElement newElement : newMp.getElements()) {
if (element.getURI().equals(newElement.getURI())) {
logger.debug("Not retracting element {} with URI {} from download distribution because it is still used by updated live media package", element.getIdentifier(), element.getURI());
canBeDeleted = false;
break;
}
}
if (canBeDeleted)
elementIds.add(element.getIdentifier());
}
}
if (elementIds.size() > 0) {
Job job = downloadDistributionService.retract(CHANNEL_ID, previousMp, elementIds);
// Wait for retraction to finish
if (!waitForStatus(job).isSuccess())
logger.warn("One of the download retract jobs did not complete successfully");
else
logger.debug("Retraction of previously published elements complete");
}
} catch (DistributionException e) {
throw new LiveScheduleException(e);
}
}
use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class OaiPmhPublicationServiceImpl method retract.
protected Publication retract(Job job, MediaPackage mediaPackage, String repository) throws PublicationException, NotFoundException {
String mpId = mediaPackage.getIdentifier().compact();
// track elements for retraction
MediaPackage oaiPmhMp = null;
SearchResult searchResult = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mpId).isDeleted(false).build());
for (SearchResultItem searchResultItem : searchResult.getItems()) {
if (oaiPmhMp == null) {
oaiPmhMp = searchResultItem.getMediaPackage();
} else {
for (MediaPackageElement mpe : searchResultItem.getMediaPackage().getElements()) {
oaiPmhMp.add(mpe);
}
}
}
// retract oai-pmh
try {
oaiPmhDatabase.delete(mpId, repository);
} catch (OaiPmhDatabaseException e) {
throw new PublicationException(format("Unable to retract media package %s from OAI-PMH repository %s", mpId, repository), e);
}
if (oaiPmhMp != null && oaiPmhMp.getElements().length > 0) {
// retract files from distribution channels
Set<String> mpeIds = new HashSet<>();
for (MediaPackageElement mpe : oaiPmhMp.elements()) {
if (MediaPackageElement.Type.Publication == mpe.getElementType())
continue;
mpeIds.add(mpe.getIdentifier());
}
if (!mpeIds.isEmpty()) {
List<Job> retractionJobs = new ArrayList<>();
// retract download
try {
Job retractDownloadJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhMp, mpeIds);
if (retractDownloadJob != null) {
retractionJobs.add(retractDownloadJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to create retraction job from distribution channel download for the media package %s ", mpId), e);
}
// retract streaming
try {
Job retractDownloadJob = streamingDistributionService.retract(getPublicationChannelName(repository), oaiPmhMp, mpeIds);
if (retractDownloadJob != null) {
retractionJobs.add(retractDownloadJob);
}
} catch (DistributionException e) {
throw new PublicationException(format("Unable to create retraction job from distribution channel streaming for the media package %s ", mpId), e);
}
if (retractionJobs.size() > 0) {
// wait for distribution jobs
if (!waitForJobs(job, serviceRegistry, retractionJobs).isSuccess())
throw new PublicationException(format("Unable to retract elements of media package %s from distribution channels.", mpId));
}
}
}
String publicationChannel = getPublicationChannelName(repository);
for (Publication p : mediaPackage.getPublications()) {
if (StringUtils.equals(publicationChannel, p.getChannel()))
return p;
}
return null;
}
use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class OaiPmhPublicationServiceImpl method updateMetadata.
protected Publication updateMetadata(Job job, MediaPackage mediaPackage, String repository, Set<String> flavors, Set<String> tags, boolean checkAvailability) throws PublicationException {
final Set<MediaPackageElementFlavor> parsedFlavors = new HashSet<>();
for (String flavor : flavors) {
parsedFlavors.add(MediaPackageElementFlavor.parseFlavor(flavor));
}
final MediaPackage filteredMp;
final SearchResult result = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mediaPackage).isDeleted(false).build());
if (result.size() == 1) {
// apply tags and flavors to the current media package
try {
logger.debug("filter elements with flavors {} and tags {} on media package {}", StringUtils.join(flavors, ", "), StringUtils.join(tags, ", "), MediaPackageParser.getAsXml(mediaPackage));
filteredMp = filterMediaPackage(mediaPackage, parsedFlavors, tags);
} catch (MediaPackageException e) {
throw new PublicationException("Error filtering media package", e);
}
} else if (result.size() == 0) {
logger.info(format("Skipping update of media package %s since it is not currently published to %s", mediaPackage, repository));
return null;
} else {
final String msg = format("More than one media package with id %s found", mediaPackage.getIdentifier().compact());
logger.warn(msg);
throw new PublicationException(msg);
}
// re-distribute elements to download
Set<String> elementIdsToDistribute = new HashSet<>();
for (MediaPackageElement mpe : filteredMp.getElements()) {
// do not distribute publications
if (MediaPackageElement.Type.Publication == mpe.getElementType())
continue;
elementIdsToDistribute.add(mpe.getIdentifier());
}
if (elementIdsToDistribute.isEmpty()) {
logger.debug("The media package {} does not contain any elements to update. " + "Skip OAI-PMH metadata update operation for repository {}", mediaPackage.getIdentifier().compact(), repository);
return null;
}
logger.debug("distribute elements {}", StringUtils.join(elementIdsToDistribute, ", "));
final List<MediaPackageElement> distributedElements = new ArrayList<>();
try {
Job distJob = downloadDistributionService.distribute(getPublicationChannelName(repository), filteredMp, elementIdsToDistribute, checkAvailability);
if (job == null)
throw new PublicationException("The distribution service can not handle this type of media package elements.");
if (!waitForJobs(job, serviceRegistry, distJob).isSuccess()) {
throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()));
}
if (distJob.getPayload() != null) {
for (MediaPackageElement mpe : MediaPackageElementParser.getArrayFromXml(distJob.getPayload())) {
distributedElements.add(mpe);
}
}
} catch (DistributionException | MediaPackageException e) {
throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()), e);
}
// update elements (URLs)
for (MediaPackageElement e : filteredMp.getElements()) {
if (MediaPackageElement.Type.Publication.equals(e.getElementType()))
continue;
filteredMp.remove(e);
}
for (MediaPackageElement e : distributedElements) {
filteredMp.add(e);
}
MediaPackage publishedMp = merge(filteredMp, removeMatchingNonExistantElements(filteredMp, (MediaPackage) result.getItems().get(0).getMediaPackage().clone(), parsedFlavors, tags));
// Does the media package have a title and track?
if (!MediaPackageSupport.isPublishable(publishedMp)) {
throw new PublicationException("Media package does not meet criteria for publication");
}
// Publish the media package to OAI-PMH
try {
logger.debug(format("Updating metadata of media package %s in %s", publishedMp.getIdentifier().compact(), repository));
oaiPmhDatabase.store(publishedMp, repository);
} catch (OaiPmhDatabaseException e) {
throw new PublicationException(format("Media package %s could not be updated", publishedMp.getIdentifier().compact()));
}
// retract orphaned elements from download distribution
// orphaned elements are all those elements to which the updated media package no longer refers (in terms of element uri)
Map<URI, MediaPackageElement> elementUriMap = new Hashtable<>();
for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
for (MediaPackageElement mpe : oaiPmhSearchResultItem.getMediaPackage().getElements()) {
if (MediaPackageElement.Type.Publication == mpe.getElementType() || null == mpe.getURI())
continue;
elementUriMap.put(mpe.getURI(), mpe);
}
}
for (MediaPackageElement publishedMpe : publishedMp.getElements()) {
if (MediaPackageElement.Type.Publication == publishedMpe.getElementType())
continue;
if (elementUriMap.containsKey(publishedMpe.getURI()))
elementUriMap.remove(publishedMpe.getURI());
}
Set<String> orphanedElementIds = new HashSet<>();
for (MediaPackageElement orphanedMpe : elementUriMap.values()) {
orphanedElementIds.add(orphanedMpe.getIdentifier());
}
if (!orphanedElementIds.isEmpty()) {
for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
try {
Job retractJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhSearchResultItem.getMediaPackage(), orphanedElementIds);
if (retractJob != null) {
if (!waitForJobs(job, serviceRegistry, retractJob).isSuccess())
logger.warn("The download distribution retract job for the orphaned elements from media package {} does not end successfully", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact());
}
} catch (DistributionException e) {
logger.warn("Unable to retract orphaned elements from download distribution service for the media package {} channel {}", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact(), getPublicationChannelName(repository), e);
}
}
}
// return the publication
String publicationChannel = getPublicationChannelName(repository);
for (Publication p : mediaPackage.getPublications()) {
if (StringUtils.equals(publicationChannel, p.getChannel()))
return p;
}
return null;
}
use of org.opencastproject.distribution.api.DistributionException in project opencast by opencast.
the class StreamingDistributionServiceImpl method distributeElement.
/**
* Distribute a Mediapackage element to the download distribution service.
*
* @param mp
* The media package that contains the element to distribute.
* @param mpeId
* The id of the element that should be distributed contained within the media package.
* @return A reference to the MediaPackageElement that has been distributed.
* @throws DistributionException
* Thrown if the parent directory of the MediaPackageElement cannot be created, if the MediaPackageElement
* cannot be copied or another unexpected exception occurs.
*/
private MediaPackageElement distributeElement(String channelId, final MediaPackage mp, String mpeId) throws DistributionException {
RequireUtil.notNull(channelId, "channelId");
RequireUtil.notNull(mp, "mp");
RequireUtil.notNull(mpeId, "mpeId");
//
final MediaPackageElement element = mp.getElementById(mpeId);
// Make sure the element exists
if (element == null) {
throw new IllegalStateException("No element " + mpeId + " found in media package");
}
try {
File source;
try {
source = workspace.get(element.getURI());
} catch (NotFoundException e) {
throw new DistributionException("Unable to find " + element.getURI() + " in the workspace", e);
} catch (IOException e) {
throw new DistributionException("Error loading " + element.getURI() + " from the workspace", e);
}
// Try to find a duplicated element source
try {
source = findDuplicatedElementSource(source, mp.getIdentifier().compact());
} catch (IOException e) {
logger.warn("Unable to find duplicated source {}: {}", source, ExceptionUtils.getMessage(e));
}
final File destination = locations.get().createDistributionFile(securityService.getOrganization().getId(), channelId, mp.getIdentifier().compact(), element.getIdentifier(), element.getURI());
if (!destination.equals(source)) {
// Put the file in place if sourcesfile differs destinationfile
try {
FileUtils.forceMkdir(destination.getParentFile());
} catch (IOException e) {
throw new DistributionException("Unable to create " + destination.getParentFile(), e);
}
logger.info("Distributing {} to {}", mpeId, destination);
try {
FileSupport.link(source, destination, true);
} catch (IOException e) {
throw new DistributionException("Unable to copy " + source + " to " + destination, e);
}
}
// Create a representation of the distributed file in the mediapackage
final MediaPackageElement distributedElement = (MediaPackageElement) element.clone();
distributedElement.setURI(locations.get().createDistributionUri(securityService.getOrganization().getId(), channelId, mp.getIdentifier().compact(), element.getIdentifier(), element.getURI()));
distributedElement.setIdentifier(null);
((TrackImpl) distributedElement).setTransport(TrackImpl.StreamingProtocol.RTMP);
logger.info("Finished distribution of {}", element);
return distributedElement;
} catch (Exception e) {
logger.warn("Error distributing " + element, e);
if (e instanceof DistributionException) {
throw (DistributionException) e;
} else {
throw new DistributionException(e);
}
}
}
Aggregations