Search in sources :

Example 66 with MediaPackageElementFlavor

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

the class ExecuteOnceWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.debug("Running execute workflow operation with ID {}", operation.getId());
    // Get operation parameters
    String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
    String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
    float load = 1.0f;
    String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
    if (StringUtils.isNotBlank(loadPropertyStr)) {
        try {
            load = Float.parseFloat(loadPropertyStr);
        } catch (NumberFormatException e) {
            String description = StringUtils.trimToEmpty(operation.getDescription());
            logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
        }
    }
    String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
    String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
    boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));
    // Unmarshall target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (targetFlavorStr != null)
        targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
    // Unmarshall expected mediapackage element type
    MediaPackageElement.Type expectedType = null;
    if (expectedTypeStr != null) {
        for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
            expectedType = type;
            break;
        }
        if (expectedType == null)
            throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
    }
    // Process the result element
    MediaPackageElement resultElement = null;
    try {
        Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);
        WorkflowOperationResult result = null;
        // Wait for all jobs to be finished
        if (!waitForStatus(job).isSuccess())
            throw new WorkflowOperationException("Execute operation failed");
        if (StringUtils.isNotBlank(job.getPayload())) {
            if (setWfProps) {
                // The job payload is a file with set of properties for the workflow
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                final Properties properties = new Properties();
                File propertiesFile = workspace.get(resultElement.getURI());
                try (InputStream is = new FileInputStream(propertiesFile)) {
                    properties.load(is);
                }
                logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
                workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());
                Map<String, String> wfProps = new HashMap<String, String>((Map) properties);
                result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
            } else {
                // The job payload is a new element for the MediaPackage
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
                    // Have the track inspected and return the result
                    Job inspectionJob = null;
                    inspectionJob = inspectionService.inspect(resultElement.getURI());
                    JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
                    if (!barrier.waitForJobs().isSuccess()) {
                        throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
                    }
                    resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
                }
                // Store new element to mediaPackage
                mediaPackage.add(resultElement);
                URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename);
                resultElement.setURI(uri);
                // Set new flavor
                if (targetFlavor != null)
                    resultElement.setFlavor(targetFlavor);
                // Set new tags
                if (targetTags != null) {
                    // Assume the tags starting with "-" means we want to eliminate such tags form the result element
                    for (String tag : asList(targetTags)) {
                        if (tag.startsWith("-"))
                            // We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
                            resultElement.removeTag(tag.replaceAll("^-+", ""));
                        else
                            resultElement.addTag(tag);
                    }
                }
                result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
            }
        } else {
            // Payload is empty
            result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
        }
        logger.debug("Execute operation {} completed", operation.getId());
        return result;
    } catch (ExecuteException e) {
        throw new WorkflowOperationException(e);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException("Some result element couldn't be serialized", e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException("Could not find mediapackage", e);
    } catch (IOException e) {
        throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
    } catch (MediaInspectionException e) {
        throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
    }
}
Also used : HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Properties(java.util.Properties) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) Job(org.opencastproject.job.api.Job) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) JobBarrier(org.opencastproject.job.api.JobBarrier) FileInputStream(java.io.FileInputStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) File(java.io.File)

Example 67 with MediaPackageElementFlavor

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

the class FileUploadServiceImpl method putPayloadIntoMediaPackage.

/**
 * Puts the payload of an upload job into a MediaPackage in the WFR, adds the files as a track to the MediaPackage and
 * returns the files URL in the WFR.
 *
 * @param job
 * @return URL of the file in the WFR
 * @throws FileUploadException
 */
private URL putPayloadIntoMediaPackage(FileUploadJob job) throws FileUploadException {
    MediaPackage mediaPackage = job.getPayload().getMediaPackage();
    MediaPackageElementFlavor flavor = job.getPayload().getFlavor();
    List<Track> excludeTracks = Arrays.asList(mediaPackage.getTracks(flavor));
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(getPayloadFile(job.getId()));
        MediaPackage mp = ingestService.addTrack(fileInputStream, job.getPayload().getFilename(), job.getPayload().getFlavor(), mediaPackage);
        List<Track> tracks = new ArrayList<Track>(Arrays.asList(mp.getTracks(flavor)));
        tracks.removeAll(excludeTracks);
        if (tracks.size() != 1)
            throw new FileUploadException("Ingested track not found");
        return tracks.get(0).getURI().toURL();
    } catch (Exception e) {
        throw fileUploadException(Severity.error, "Failed to add payload to MediaPackage.", e);
    } finally {
        IOUtils.closeQuietly(fileInputStream);
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) ArrayList(java.util.ArrayList) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) Track(org.opencastproject.mediapackage.Track) FileInputStream(java.io.FileInputStream) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException) ConfigurationException(org.osgi.service.cm.ConfigurationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) FileUploadException(org.opencastproject.fileupload.api.exception.FileUploadException)

Example 68 with MediaPackageElementFlavor

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

the class EventsEndpoint method getEventMetadataByType.

