Search in sources :

Example 26 with MediaPackageException

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

the class CaptionServiceRemoteImpl method convert.

/**
 * @see org.opencastproject.caption.api.CaptionService#convert(MediaPackageElement, String, String, String)
 */
@Override
public Job convert(MediaPackageElement input, String inputFormat, String outputFormat, String language) throws UnsupportedCaptionFormatException, CaptionConverterException, MediaPackageException {
    HttpPost post = new HttpPost("/convert");
    try {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("captions", MediaPackageElementParser.getAsXml(input)));
        params.add(new BasicNameValuePair("input", inputFormat));
        params.add(new BasicNameValuePair("output", outputFormat));
        if (StringUtils.isNotBlank(language))
            params.add(new BasicNameValuePair("language", language));
        post.setEntity(new UrlEncodedFormEntity(params));
    } catch (Exception e) {
        throw new CaptionConverterException(e);
    }
    HttpResponse response = null;
    try {
        response = getResponse(post);
        if (response != null) {
            String content = EntityUtils.toString(response.getEntity());
            Job r = JobParser.parseJob(content);
            logger.info("Converting job {} started on a remote caption service", r.getId());
            return r;
        }
    } catch (Exception e) {
        throw new CaptionConverterException("Unable to convert catalog " + input + " using a remote caption service", e);
    } finally {
        closeConnection(response);
    }
    throw new CaptionConverterException("Unable to convert catalog " + input + " using a remote caption service");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) Job(org.opencastproject.job.api.Job) CaptionConverterException(org.opencastproject.caption.api.CaptionConverterException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) UnsupportedCaptionFormatException(org.opencastproject.caption.api.UnsupportedCaptionFormatException)

Example 27 with MediaPackageException

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

the class ExecuteServiceImpl method runCommand.

private String runCommand(List<String> command, File outFile, Type expectedType) throws ExecuteException {
    Process p = null;
    int result = 0;
    try {
        logger.info("Running command {}", command.get(0));
        logger.debug("Starting subprocess {} with arguments {}", command.get(0), StringUtils.join(command.subList(1, command.size()), ", "));
        ProcessBuilder pb = new ProcessBuilder(command);
        pb.redirectErrorStream(true);
        p = pb.start();
        result = p.waitFor();
        logger.debug("Command {} finished with result {}", command.get(0), result);
        if (result == 0) {
            // Read the command output
            if (outFile != null) {
                if (outFile.isFile()) {
                    URI newURI = workspace.putInCollection(ExecuteService.COLLECTION, outFile.getName(), new FileInputStream(outFile));
                    if (outFile.delete()) {
                        logger.debug("Deleted the local copy of the encoded file at {}", outFile.getAbsolutePath());
                    } else {
                        logger.warn("Unable to delete the encoding output at {}", outFile.getAbsolutePath());
                    }
                    return MediaPackageElementParser.getAsXml(MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(newURI, expectedType, null));
                } else {
                    throw new ExecuteException("Expected output file does not exist: " + outFile.getAbsolutePath());
                }
            }
            return "";
        } else {
            // 'Scanner' reads tokens delimited by an specific character (set).
            // By telling a Scanner to use the 'beginning of the input boundary' character as delimiter, which of course
            // will never find, yields the whole String as the next token.
            String line;
            try (Scanner scanner = new Scanner(p.getInputStream())) {
                scanner.useDelimiter("\\A");
                line = scanner.next();
            } catch (NoSuchElementException e) {
                line = "";
            }
            throw new ExecuteException(String.format("Process %s returned error code %d with this output:\n%s", command.get(0), result, line.trim()));
        }
    } catch (InterruptedException e) {
        throw new ExecuteException("The executor thread has been unexpectedly interrupted", e);
    } catch (IOException e) {
        // Only log the first argument, the executable, as other arguments may contain sensitive values
        // e.g. MySQL password/user, paths, etc. that should not be shown to caller
        logger.error("Could not start subprocess {}", command.get(0));
        throw new ExecuteException("Could not start subprocess: " + command.get(0), e);
    } catch (UnsupportedElementException e) {
        throw new ExecuteException("Couldn't create a new MediaPackage element of type " + expectedType.toString(), e);
    } catch (ConfigurationException e) {
        throw new ExecuteException("Couldn't instantiate a new MediaPackage element builder", e);
    } catch (MediaPackageException e) {
        throw new ExecuteException("Couldn't serialize a new Mediapackage element of type " + expectedType.toString(), e);
    } finally {
        IoSupport.closeQuietly(p);
    }
}
Also used : Scanner(java.util.Scanner) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException) URI(java.net.URI) FileInputStream(java.io.FileInputStream) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) ConfigurationException(org.opencastproject.util.ConfigurationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) NoSuchElementException(java.util.NoSuchElementException)

