Search in sources :

Example 6 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException in project opencast by opencast.

the class ExecuteServiceImpl method doProcess.

/**
 * Does the actual processing, given a mediapackage element (Execute Many WOH)
 *
 * @param arguments
 *          The list containing the program and its arguments
 * @param outFileName
 *          The name of the resulting file
 * @param expectedType
 *          The expected element type
 * @return A {@code String} containing the command output
 * @throws ExecuteException
 *           if some internal error occurred
 */
protected String doProcess(List<String> arguments, MediaPackageElement element, String outFileName, Type expectedType) throws ExecuteException {
    // arguments(1) contains a list of space-separated arguments for the command
    String params = arguments.remove(1);
    arguments.addAll(splitParameters(params));
    File outFile = null;
    try {
        // Get the track file from the workspace
        File trackFile = workspace.get(element.getURI());
        // Put the destination file in the same folder as the source file
        if (outFileName != null)
            outFile = new File(trackFile.getParentFile(), outFileName);
        // Substitute the appearances of the patterns with the actual absolute paths
        for (int i = 1; i < arguments.size(); i++) {
            if (arguments.get(i).contains(INPUT_FILE_PATTERN)) {
                arguments.set(i, arguments.get(i).replace(INPUT_FILE_PATTERN, trackFile.getAbsolutePath()));
                continue;
            }
            if (arguments.get(i).contains(OUTPUT_FILE_PATTERN)) {
                if (outFile != null) {
                    arguments.set(i, arguments.get(i).replace(OUTPUT_FILE_PATTERN, outFile.getAbsolutePath()));
                    continue;
                } else {
                    logger.error("{} pattern found, but no valid output filename was specified", OUTPUT_FILE_PATTERN);
                    throw new ExecuteException(OUTPUT_FILE_PATTERN + " pattern found, but no valid output filename was specified");
                }
            }
        }
        return runCommand(arguments, outFile, expectedType);
    } catch (IOException e) {
        logger.error("Error retrieving file from workspace: {}", element.getURI());
        throw new ExecuteException("Error retrieving file from workspace: " + element.getURI(), e);
    } catch (NotFoundException e) {
        logger.error("Element '{}' cannot be found in the workspace.", element.getURI());
        throw new ExecuteException("Element " + element.getURI() + " cannot be found in the workspace");
    }
}
Also used : ExecuteException(org.opencastproject.execute.api.ExecuteException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 7 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException 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 8 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException 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 9 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException in project opencast by opencast.

the class ExecuteServiceRemoteImpl method execute.

/**
 * @see org.opencastproject.execute.api.ExecuteService#execute(java.lang.String, java.lang.String, org.opencastproject.mediapackage.MediaPackage, java.lang.String, org.opencastproject.mediapackage.MediaPackageElement.Type, float)
 */
@Override
public Job execute(String exec, String params, MediaPackage mp, String outFileName, Type type, float load) throws ExecuteException {
    HttpPost post = null;
    HttpResponse response = null;
    try {
        String mpStr = MediaPackageParser.getAsXml(mp);
        List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
        formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
        formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
        formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
        formStringParams.add(new BasicNameValuePair(INPUT_MP_FORM_PARAM, mpStr));
        if (outFileName != null)
            formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
        if (type != null)
            formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
        logger.info("Executing command {} using a remote execute service", exec);
        post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
        post.setEntity(new UrlEncodedFormEntity(formStringParams));
        response = getResponse(post);
        if (response != null) {
            Job job = JobParser.parseJob(response.getEntity().getContent());
            logger.info("Completing execution of command {} using a remote execute service", exec);
            return job;
        } else {
            logger.error("Failed to execute the command {} using a remote execute service", exec);
            throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service", exec));
        }
    } catch (IllegalStateException e) {
        throw new ExecuteException(e);
    } catch (IOException e) {
        throw new ExecuteException(e);
    } finally {
        closeConnection(response);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) ExecuteException(org.opencastproject.execute.api.ExecuteException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) Job(org.opencastproject.job.api.Job)

Example 10 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException in project opencast by opencast.

the class ExecuteServiceRemoteImpl method execute.

/**
 * @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)
 */
public Job execute(String exec, String params, MediaPackageElement inElement, String outFileName, Type type, float load) throws ExecuteException {
    HttpPost post = null;
    HttpResponse response = null;
    try {
        String inElementStr = MediaPackageElementParser.getAsXml(inElement);
        List<NameValuePair> formStringParams = new ArrayList<NameValuePair>();
        formStringParams.add(new BasicNameValuePair(EXEC_FORM_PARAM, exec));
        formStringParams.add(new BasicNameValuePair(PARAMS_FORM_PARAM, params));
        formStringParams.add(new BasicNameValuePair(LOAD_FORM_PARAM, String.valueOf(load)));
        formStringParams.add(new BasicNameValuePair(INPUT_ELEM_FORM_PARAM, inElementStr));
        if (outFileName != null)
            formStringParams.add(new BasicNameValuePair(OUTPUT_NAME_FORM_PARAMETER, outFileName));
        if (type != null)
            formStringParams.add(new BasicNameValuePair(TYPE_FORM_PARAMETER, type.toString()));
        logger.info("Executing command {} using a remote execute service", exec);
        post = new HttpPost("/" + ExecuteService.ENDPOINT_NAME);
        post.setEntity(new UrlEncodedFormEntity(formStringParams));
        response = getResponse(post);
        if (response != null) {
            Job job = JobParser.parseJob(response.getEntity().getContent());
            logger.info("Completing execution of command {} using a remote execute service", exec);
            return job;
        } else
            throw new ExecuteException(String.format("Failed to execute the command %s using a remote execute service", exec));
    } catch (MediaPackageException e) {
        throw new ExecuteException("Error serializing the MediaPackage element", e);
    } catch (IllegalStateException e) {
        throw new ExecuteException(e);
    } catch (IOException e) {
        throw new ExecuteException(e);
    } finally {
        closeConnection(response);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ExecuteException(org.opencastproject.execute.api.ExecuteException) Job(org.opencastproject.job.api.Job)

Aggregations

ExecuteException (org.opencastproject.execute.api.ExecuteException)11 IOException (java.io.IOException)7 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)7 ArrayList (java.util.ArrayList)5 Job (org.opencastproject.job.api.Job)5 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)5 MediaPackage (org.opencastproject.mediapackage.MediaPackage)4 NotFoundException (org.opencastproject.util.NotFoundException)4 File (java.io.File)3 URI (java.net.URI)3 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2 HttpResponse (org.apache.http.HttpResponse)2 NameValuePair (org.apache.http.NameValuePair)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)2