Search in sources :

Example 96 with MediaPackageElement

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()]);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList)

Example 97 with MediaPackageElement

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());
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) File(java.io.File) Test(org.junit.Test)

Example 98 with MediaPackageElement

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);
        }
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) NotFoundException(org.opencastproject.util.NotFoundException) DistributionException(org.opencastproject.distribution.api.DistributionException) IOException(java.io.IOException) File(java.io.File) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Example 99 with MediaPackageElement

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()]);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList)

Example 100 with MediaPackageElement

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()]);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList)

Aggregations

MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)153 MediaPackage (org.opencastproject.mediapackage.MediaPackage)72 NotFoundException (org.opencastproject.util.NotFoundException)50 Job (org.opencastproject.job.api.Job)49 URI (java.net.URI)48 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)39 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)39 Test (org.junit.Test)36 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)27 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)25 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)24 File (java.io.File)21 Track (org.opencastproject.mediapackage.Track)21 DistributionException (org.opencastproject.distribution.api.DistributionException)20 InputStream (java.io.InputStream)19 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 HashSet (java.util.HashSet)18 URISyntaxException (java.net.URISyntaxException)16 Path (javax.ws.rs.Path)16