Search in sources :

Example 51 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.

the class LiveScheduleServiceImpl method retractPreviousElements.

void retractPreviousElements(MediaPackage previousMp, MediaPackage newMp) throws LiveScheduleException {
    try {
        // Now can retract elements from previous publish. Before creating a retraction
        // job, check if the element url is still used by the new media package.
        Set<String> elementIds = new HashSet<String>();
        for (MediaPackageElement element : previousMp.getElements()) {
            // We don't retract tracks because they are just live links
            if (!Track.TYPE.equals(element.getElementType())) {
                boolean canBeDeleted = true;
                for (MediaPackageElement newElement : newMp.getElements()) {
                    if (element.getURI().equals(newElement.getURI())) {
                        logger.debug("Not retracting element {} with URI {} from download distribution because it is still used by updated live media package", element.getIdentifier(), element.getURI());
                        canBeDeleted = false;
                        break;
                    }
                }
                if (canBeDeleted)
                    elementIds.add(element.getIdentifier());
            }
        }
        if (elementIds.size() > 0) {
            Job job = downloadDistributionService.retract(CHANNEL_ID, previousMp, elementIds);
            // Wait for retraction to finish
            if (!waitForStatus(job).isSuccess())
                logger.warn("One of the download retract jobs did not complete successfully");
            else
                logger.debug("Retraction of previously published elements complete");
        }
    } catch (DistributionException e) {
        throw new LiveScheduleException(e);
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) DistributionException(org.opencastproject.distribution.api.DistributionException) LiveScheduleException(org.opencastproject.liveschedule.api.LiveScheduleException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet)

Example 52 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.

the class TimelinePreviewsServiceImplTest method testProcess.

/**
 * Test of process method of class TimelinePreviewsServiceImpl.
 * @throws java.lang.Exception
 */
@Test
public void testProcess() throws Exception {
    File file = new File(track.getURI());
    Workspace workspace = EasyMock.createNiceMock(Workspace.class);
    EasyMock.expect(workspace.get((URI) EasyMock.anyObject())).andReturn(file);
    Capture filenameCapture = new Capture();
    EasyMock.expect(workspace.putInCollection(EasyMock.anyString(), (String) EasyMock.capture(filenameCapture), (InputStream) EasyMock.anyObject())).andReturn(new URI("timelinepreviews.png"));
    EasyMock.replay(workspace);
    TimelinePreviewsServiceImpl instance = new TimelinePreviewsServiceImpl();
    instance.setWorkspace(workspace);
    String trackXml = MediaPackageElementParser.getAsXml(track);
    Job job = new JobImpl(1);
    job.setJobType(TimelinePreviewsServiceImpl.JOB_TYPE);
    job.setOperation(TimelinePreviewsServiceImpl.Operation.TimelinePreview.toString());
    job.setArguments(Arrays.asList(trackXml, String.valueOf(10)));
    String result = instance.process(job);
    assertNotNull(result);
    MediaPackageElement timelinepreviewsAttachment = MediaPackageElementParser.getFromXml(result);
    assertEquals(new URI("timelinepreviews.png"), timelinepreviewsAttachment.getURI());
    assertTrue(filenameCapture.hasCaptured());
}
Also used : JobImpl(org.opencastproject.job.api.JobImpl) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) Job(org.opencastproject.job.api.Job) File(java.io.File) URI(java.net.URI) Capture(org.easymock.Capture) Workspace(org.opencastproject.workspace.api.Workspace) Test(org.junit.Test)

Example 53 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.

the class TimelinePreviewsWorkflowOperationHandler method cleanupWorkspace.

/**
 * Remove all files created by the given jobs
 * @param jobs
 */
