use of org.opencastproject.publication.api.PublicationException 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.publication.api.PublicationException 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.publication.api.PublicationException in project opencast by opencast.
the class OaiPmhPublicationServiceRemoteImpl method updateMetadata.
@Override
public Job updateMetadata(MediaPackage mediaPackage, String repository, Set<String> flavors, Set<String> tags, boolean checkAvailability) throws PublicationException {
final String mediapackageXml = MediaPackageParser.getAsXml(mediaPackage);
final List<BasicNameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("mediapackage", mediapackageXml));
params.add(new BasicNameValuePair("channel", repository));
params.add(new BasicNameValuePair("flavors", StringUtils.join(flavors, ";;")));
params.add(new BasicNameValuePair("tags", StringUtils.join(tags, ";;")));
params.add(new BasicNameValuePair("checkAvailability", Boolean.toString(checkAvailability)));
final HttpPost post = new HttpPost("/updateMetadata");
HttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(params, UTF_8));
response = getResponse(post);
if (response != null) {
logger.info("Update media package {} metadata in OAI-PMH channel {} using a remote publication service", mediaPackage.getIdentifier().compact(), repository);
return JobParser.parseJob(response.getEntity().getContent());
}
} catch (Exception e) {
throw new PublicationException(format("Unable to update media package %s metadata in OAI-PMH repository %s using a remote publication service.", mediaPackage.getIdentifier().compact(), repository), e);
} finally {
closeConnection(response);
}
throw new PublicationException(format("Unable to update media package %s metadata in OAI-PMH repository %s using a remote publication service.", mediaPackage.getIdentifier().compact(), repository));
}
use of org.opencastproject.publication.api.PublicationException in project opencast by opencast.
the class OaiPmhPublicationServiceRemoteImpl method retract.
@Override
public Job retract(MediaPackage mediaPackage, String repository) throws PublicationException {
String mediapackageXml = MediaPackageParser.getAsXml(mediaPackage);
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("mediapackage", mediapackageXml));
params.add(new BasicNameValuePair("channel", repository));
HttpPost post = new HttpPost("/retract");
HttpResponse response = null;
post.setEntity(new UrlEncodedFormEntity(params, UTF_8));
try {
response = getResponse(post);
Job receipt = null;
if (response != null) {
logger.info("Retracting {} from OAI-PMH channel {} using a remote publication service", mediaPackage.getIdentifier().compact(), repository);
try {
receipt = JobParser.parseJob(response.getEntity().getContent());
return receipt;
} catch (Exception e) {
throw new PublicationException(format("Unable to retract media package %s from OAI-PMH channel %s using a remote publication service", mediaPackage.getIdentifier().compact(), repository), e);
}
}
} finally {
closeConnection(response);
}
throw new PublicationException(format("Unable to retract media package %s from OAI-PMH channel %s using a remote publication service", mediaPackage.getIdentifier().compact(), repository));
}
use of org.opencastproject.publication.api.PublicationException in project opencast by opencast.
the class OaiPmhPublicationServiceRemoteImpl method publish.
@Override
public Job publish(MediaPackage mediaPackage, String repository, Set<String> downloadIds, Set<String> streamingIds, boolean checkAvailability) throws PublicationException, MediaPackageException {
final String mediapackageXml = MediaPackageParser.getAsXml(mediaPackage);
final List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("mediapackage", mediapackageXml));
params.add(new BasicNameValuePair("channel", repository));
params.add(new BasicNameValuePair("downloadElementIds", StringUtils.join(downloadIds, ";;")));
params.add(new BasicNameValuePair("streamingElementIds", StringUtils.join(streamingIds, ";;")));
params.add(new BasicNameValuePair("checkAvailability", Boolean.toString(checkAvailability)));
final HttpPost post = new HttpPost();
HttpResponse response = null;
try {
post.setEntity(new UrlEncodedFormEntity(params, UTF_8));
response = getResponse(post);
if (response != null) {
logger.info("Publishing media package {} to OAI-PMH channel {} using a remote publication service", mediaPackage, repository);
try {
return JobParser.parseJob(response.getEntity().getContent());
} catch (Exception e) {
throw new PublicationException("Unable to publish media package '" + mediaPackage + "' using a remote OAI-PMH publication service", e);
}
}
} catch (Exception e) {
throw new PublicationException("Unable to publish media package " + mediaPackage + " using a remote OAI-PMH publication service.", e);
} finally {
closeConnection(response);
}
throw new PublicationException("Unable to publish mediapackage " + mediaPackage + " using a remote OAI-PMH publication service.");
}
Aggregations