use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class DownloadDistributionServiceImpl method retractElements.
/**
* 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 elementIds
* the element identifiers
* @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[] retractElements(String channelId, MediaPackage mediapackage, Set<String> elementIds) throws DistributionException {
notNull(mediapackage, "mediapackage");
notNull(elementIds, "elementIds");
notNull(channelId, "channelId");
Set<MediaPackageElement> elements = getElements(mediapackage, elementIds);
List<MediaPackageElement> retractedElements = new ArrayList<MediaPackageElement>();
for (MediaPackageElement element : elements) {
MediaPackageElement retractedElement = retractElement(channelId, mediapackage, element);
retractedElements.add(retractedElement);
}
return retractedElements.toArray(new MediaPackageElement[retractedElements.size()]);
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class DownloadDistributionServiceImplTest method testRetractByDistributed.
@Test
public void testRetractByDistributed() throws Exception {
int elementCount = mp.getElements().length;
// Distribute the mediapackage and all of its elements
Job job1 = service.distribute("engage-player", mp, "track-1");
Job job2 = service.distribute("engage-player", mp, "catalog-1");
Job job3 = service.distribute("engage-player", mp, "catalog-2");
Job job4 = service.distribute("engage-player", mp, "notes");
Job job5 = service.distribute("oai-pmh", mp, "notes");
JobBarrier jobBarrier = new JobBarrier(null, serviceRegistry, 500, job1, job2, job3, job4, job5);
jobBarrier.waitForJobs();
// Add the new elements to the mediapackage
MediaPackageElement job1Element = MediaPackageElementParser.getFromXml(job1.getPayload());
mp.add(job1Element);
MediaPackageElement job2Element = MediaPackageElementParser.getFromXml(job2.getPayload());
mp.add(job2Element);
MediaPackageElement job3Element = MediaPackageElementParser.getFromXml(job3.getPayload());
mp.add(job3Element);
MediaPackageElement job4Element = MediaPackageElementParser.getFromXml(job4.getPayload());
mp.add(job4Element);
MediaPackageElement job5Element = MediaPackageElementParser.getFromXml(job5.getPayload());
mp.add(job5Element);
File mpDir = new File(distributionRoot, PathSupport.path(defaultOrganization.getId(), "engage-player", mp.getIdentifier().compact()));
File mediaDir = new File(mpDir, "track-1");
File metadata1Dir = new File(mpDir, "catalog-1");
File metadata2Dir = new File(mpDir, "catalog-2");
File attachmentsDir = new File(mpDir, "notes");
Assert.assertTrue(mediaDir.exists());
Assert.assertTrue(metadata1Dir.exists());
Assert.assertTrue(metadata2Dir.exists());
Assert.assertTrue(attachmentsDir.exists());
// the filenames are changed to reflect the element ID
Assert.assertTrue(new File(mediaDir, "media.mov").exists());
Assert.assertTrue(new File(metadata1Dir, "dublincore.xml").exists());
Assert.assertTrue(new File(metadata2Dir, "mpeg7.xml").exists());
Assert.assertTrue(new File(attachmentsDir, "attachment.txt").exists());
// Now retract the mediapackage and ensure that the distributed files have been removed
Job job6 = service.retract("engage-player", mp, job1Element.getIdentifier());
Job job7 = service.retract("engage-player", mp, job2Element.getIdentifier());
Job job8 = service.retract("engage-player", mp, job3Element.getIdentifier());
Job job9 = service.retract("engage-player", mp, job4Element.getIdentifier());
jobBarrier = new JobBarrier(null, serviceRegistry, 500, job6, job7, job8, job9);
jobBarrier.waitForJobs();
// Remove the distributed elements from the mediapackage
mp.remove(MediaPackageElementParser.getFromXml(job6.getPayload()));
mp.remove(MediaPackageElementParser.getFromXml(job7.getPayload()));
mp.remove(MediaPackageElementParser.getFromXml(job8.getPayload()));
mp.remove(MediaPackageElementParser.getFromXml(job9.getPayload()));
// +1 since the oai-pmh distribution has not been retracted
Assert.assertEquals(elementCount + 1, mp.getElements().length);
Assert.assertNotNull(mp.getElementById("track-1"));
Assert.assertNotNull(mp.getElementById("catalog-1"));
Assert.assertNotNull(mp.getElementById("catalog-2"));
Assert.assertNotNull(mp.getElementById("notes"));
Assert.assertFalse(service.getDistributionFile("engage-player", mp, mp.getElementById("track-1")).isFile());
Assert.assertFalse(service.getDistributionFile("engage-player", mp, mp.getElementById("catalog-1")).isFile());
Assert.assertFalse(service.getDistributionFile("engage-player", mp, mp.getElementById("catalog-2")).isFile());
Assert.assertFalse(service.getDistributionFile("engage-player", mp, mp.getElementById("notes")).isFile());
Assert.assertTrue(service.getDistributionFile("oai-pmh", mp, mp.getElementById("notes")).isFile());
}
use of org.opencastproject.mediapackage.MediaPackageElement 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);
}
}
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class StreamingDistributionServiceImpl method distributeElements.
/**
* Distribute Mediapackage elements to the download distribution service.
*
* @param channelId The id of the publication channel to be distributed to.
* @param mediapackage The media package that contains the elements to be distributed.
* @param elementIds The ids of the elements that should be distributed
* contained within the media package.
* @return A reference to the MediaPackageElements that have 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.
*/
public MediaPackageElement[] distributeElements(String channelId, MediaPackage mediapackage, Set<String> elementIds) throws DistributionException {
notNull(mediapackage, "mediapackage");
notNull(elementIds, "elementIds");
notNull(channelId, "channelId");
final Set<MediaPackageElement> elements = getElements(mediapackage, elementIds);
List<MediaPackageElement> distributedElements = new ArrayList<>();
for (MediaPackageElement element : elements) {
if (MediaPackageElement.Type.Track.equals(element.getElementType())) {
// Streaming servers only deal with tracks
MediaPackageElement distributedElement = distributeElement(channelId, mediapackage, element.getIdentifier());
distributedElements.add(distributedElement);
} else {
logger.warn("Skipping {} {} for distribution to the streaming server (only media tracks supported)", element.getElementType().toString().toLowerCase(), element.getIdentifier());
}
}
return distributedElements.toArray(new MediaPackageElement[distributedElements.size()]);
}
use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.
the class StreamingDistributionServiceImpl method retractElements.
/**
* 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 elementIds
* the element identifiers
* @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[] retractElements(String channelId, MediaPackage mediapackage, Set<String> elementIds) throws DistributionException {
notNull(mediapackage, "mediapackage");
notNull(elementIds, "elementIds");
notNull(channelId, "channelId");
Set<MediaPackageElement> elements = getElements(mediapackage, elementIds);
List<MediaPackageElement> retractedElements = new ArrayList<>();
for (MediaPackageElement element : elements) {
MediaPackageElement retractedElement = retractElement(channelId, mediapackage, element.getIdentifier());
retractedElements.add(retractedElement);
}
return retractedElements.toArray(new MediaPackageElement[retractedElements.size()]);
}
Aggregations