private void cleanupWorkspace(List<Job> jobs) {
    for (Job job : jobs) {
        String jobPayload = job.getPayload();
        if (StringUtils.isNotEmpty(jobPayload)) {
            try {
                MediaPackageElement timelinepreviewsMpe = MediaPackageElementParser.getFromXml(jobPayload);
                URI timelinepreviewsUri = timelinepreviewsMpe.getURI();
                workspace.delete(timelinepreviewsUri);
            } catch (MediaPackageException ex) {
                // unexpected job payload
                logger.error("Can't parse timeline previews attachment from job {}", job.getId());
            } catch (NotFoundException ex) {
            // this is ok, because we want delete the file
            } catch (IOException ex) {
                logger.warn("Deleting timeline previews image file from workspace failed: {}", ex.getMessage());
            // this is ok, because workspace cleaner will remove old files if they exist
            }
        }
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) NotFoundException(org.opencastproject.util.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) Job(org.opencastproject.job.api.Job) URI(java.net.URI)

Example 54 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.

the class IBMWatsonTranscriptionServiceTest method testGetGeneratedTranscriptionNotInWorkspace.

@Test
public void testGetGeneratedTranscriptionNotInWorkspace() throws Exception {
    InputStream stream = IBMWatsonTranscriptionServiceTest.class.getResourceAsStream("/" + PULLED_TRANSCRIPTION_FILE);
    database.storeJobControl(MP_ID, TRACK_ID, JOB_ID, TranscriptionJobControl.Status.Progress.name(), TRACK_DURATION);
    URI uri = new URI("http://ADMIN_SERVER/collection/" + IBMWatsonTranscriptionService.TRANSCRIPT_COLLECTION + "/" + JOB_ID + ".json");
    EasyMock.expect(workspace.getCollectionURI(IBMWatsonTranscriptionService.TRANSCRIPT_COLLECTION, JOB_ID + ".json")).andReturn(uri);
    EasyMock.expect(workspace.get(uri)).andThrow(new NotFoundException());
    EasyMock.expect(workspace.putInCollection(EasyMock.anyObject(String.class), EasyMock.anyObject(String.class), EasyMock.anyObject(InputStream.class))).andReturn(uri);
    EasyMock.replay(workspace);
    HttpEntity httpEntity = EasyMock.createNiceMock(HttpEntity.class);
    EasyMock.expect(httpEntity.getContent()).andReturn(stream);
    CloseableHttpResponse response = EasyMock.createNiceMock(CloseableHttpResponse.class);
    StatusLine status = EasyMock.createNiceMock(StatusLine.class);
    EasyMock.expect(response.getStatusLine()).andReturn(status).anyTimes();
    EasyMock.expect(response.getEntity()).andReturn(httpEntity).anyTimes();
    EasyMock.expect(status.getStatusCode()).andReturn(HttpStatus.SC_OK).anyTimes();
    EasyMock.replay(httpEntity, response, status);
    EasyMock.expect(httpClient.execute(EasyMock.anyObject(HttpGet.class))).andReturn(response).anyTimes();
    EasyMock.replay(httpClient);
    MediaPackageElement mpe = service.getGeneratedTranscription(MP_ID, JOB_ID);
    Assert.assertEquals("captions", mpe.getFlavor().getType());
    Assert.assertEquals("ibm-watson-json", mpe.getFlavor().getSubtype());
    Assert.assertEquals(uri.toString(), mpe.getURI().toString());
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) NotFoundException(org.opencastproject.util.NotFoundException) URI(java.net.URI) Test(org.junit.Test)

Example 55 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement in project opencast by opencast.

the class AttachTranscriptionOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.debug("Attach transcription for mediapackage {} started", mediaPackage);
    // Get job id.
    String jobId = StringUtils.trimToNull(operation.getConfiguration(TRANSCRIPTION_JOB_ID));
    if (jobId == null)
        throw new WorkflowOperationException(TRANSCRIPTION_JOB_ID + " missing");
    // Check which tags/flavors have been configured
    String targetTagOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAG));
    String targetFlavorOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR));
    String captionFormatOption = StringUtils.trimToNull(operation.getConfiguration(TARGET_CAPTION_FORMAT));
    // Target flavor is mandatory if target-caption-format was NOT informed and no conversion is done
    if (targetFlavorOption == null && captionFormatOption == null)
        throw new WorkflowOperationException(TARGET_FLAVOR + " missing");
    // Target flavor is optional if target-caption-format was informed because the default flavor
    // will be "captions/<format>". If informed, will override the default.
    MediaPackageElementFlavor flavor = null;
    if (targetFlavorOption != null)
        flavor = MediaPackageElementFlavor.parseFlavor(targetFlavorOption);
    try {
        // Get transcription file from the service
        MediaPackageElement original = service.getGeneratedTranscription(mediaPackage.getIdentifier().compact(), jobId);
        MediaPackageElement transcription = original;
        // If caption format passed, convert to desired format
        if (captionFormatOption != null) {
            Job job = captionService.convert(transcription, "ibm-watson", captionFormatOption, service.getLanguage());
            if (!waitForStatus(job).isSuccess()) {
                throw new WorkflowOperationException("Transcription format conversion job did not complete successfully");
            }
            transcription = MediaPackageElementParser.getFromXml(job.getPayload());
        }
        // Set the target flavor if informed
        if (flavor != null)
            transcription.setFlavor(flavor);
        // Add tags
        if (targetTagOption != null) {
            for (String tag : asList(targetTagOption)) {
                if (StringUtils.trimToNull(tag) != null)
                    transcription.addTag(tag);
            }
        }
        // Add to media package
        mediaPackage.add(transcription);
        String uri = transcription.getURI().toString();
        String ext = uri.substring(uri.lastIndexOf("."));
        transcription.setURI(workspace.moveTo(transcription.getURI(), mediaPackage.getIdentifier().toString(), transcription.getIdentifier(), "captions." + ext));
    } catch (Exception e) {
        throw new WorkflowOperationException(e);
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

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