use of org.opencastproject.mediapackage.MediaPackageException 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);
}
}
use of org.opencastproject.mediapackage.MediaPackageException 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();
}
}
use of org.opencastproject.mediapackage.MediaPackageException 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);
}
}
use of org.opencastproject.mediapackage.MediaPackageException 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);
}
}
use of org.opencastproject.mediapackage.MediaPackageException in project opencast by opencast.
the class ConfigurablePublishWorkflowOperationHandler method distribute.
private Set<MediaPackageElement> distribute(Collection<MediaPackageElement> elements, MediaPackage mediapackage, String channelId, String mode, boolean checkAvailability) throws WorkflowOperationException {
Set<MediaPackageElement> result = new HashSet<>();
Set<String> bulkElementIds = new HashSet<>();
Set<String> singleElementIds = new HashSet<>();
for (MediaPackageElement element : elements) {
if (MODE_BULK.equals(mode) || (MODE_MIXED.equals(mode) && (element.getElementType() != MediaPackageElement.Type.Track))) {
bulkElementIds.add(element.getIdentifier());
} else {
singleElementIds.add(element.getIdentifier());
}
}
Set<Job> jobs = new HashSet<>();
if (bulkElementIds.size() > 0) {
logger.info("Start bulk publishing of {} elements of media package '{}' to publication channel '{}'", bulkElementIds.size(), mediapackage, channelId);
try {
Job job = distributionService.distribute(channelId, mediapackage, bulkElementIds, checkAvailability);
jobs.add(job);
} catch (DistributionException | MediaPackageException e) {
logger.error("Creating the distribution job for {} elements of media package '{}' failed: {}", bulkElementIds.size(), mediapackage, getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
if (singleElementIds.size() > 0) {
logger.info("Start single publishing of {} elements of media package '{}' to publication channel '{}'", singleElementIds.size(), mediapackage, channelId);
for (String elementId : singleElementIds) {
try {
Job job = distributionService.distribute(channelId, mediapackage, elementId, checkAvailability);
jobs.add(job);
} catch (DistributionException | MediaPackageException e) {
logger.error("Creating the distribution job for element '{}' of media package '{}' failed: {}", elementId, mediapackage, getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
}
if (jobs.size() > 0) {
if (!waitForStatus(jobs.toArray(new Job[jobs.size()])).isSuccess()) {
throw new WorkflowOperationException("At least one of the distribution jobs did not complete successfully");
}
for (Job job : jobs) {
try {
List<? extends MediaPackageElement> elems = MediaPackageElementParser.getArrayFromXml(job.getPayload());
result.addAll(elems);
} catch (MediaPackageException e) {
logger.error("Job '{}' returned payload ({}) that could not be parsed to media package elements: {}", job, job.getPayload(), ExceptionUtils.getStackTrace(e));
throw new WorkflowOperationException(e);
}
}
logger.info("Published {} elements of media package {} to publication channel {}", bulkElementIds.size() + singleElementIds.size(), mediapackage, channelId);
}
return result;
}
Aggregations