use of org.opencastproject.mediapackage.Publication in project opencast by opencast.
the class EventsEndpointTest method testGetEventJsonResponse.
@Ignore
@Test
public void testGetEventJsonResponse() throws Exception {
String eventJson = IOUtils.toString(getClass().getResource("/event-single.json"));
List<String> contributors = new ArrayList<String>();
contributors.add("Physics Department");
List<Publication> publications = getExamplePublications();
Event event = new Event("e6aeb8df-a852-46cd-8128-b89de696f20e", defaultOrg.getId());
event.setArchiveVersion(2L);
event.setCreated("2015-03-12T10:38:54Z");
event.setCreator("Opencast Administrator");
event.setContributors(contributors);
event.setDescription("Cooling without moving parts and using only heat as an input");
event.setDuration(7200000L);
event.setHasPreview(true);
event.setLocation("physics-e-01");
List<String> presenters = new ArrayList<String>();
presenters.add("Prof. A. Einstein");
event.setPresenters(presenters);
event.setPublications(publications);
event.setWorkflowState(WorkflowState.SUCCEEDED);
event.setRecordingStartDate("2015-03-20T04:00:00Z");
event.setSubject("Space Final Frontier, Chemistry");
event.setTitle("Einstein refrigerator");
EventsEndpoint endpoint = new EventsEndpoint();
Response result = ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, endpoint.eventToJSON(event, false, false, false, false));
assertTrue(result.getMetadata().get("Content-Type") != null);
assertEquals("application/v1.0.0+json", result.getMetadata().get("Content-Type").get(0).toString().toLowerCase());
assertThat(eventJson, SameJSONAs.sameJSONAs(result.getEntity().toString()).allowingAnyArrayOrdering());
}
use of org.opencastproject.mediapackage.Publication 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.mediapackage.Publication 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.mediapackage.Publication in project opencast by opencast.
the class OaiPmhPublicationServiceImpl method filterMediaPackage.
/**
* Creates a clone of the media package and removes those elements that do not match the flavor and tags filter
* criteria.
*
* @param mediaPackage
* the media package
* @param flavors
* the flavors
* @param tags
* the tags
* @return the filtered media package
*/
private MediaPackage filterMediaPackage(MediaPackage mediaPackage, Set<MediaPackageElementFlavor> flavors, Set<String> tags) throws MediaPackageException {
if (flavors.isEmpty() && tags.isEmpty())
throw new IllegalArgumentException("Flavors or tags parameter must be set");
MediaPackage filteredMediaPackage = (MediaPackage) mediaPackage.clone();
// The list of elements to keep
List<MediaPackageElement> keep = new ArrayList<>();
SimpleElementSelector selector = new SimpleElementSelector();
// Filter elements
for (MediaPackageElementFlavor flavor : flavors) {
selector.addFlavor(flavor);
}
for (String tag : tags) {
selector.addTag(tag);
}
keep.addAll(selector.select(mediaPackage, true));
// Keep publications
for (Publication p : filteredMediaPackage.getPublications()) keep.add(p);
// Fix references and flavors
for (MediaPackageElement element : filteredMediaPackage.getElements()) {
if (!keep.contains(element)) {
logger.debug("Removing {} '{}' from media package '{}'", element.getElementType().toString().toLowerCase(), element.getIdentifier(), filteredMediaPackage.getIdentifier().toString());
filteredMediaPackage.remove(element);
continue;
}
// Is the element referencing anything?
MediaPackageReference reference = element.getReference();
if (reference != null) {
Map<String, String> referenceProperties = reference.getProperties();
MediaPackageElement referencedElement = mediaPackage.getElementByReference(reference);
// if we are distributing the referenced element, everything is fine. Otherwise...
if (referencedElement != null && !keep.contains(referencedElement)) {
// Follow the references until we find a flavor
MediaPackageElement parent = null;
while ((parent = mediaPackage.getElementByReference(reference)) != null) {
if (parent.getFlavor() != null && element.getFlavor() == null) {
element.setFlavor(parent.getFlavor());
}
if (parent.getReference() == null)
break;
reference = parent.getReference();
}
// Done. Let's cut the path but keep references to the mediapackage itself
if (reference != null && reference.getType().equals(MediaPackageReference.TYPE_MEDIAPACKAGE))
element.setReference(reference);
else if (reference != null && (referenceProperties == null || referenceProperties.size() == 0))
element.clearReference();
else {
// Ok, there is more to that reference than just pointing at an element. Let's keep the original,
// you never know.
referencedElement.setURI(null);
referencedElement.setChecksum(null);
}
}
}
}
return filteredMediaPackage;
}
use of org.opencastproject.mediapackage.Publication in project opencast by opencast.
the class OaiPmhPublicationServiceImplTest method testPublishInternal.
@Ignore
@Test
public void testPublishInternal() throws PublicationException, OaiPmhDatabaseException, MediaPackageException, DistributionException {
OaiPmhDatabase oaiDb = EasyMock.createNiceMock(OaiPmhDatabase.class);
// mock empty DB
EasyMock.expect(oaiDb.search(EasyMock.anyObject(Query.class))).andReturn(new SearchResultImpl(0, 0, new ArrayList<>())).once();
Capture<MediaPackage> storedMpCap = EasyMock.newCapture();
// capture stored media package
oaiDb.store(capture(storedMpCap), eq("default"));
EasyMock.replay(oaiDb);
service.setOaiPmhDatabase(oaiDb);
// mock download distribution service
DownloadDistributionService downloadDistributionService = EasyMock.createNiceMock(DownloadDistributionService.class);
Capture<MediaPackage> downloadDistributedMpCap = EasyMock.newCapture();
Capture<Set<String>> downloadDistributedElemIdsCap = EasyMock.newCapture();
EasyMock.expect(downloadDistributionService.distribute(EasyMock.contains("default"), capture(downloadDistributedMpCap), capture(downloadDistributedElemIdsCap), eq(true))).andAnswer(() -> serviceRegistry.createJob("distribute", "download", null, serializeMediaPackageElements((MediaPackage) EasyMock.getCurrentArguments()[1]))).anyTimes();
EasyMock.replay(downloadDistributionService);
service.setDownloadDistributionService(downloadDistributionService);
// mock streaming distribution service
StreamingDistributionService streamingDistributionService = EasyMock.createNiceMock(StreamingDistributionService.class);
Capture<MediaPackage> streamingDistributedMpCap = EasyMock.newCapture();
Capture<Set<String>> streamingDistributedElemIdsCap = EasyMock.newCapture();
EasyMock.expect(streamingDistributionService.distribute(EasyMock.contains("default"), capture(streamingDistributedMpCap), capture(streamingDistributedElemIdsCap))).andAnswer(() -> serviceRegistry.createJob("distribute", "streaming", null, serializeMediaPackageElements((MediaPackage) EasyMock.getCurrentArguments()[1]))).anyTimes();
EasyMock.replay(streamingDistributionService);
service.setStreamingDistributionService(streamingDistributionService);
Publication publication = service.publish(null, mp, "default", Collections.set("catalog-1", "track-1"), Collections.set("track-1"), true);
Assert.assertNotNull(publication);
Assert.assertNotNull(publication.getChannel());
Assert.assertTrue(publication.getChannel().contains("default"));
Assert.assertNotNull(publication.getURI());
Assert.assertEquals(URI.create(OAI_PMH_SERVER_URL).getHost(), publication.getURI().getHost());
Assert.assertTrue(publication.getURI().getPath().startsWith(OAI_PMH_SERVER_MOUNT_POINT));
Assert.assertTrue(downloadDistributedMpCap.hasCaptured());
// check distributed elements
// download distribution elements
MediaPackage mp = downloadDistributedMpCap.getValue();
Assert.assertEquals(2, mp.getElements().length);
Assert.assertEquals(1, mp.getElementsByFlavor(parseFlavor("dublincore/episode")).length);
Assert.assertNotEquals("catalog-1", mp.getElementsByFlavor(parseFlavor("dublincore/episode"))[0].getIdentifier());
Assert.assertEquals(1, mp.getElementsByFlavor(parseFlavor("presentation/source")).length);
Assert.assertNotEquals("track-1", mp.getElementsByFlavor(parseFlavor("presentation/source"))[0].getIdentifier());
// streaming distribution elements
Assert.assertTrue(streamingDistributedMpCap.hasCaptured());
mp = streamingDistributedMpCap.getValue();
Assert.assertEquals(1, mp.getElements().length);
Assert.assertEquals(1, mp.getElementsByFlavor(parseFlavor("presentation/source")).length);
Assert.assertNotEquals("track-1", mp.getElementsByFlavor(parseFlavor("presentation/source"))[0].getIdentifier());
// check stored media package
Assert.assertTrue(storedMpCap.hasCaptured());
mp = storedMpCap.getValue();
Assert.assertEquals(4, mp.getElements().length);
Assert.assertEquals(1, mp.getElementsByFlavor(parseFlavor("dublincore/episode")).length);
Assert.assertEquals(2, mp.getElementsByFlavor(parseFlavor("presentation/source")).length);
Assert.assertEquals(1, mp.getPublications().length);
}
Aggregations