Java Code Examples for org.wso2.balana.attr.BooleanAttribute#getInstance()

The following examples show how to use org.wso2.balana.attr.BooleanAttribute#getInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MobiAttributeFinder.java    From mobi with GNU Affero General Public License v3.0 6 votes vote down vote up
private AttributeValue getAttributeValue(Literal literal) {
    IRI datatype = literal.getDatatype();
    switch (datatype.stringValue()) {
        case "http://www.w3.org/2001/XMLSchema#string":
            return new StringAttribute(literal.stringValue());
        case "http://www.w3.org/2001/XMLSchema#boolean":
            return BooleanAttribute.getInstance(literal.booleanValue());
        case "http://www.w3.org/2001/XMLSchema#double":
            return new DoubleAttribute(literal.doubleValue());
        case "http://www.w3.org/2001/XMLSchema#integer":
            return new IntegerAttribute(literal.longValue());
        case "http://www.w3.org/2001/XMLSchema#anyURI":
            try {
                return new AnyURIAttribute(new URI(literal.stringValue()));
            } catch (URISyntaxException e) {
                throw new ProcessingException("Not a valid URI");
            }
        case "https://www.w3.org/2001/XMLSchema#dateTime":
            return new DateTimeAttribute(new Date(literal.dateTimeValue().toInstant().toEpochMilli()));
        default:
            throw new ProcessingException("Datatype " + datatype + " is not supported");
    }
}
 
Example 2
Source File: ConditionBagFunction.java    From balana with Apache License 2.0 6 votes vote down vote up
/**
 * Evaluate the function, using the specified parameters.
 * 
 * @param inputs a <code>List</code> of <code>Evaluatable</code> objects representing the
 *            arguments passed to the function
 * @param context an <code>EvaluationCtx</code> so that the <code>Evaluatable</code> objects can
 *            be evaluated
 * @return an <code>EvaluationResult</code> representing the function's result
 */
public EvaluationResult evaluate(List inputs, EvaluationCtx context) {

    // Evaluate the arguments
    AttributeValue[] argValues = new AttributeValue[inputs.size()];
    EvaluationResult result = evalArgs(inputs, context, argValues);
    if (result != null)
        return result;

    // *-is-in takes a bag and an element of baseType and
    // returns a single boolean value
    AttributeValue item = (AttributeValue) (argValues[0]);
    BagAttribute bag = (BagAttribute) (argValues[1]);

    return new EvaluationResult(BooleanAttribute.getInstance(bag.contains(item)));
}
 
Example 3
Source File: XACML3HigherOrderFunction.java    From balana with Apache License 2.0 5 votes vote down vote up
private EvaluationResult anyAndAllHelper(EvaluationCtx context, Function function, List<AttributeValue> values,
                                         BagAttribute bag, boolean isAllFunction) {

    Iterator it = bag.iterator();
    while (it.hasNext()) {
        AttributeValue bagValue = (AttributeValue) (it.next());
        for (AttributeValue value : values) {
            EvaluationResult result = getEvaluationResult(context, function, value, bagValue, isAllFunction);
            if (result != null) {
                return result;
            }
        }
    }
    return new EvaluationResult(BooleanAttribute.getInstance(isAllFunction));
}
 
Example 4
Source File: HigherOrderFunction.java    From balana with Apache License 2.0 5 votes vote down vote up
/**
 * Private helper for any & all functions
 */
private EvaluationResult anyAndAllHelper(AttributeValue value, BagAttribute bag,
		Function function, EvaluationCtx context, boolean allFunction,
		boolean argumentsAreSwapped) {
	BooleanAttribute attr = BooleanAttribute.getInstance(allFunction);
	Iterator it = bag.iterator();

	while (it.hasNext()) {
		List<Evaluatable> params = new ArrayList<Evaluatable>();

		if (!argumentsAreSwapped) {
			params.add(value);
			params.add((AttributeValue) (it.next()));
		} else {
			params.add((AttributeValue) (it.next()));
			params.add(value);
		}

		EvaluationResult result = function.evaluate(params, context);

		if (result.indeterminate())
			return result;

		BooleanAttribute bool = (BooleanAttribute) (result.getAttributeValue());
		if (bool.getValue() != allFunction) {
			attr = bool;
			break;
		}
	}

	return new EvaluationResult(attr);
}
 