Example 28 with MediaPackageException

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

the class ExecuteServiceImpl method execute.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.execute.api.ExecuteService#execute(java.lang.String, java.lang.String,
 *      org.opencastproject.mediapackage.MediaPackageElement, java.lang.String,
 *      org.opencastproject.mediapackage.MediaPackageElement.Type, float)
 * @throws IllegalArgumentException
 *           if the input arguments are incorrect
 * @throws ExecuteException
 *           if an internal error occurs
 */
@Override
public Job execute(String exec, String params, MediaPackageElement inElement, String outFileName, Type expectedType, float load) throws ExecuteException, IllegalArgumentException {
    logger.debug("Creating Execute Job for command: {}", exec);
    if (StringUtils.isBlank(exec))
        throw new IllegalArgumentException("The command to execute cannot be null");
    if (StringUtils.isBlank(params))
        throw new IllegalArgumentException("The command arguments cannot be null");
    if (inElement == null)
        throw new IllegalArgumentException("The input MediaPackage element cannot be null");
    outFileName = StringUtils.trimToNull(outFileName);
    if ((outFileName == null) && (expectedType != null) || (outFileName != null) && (expectedType == null))
        throw new IllegalArgumentException("Expected element type and output filename cannot be null");
    try {
        List<String> paramList = new ArrayList<String>(5);
        paramList.add(exec);
        paramList.add(params);
        paramList.add(MediaPackageElementParser.getAsXml(inElement));
        paramList.add(outFileName);
        paramList.add((expectedType == null) ? null : expectedType.toString());
        return serviceRegistry.createJob(JOB_TYPE, Operation.Execute_Element.toString(), paramList, load);
    } catch (ServiceRegistryException e) {
        throw new ExecuteException(String.format("Unable to create a job of type '%s'", JOB_TYPE), e);
    } catch (MediaPackageException e) {
        throw new ExecuteException("Error serializing an element", e);
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ArrayList(java.util.ArrayList) ExecuteException(org.opencastproject.execute.api.ExecuteException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException)

Example 29 with MediaPackageException

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

the class ExecuteManyWorkflowOperationHandler 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 sourceFlavor = StringUtils.trimToNull(operation.getConfiguration(SOURCE_FLAVOR_PROPERTY));
    String sourceTags = StringUtils.trimToNull(operation.getConfiguration(SOURCE_TAGS_PROPERTY));
    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));
    MediaPackageElementFlavor matchingFlavor = null;
    if (sourceFlavor != null)
        matchingFlavor = MediaPackageElementFlavor.parseFlavor(sourceFlavor);
    // 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");
    }
    List<String> sourceTagList = asList(sourceTags);
    // Select the tracks based on source flavors and tags
    Set<MediaPackageElement> inputSet = new HashSet<>();
    for (MediaPackageElement element : mediaPackage.getElementsByTags(sourceTagList)) {
        MediaPackageElementFlavor elementFlavor = element.getFlavor();
        if (sourceFlavor == null || (elementFlavor != null && elementFlavor.matches(matchingFlavor))) {
            inputSet.add(element);
        }
    }
    if (inputSet.size() == 0) {
        logger.warn("Mediapackage {} has no suitable elements to execute the command {} based on tags {} and flavor {}", mediaPackage, exec, sourceTags, sourceFlavor);
        return createResult(mediaPackage, Action.CONTINUE);
    }
    MediaPackageElement[] inputElements = inputSet.toArray(new MediaPackageElement[inputSet.size()]);
    try {
        Job[] jobs = new Job[inputElements.length];
        MediaPackageElement[] resultElements = new MediaPackageElement[inputElements.length];
        long totalTimeInQueue = 0;
        for (int i = 0; i < inputElements.length; i++) jobs[i] = executeService.execute(exec, params, inputElements[i], outputFilename, expectedType, load);
        // Wait for all jobs to be finished
        if (!waitForStatus(jobs).isSuccess())
            throw new WorkflowOperationException("Execute operation failed");
        // Find which output elements are tracks and inspect them
        HashMap<Integer, Job> jobMap = new HashMap<>();
        for (int i = 0; i < jobs.length; i++) {
            // Add this job's queue time to the total
            totalTimeInQueue += jobs[i].getQueueTime();
            if (StringUtils.trimToNull(jobs[i].getPayload()) != null) {
                resultElements[i] = MediaPackageElementParser.getFromXml(jobs[i].getPayload());
                if (resultElements[i].getElementType() == MediaPackageElement.Type.Track) {
                    jobMap.put(i, inspectionService.inspect(resultElements[i].getURI()));
                }
            } else
                resultElements[i] = inputElements[i];
        }
        if (jobMap.size() > 0) {
            if (!waitForStatus(jobMap.values().toArray(new Job[jobMap.size()])).isSuccess())
                throw new WorkflowOperationException("Execute operation failed in track inspection");
            for (Entry<Integer, Job> entry : jobMap.entrySet()) {
                // Add this job's queue time to the total
                totalTimeInQueue += entry.getValue().getQueueTime();
                resultElements[entry.getKey()] = MediaPackageElementParser.getFromXml(entry.getValue().getPayload());
            }
        }
        for (int i = 0; i < resultElements.length; i++) {
            if (resultElements[i] != inputElements[i]) {
                // Store new element to mediaPackage
                mediaPackage.addDerived(resultElements[i], inputElements[i]);
                // Store new element to mediaPackage
                URI uri = workspace.moveTo(resultElements[i].getURI(), mediaPackage.getIdentifier().toString(), resultElements[i].getIdentifier(), outputFilename);
                resultElements[i].setURI(uri);
                // Set new flavor
                if (targetFlavor != null)
                    resultElements[i].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
                        resultElements[i].removeTag(tag.replaceAll("^-+", ""));
                    else
                        resultElements[i].addTag(tag);
                }
            }
        }
        WorkflowOperationResult result = createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
        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("Error inspecting one of the created tracks", e);
    }
}
Also used : HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) 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) HashSet(java.util.HashSet) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Example 30 with MediaPackageException

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

