Search in sources :

Example 16 with NotFoundException

use of org.opencastproject.util.NotFoundException 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 17 with NotFoundException

use of org.opencastproject.util.NotFoundException in project opencast by opencast.

the class IngestServiceImpl method addZippedMediaPackage.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.ingest.api.IngestService#addZippedMediaPackage(java.io.InputStream, java.lang.String,
 *      java.util.Map, java.lang.Long)
 */
@Override
public WorkflowInstance addZippedMediaPackage(InputStream zipStream, String workflowDefinitionId, Map<String, String> workflowConfig, Long workflowInstanceId) throws MediaPackageException, IOException, IngestException, NotFoundException, UnauthorizedException {
    // Start a job synchronously. We can't keep the open input stream waiting around.
    Job job = null;
    if (StringUtils.isNotBlank(workflowDefinitionId)) {
        try {
            workflowService.getWorkflowDefinitionById(workflowDefinitionId);
        } catch (WorkflowDatabaseException e) {
            throw new IngestException(e);
        } catch (NotFoundException nfe) {
            logger.warn("Workflow definition {} not found, using default workflow {} instead", workflowDefinitionId, defaultWorkflowDefinionId);
            workflowDefinitionId = defaultWorkflowDefinionId;
        }
    }
    if (workflowInstanceId != null) {
        logger.warn("Deprecated method! Ingesting zipped mediapackage with workflow {}", workflowInstanceId);
    } else {
        logger.info("Ingesting zipped mediapackage");
    }
    ZipArchiveInputStream zis = null;
    Set<String> collectionFilenames = new HashSet<>();
    try {
        // We don't need anybody to do the dispatching for us. Therefore we need to make sure that the job is never in
        // QUEUED state but set it to INSTANTIATED in the beginning and then manually switch it to RUNNING.
        job = serviceRegistry.createJob(JOB_TYPE, INGEST_ZIP, null, null, false, ingestZipJobLoad);
        job.setStatus(Status.RUNNING);
        job = serviceRegistry.updateJob(job);
        // Create the working file target collection for this ingest operation
        String wfrCollectionId = Long.toString(job.getId());
        zis = new ZipArchiveInputStream(zipStream);
        ZipArchiveEntry entry;
        MediaPackage mp = null;
        Map<String, URI> uris = new HashMap<>();
        // Sequential number to append to file names so that, if two files have the same
        // name, one does not overwrite the other (see MH-9688)
        int seq = 1;
        // Folder name to compare with next one to figure out if there's a root folder
        String folderName = null;
        // Indicates if zip has a root folder or not, initialized as true
        boolean hasRootFolder = true;
        // While there are entries write them to a collection
        while ((entry = zis.getNextZipEntry()) != null) {
            try {
                if (entry.isDirectory() || entry.getName().contains("__MACOSX"))
                    continue;
                if (entry.getName().endsWith("manifest.xml") || entry.getName().endsWith("index.xml")) {
                    // Build the mediapackage
                    mp = loadMediaPackageFromManifest(new ZipEntryInputStream(zis, entry.getSize()));
                } else {
                    logger.info("Storing zip entry {}/{} in working file repository collection '{}'", job.getId(), entry.getName(), wfrCollectionId);
                    // Since the directory structure is not being mirrored, makes sure the file
                    // name is different than the previous one(s) by adding a sequential number
                    String fileName = FilenameUtils.getBaseName(entry.getName()) + "_" + seq++ + "." + FilenameUtils.getExtension(entry.getName());
                    URI contentUri = workingFileRepository.putInCollection(wfrCollectionId, fileName, new ZipEntryInputStream(zis, entry.getSize()));
                    collectionFilenames.add(fileName);
                    // Key is the zip entry name as it is
                    String key = entry.getName();
                    uris.put(key, contentUri);
                    ingestStatistics.add(entry.getSize());
                    logger.info("Zip entry {}/{} stored at {}", job.getId(), entry.getName(), contentUri);
                    // Figures out if there's a root folder. Does entry name starts with a folder?
                    int pos = entry.getName().indexOf('/');
                    if (pos == -1) {
                        // No, we can conclude there's no root folder
                        hasRootFolder = false;
                    } else if (hasRootFolder && folderName != null && !folderName.equals(entry.getName().substring(0, pos))) {
                        // Folder name different from previous so there's no root folder
                        hasRootFolder = false;
                    } else if (folderName == null) {
                        // Just initialize folder name
                        folderName = entry.getName().substring(0, pos);
                    }
                }
            } catch (IOException e) {
                logger.warn("Unable to process zip entry {}: {}", entry.getName(), e);
                throw e;
            }
        }
        if (mp == null)
            throw new MediaPackageException("No manifest found in this zip");
        // Determine the mediapackage identifier
        if (mp.getIdentifier() == null || isBlank(mp.getIdentifier().toString()))
            mp.setIdentifier(new UUIDIdBuilderImpl().createNew());
        String mediaPackageId = mp.getIdentifier().toString();
        logger.info("Ingesting mediapackage {} is named '{}'", mediaPackageId, mp.getTitle());
        // Make sure there are tracks in the mediapackage
        if (mp.getTracks().length == 0) {
            logger.warn("Mediapackage {} has no media tracks", mediaPackageId);
        }
        // Update the element uris to point to their working file repository location
        for (MediaPackageElement element : mp.elements()) {
            // Key has root folder name if there is one
            URI uri = uris.get((hasRootFolder ? folderName + "/" : "") + element.getURI().toString());
            if (uri == null)
                throw new MediaPackageException("Unable to map element name '" + element.getURI() + "' to workspace uri");
            logger.info("Ingested mediapackage element {}/{} located at {}", mediaPackageId, element.getIdentifier(), uri);
            URI dest = workingFileRepository.moveTo(wfrCollectionId, FilenameUtils.getName(uri.toString()), mediaPackageId, element.getIdentifier(), FilenameUtils.getName(element.getURI().toString()));
            element.setURI(dest);
            // TODO: This should be triggered somehow instead of being handled here
            if (MediaPackageElements.SERIES.equals(element.getFlavor())) {
                logger.info("Ingested mediapackage {} contains updated series information", mediaPackageId);
                updateSeries(element.getURI());
            }
        }
        // Now that all elements are in place, start with ingest
        logger.info("Initiating processing of ingested mediapackage {}", mediaPackageId);
        WorkflowInstance workflowInstance = ingest(mp, workflowDefinitionId, workflowConfig, workflowInstanceId);
        logger.info("Ingest of mediapackage {} done", mediaPackageId);
        job.setStatus(Job.Status.FINISHED);
        return workflowInstance;
    } catch (ServiceRegistryException e) {
        throw new IngestException(e);
    } catch (MediaPackageException e) {
        job.setStatus(Job.Status.FAILED, Job.FailureReason.DATA);
        throw e;
    } catch (Exception e) {
        if (e instanceof IngestException)
            throw (IngestException) e;
        throw new IngestException(e);
    } finally {
        IOUtils.closeQuietly(zis);
        finallyUpdateJob(job);
        for (String filename : collectionFilenames) {
            workingFileRepository.deleteFromCollection(Long.toString(job.getId()), filename, true);
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ZipArchiveInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveInputStream) HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) IngestException(org.opencastproject.ingest.api.IngestException) HandleException(org.opencastproject.mediapackage.identifier.HandleException) ConfigurationException(org.opencastproject.util.ConfigurationException) IOException(java.io.IOException) JDOMException(org.jdom.JDOMException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IngestException(org.opencastproject.ingest.api.IngestException) JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) UUIDIdBuilderImpl(org.opencastproject.mediapackage.identifier.UUIDIdBuilderImpl)

Example 18 with NotFoundException

use of org.opencastproject.util.NotFoundException in project opencast by opencast.

the class IngestServiceImplTest method testSmilCreation.

@Test
public void testSmilCreation() throws Exception {
    service.setWorkingFileRepository(new WorkingFileRepositoryImpl() {

        @Override
        public URI put(String mediaPackageID, String mediaPackageElementID, String filename, InputStream in) throws IOException {
            File file = new File(FileUtils.getTempDirectory(), mediaPackageElementID);
            file.deleteOnExit();
            FileUtils.write(file, IOUtils.toString(in), "UTF-8");
            return file.toURI();
        }

        @Override
        public InputStream get(String mediaPackageID, String mediaPackageElementID) throws NotFoundException, IOException {
            File file = new File(FileUtils.getTempDirectory(), mediaPackageElementID);
            return new FileInputStream(file);
        }
    });
    URI presenterUri = URI.create("http://localhost:8080/presenter.mp4");
    URI presenterUri2 = URI.create("http://localhost:8080/presenter2.mp4");
    URI presentationUri = URI.create("http://localhost:8080/presentation.mp4");
    MediaPackage mediaPackage = service.createMediaPackage();
    Catalog[] catalogs = mediaPackage.getCatalogs(MediaPackageElements.SMIL);
    Assert.assertEquals(0, catalogs.length);
    mediaPackage = service.addPartialTrack(presenterUri, MediaPackageElements.PRESENTER_SOURCE_PARTIAL, 60000L, mediaPackage);
    mediaPackage = service.addPartialTrack(presenterUri2, MediaPackageElements.PRESENTER_SOURCE_PARTIAL, 120000L, mediaPackage);
    mediaPackage = service.addPartialTrack(presentationUri, MediaPackageElements.PRESENTATION_SOURCE_PARTIAL, 0L, mediaPackage);
    catalogs = mediaPackage.getCatalogs(MediaPackageElements.SMIL);
    Assert.assertEquals(0, catalogs.length);
    service.ingest(mediaPackage);
    catalogs = mediaPackage.getCatalogs(MediaPackageElements.SMIL);
    Assert.assertEquals(1, catalogs.length);
    Assert.assertEquals(MimeTypes.SMIL, catalogs[0].getMimeType());
    Either<Exception, Document> eitherDoc = XmlUtil.parseNs(new InputSource(catalogs[0].getURI().toURL().openStream()));
    Assert.assertTrue(eitherDoc.isRight());
    Document document = eitherDoc.right().value();
    Assert.assertEquals(1, document.getElementsByTagName("par").getLength());
    Assert.assertEquals(2, document.getElementsByTagName("seq").getLength());
    Assert.assertEquals(2, document.getElementsByTagName("video").getLength());
    Assert.assertEquals(1, document.getElementsByTagName("audio").getLength());
}
Also used : InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URI(java.net.URI) FileInputStream(java.io.FileInputStream) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkingFileRepositoryImpl(org.opencastproject.workingfilerepository.impl.WorkingFileRepositoryImpl) File(java.io.File) Test(org.junit.Test)

Example 19 with NotFoundException

use of org.opencastproject.util.NotFoundException in project opencast by opencast.

the class IngestServiceImplTest method testSeriesUpdateNewAndExisting.

/**
 * Test method for {@link org.opencastproject.ingest.impl.IngestServiceImpl#updateSeries(java.net.URI)}
 */
private void testSeriesUpdateNewAndExisting(Dictionary<String, String> properties) throws Exception {
    // default expectation for series overwrite is True
    boolean isExpectSeriesOverwrite = true;
    if (properties != null) {
        service.updated(properties);
        try {
            boolean testForValue = Boolean.parseBoolean(properties.get(IngestServiceImpl.PROPKEY_OVERWRITE_SERIES).trim());
            isExpectSeriesOverwrite = testForValue;
        } catch (Exception e) {
        // If key or value not found or not boolean, use the default overwrite expectation
        }
    }
    // Get test series dublin core for the mock return value
    File catalogFile = new File(urlCatalog2);
    if (!catalogFile.exists() || !catalogFile.canRead())
        throw new Exception("Unable to access test catalog " + urlCatalog2.getPath());
    FileInputStream in = new FileInputStream(catalogFile);
    DublinCoreCatalog series = DublinCores.read(in);
    IOUtils.closeQuietly(in);
    // Set dublinCore service to return test dublin core
    dublinCoreService = org.easymock.EasyMock.createNiceMock(DublinCoreCatalogService.class);
    org.easymock.EasyMock.expect(dublinCoreService.load((InputStream) EasyMock.anyObject())).andReturn(series).anyTimes();
    org.easymock.EasyMock.replay(dublinCoreService);
    service.setDublinCoreService(dublinCoreService);
    // Test with mock found series
    seriesService = EasyMock.createNiceMock(SeriesService.class);
    EasyMock.expect(seriesService.getSeries((String) EasyMock.anyObject())).andReturn(series).once();
    EasyMock.expect(seriesService.updateSeries(series)).andReturn(series).once();
    EasyMock.replay(seriesService);
    service.setSeriesService(seriesService);
    // This is true or false depending on the isOverwrite value
    Assert.assertEquals("Desire to update series is " + String.valueOf(isExpectSeriesOverwrite) + ".", isExpectSeriesOverwrite, service.updateSeries(urlCatalog2));
    // Test with mock not found exception
    EasyMock.reset(seriesService);
    EasyMock.expect(seriesService.updateSeries(series)).andReturn(series).once();
    EasyMock.expect(seriesService.getSeries((String) EasyMock.anyObject())).andThrow(new NotFoundException()).once();
    EasyMock.replay(seriesService);
    service.setSeriesService(seriesService);
    // This should be true, i.e. create new series, in all cases
    Assert.assertEquals("Always create a new series catalog.", true, service.updateSeries(urlCatalog2));
}
Also used : SeriesService(org.opencastproject.series.api.SeriesService) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) File(java.io.File) URISyntaxException(java.net.URISyntaxException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DublinCoreCatalogService(org.opencastproject.metadata.dublincore.DublinCoreCatalogService)

Example 20 with NotFoundException

use of org.opencastproject.util.NotFoundException in project opencast by opencast.

the class MediaInspector method enrichElement.

/**
 * Enriches the media package element metadata such as the mime type, the file size etc. The method mutates the
 * argument element.
 *
 * @param element
 *          the media package element
 * @param override
 *          <code>true</code> to overwrite existing metadata
 * @return the enriched element
 * @throws MediaInspectionException
 *           if enriching fails
 */
private MediaPackageElement enrichElement(final MediaPackageElement element, final boolean override, final Map<String, String> options) throws MediaInspectionException {
    try {
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (NotFoundException e) {
            throw new MediaInspectionException("Unable to find " + element.getURI() + " in the workspace", e);
        } catch (IOException e) {
            throw new MediaInspectionException("Error accessing " + element.getURI() + " in the workspace", e);
        }
        // Checksum
        if (element.getChecksum() == null || override) {
            try {
                element.setChecksum(Checksum.create(ChecksumType.DEFAULT_TYPE, file));
            } catch (IOException e) {
                throw new MediaInspectionException("Error generating checksum for " + element.getURI(), e);
            }
        }
        // Mimetype
        if (element.getMimeType() == null || override) {
            try {
                element.setMimeType(MimeTypes.fromURI(file.toURI()));
            } catch (UnknownFileTypeException e) {
                logger.info("unable to determine the mime type for {}", file.getName());
            }
        }
        logger.info("Successfully inspected element {}", element);
        return element;
    } catch (Exception e) {
        logger.warn("Error enriching element " + element, e);
        if (e instanceof MediaInspectionException) {
            throw (MediaInspectionException) e;
        } else {
            throw new MediaInspectionException(e);
        }
    }
}
Also used : MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File) MediaAnalyzerException(org.opencastproject.inspection.ffmpeg.api.MediaAnalyzerException) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) UnknownFileTypeException(org.opencastproject.util.UnknownFileTypeException)

Aggregations

NotFoundException (org.opencastproject.util.NotFoundException)382 IOException (java.io.IOException)137 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)130 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)79 MediaPackage (org.opencastproject.mediapackage.MediaPackage)69 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)67 EntityManager (javax.persistence.EntityManager)55 SeriesException (org.opencastproject.series.api.SeriesException)53 Path (javax.ws.rs.Path)52 WebApplicationException (javax.ws.rs.WebApplicationException)52 RestQuery (org.opencastproject.util.doc.rest.RestQuery)51 ConfigurationException (org.osgi.service.cm.ConfigurationException)51 URI (java.net.URI)50 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)50 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)49 Date (java.util.Date)48 Test (org.junit.Test)47 File (java.io.File)46 HttpResponse (org.apache.http.HttpResponse)46 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)46