private Response getEventMetadataByType(String id, String type) throws IndexServiceException, Exception {
    for (final Event event : indexService.getEvent(id, externalIndex)) {
        Opt<MediaPackageElementFlavor> flavor = getFlavor(type);
        if (flavor.isNone()) {
            return R.badRequest(String.format("Unable to parse type '%s' as a flavor so unable to find the matching catalog.", type));
        }
        // Try the main catalog first as we load it from the index.
        if (flavor.get().equals(eventCatalogUIAdapter.getFlavor())) {
            MetadataCollection collection = EventUtils.getEventMetadata(event, eventCatalogUIAdapter);
            ExternalMetadataUtils.changeSubjectToSubjects(collection);
            ExternalMetadataUtils.removeCollectionList(collection);
            convertStartDateTimeToApiV1(collection);
            ExternalMetadataUtils.changeTypeOrderedTextToText(collection);
            return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, collection.toJSON());
        }
        // Try the other catalogs
        List<EventCatalogUIAdapter> catalogUIAdapters = getEventCatalogUIAdapters();
        catalogUIAdapters.remove(eventCatalogUIAdapter);
        MediaPackage mediaPackage = indexService.getEventMediapackage(event);
        if (catalogUIAdapters.size() > 0) {
            for (EventCatalogUIAdapter catalogUIAdapter : catalogUIAdapters) {
                if (flavor.get().equals(catalogUIAdapter.getFlavor())) {
                    MetadataCollection fields = catalogUIAdapter.getFields(mediaPackage);
                    ExternalMetadataUtils.removeCollectionList(fields);
                    convertStartDateTimeToApiV1(fields);
                    ExternalMetadataUtils.changeTypeOrderedTextToText(fields);
                    return ApiResponses.Json.ok(ApiVersion.VERSION_1_0_0, fields.toJSON());
                }
            }
        }
        return ApiResponses.notFound("Cannot find a catalog with type '%s' for event with id '%s'.", type, id);
    }
    return ApiResponses.notFound("Cannot find an event with id '%s'.", id);
}
Also used : CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Event(org.opencastproject.index.service.impl.index.event.Event) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 69 with MediaPackageElementFlavor

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

the class TestEventsEndpoint method setupEventCatalogUIAdapters.

private void setupEventCatalogUIAdapters() throws ConfigurationException {
    // Setup common event catalog
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = new CommonEventCatalogUIAdapter();
    Properties episodeCatalogProperties = getCatalogProperties(getClass(), "/episode-catalog.properties");
    commonEventCatalogUIAdapter.updated(PropertiesUtil.toDictionary(episodeCatalogProperties));
    this.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
    addCatalogUIAdapter(commonEventCatalogUIAdapter);
    // Setup catalog to be deleted.
    EventCatalogUIAdapter deleteAdapter = EasyMock.createMock(EventCatalogUIAdapter.class);
    EasyMock.expect(deleteAdapter.getFlavor()).andReturn(new MediaPackageElementFlavor(DELETE_CATALOG_TYPE, "episode")).anyTimes();
    MetadataCollection collectionMock = EasyMock.createNiceMock(MetadataCollection.class);
    EasyMock.expect(deleteAdapter.getOrganization()).andReturn(defaultOrg.getId()).anyTimes();
    EasyMock.expect(deleteAdapter.getFields(EasyMock.anyObject(MediaPackage.class))).andReturn(null).anyTimes();
    EasyMock.expect(deleteAdapter.getUITitle()).andReturn(null).anyTimes();
    EasyMock.replay(deleteAdapter);
    addCatalogUIAdapter(deleteAdapter);
}
Also used : EventCatalogUIAdapter(org.opencastproject.metadata.dublincore.EventCatalogUIAdapter) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) Properties(java.util.Properties) CatalogAdapterUtil.getCatalogProperties(org.opencastproject.index.service.util.CatalogAdapterUtil.getCatalogProperties) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 70 with MediaPackageElementFlavor

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

the class PublishEngageWorkflowOperationHandler method getMediaPackageForSearchIndex.

/**
 * Returns a mediapackage that only contains elements that are marked for distribution.
 *
 * @param current
 *          the current mediapackage
 * @param jobs
 *          the distribution jobs
 * @param downloadSubflavor
 *          flavor to be applied to elements distributed to download
 * @param downloadTargetTags
 *          tags to be applied to elements distributed to downloads
 * @param downloadElementIds
 *          identifiers for elements that have been distributed to downloads
 * @param streamingSubflavor
 *          flavor to be applied to elements distributed to streaming
 * @param streamingElementIds
 *          identifiers for elements that have been distributed to streaming
 * @param streamingTargetTags
 *          tags to be applied to elements distributed to streaming
 * @return the new mediapackage
 */
