Search in sources :

Example 71 with MediaPackageElementFlavor

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

the class PublishEngageWorkflowOperationHandler 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 {
    logger.debug("Running engage publication workflow operation");
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance op = workflowInstance.getCurrentOperation();
    // Check which tags have been configured
    String downloadSourceTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_TAGS));
    String downloadTargetTags = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_TARGET_TAGS));
    String downloadSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(DOWNLOAD_SOURCE_FLAVORS));
    String downloadTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(DOWNLOAD_TARGET_SUBFLAVOR));
    String streamingSourceTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_TAGS));
    String streamingTargetTags = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_TARGET_TAGS));
    String streamingSourceFlavors = StringUtils.trimToEmpty(op.getConfiguration(STREAMING_SOURCE_FLAVORS));
    String streamingTargetSubflavor = StringUtils.trimToNull(op.getConfiguration(STREAMING_TARGET_SUBFLAVOR));
    String republishStrategy = StringUtils.trimToEmpty(op.getConfiguration(STRATEGY));
    boolean checkAvailability = option(op.getConfiguration(CHECK_AVAILABILITY)).bind(trimToNone).map(toBool).getOrElse(true);
    String[] sourceDownloadTags = StringUtils.split(downloadSourceTags, ",");
    String[] targetDownloadTags = StringUtils.split(downloadTargetTags, ",");
    String[] sourceDownloadFlavors = StringUtils.split(downloadSourceFlavors, ",");
    String[] sourceStreamingTags = StringUtils.split(streamingSourceTags, ",");
    String[] targetStreamingTags = StringUtils.split(streamingTargetTags, ",");
    String[] sourceStreamingFlavors = StringUtils.split(streamingSourceFlavors, ",");
    if (sourceDownloadTags.length == 0 && sourceDownloadFlavors.length == 0 && sourceStreamingTags.length == 0 && sourceStreamingFlavors.length == 0) {
        logger.warn("No tags or flavors have been specified, so nothing will be published to the engage publication channel");
        return createResult(mediaPackage, Action.CONTINUE);
    }
    // Parse the download target flavor
    MediaPackageElementFlavor downloadSubflavor = null;
    if (downloadTargetSubflavor != null) {
        try {
            downloadSubflavor = MediaPackageElementFlavor.parseFlavor(downloadTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Parse the streaming target flavor
    MediaPackageElementFlavor streamingSubflavor = null;
    if (streamingTargetSubflavor != null) {
        try {
            streamingSubflavor = MediaPackageElementFlavor.parseFlavor(streamingTargetSubflavor);
        } catch (IllegalArgumentException e) {
            throw new WorkflowOperationException(e);
        }
    }
    // Configure the download element selector
    SimpleElementSelector downloadElementSelector = new SimpleElementSelector();
    for (String flavor : sourceDownloadFlavors) {
        downloadElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceDownloadTags) {
        downloadElementSelector.addTag(tag);
    }
    // Configure the streaming element selector
    SimpleElementSelector streamingElementSelector = new SimpleElementSelector();
    for (String flavor : sourceStreamingFlavors) {
        streamingElementSelector.addFlavor(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    for (String tag : sourceStreamingTags) {
        streamingElementSelector.addTag(tag);
    }
    // Select the appropriate elements for download and streaming
    Collection<MediaPackageElement> downloadElements = downloadElementSelector.select(mediaPackage, false);
    Collection<MediaPackageElement> streamingElements = streamingElementSelector.select(mediaPackage, false);
    try {
        Set<String> downloadElementIds = new HashSet<String>();
        Set<String> streamingElementIds = new HashSet<String>();
        // Look for elements matching the tag
        for (MediaPackageElement elem : downloadElements) {
            downloadElementIds.add(elem.getIdentifier());
        }
        for (MediaPackageElement elem : streamingElements) {
            streamingElementIds.add(elem.getIdentifier());
        }
        // Also distribute the security configuration
        // -----
        // This was removed in the meantime by a fix for MH-8515, but could now be used again.
        // -----
        Attachment[] securityAttachments = mediaPackage.getAttachments(MediaPackageElements.XACML_POLICY);
        if (securityAttachments != null && securityAttachments.length > 0) {
            for (Attachment a : securityAttachments) {
                downloadElementIds.add(a.getIdentifier());
                streamingElementIds.add(a.getIdentifier());
            }
        }
        removePublicationElement(mediaPackage);
        switch(republishStrategy) {
            case ("merge"):
                // nothing to do here. other publication strategies can be added to this list later on
                break;
            default:
                retractFromEngage(mediaPackage);
        }
        List<Job> jobs = new ArrayList<Job>();
        // distribute Elements
        try {
            if (downloadElementIds.size() > 0) {
                Job job = downloadDistributionService.distribute(CHANNEL_ID, mediaPackage, downloadElementIds, checkAvailability);
                if (job != null) {
                    jobs.add(job);
                }
            }
            if (distributeStreaming) {
                for (String elementId : streamingElementIds) {
                    Job job = streamingDistributionService.distribute(CHANNEL_ID, mediaPackage, elementId);
                    if (job != null) {
                        jobs.add(job);
                    }
                }
            }
        } catch (DistributionException e) {
            throw new WorkflowOperationException(e);
        }
        if (jobs.size() < 1) {
            logger.info("No mediapackage element was found for distribution to engage");
            return createResult(mediaPackage, Action.CONTINUE);
        }
        // Wait until all distribution jobs have returned
        if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess())
            throw new WorkflowOperationException("One of the distribution jobs did not complete successfully");
        logger.debug("Distribute of mediapackage {} completed", mediaPackage);
        String engageUrlString = null;
        try {
            MediaPackage mediaPackageForSearch = getMediaPackageForSearchIndex(mediaPackage, jobs, downloadSubflavor, targetDownloadTags, downloadElementIds, streamingSubflavor, streamingElementIds, targetStreamingTags);
            // MH-10216, check if only merging into existing mediapackage
            removePublicationElement(mediaPackage);
            switch(republishStrategy) {
                case ("merge"):
                    // merge() returns merged mediapackage or null mediaPackage is not published
                    mediaPackageForSearch = merge(mediaPackageForSearch);
                    if (mediaPackageForSearch == null) {
                        logger.info("Skipping republish for {} since it is not currently published", mediaPackage.getIdentifier().toString());
                        return createResult(mediaPackage, Action.SKIP);
                    }
                    break;
                default:
            }
            if (!isPublishable(mediaPackageForSearch))
                throw new WorkflowOperationException("Media package does not meet criteria for publication");
            logger.info("Publishing media package {} to search index", mediaPackageForSearch);
            URL engageBaseUrl = null;
            engageUrlString = StringUtils.trimToNull(workflowInstance.getOrganization().getProperties().get(ENGAGE_URL_PROPERTY));
            if (engageUrlString != null) {
                engageBaseUrl = new URL(engageUrlString);
            } else {
                engageBaseUrl = serverUrl;
                logger.info("Using 'server.url' as a fallback for the non-existing organization level key '{}' for the publication url", ENGAGE_URL_PROPERTY);
            }
            // create the publication URI (used by Admin UI for event details link)
            URI engageUri = this.createEngageUri(engageBaseUrl.toURI(), mediaPackage);
            // Create new distribution element
            Publication publicationElement = PublicationImpl.publication(UUID.randomUUID().toString(), CHANNEL_ID, engageUri, MimeTypes.parseMimeType("text/html"));
            mediaPackage.add(publicationElement);
            // Adding media package to the search index
            Job publishJob = null;
            try {
                publishJob = searchService.add(mediaPackageForSearch);
                if (!waitForStatus(publishJob).isSuccess()) {
                    throw new WorkflowOperationException("Mediapackage " + mediaPackageForSearch.getIdentifier() + " could not be published");
                }
            } catch (SearchException e) {
                throw new WorkflowOperationException("Error publishing media package", e);
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Error parsing media package", e);
            }
            logger.debug("Publishing of mediapackage {} completed", mediaPackage);
            return createResult(mediaPackage, Action.CONTINUE);
        } catch (MalformedURLException e) {
            logger.error("{} is malformed: {}", ENGAGE_URL_PROPERTY, engageUrlString);
            throw new WorkflowOperationException(e);
        } catch (Throwable t) {
            if (t instanceof WorkflowOperationException)
                throw (WorkflowOperationException) t;
            else
                throw new WorkflowOperationException(t);
        }
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            throw new WorkflowOperationException(e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) SearchException(org.opencastproject.search.api.SearchException) Attachment(org.opencastproject.mediapackage.Attachment) SimpleElementSelector(org.opencastproject.mediapackage.selector.SimpleElementSelector) URI(java.net.URI) URL(java.net.URL) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) Publication(org.opencastproject.mediapackage.Publication) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) SearchException(org.opencastproject.search.api.SearchException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) MalformedURLException(java.net.MalformedURLException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException)

Example 72 with MediaPackageElementFlavor

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

the class XACMLAuthorizationService method getAcl.

/**
 * Get the ACL of the given flavor from a media package.
 */
private Option<AccessControlList> getAcl(final MediaPackage mp, final List<MediaPackageElementFlavor> flavors) {
    Option<AccessControlList> result = Option.none();
    Set<Attachment> attachments = new HashSet<>();
    for (MediaPackageElementFlavor flavor : flavors) {
        Attachment[] attachmentsArray = mp.getAttachments(flavor);
        attachments.addAll(Arrays.asList(attachmentsArray));
    }
    if (attachments.size() == 1) {
        logger.debug("One security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
        for (Attachment attachment : attachments) {
            result = loadAcl(attachment.getURI());
        }
    } else if (attachments.size() < 1) {
        logger.debug("No security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
    } else if (attachments.size() > 1) {
        logger.warn("More than one security attachment found for media package {} with flavors {}", mp.getIdentifier(), flavors);
    }
    return result;
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) Attachment(org.opencastproject.mediapackage.Attachment) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) HashSet(java.util.HashSet)

Example 73 with MediaPackageElementFlavor

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

the class ConfigurableEventDCCatalogUIAdapter method updated.

public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    config = CatalogUIAdapterConfiguration.loadFromDictionary(properties);
    organization = getCfg(properties, CONF_ORGANIZATION_KEY);
    String flavorString = getCfg(properties, CONF_FLAVOR_KEY);
    if (StringUtils.isBlank(flavorString) || flavorString.split("/").length != 2) {
        throw new ConfigurationException(CONF_FLAVOR_KEY, "The flavor " + flavorString + " is not a valid flavor. It should be defined as 'type/subtype'");
    }
    flavor = new MediaPackageElementFlavor(flavorString.split("/")[0], flavorString.split("/")[1]);
    title = getCfg(properties, "title");
    dublinCoreProperties = DublinCoreMetadataUtil.getDublinCoreProperties(properties);
    logger.info("Updated dublin core catalog UI adapter {} for flavor {}", getUITitle(), getFlavor());
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 74 with MediaPackageElementFlavor

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

the class MetadataList method fromJSON.

public void fromJSON(String json) throws MetadataParsingException {
    if (StringUtils.isBlank(json))
        throw new IllegalArgumentException("The JSON string must not be empty or null!");
    JSONParser parser = new JSONParser();
    JSONArray metadataJSON;
    try {
        metadataJSON = (JSONArray) parser.parse(json);
    } catch (ParseException e) {
        throw new MetadataParsingException("Not able to parse the given string as JSON metadata list.", e.getCause());
    }
    ListIterator<JSONObject> listIterator = metadataJSON.listIterator();
    while (listIterator.hasNext()) {
        JSONObject item = listIterator.next();
        MediaPackageElementFlavor flavor = MediaPackageElementFlavor.parseFlavor((String) item.get(KEY_METADATA_FLAVOR));
        String title = (String) item.get(KEY_METADATA_TITLE);
        if (flavor == null || title == null)
            continue;
        JSONArray value = (JSONArray) item.get(KEY_METADATA_FIELDS);
        if (value == null)
            continue;
        Tuple<String, MetadataCollection> metadata = metadataList.get(flavor.toString());
        if (metadata == null)
            continue;
        metadata.getB().fromJSON(value.toJSONString());
        metadataList.put(flavor.toString(), metadata);
    }
}
Also used : MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) MetadataCollection(org.opencastproject.metadata.dublincore.MetadataCollection) ParseException(org.json.simple.parser.ParseException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor)

Example 75 with MediaPackageElementFlavor

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

the class IndexServiceImpl method updateMpAssetFlavor.

/**
 * Update the flavor of newly added asset with the passed metadata
 *
 * @param assetList
 *          the list of assets to update
 * @param mp
 *          the mediapackage to update
 * @param assetMetadata
 *          a set of mapping metadata for the asset list
 * @param overwriteExisting
 *          true if the existing asset of the same flavor should be overwritten
 * @return mediapackage updated with assets
 */
@SuppressWarnings("unchecked")
protected MediaPackage updateMpAssetFlavor(List<String> assetList, MediaPackage mp, JSONArray assetMetadata, Boolean overwriteExisting) {
    // Create JSONObject data map
    JSONObject assetDataMap = new JSONObject();
    for (int i = 0; i < assetMetadata.size(); i++) {
        try {
            assetDataMap.put(((JSONObject) assetMetadata.get(i)).get("id"), assetMetadata.get(i));
        } catch (Exception e) {
            throw new IllegalArgumentException("Unable to parse metadata", e);
        }
    }
    // Find the correct flavor for each asset.
    for (String assetOrig : assetList) {
        // expecting file assets to contain postfix "track_trackpart.0"
        String asset = assetOrig;
        String assetNumber = null;
        String[] assetNameParts = asset.split(Pattern.quote("."));
        if (assetNameParts.length > 1) {
            asset = assetNameParts[0];
            assetNumber = assetNameParts[1];
        }
        try {
            if ((assetMetadata != null) && (assetDataMap.get(asset) != null)) {
                String type = (String) ((JSONObject) assetDataMap.get(asset)).get("type");
                String flavorType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorType");
                String flavorSubType = (String) ((JSONObject) assetDataMap.get(asset)).get("flavorSubType");
                if (patternNumberedAsset.matcher(flavorSubType).matches() && (assetNumber != null)) {
                    flavorSubType = assetNumber;
                }
                MediaPackageElementFlavor newElemflavor = new MediaPackageElementFlavor(flavorType, flavorSubType);
                if (patternAttachment.matcher(type).matches()) {
                    if (overwriteExisting) {
                        // remove existing attachments of the new flavor
                        Attachment[] existing = mp.getAttachments(newElemflavor);
                        for (int i = 0; i < existing.length; i++) {
                            mp.remove(existing[i]);
                            logger.info("Overwriting existing asset {} {}", type, newElemflavor);
                        }
                    }
                    // correct the flavor of the new attachment
                    Attachment[] elArray = mp.getAttachments(new MediaPackageElementFlavor(assetOrig, "*"));
                    elArray[0].setFlavor(newElemflavor);
                    logger.info("Updated asset {} {}", type, newElemflavor);
                } else if (patternCatalog.matcher(type).matches()) {
                    if (overwriteExisting) {
                        // remove existing catalogs of the new flavor
                        Catalog[] existing = mp.getCatalogs(newElemflavor);
                        for (int i = 0; i < existing.length; i++) {
                            mp.remove(existing[i]);
                            logger.info("Overwriting existing asset {} {}", type, newElemflavor);
                        }
                    }
                    Catalog[] catArray = mp.getCatalogs(new MediaPackageElementFlavor(assetOrig, "*"));
                    if (catArray.length > 1) {
                        throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
                    }
                    catArray[0].setFlavor(newElemflavor);
                    logger.info("Update asset {} {}", type, newElemflavor);
                } else if (patternTrack.matcher(type).matches()) {
                    // Overwriting of existing tracks of same flavor is currently not allowed.
                    // TODO: allow overwriting of existing tracks of same flavor
                    Track[] trackArray = mp.getTracks(new MediaPackageElementFlavor(assetOrig, "*"));
                    if (trackArray.length > 1) {
                        throw new IllegalArgumentException("More than one " + asset + " found, only one expected.");
                    }
                    trackArray[0].setFlavor(newElemflavor);
                    logger.info("Update asset {} {}", type, newElemflavor);
                } else {
                    logger.warn("Unknown asset type {} {} for field {}", type, newElemflavor, asset);
                }
            }
        } catch (Exception e) {
            // Assuming a parse error versus a file error and logging the error type
            logger.warn("Unable to process asset metadata {}", assetMetadata.toJSONString(), e);
            throw new IllegalArgumentException("Unable to parse metadata", e);
        }
    }
    return mp;
}
Also used : JSONObject(org.json.simple.JSONObject) Attachment(org.opencastproject.mediapackage.Attachment) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) IngestException(org.opencastproject.ingest.api.IngestException) WebApplicationException(javax.ws.rs.WebApplicationException) MetadataParsingException(org.opencastproject.metadata.dublincore.MetadataParsingException) EventCommentException(org.opencastproject.event.comment.EventCommentException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) SearchIndexException(org.opencastproject.matterhorn.search.SearchIndexException) ParseException(java.text.ParseException) SeriesException(org.opencastproject.series.api.SeriesException) WorkflowException(org.opencastproject.workflow.api.WorkflowException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IndexServiceException(org.opencastproject.index.service.exception.IndexServiceException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) WorkflowDatabaseException(org.opencastproject.workflow.api.WorkflowDatabaseException) AssetManagerException(org.opencastproject.assetmanager.api.AssetManagerException) Track(org.opencastproject.mediapackage.Track)

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