Example 5
Source File: XACML3HigherOrderFunction.java    From balana with Apache License 2.0 4 votes vote down vote up
private EvaluationResult anyOfAny(EvaluationCtx context, Function function, List<AttributeValue> args,
                                  List<BagAttribute> bagArgs) {

    // The expression SHALL be evaluated as if the function named in the <Function> argument was applied
    // between every tuple of the cross product on all bags and the primitive values, and the results were
    // combined using “urn:oasis:names:tc:xacml:1.0:function:or”

    EvaluationResult result = new EvaluationResult(BooleanAttribute.getInstance(false));
    if (!args.isEmpty()) {
        for (int i = 0; i < args.size() - 1; i++) {
            AttributeValue value = args.get(i);
            List<AttributeValue> bagValue = new ArrayList<>();
            bagValue.add(value);
            BagAttribute bagArg = new BagAttribute(value.getType(), bagValue);
            result = anyAndAllHelper(context, function, args.subList(i + 1, args.size()), bagArg, false);
            if (result.indeterminate() || ((BooleanAttribute) (result.getAttributeValue())).getValue()) {
                return result;
            }
        }
        return new EvaluationResult(BooleanAttribute.getInstance(false));
    }
    if (!bagArgs.isEmpty()) {
        for (int i = 0; i < bagArgs.size(); i++) {
            for (int j = i + 1; j < bagArgs.size(); j++) {
                Iterator iIterator = bagArgs.get(i).iterator();
                while (iIterator.hasNext()) {
                    AttributeValue iValue = (AttributeValue) (iIterator.next());
                    Iterator jIterator = bagArgs.get(j).iterator();
                    while (jIterator.hasNext()) {
                        AttributeValue jValue = (AttributeValue) (jIterator.next());
                        result = getEvaluationResult(context, function, jValue, iValue, false);
                        if (result != null && (result.indeterminate() ||
                                ((BooleanAttribute) (result.getAttributeValue())).getValue())) {
                            return result;
                        }
                    }
                }
            }
        }
        return new EvaluationResult(BooleanAttribute.getInstance(false));
    }
    return null;
}
 
Example 6
Source File: ConditionSetFunction.java    From balana with Apache License 2.0 4 votes vote down vote up
/**
 * Evaluates the function, using the specified parameters.
 * 
 * @param inputs a <code>List</code> of <code>Evaluatable</code> objects representing the
 *            arguments passed to the function
 * @param context an <code>EvaluationCtx</code> so that the <code>Evaluatable</code> objects can
 *            be evaluated
 * @return an <code>EvaluationResult</code> representing the function's result
 */
public EvaluationResult evaluate(List inputs, EvaluationCtx context) {

	// Evaluate the arguments
	AttributeValue[] argValues = new AttributeValue[inputs.size()];
	EvaluationResult evalResult = evalArgs(inputs, context, argValues);
	if (evalResult != null)
		return evalResult;

	// setup the two bags we'll be using
	BagAttribute[] bags = new BagAttribute[2];
	bags[0] = (BagAttribute) (argValues[0]);
	bags[1] = (BagAttribute) (argValues[1]);

	AttributeValue result = null;

	switch (getFunctionId()) {
	// *-at-least-one-member-of takes two bags of the same type and
	// returns a boolean
	case ID_BASE_AT_LEAST_ONE_MEMBER_OF:
		// true if at least one element in the first argument is in the
		// second argument (using the *-is-in semantics)

		result = BooleanAttribute.getFalseInstance();
		Iterator it = bags[0].iterator();

		while (it.hasNext()) {
			if (bags[1].contains((AttributeValue) (it.next()))) {
				result = BooleanAttribute.getTrueInstance();
				break;
			}
		}

		break;

	// *-set-equals takes two bags of the same type and returns
	// a boolean
	case ID_BASE_SUBSET:
		// returns true if the first argument is a subset of the second
		// argument (ie, all the elements in the first bag appear in
		// the second bag) ... ignore all duplicate values in both
		// input bags

		boolean subset = bags[1].containsAll(bags[0]);
		result = BooleanAttribute.getInstance(subset);

		break;

	// *-set-equals takes two bags of the same type and returns
	// a boolean
	case ID_BASE_SET_EQUALS:

		// returns true if the two inputs contain the same elements
		// discounting any duplicates in either input ... this is the same
		// as applying the and function on the subset function with
		// the two inputs, and then the two inputs reversed (ie, are the
		// two inputs subsets of each other)

		boolean equals = (bags[1].containsAll(bags[0]) && bags[0].containsAll(bags[1]));
		result = BooleanAttribute.getInstance(equals);

		break;
	}

	return new EvaluationResult(result);
}
 
Example 7
Source File: BooleanAttributeProxy.java    From balana with Apache License 2.0 4 votes vote down vote up
public AttributeValue getInstance(Node root) throws Exception {
    return BooleanAttribute.getInstance(root);
}
 
Example 8
Source File: BooleanAttributeProxy.java    From balana with Apache License 2.0 4 votes vote down vote up
public AttributeValue getInstance(String value) throws Exception {
    return BooleanAttribute.getInstance(value);
}