protected MediaPackage getMediaPackageForSearchIndex(MediaPackage current, List<Job> jobs, MediaPackageElementFlavor downloadSubflavor, String[] downloadTargetTags, Set<String> downloadElementIds, MediaPackageElementFlavor streamingSubflavor, Set<String> streamingElementIds, String[] streamingTargetTags) throws MediaPackageException, NotFoundException, ServiceRegistryException, WorkflowOperationException {
    MediaPackage mp = (MediaPackage) current.clone();
    // All the jobs have passed, let's update the mediapackage with references to the distributed elements
    List<String> elementsToPublish = new ArrayList<String>();
    Map<String, String> distributedElementIds = new HashMap<String, String>();
    for (Job entry : jobs) {
        Job job = serviceRegistry.getJob(entry.getId());
        // If there is no payload, then the item has not been distributed.
        if (job.getPayload() == null)
            continue;
        List<MediaPackageElement> distributedElements = null;
        try {
            distributedElements = (List<MediaPackageElement>) MediaPackageElementParser.getArrayFromXml(job.getPayload());
        } catch (MediaPackageException e) {
            throw new WorkflowOperationException(e);
        }
        // kind of element. So we just keep on looping.
        if (distributedElements == null || distributedElements.size() < 1)
            continue;
        for (MediaPackageElement distributedElement : distributedElements) {
            String sourceElementId = distributedElement.getIdentifier();
            if (sourceElementId != null) {
                MediaPackageElement sourceElement = mp.getElementById(sourceElementId);
                // Make sure the mediapackage is prompted to create a new identifier for this element
                distributedElement.setIdentifier(null);
                if (sourceElement != null) {
                    // Adjust the flavor and tags for downloadable elements
                    if (downloadElementIds.contains(sourceElementId)) {
                        if (downloadSubflavor != null) {
                            MediaPackageElementFlavor flavor = sourceElement.getFlavor();
                            if (flavor != null) {
                                MediaPackageElementFlavor newFlavor = new MediaPackageElementFlavor(flavor.getType(), downloadSubflavor.getSubtype());
                                distributedElement.setFlavor(newFlavor);
                            }
                        }
                    } else // Adjust the flavor and tags for streaming elements
                    if (streamingElementIds.contains(sourceElementId)) {
                        if (streamingSubflavor != null && streamingElementIds.contains(sourceElementId)) {
                            MediaPackageElementFlavor flavor = sourceElement.getFlavor();
                            if (flavor != null) {
                                MediaPackageElementFlavor newFlavor = new MediaPackageElementFlavor(flavor.getType(), streamingSubflavor.getSubtype());
                                distributedElement.setFlavor(newFlavor);
                            }
                        }
                    }
                    // Copy references from the source elements to the distributed elements
                    MediaPackageReference ref = sourceElement.getReference();
                    if (ref != null && mp.getElementByReference(ref) != null) {
                        MediaPackageReference newReference = (MediaPackageReference) ref.clone();
                        distributedElement.setReference(newReference);
                    }
                }
            }
            if (isStreamingFormat(distributedElement))
                applyTags(distributedElement, streamingTargetTags);
            else
                applyTags(distributedElement, downloadTargetTags);
            // Add the new element to the mediapackage
            mp.add(distributedElement);
            elementsToPublish.add(distributedElement.getIdentifier());
            distributedElementIds.put(sourceElementId, distributedElement.getIdentifier());
        }
    }
    // Mark everything that is set for removal
    List<MediaPackageElement> removals = new ArrayList<MediaPackageElement>();
    for (MediaPackageElement element : mp.getElements()) {
        if (!elementsToPublish.contains(element.getIdentifier())) {
            removals.add(element);
        }
    }
    // Translate references to the distributed artifacts
    for (MediaPackageElement element : mp.getElements()) {
        if (removals.contains(element))
            continue;
        // Is the element referencing anything?
        MediaPackageReference reference = element.getReference();
        if (reference == null)
            continue;
        // See if the element has been distributed
        String distributedElementId = distributedElementIds.get(reference.getIdentifier());
        if (distributedElementId == null)
            continue;
        MediaPackageReference translatedReference = new MediaPackageReferenceImpl(mp.getElementById(distributedElementId));
        if (reference.getProperties() != null) {
            translatedReference.getProperties().putAll(reference.getProperties());
        }
        // Set the new reference
        element.setReference(translatedReference);
    }
    // Remove everything we don't want to add to publish
    for (MediaPackageElement element : removals) {
        mp.remove(element);
    }
    return mp;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MediaPackageReference(org.opencastproject.mediapackage.MediaPackageReference) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageReferenceImpl(org.opencastproject.mediapackage.MediaPackageReferenceImpl) Job(org.opencastproject.job.api.Job)

Aggregations

MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)109 MediaPackage (org.opencastproject.mediapackage.MediaPackage)49 Track (org.opencastproject.mediapackage.Track)34 URI (java.net.URI)31 Test (org.junit.Test)31 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)28 IOException (java.io.IOException)27 NotFoundException (org.opencastproject.util.NotFoundException)27 ArrayList (java.util.ArrayList)26 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)26 HashMap (java.util.HashMap)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)22 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)22 Job (org.opencastproject.job.api.Job)21 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 InputStream (java.io.InputStream)18 TrackImpl (org.opencastproject.mediapackage.track.TrackImpl)17 TrackSelector (org.opencastproject.mediapackage.selector.TrackSelector)16 File (java.io.File)13 Catalog (org.opencastproject.mediapackage.Catalog)13