Search in sources :

Example 91 with MediaPackageElement

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

the class ExecuteServiceImpl method process.

/**
 * {@inheritDoc}
 *
 * @throws ExecuteException
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
 */
@Override
protected String process(Job job) throws ExecuteException {
    List<String> arguments = new ArrayList<String>(job.getArguments());
    // Check this operation is allowed
    if (!allowedCommands.contains("*") && !allowedCommands.contains(arguments.get(0)))
        throw new ExecuteException("Command '" + arguments.get(0) + "' is not allowed");
    String outFileName = null;
    String strAux = null;
    MediaPackage mp = null;
    Type expectedType = null;
    MediaPackageElement element = null;
    Operation op = null;
    try {
        op = Operation.valueOf(job.getOperation());
        int nargs = arguments.size();
        if (nargs != 3 && nargs != 5) {
            throw new IndexOutOfBoundsException("Incorrect number of parameters for operation execute_" + op + ": " + arguments.size());
        }
        if (nargs == 5) {
            strAux = arguments.remove(4);
            expectedType = (strAux == null) ? null : Type.valueOf(strAux);
            outFileName = StringUtils.trimToNull(arguments.remove(3));
            if ((StringUtils.isNotBlank(outFileName) && (expectedType == null)) || (StringUtils.isBlank(outFileName) && (expectedType != null))) {
                throw new ExecuteException("The output type and filename must be both specified");
            }
            outFileName = (outFileName == null) ? null : job.getId() + "_" + outFileName;
        }
        switch(op) {
            case Execute_Mediapackage:
                mp = MediaPackageParser.getFromXml(arguments.remove(2));
                return doProcess(arguments, mp, outFileName, expectedType);
            case Execute_Element:
                element = MediaPackageElementParser.getFromXml(arguments.remove(2));
                return doProcess(arguments, element, outFileName, expectedType);
            default:
                throw new IllegalStateException("Don't know how to handle operation '" + job.getOperation() + "'");
        }
    } catch (MediaPackageException e) {
        throw new ExecuteException("Error unmarshalling the input mediapackage/element", e);
    } catch (IllegalArgumentException e) {
        throw new ExecuteException("This service can't handle operations of type '" + op + "'", e);
    } catch (IndexOutOfBoundsException e) {
        throw new ExecuteException("The argument list for operation '" + op + "' does not meet expectations", e);
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ArrayList(java.util.ArrayList) Type(org.opencastproject.mediapackage.MediaPackageElement.Type) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ExecuteException(org.opencastproject.execute.api.ExecuteException)

Example 92 with MediaPackageElement

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

the class ExecuteServiceImplTest method testWithInputElement.

@Test
public void testWithInputElement() throws ExecuteException, NotFoundException {
    List<String> params = new ArrayList<>();
    params.add("echo");
    params.add(pattern);
    MediaPackageElement element = MediaPackageElementBuilderFactory.newInstance().newElementBuilder().elementFromURI(baseDirURI);
    String result = executor.doProcess(params, element, null, null);
    Assert.assertEquals(result, "");
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 93 with MediaPackageElement

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

the class ExecuteRestEndpoint method execute.

@POST
@Produces(MediaType.TEXT_XML)
@Path(ExecuteService.ENDPOINT_NAME)
@RestQuery(name = "name", description = "Executes the given command", restParameters = { @RestParameter(description = "The command to execute", isRequired = true, name = ExecuteService.EXEC_FORM_PARAM, type = RestParameter.Type.STRING), @RestParameter(description = "The arguments to the command", isRequired = true, name = ExecuteService.PARAMS_FORM_PARAM, type = RestParameter.Type.STRING), @RestParameter(description = "The estimated load placed on the system by this command", isRequired = false, name = ExecuteService.LOAD_FORM_PARAM, type = RestParameter.Type.FLOAT), @RestParameter(description = "The mediapackage to apply the command to. Either this or " + ExecuteService.INPUT_ELEM_FORM_PARAM + " are required", isRequired = false, name = ExecuteService.INPUT_MP_FORM_PARAM, type = RestParameter.Type.TEXT), @RestParameter(description = "The mediapackage element to apply the command to. Either this or " + ExecuteService.INPUT_MP_FORM_PARAM + " are required", isRequired = false, name = ExecuteService.INPUT_ELEM_FORM_PARAM, type = RestParameter.Type.TEXT), @RestParameter(description = "The mediapackage element produced by the command", isRequired = false, name = ExecuteService.OUTPUT_NAME_FORM_PARAMETER, type = RestParameter.Type.STRING), @RestParameter(description = "The type of the returned element", isRequired = false, name = ExecuteService.TYPE_FORM_PARAMETER, type = RestParameter.Type.STRING) }, reponses = { @RestResponse(description = "XML-encoded Job is returned.", responseCode = HttpServletResponse.SC_NO_CONTENT), @RestResponse(description = "Service unavailabe or not currently present", responseCode = HttpServletResponse.SC_SERVICE_UNAVAILABLE), @RestResponse(description = "Incorrect parameters", responseCode = HttpServletResponse.SC_BAD_REQUEST), @RestResponse(description = "Problem executing the command or serializing the arguments/results", responseCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR) }, returnDescription = "")
public Response execute(@FormParam(ExecuteService.EXEC_FORM_PARAM) String exec, @FormParam(ExecuteService.PARAMS_FORM_PARAM) String params, @FormParam(ExecuteService.LOAD_FORM_PARAM) Float loadParam, @FormParam(ExecuteService.INPUT_ELEM_FORM_PARAM) String inputElementStr, @FormParam(ExecuteService.INPUT_MP_FORM_PARAM) String inputMpStr, @FormParam(ExecuteService.OUTPUT_NAME_FORM_PARAMETER) String outputFileName, @FormParam(ExecuteService.TYPE_FORM_PARAMETER) String elementTypeStr) {
    checkNotNull(service);
    try {
        MediaPackageElement.Type expectedType = null;
        if (StringUtils.isNotBlank(elementTypeStr)) {
            for (MediaPackageElement.Type candidateType : MediaPackageElement.Type.values()) if (candidateType.toString().equalsIgnoreCase(elementTypeStr)) {
                expectedType = candidateType;
                break;
            }
            if (expectedType == null) {
                logger.error("Wrong element type specified: {}", elementTypeStr);
                return Response.status(Response.Status.BAD_REQUEST).build();
            }
        }
        float load = 1.0f;
        if (loadParam != null) {
            load = loadParam;
        }
        Job retJob = null;
        if (StringUtils.isNotBlank(inputElementStr) && StringUtils.isNotBlank(inputMpStr)) {
            logger.error("Only one input MediaPackage OR input MediaPackageElement can be set at the same time");
            return Response.status(Response.Status.BAD_REQUEST).build();
        } else if (StringUtils.isNotBlank(inputElementStr)) {
            MediaPackageElement inputElement = MediaPackageElementParser.getFromXml(inputElementStr);
            retJob = service.execute(exec, params, inputElement, outputFileName, expectedType, load);
        } else if (StringUtils.isNotBlank(inputMpStr)) {
            MediaPackage inputMp = MediaPackageParser.getFromXml(inputMpStr);
            retJob = service.execute(exec, params, inputMp, outputFileName, expectedType, load);
        } else {
            logger.error("A MediaPackage OR MediaPackageElement must be provided");
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
        return Response.ok(new JaxbJob(retJob)).build();
    } catch (IllegalArgumentException e) {
        logger.error("The expected element type is required if an output filename is specified");
        return Response.status(Response.Status.BAD_REQUEST).build();
    } catch (MediaPackageException e) {
        logger.error("Received excepcion: {}", e.getMessage());
        return Response.serverError().build();
    } catch (ExecuteException e) {
        logger.error("Received error from the execute service: {}", e.getMessage());
        return Response.serverError().build();
    }
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) JaxbJob(org.opencastproject.job.api.JaxbJob) MediaPackage(org.opencastproject.mediapackage.MediaPackage) ExecuteException(org.opencastproject.execute.api.ExecuteException) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 94 with MediaPackageElement

use of org.opencastproject.mediapackage.MediaPackageElement 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 95 with MediaPackageElement

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

the class OaiPmhPersistenceTest method testModifyElement.

@Test
public void testModifyElement() throws Exception {
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    MediaPackageElement catalog1 = mp1.getElementById("catalog-1");
    catalog1.setURI(URI.create("foo.xml"));
    catalog1.setChecksum(null);
    oaiPmhDatabase.store(mp1, REPOSITORY_ID_1);
    int count = 0;
    for (SearchResultItem searchResultItem : oaiPmhDatabase.search(queryRepo(REPOSITORY_ID_1).mediaPackageId(mp1).build()).getItems()) {
        count++;
        Assert.assertNotNull(searchResultItem.getMediaPackage());
        MediaPackageElement mpe = searchResultItem.getMediaPackage().getElementById("catalog-1");
        Assert.assertNotNull(mpe);
        Assert.assertEquals("foo.xml", mpe.getURI().toString());
    }
    Assert.assertEquals(1, count);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) SearchResultItem(org.opencastproject.oaipmh.persistence.SearchResultItem) Test(org.junit.Test)

Aggregations

MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)153 MediaPackage (org.opencastproject.mediapackage.MediaPackage)72 NotFoundException (org.opencastproject.util.NotFoundException)50 Job (org.opencastproject.job.api.Job)49 URI (java.net.URI)48 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)39 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)39 Test (org.junit.Test)36 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)27 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)25 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)24 File (java.io.File)21 Track (org.opencastproject.mediapackage.Track)21 DistributionException (org.opencastproject.distribution.api.DistributionException)20 InputStream (java.io.InputStream)19 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)19 HashSet (java.util.HashSet)18 URISyntaxException (java.net.URISyntaxException)16 Path (javax.ws.rs.Path)16