Search in sources :

Example 6 with IngestException

use of org.opencastproject.ingest.api.IngestException in project opencast by opencast.

the class IngestServiceImpl method schedule.

@Override
public void schedule(MediaPackage mediaPackage, String workflowDefinitionID, Map<String, String> properties) throws IllegalStateException, IngestException, NotFoundException, UnauthorizedException, SchedulerException {
    MediaPackageElement[] mediaPackageElements = mediaPackage.getElementsByFlavor(MediaPackageElements.EPISODE);
    if (mediaPackageElements.length != 1) {
        logger.debug("There can be only one (and exactly one) episode dublin core catalog: https://youtu.be/_J3VeogFUOs");
        throw new IngestException("There can be only one (and exactly one) episode dublin core catalog");
    }
    InputStream inputStream;
    DublinCoreCatalog dublinCoreCatalog;
    try {
        inputStream = workingFileRepository.get(mediaPackage.getIdentifier().toString(), mediaPackageElements[0].getIdentifier());
        dublinCoreCatalog = dublinCoreService.load(inputStream);
    } catch (IOException e) {
        throw new IngestException(e);
    }
    EName temporal = new EName(DublinCore.TERMS_NS_URI, "temporal");
    List<DublinCoreValue> periods = dublinCoreCatalog.get(temporal);
    if (periods.size() != 1) {
        logger.debug("There can be only one (and exactly one) period");
        throw new IngestException("There can be only one (and exactly one) period");
    }
    DCMIPeriod period = EncodingSchemeUtils.decodeMandatoryPeriod(periods.get(0));
    if (!period.hasStart() || !period.hasEnd()) {
        logger.debug("A scheduled recording needs to have a start and end.");
        throw new IngestException("A scheduled recording needs to have a start and end.");
    }
    EName createdEName = new EName(DublinCore.TERMS_NS_URI, "created");
    List<DublinCoreValue> created = dublinCoreCatalog.get(createdEName);
    if (created.size() == 0) {
        logger.debug("Created not set");
    } else if (created.size() == 1) {
        Date date = EncodingSchemeUtils.decodeMandatoryDate(created.get(0));
        if (date.getTime() != period.getStart().getTime()) {
            logger.debug("start and created date differ ({} vs {})", date.getTime(), period.getStart().getTime());
            throw new IngestException("Temporal start and created date differ");
        }
    } else {
        logger.debug("There can be only one created date");
        throw new IngestException("There can be only one created date");
    }
    // spatial
    EName spatial = new EName(DublinCore.TERMS_NS_URI, "spatial");
    List<DublinCoreValue> captureAgents = dublinCoreCatalog.get(spatial);
    if (captureAgents.size() != 1) {
        logger.debug("Exactly one capture agent needs to be set");
        throw new IngestException("Exactly one capture agent needs to be set");
    }
    String captureAgent = captureAgents.get(0).getValue();
    // Go through properties
    Map<String, String> agentProperties = new HashMap<>();
    Map<String, String> workflowProperties = new HashMap<>();
    for (String key : properties.keySet()) {
        if (key.startsWith("org.opencastproject.workflow.config.")) {
            workflowProperties.put(key, properties.get(key));
        } else {
            agentProperties.put(key, properties.get(key));
        }
    }
    try {
        schedulerService.addEvent(period.getStart(), period.getEnd(), captureAgent, new HashSet<>(), mediaPackage, workflowProperties, agentProperties, Opt.none(), Opt.none(), "ingest-service");
    } finally {
        for (MediaPackageElement mediaPackageElement : mediaPackage.getElements()) {
            try {
                workingFileRepository.delete(mediaPackage.getIdentifier().toString(), mediaPackageElement.getIdentifier());
            } catch (IOException e) {
                logger.warn("Failed to delete media package element", e);
            }
        }
    }
}
Also used : EName(org.opencastproject.mediapackage.EName) DublinCoreValue(org.opencastproject.metadata.dublincore.DublinCoreValue) HashMap(java.util.HashMap) ProgressInputStream(org.opencastproject.util.ProgressInputStream) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DCMIPeriod(org.opencastproject.metadata.dublincore.DCMIPeriod) IOException(java.io.IOException) Date(java.util.Date) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) IngestException(org.opencastproject.ingest.api.IngestException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 7 with IngestException

use of org.opencastproject.ingest.api.IngestException in project opencast by opencast.

the class IngestServiceImpl method createSmil.

private MediaPackage createSmil(MediaPackage mediaPackage) throws IOException, IngestException {
    Stream<Track> partialTracks = Stream.empty();
    for (Track track : mediaPackage.getTracks()) {
        Long startTime = partialTrackStartTimes.getIfPresent(track.getIdentifier());
        if (startTime == null)
            continue;
        partialTracks = partialTracks.append(Opt.nul(track));
    }
    // No partial track available return without adding SMIL catalog
    if (partialTracks.isEmpty())
        return mediaPackage;
    // Inspect the partial tracks
    List<Track> tracks = partialTracks.map(newEnrichJob(mediaInspectionService).toFn()).map(payloadAsTrack(getServiceRegistry()).toFn()).each(MediaPackageSupport.updateElement(mediaPackage).toFn().toFx()).toList();
    // Create the SMIL document
    org.w3c.dom.Document smilDocument = SmilUtil.createSmil();
    for (Track track : tracks) {
        Long startTime = partialTrackStartTimes.getIfPresent(track.getIdentifier());
        if (startTime == null) {
            logger.error("No start time found for track {}", track);
            throw new IngestException("No start time found for track " + track.getIdentifier());
        }
        smilDocument = addSmilTrack(smilDocument, track, startTime);
        partialTrackStartTimes.invalidate(track.getIdentifier());
    }
    // Store the SMIL document in the mediapackage
    return addSmilCatalog(smilDocument, mediaPackage);
}
Also used : IngestException(org.opencastproject.ingest.api.IngestException) Track(org.opencastproject.mediapackage.Track)

Example 8 with IngestException

use of org.opencastproject.ingest.api.IngestException in project opencast by opencast.

the class IngestServiceImpl method addPartialTrack.

@Override
public MediaPackage addPartialTrack(InputStream in, String fileName, MediaPackageElementFlavor flavor, long startTime, MediaPackage mediaPackage) throws IOException, IngestException {
    Job job = null;
    try {
        job = serviceRegistry.createJob(JOB_TYPE, INGEST_TRACK, null, null, false);
        job.setStatus(Status.RUNNING);
        job = serviceRegistry.updateJob(job);
        String elementId = UUID.randomUUID().toString();
        logger.info("Start adding partial track {} from input stream on mediapackage {}", elementId, mediaPackage);
        URI newUrl = addContentToRepo(mediaPackage, elementId, fileName, in);
        MediaPackage mp = addContentToMediaPackage(mediaPackage, elementId, newUrl, MediaPackageElement.Type.Track, flavor);
        job.setStatus(Job.Status.FINISHED);
        // store startTime
        partialTrackStartTimes.put(elementId, startTime);
        logger.debug("Added start time {} for track {}", startTime, elementId);
        logger.info("Successful added partial track {} on mediapackage {} at URL {}", elementId, mediaPackage, newUrl);
        return mp;
    } catch (ServiceRegistryException e) {
        throw new IngestException(e);
    } catch (NotFoundException e) {
        throw new IngestException("Unable to update ingest job", e);
    } finally {
        finallyUpdateJob(job);
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) NotFoundException(org.opencastproject.util.NotFoundException) IngestException(org.opencastproject.ingest.api.IngestException) JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Job(org.opencastproject.job.api.Job) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 9 with IngestException

use of org.opencastproject.ingest.api.IngestException in project opencast by opencast.

the class IngestServiceImpl method addAttachment.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#addAttachment(java.io.InputStream, java.lang.String,
 *      org.opencastproject.mediapackage.MediaPackageElementFlavor, org.opencastproject.mediapackage.MediaPackage)
 */
@Override
public MediaPackage addAttachment(InputStream in, String fileName, MediaPackageElementFlavor flavor, String[] tags, MediaPackage mediaPackage) throws IOException, IngestException {
    Job job = null;
    try {
        job = serviceRegistry.createJob(JOB_TYPE, INGEST_ATTACHMENT, null, null, false, ingestFileJobLoad);
        job.setStatus(Status.RUNNING);
        job = serviceRegistry.updateJob(job);
        String elementId = UUID.randomUUID().toString();
        logger.info("Start adding attachment {} from input stream on mediapackage {}", elementId, mediaPackage);
        URI newUrl = addContentToRepo(mediaPackage, elementId, fileName, in);
        MediaPackage mp = addContentToMediaPackage(mediaPackage, elementId, newUrl, MediaPackageElement.Type.Attachment, flavor);
        if (tags != null && tags.length > 0) {
            MediaPackageElement trackElement = mp.getAttachment(elementId);
            for (String tag : tags) {
                logger.info("Adding Tag: " + tag + " to Element: " + elementId);
                trackElement.addTag(tag);
            }
        }
        job.setStatus(Job.Status.FINISHED);
        logger.info("Successful added attachment {} on mediapackage {} at URL {}", elementId, mediaPackage, newUrl);
        return mp;
    } catch (ServiceRegistryException e) {
        throw new IngestException(e);
    } catch (NotFoundException e) {
        throw new IngestException("Unable to update ingest job", e);
    } finally {
        finallyUpdateJob(job);
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) NotFoundException(org.opencastproject.util.NotFoundException) IngestException(org.opencastproject.ingest.api.IngestException) JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Job(org.opencastproject.job.api.Job) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 10 with IngestException

use of org.opencastproject.ingest.api.IngestException in project opencast by opencast.

the class IngestServiceImpl method loadMediaPackageFromManifest.

private MediaPackage loadMediaPackageFromManifest(InputStream manifest) throws IOException, MediaPackageException, IngestException {
    // TODO: Uncomment the following line and remove the patch when the compatibility with pre-1.4 MediaPackages is
    // discarded
    // 
    // mp = builder.loadFromXml(manifestStream);
    // 
    // =========================================================================================
    // =================================== PATCH BEGIN =========================================
    // =========================================================================================
    ByteArrayOutputStream baos = null;
    ByteArrayInputStream bais = null;
    try {
        Document domMP = new SAXBuilder().build(manifest);
        String mpNSUri = "http://mediapackage.opencastproject.org";
        Namespace oldNS = domMP.getRootElement().getNamespace();
        Namespace newNS = Namespace.getNamespace(oldNS.getPrefix(), mpNSUri);
        if (!newNS.equals(oldNS)) {
            @SuppressWarnings("rawtypes") Iterator it = domMP.getDescendants(new ElementFilter(oldNS));
            while (it.hasNext()) {
                Element elem = (Element) it.next();
                elem.setNamespace(newNS);
            }
        }
        baos = new ByteArrayOutputStream();
        new XMLOutputter().output(domMP, baos);
        bais = new ByteArrayInputStream(baos.toByteArray());
        return MediaPackageParser.getFromXml(IOUtils.toString(bais, "UTF-8"));
    } catch (JDOMException e) {
        throw new IngestException("Error unmarshalling mediapackage", e);
    } finally {
        IOUtils.closeQuietly(bais);
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(manifest);
    }
// =========================================================================================
// =================================== PATCH END ===========================================
// =========================================================================================
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) SAXBuilder(org.jdom.input.SAXBuilder) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Element(org.jdom.Element) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException) Namespace(org.jdom.Namespace) ByteArrayInputStream(java.io.ByteArrayInputStream) ElementFilter(org.jdom.filter.ElementFilter) Iterator(java.util.Iterator) IngestException(org.opencastproject.ingest.api.IngestException)

Aggregations

IngestException (org.opencastproject.ingest.api.IngestException)19 NotFoundException (org.opencastproject.util.NotFoundException)14 MediaPackage (org.opencastproject.mediapackage.MediaPackage)12 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)11 URI (java.net.URI)10 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)10 Job (org.opencastproject.job.api.Job)9 JobUtil.waitForJob (org.opencastproject.util.JobUtil.waitForJob)9 IOException (java.io.IOException)8 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)6 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)6 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)5 WorkflowDatabaseException (org.opencastproject.workflow.api.WorkflowDatabaseException)5 WorkflowException (org.opencastproject.workflow.api.WorkflowException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)4 JDOMException (org.jdom.JDOMException)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 HandleException (org.opencastproject.mediapackage.identifier.HandleException)3