use of org.ow2.authzforce.core.pdp.api.expression.Expression in project scheduling by ow2-proactive.
the class SpelValidator method validate.
@Override
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
try {
context.getSpELContext().setVariable("value", parameterValue);
Object untypedResult = spelExpression.getValue(context.getSpELContext());
if (!(untypedResult instanceof Boolean)) {
throw new ValidationException("SPEL expression did not return a boolean value.");
}
boolean evaluationResult = (Boolean) untypedResult;
if (!evaluationResult) {
throw new ValidationException("SPEL expression returned false, received " + parameterValue);
}
} catch (EvaluationException e) {
throw new ValidationException("SPEL expression raised an error: " + e.getMessage(), e);
}
return parameterValue;
}
use of org.ow2.authzforce.core.pdp.api.expression.Expression in project scheduling by ow2-proactive.
the class CatalogObjectValidator method exist.
private boolean exist(String catalogObjectValue, String sessionId) throws PermissionException, IOException, ValidationException {
String[] splitCatalog = catalogObjectValue.split("/");
String url;
// catalogObjectValue is already checked (with the regular expression) to have either 2 to 3 parts (bucketName/objectName[/revision]) after split
if (splitCatalog.length == 2) {
url = String.format(CATALOG_URL_WITHOUT_REVISION, splitCatalog[0], splitCatalog[1]);
} else if (splitCatalog.length == 3) {
url = String.format(CATALOG_URL_WITH_REVISION, splitCatalog[0], splitCatalog[1], splitCatalog[2]);
} else {
throw new ValidationException("Expected value should match the format: bucketName/objectName[/revision]");
}
CommonHttpResourceDownloader.ResponseContent response = CommonHttpResourceDownloader.getInstance().getResponse(sessionId, url, true);
return analyseResponseCode(response) && matchKindAndContentType(response, catalogObjectValue);
}
use of org.ow2.authzforce.core.pdp.api.expression.Expression in project scheduling by ow2-proactive.
the class SpelValidator method validate.
@Override
public String validate(String parameterValue, ModelValidatorContext context, boolean isVariableHidden) throws ValidationException {
try {
context.getSpELContext().setVariable("value", parameterValue);
// register true / false functions
context.getSpELContext().registerFunction("t", ModelValidatorContext.SpELVariables.class.getDeclaredMethod("t", new Class[] { Object.class }));
context.getSpELContext().registerFunction("f", ModelValidatorContext.SpELVariables.class.getDeclaredMethod("f", new Class[] { Object.class }));
context.getSpELContext().registerFunction("s", ModelValidatorContext.SpELVariables.class.getDeclaredMethod("s", new Class[] { Object.class }));
Object untypedResult = spelExpression.getValue(context.getSpELContext());
// validation can use either the 'valid' variable or the expression result as boolean
Boolean validVariable = null;
if (context.getSpELVariables() != null) {
validVariable = context.getSpELVariables().getValid();
}
if (!(untypedResult instanceof Boolean) && (validVariable == null)) {
throw new ValidationException("'valid' variable has not been set and SPEL expression did not return a boolean value.");
}
boolean evaluationResult;
if (validVariable != null) {
evaluationResult = validVariable;
} else {
evaluationResult = (Boolean) untypedResult;
}
if (!evaluationResult) {
throw new ValidationException("SPEL expression returned false, received " + parameterValue);
}
} catch (EvaluationException e) {
throw new ValidationException("SPEL expression raised an error: " + e.getMessage(), e);
} catch (NoSuchMethodException e) {
throw new ValidationException("Unexpected error: " + e.getMessage(), e);
}
return parameterValue;
}
use of org.ow2.authzforce.core.pdp.api.expression.Expression in project scheduling by ow2-proactive.
the class ModelFromURLParserValidator method createValidator.
@Override
protected Validator<String> createValidator(String model, Converter<String> converter) throws ModelSyntaxException {
String urlString = parseAndGetOneGroup(model, MODEL_FROM_URL_REGEXP);
URL url = null;
if (urlString.contains("${") && urlString.contains("}")) {
// if the url contains a non-resolved variable, it cannot be validated.
return new AcceptAllValidator<>();
}
try {
url = new URL(urlString);
String modelReceivedFromURL = null;
if (url.getProtocol().equals("http") || url.getProtocol().equals("https")) {
CommonHttpResourceDownloader.UrlContent content = CommonHttpResourceDownloader.getInstance().getResourceContent(null, url.toExternalForm(), true);
modelReceivedFromURL = content.getContent().trim();
} else {
List<String> lines = IOUtils.readLines(url.openStream(), Charset.defaultCharset());
modelReceivedFromURL = findFirstNonEmptyLineTrimmed(lines);
}
if (Strings.isNullOrEmpty(modelReceivedFromURL)) {
throw new ModelSyntaxException("In " + type + " expression, model received from defined url '" + urlString + "' is empty.");
}
if (modelReceivedFromURL.startsWith(type.name())) {
throw new ModelSyntaxException("In " + type + " expression, model received from defined url '" + urlString + "' is recursive.");
}
return new ModelValidator(modelReceivedFromURL);
} catch (MalformedURLException e) {
throw new ModelSyntaxException("In " + type + " expression, defined url '" + urlString + "' is invalid: " + e.getMessage(), e);
} catch (IOException e) {
throw new ModelSyntaxException("In " + type + " expression, defined url '" + urlString + "' could not be reached: " + e.getMessage(), e);
}
}
use of org.ow2.authzforce.core.pdp.api.expression.Expression in project scheduling by ow2-proactive.
the class TerminateLoopHandler method terminateLoopTask.
public boolean terminateLoopTask(FlowAction action, InternalTask initiator, ChangedTasksInfo changesInfo, SchedulerStateUpdate frontend) {
// find the target of the loop
InternalTask target = null;
if (action.getTarget().equals(initiator.getName())) {
target = initiator;
} else {
target = internalJob.findTask(action.getTarget());
}
boolean replicateForNextLoopIteration = internalJob.replicateForNextLoopIteration(initiator, target, changesInfo, frontend, action);
if (replicateForNextLoopIteration && action.getCronExpr() != null) {
for (TaskId tid : changesInfo.getNewTasks()) {
InternalTask newTask = internalJob.getIHMTasks().get(tid);
try {
Date startAt = (new Predictor(action.getCronExpr())).nextMatchingDate();
newTask.addGenericInformation(InternalJob.GENERIC_INFO_START_AT_KEY, ISO8601DateUtil.parse(startAt));
newTask.setScheduledTime(startAt.getTime());
} catch (InvalidPatternException e) {
// this will not happen as the cron expression is
// already being validated in FlowScript class.
LOGGER.debug(e.getMessage());
}
}
}
return replicateForNextLoopIteration;
}
Aggregations