Search in sources :

Example 11 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class MappingUtils method evaluateNAME.

/**
 * Build __NAME__ for propagation.
 * First look if there is a defined connObjectLink for the given resource (and in
 * this case evaluate as JEXL); otherwise, take given connObjectKey.
 *
 * @param any given any object
 * @param provision external resource
 * @param connObjectKey connector object key
 * @return the value to be propagated as __NAME__
 */
public static Name evaluateNAME(final Any<?> any, final Provision provision, final String connObjectKey) {
    if (StringUtils.isBlank(connObjectKey)) {
        // LOG error but avoid to throw exception: leave it to the external resource
        LOG.warn("Missing ConnObjectKey value for {}: ", provision.getResource());
    }
    // Evaluate connObjectKey expression
    String connObjectLink = provision == null || provision.getMapping() == null ? null : provision.getMapping().getConnObjectLink();
    String evalConnObjectLink = null;
    if (StringUtils.isNotBlank(connObjectLink)) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addFieldsToContext(any, jexlContext);
        JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
        JexlUtils.addDerAttrsToContext(any, jexlContext);
        evalConnObjectLink = JexlUtils.evaluate(connObjectLink, jexlContext);
    }
    return getName(evalConnObjectLink, connObjectKey);
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 12 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class MappingUtils method evaluateNAME.

/**
 * Build __NAME__ for propagation.
 * First look if there is a defined connObjectLink for the given resource (and in
 * this case evaluate as JEXL); otherwise, take given connObjectKey.
 *
 * @param realm given any object
 * @param orgUnit external resource
 * @param connObjectKey connector object key
 * @return the value to be propagated as __NAME__
 */
public static Name evaluateNAME(final Realm realm, final OrgUnit orgUnit, final String connObjectKey) {
    if (StringUtils.isBlank(connObjectKey)) {
        // LOG error but avoid to throw exception: leave it to the external resource
        LOG.warn("Missing ConnObjectKey value for {}: ", orgUnit.getResource());
    }
    // Evaluate connObjectKey expression
    String connObjectLink = orgUnit == null ? null : orgUnit.getConnObjectLink();
    String evalConnObjectLink = null;
    if (StringUtils.isNotBlank(connObjectLink)) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addFieldsToContext(realm, jexlContext);
        evalConnObjectLink = JexlUtils.evaluate(connObjectLink, jexlContext);
    }
    return getName(evalConnObjectLink, connObjectKey);
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 13 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class DerAttrHandlerImpl method getValues.

private Map<DerSchema, String> getValues(final GroupableRelatable<?, ?, ?, ?, ?> any, final Membership<?> membership, final Set<DerSchema> schemas) {
    Map<DerSchema, String> result = new HashMap<>(schemas.size());
    for (DerSchema schema : schemas) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addPlainAttrsToContext(any.getPlainAttrs(membership), jexlContext);
        JexlUtils.addFieldsToContext(any, jexlContext);
        result.put(schema, JexlUtils.evaluate(schema.getExpression(), jexlContext));
    }
    return result;
}
Also used : DerSchema(org.apache.syncope.core.persistence.api.entity.DerSchema) HashMap(java.util.HashMap) JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 14 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class JexlUtils method evaluateMandatoryCondition.

public static boolean evaluateMandatoryCondition(final String mandatoryCondition, final Any<?> any) {
    JexlContext jexlContext = new MapContext();
    addPlainAttrsToContext(any.getPlainAttrs(), jexlContext);
    addDerAttrsToContext(any, jexlContext);
    return Boolean.parseBoolean(evaluate(mandatoryCondition, jexlContext));
}
Also used : JexlContext(org.apache.commons.jexl3.JexlContext) MapContext(org.apache.commons.jexl3.MapContext)

Example 15 with JexlContext

use of org.apache.commons.jexl2.JexlContext in project syncope by apache.

the class ConfigurationDataBinderImpl method fillAttr.

private void fillAttr(final List<String> values, final PlainSchema schema, final CPlainAttr attr, final SyncopeClientException invalidValues) {
    // if schema is multivalue, all values are considered for addition;
    // otherwise only the fist one - if provided - is considered
    List<String> valuesProvided = schema.isMultivalue() ? values : (values.isEmpty() ? Collections.<String>emptyList() : Collections.singletonList(values.iterator().next()));
    if (valuesProvided.isEmpty()) {
        JexlContext jexlContext = new MapContext();
        JexlUtils.addPlainAttrsToContext(confDAO.get().getPlainAttrs(), jexlContext);
        if (!schema.isReadonly() && Boolean.parseBoolean(JexlUtils.evaluate(schema.getMandatoryCondition(), jexlContext))) {
            LOG.error("Mandatory schema " + schema.getKey() + " not provided with values");
            SyncopeClientException reqValMissing = SyncopeClientException.build(ClientExceptionType.RequiredValuesMissing);
            reqValMissing.getElements().add(schema.getKey());
            throw reqValMissing;
        }
    }
    for (String value : valuesProvided) {
        if (value == null || value.isEmpty()) {
            LOG.debug("Null value for {}, ignoring", schema.getKey());
        } else {
            try {
                PlainAttrValue attrValue;
                if (schema.isUniqueConstraint()) {
                    attrValue = entityFactory.newEntity(CPlainAttrUniqueValue.class);
                    ((PlainAttrUniqueValue) attrValue).setSchema(schema);
                } else {
                    attrValue = entityFactory.newEntity(CPlainAttrValue.class);
                }
                attr.add(value, attrValue);
            } catch (InvalidPlainAttrValueException e) {
                LOG.warn("Invalid value for attribute " + schema.getKey() + ": " + value, e);
                invalidValues.getElements().add(schema.getKey() + ": " + value + " - " + e.getMessage());
            }
        }
    }
}
Also used : CPlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrUniqueValue) PlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.PlainAttrUniqueValue) CPlainAttrValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrValue) CPlainAttrUniqueValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrUniqueValue) JexlContext(org.apache.commons.jexl3.JexlContext) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) PlainAttrValue(org.apache.syncope.core.persistence.api.entity.PlainAttrValue) CPlainAttrValue(org.apache.syncope.core.persistence.api.entity.conf.CPlainAttrValue) MapContext(org.apache.commons.jexl3.MapContext) InvalidPlainAttrValueException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidPlainAttrValueException)

Aggregations

JexlContext (org.apache.commons.jexl2.JexlContext)31 Expression (org.apache.commons.jexl2.Expression)25 MapContext (org.apache.commons.jexl2.MapContext)23 JexlContext (org.apache.commons.jexl3.JexlContext)21 JexlEngine (org.apache.commons.jexl2.JexlEngine)20 MapContext (org.apache.commons.jexl3.MapContext)17 Test (org.testng.annotations.Test)13 HashMap (java.util.HashMap)5 Map (java.util.Map)4 ArrayList (java.util.ArrayList)3 JexlException (org.apache.commons.jexl2.JexlException)3 ObjectContext (org.apache.commons.jexl2.ObjectContext)3 IOException (java.io.IOException)2 ReadonlyContext (org.apache.commons.jexl2.ReadonlyContext)2 JexlScript (org.apache.commons.jexl3.JexlScript)2 NumberUtils (org.apache.commons.lang.math.NumberUtils)2 FloatWritable (org.apache.hadoop.io.FloatWritable)2 IntWritable (org.apache.hadoop.io.IntWritable)2 Text (org.apache.hadoop.io.Text)2 VersionMismatchException (org.apache.hadoop.io.VersionMismatchException)2