the class OaiPmhPublicationServiceImpl method updateMetadata.

protected Publication updateMetadata(Job job, MediaPackage mediaPackage, String repository, Set<String> flavors, Set<String> tags, boolean checkAvailability) throws PublicationException {
    final Set<MediaPackageElementFlavor> parsedFlavors = new HashSet<>();
    for (String flavor : flavors) {
        parsedFlavors.add(MediaPackageElementFlavor.parseFlavor(flavor));
    }
    final MediaPackage filteredMp;
    final SearchResult result = oaiPmhDatabase.search(QueryBuilder.queryRepo(repository).mediaPackageId(mediaPackage).isDeleted(false).build());
    if (result.size() == 1) {
        // apply tags and flavors to the current media package
        try {
            logger.debug("filter elements with flavors {} and tags {} on media package {}", StringUtils.join(flavors, ", "), StringUtils.join(tags, ", "), MediaPackageParser.getAsXml(mediaPackage));
            filteredMp = filterMediaPackage(mediaPackage, parsedFlavors, tags);
        } catch (MediaPackageException e) {
            throw new PublicationException("Error filtering media package", e);
        }
    } else if (result.size() == 0) {
        logger.info(format("Skipping update of media package %s since it is not currently published to %s", mediaPackage, repository));
        return null;
    } else {
        final String msg = format("More than one media package with id %s found", mediaPackage.getIdentifier().compact());
        logger.warn(msg);
        throw new PublicationException(msg);
    }
    // re-distribute elements to download
    Set<String> elementIdsToDistribute = new HashSet<>();
    for (MediaPackageElement mpe : filteredMp.getElements()) {
        // do not distribute publications
        if (MediaPackageElement.Type.Publication == mpe.getElementType())
            continue;
        elementIdsToDistribute.add(mpe.getIdentifier());
    }
    if (elementIdsToDistribute.isEmpty()) {
        logger.debug("The media package {} does not contain any elements to update. " + "Skip OAI-PMH metadata update operation for repository {}", mediaPackage.getIdentifier().compact(), repository);
        return null;
    }
    logger.debug("distribute elements {}", StringUtils.join(elementIdsToDistribute, ", "));
    final List<MediaPackageElement> distributedElements = new ArrayList<>();
    try {
        Job distJob = downloadDistributionService.distribute(getPublicationChannelName(repository), filteredMp, elementIdsToDistribute, checkAvailability);
        if (job == null)
            throw new PublicationException("The distribution service can not handle this type of media package elements.");
        if (!waitForJobs(job, serviceRegistry, distJob).isSuccess()) {
            throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()));
        }
        if (distJob.getPayload() != null) {
            for (MediaPackageElement mpe : MediaPackageElementParser.getArrayFromXml(distJob.getPayload())) {
                distributedElements.add(mpe);
            }
        }
    } catch (DistributionException | MediaPackageException e) {
        throw new PublicationException(format("Unable to distribute updated elements from media package %s to the download distribution service", mediaPackage.getIdentifier().compact()), e);
    }
    // update elements (URLs)
    for (MediaPackageElement e : filteredMp.getElements()) {
        if (MediaPackageElement.Type.Publication.equals(e.getElementType()))
            continue;
        filteredMp.remove(e);
    }
    for (MediaPackageElement e : distributedElements) {
        filteredMp.add(e);
    }
    MediaPackage publishedMp = merge(filteredMp, removeMatchingNonExistantElements(filteredMp, (MediaPackage) result.getItems().get(0).getMediaPackage().clone(), parsedFlavors, tags));
    // Does the media package have a title and track?
    if (!MediaPackageSupport.isPublishable(publishedMp)) {
        throw new PublicationException("Media package does not meet criteria for publication");
    }
    // Publish the media package to OAI-PMH
    try {
        logger.debug(format("Updating metadata of media package %s in %s", publishedMp.getIdentifier().compact(), repository));
        oaiPmhDatabase.store(publishedMp, repository);
    } catch (OaiPmhDatabaseException e) {
        throw new PublicationException(format("Media package %s could not be updated", publishedMp.getIdentifier().compact()));
    }
    // retract orphaned elements from download distribution
    // orphaned elements are all those elements to which the updated media package no longer refers (in terms of element uri)
    Map<URI, MediaPackageElement> elementUriMap = new Hashtable<>();
    for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
        for (MediaPackageElement mpe : oaiPmhSearchResultItem.getMediaPackage().getElements()) {
            if (MediaPackageElement.Type.Publication == mpe.getElementType() || null == mpe.getURI())
                continue;
            elementUriMap.put(mpe.getURI(), mpe);
        }
    }
    for (MediaPackageElement publishedMpe : publishedMp.getElements()) {
        if (MediaPackageElement.Type.Publication == publishedMpe.getElementType())
            continue;
        if (elementUriMap.containsKey(publishedMpe.getURI()))
            elementUriMap.remove(publishedMpe.getURI());
    }
    Set<String> orphanedElementIds = new HashSet<>();
    for (MediaPackageElement orphanedMpe : elementUriMap.values()) {
        orphanedElementIds.add(orphanedMpe.getIdentifier());
    }
    if (!orphanedElementIds.isEmpty()) {
        for (SearchResultItem oaiPmhSearchResultItem : result.getItems()) {
            try {
                Job retractJob = downloadDistributionService.retract(getPublicationChannelName(repository), oaiPmhSearchResultItem.getMediaPackage(), orphanedElementIds);
                if (retractJob != null) {
                    if (!waitForJobs(job, serviceRegistry, retractJob).isSuccess())
                        logger.warn("The download distribution retract job for the orphaned elements from media package {} does not end successfully", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact());
                }
            } catch (DistributionException e) {
                logger.warn("Unable to retract orphaned elements from download distribution service for the media package {} channel {}", oaiPmhSearchResultItem.getMediaPackage().getIdentifier().compact(), getPublicationChannelName(repository), e);
            }
        }
    }
    // return the publication
    String publicationChannel = getPublicationChannelName(repository);
    for (Publication p : mediaPackage.getPublications()) {
        if (StringUtils.equals(publicationChannel, p.getChannel()))
            return p;
    }
    return null;
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) PublicationException(org.opencastproject.publication.api.PublicationException) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Publication(org.opencastproject.mediapackage.Publication) SearchResult(org.opencastproject.oaipmh.persistence.SearchResult) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) URI(java.net.URI) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job) HashSet(java.util.HashSet)

Aggregations

MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)72 Job (org.opencastproject.job.api.Job)42 IOException (java.io.IOException)34 MediaPackage (org.opencastproject.mediapackage.MediaPackage)33 NotFoundException (org.opencastproject.util.NotFoundException)31 URI (java.net.URI)25 ArrayList (java.util.ArrayList)25 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)21 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)20 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)19 Track (org.opencastproject.mediapackage.Track)17 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)15 File (java.io.File)13 HashMap (java.util.HashMap)13 InputStream (java.io.InputStream)12 HttpResponse (org.apache.http.HttpResponse)12 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)12 HttpPost (org.apache.http.client.methods.HttpPost)12 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)12 EncoderException (org.opencastproject.composer.api.EncoderException)12