Search in sources :

Example 61 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class EmailWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    // The current workflow operation instance
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    // MediaPackage from previous workflow operations
    MediaPackage srcPackage = workflowInstance.getMediaPackage();
    // "To", "CC", "BCC", subject, body can be Freemarker templates
    String to = applyTemplateIfNecessary(workflowInstance, operation, TO_PROPERTY);
    String cc = applyTemplateIfNecessary(workflowInstance, operation, CC_PROPERTY);
    String bcc = applyTemplateIfNecessary(workflowInstance, operation, BCC_PROPERTY);
    String subject = applyTemplateIfNecessary(workflowInstance, operation, SUBJECT_PROPERTY);
    String bodyText = null;
    String body = operation.getConfiguration(BODY_PROPERTY);
    // If specified, templateFile is a file that contains the Freemarker template
    String bodyTemplateFile = operation.getConfiguration(BODY_TEMPLATE_FILE_PROPERTY);
    // Body informed? If not, use the default.
    if (body == null && bodyTemplateFile == null) {
        // Set the body of the message to be the ID of the media package
        bodyText = srcPackage.getTitle() + "(" + srcPackage.getIdentifier().toString() + ")";
    } else if (body != null) {
        bodyText = applyTemplateIfNecessary(workflowInstance, operation, BODY_PROPERTY);
    } else {
        bodyText = applyTemplateIfNecessary(workflowInstance, operation, BODY_TEMPLATE_FILE_PROPERTY);
    }
    try {
        logger.debug("Sending e-mail notification with subject {} and body {} to {}, CC addresses {} and BCC addresses {}", subject, bodyText, to, cc, bcc);
        // "To", "CC" and "BCC" can be comma- or space-separated lists of emails
        smtpService.send(to, cc, bcc, subject, bodyText);
        logger.info("E-mail notification sent to {}, CC addresses {} and BCC addresses {}", to, cc, bcc);
    } catch (MessagingException e) {
        throw new WorkflowOperationException(e);
    }
    // Return the source mediapackage and tell processing to continue
    return createResult(srcPackage, Action.CONTINUE);
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MessagingException(javax.mail.MessagingException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 62 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class MediaPackagePostOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    // get configuration
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    Configuration config = new Configuration(currentOperation);
    MediaPackage workflowMP = workflowInstance.getMediaPackage();
    MediaPackage mp = workflowMP;
    /* Check if we need to replace the Mediapackage we got with the published
     * Mediapackage from the Search Service */
    if (config.mpFromSearch()) {
        SearchQuery searchQuery = new SearchQuery();
        searchQuery.withId(mp.getIdentifier().toString());
        SearchResult result = searchService.getByQuery(searchQuery);
        if (result.size() != 1) {
            throw new WorkflowOperationException("Received multiple results for identifier" + "\"" + mp.getIdentifier().toString() + "\" from search service. ");
        }
        logger.info("Getting Mediapackage from Search Service");
        mp = result.getItems()[0].getMediaPackage();
    }
    logger.info("Submitting \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") as " + config.getFormat().name() + " to " + config.getUrl().toString());
    try {
        // serialize MediaPackage to target format
        String mpStr;
        if (config.getFormat() == Configuration.Format.JSON) {
            mpStr = MediaPackageParser.getAsJSON(mp);
        } else {
            mpStr = MediaPackageParser.getAsXml(mp);
        }
        // Log mediapackge
        if (config.debug()) {
            logger.info(mpStr);
        }
        // constrcut message body
        List<NameValuePair> data = new ArrayList<NameValuePair>();
        data.add(new BasicNameValuePair("mediapackage", mpStr));
        data.addAll(config.getAdditionalFields());
        // construct POST
        HttpPost post = new HttpPost(config.getUrl());
        post.setEntity(new UrlEncodedFormEntity(data, config.getEncoding()));
        // execute POST
        DefaultHttpClient client = new DefaultHttpClient();
        // Handle authentication
        if (config.authenticate()) {
            URL targetUrl = config.getUrl().toURL();
            client.getCredentialsProvider().setCredentials(new AuthScope(targetUrl.getHost(), targetUrl.getPort()), config.getCredentials());
        }
        HttpResponse response = client.execute(post);
        // throw Exception if target host did not return 200
        int status = response.getStatusLine().getStatusCode();
        if ((status >= 200) && (status < 300)) {
            if (config.debug()) {
                logger.info("Successfully submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": " + status);
            }
        } else if (status == 418) {
            logger.warn("Submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": The target claims to be a teapot. " + "The Reason for this is probably an insane programmer.");
        } else {
            throw new WorkflowOperationException("Faild to submit \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + "), " + config.getUrl().toString() + " answered with: " + Integer.toString(status));
        }
    } catch (Exception e) {
        if (e instanceof WorkflowOperationException) {
            throw (WorkflowOperationException) e;
        } else {
            logger.error("Submitting mediapackage failed: {}", e.toString());
            throw new WorkflowOperationException(e);
        }
    }
    return createResult(workflowMP, Action.CONTINUE);
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) SearchResult(org.opencastproject.search.api.SearchResult) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) URL(java.net.URL) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) AuthScope(org.apache.http.auth.AuthScope)

Example 63 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class HttpNotificationWorkflowOperationHandlerTest method getWorkflowOperationResult.

private WorkflowOperationResult getWorkflowOperationResult(MediaPackage mp, Map<String, String> configurations) throws WorkflowOperationException {
    // Add the mediapackage to a workflow instance
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowState.RUNNING);
    workflowInstance.setMediaPackage(mp);
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("op", OperationState.RUNNING);
    operation.setTemplate("http-notify");
    operation.setState(OperationState.RUNNING);
    for (String key : configurations.keySet()) {
        operation.setConfiguration(key, configurations.get(key));
    }
    List<WorkflowOperationInstance> operationsList = new ArrayList<WorkflowOperationInstance>();
    operationsList.add(operation);
    workflowInstance.setOperations(operationsList);
    // Run the media package through the operation handler, ensuring that metadata gets added
    return operationHandler.start(workflowInstance, null);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)

Example 64 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class MediaPackagePostOperationHandlerTest method createWorkflow.

/**
 * Creates a new workflow and readies the engine for processing
 */
private InstanceAndHandler createWorkflow(String url, String format) {
    WorkflowOperationHandler handler = new MediaPackagePostOperationHandler();
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowState.RUNNING);
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("op", OperationState.RUNNING);
    List<WorkflowOperationInstance> operationsList = new ArrayList<WorkflowOperationInstance>();
    operationsList.add(operation);
    workflowInstance.setOperations(operationsList);
    operation.setConfiguration("url", url);
    operation.setConfiguration("format", format);
    operation.setConfiguration("mediapackage.type", "workflow");
    return new InstanceAndHandler(workflowInstance, handler);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationHandler(org.opencastproject.workflow.api.WorkflowOperationHandler) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)

Example 65 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance 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)

Aggregations

WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)104 ArrayList (java.util.ArrayList)51 MediaPackage (org.opencastproject.mediapackage.MediaPackage)48 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)37 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)33 Test (org.junit.Test)32 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)31 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)28 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)24 Job (org.opencastproject.job.api.Job)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)19 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)19 URI (java.net.URI)18 NotFoundException (org.opencastproject.util.NotFoundException)16 Track (org.opencastproject.mediapackage.Track)14 IOException (java.io.IOException)13 File (java.io.File)12 HashMap (java.util.HashMap)12 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10