Search in sources :

Example 1 with Expression

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;
}
Also used : ValidationException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException) EvaluationException(org.springframework.expression.EvaluationException)

Example 2 with Expression

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);
}
Also used : ValidationException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException) CommonHttpResourceDownloader(org.ow2.proactive.http.CommonHttpResourceDownloader)

Example 3 with Expression

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;
}
Also used : ValidationException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException) EvaluationException(org.springframework.expression.EvaluationException)

Example 4 with Expression

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);
    }
}
Also used : ModelSyntaxException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ModelSyntaxException) MalformedURLException(java.net.MalformedURLException) AcceptAllValidator(org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.AcceptAllValidator) CommonHttpResourceDownloader(org.ow2.proactive.http.CommonHttpResourceDownloader) ModelValidator(org.ow2.proactive.scheduler.common.job.factories.spi.model.validator.ModelValidator) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with Expression

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;
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) InternalTask(org.ow2.proactive.scheduler.task.internal.InternalTask) Predictor(it.sauronsoftware.cron4j.Predictor) Date(java.util.Date)

Aggregations

IndeterminateEvaluationException (org.ow2.authzforce.core.pdp.api.IndeterminateEvaluationException)6 XPathExpression (javax.xml.xpath.XPathExpression)4 Node (org.w3c.dom.Node)4 IOException (java.io.IOException)3 Matcher (java.util.regex.Matcher)3 Pattern (java.util.regex.Pattern)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 Test (org.junit.Test)3 AbstractTest (org.ow2.petals.flowable.AbstractTest)3 InvalidAnnotationException (org.ow2.petals.flowable.incoming.operation.annotated.exception.InvalidAnnotationException)3 ValidationException (org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException)3 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Expression (org.ow2.authzforce.core.pdp.api.expression.Expression)2 NoBpmnOperationException (org.ow2.petals.flowable.incoming.operation.annotated.exception.NoBpmnOperationException)2 ProcessInstanceIdMappingExpressionException (org.ow2.petals.flowable.incoming.operation.annotated.exception.ProcessInstanceIdMappingExpressionException)2 VariableDefinition (org.ow2.petals.flowable.incoming.variable.VariableDefinition)2 CommonHttpResourceDownloader (org.ow2.proactive.http.CommonHttpResourceDownloader)2 EvaluationException (org.springframework.expression.EvaluationException)2 Element (org.w3c.dom.Element)2