com.att.research.xacmlatt.pdp.policy.Condition Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xacml-pdp Show documentation
Show all versions of xacml-pdp Show documentation
ATT reference implementation of XACML PDP engine
/*
*
* Copyright (c) 2013,2019 AT&T Knowledge Ventures
* SPDX-License-Identifier: MIT
*/
package com.att.research.xacmlatt.pdp.policy;
import com.att.research.xacml.api.AttributeValue;
import com.att.research.xacml.api.DataTypeException;
import com.att.research.xacml.api.Status;
import com.att.research.xacml.api.StatusCode;
import com.att.research.xacml.std.StdStatus;
import com.att.research.xacml.std.StdStatusCode;
import com.att.research.xacml.std.datatypes.DataTypes;
import com.att.research.xacmlatt.pdp.eval.EvaluationContext;
import com.att.research.xacmlatt.pdp.eval.EvaluationException;
/**
* Condition extends {@link com.att.research.xacmlatt.pdp.policy.PolicyComponent} to represent the XACML Condition element
* in a XACML Rule.
*
* @author car
* @version $Revision: 1.1 $
*/
public class Condition extends PolicyComponent {
private static final Status STATUS_PE_RETURNED_BAG = new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, "Condition Expression returned a bag");
private static final ExpressionResultBoolean ERB_RETURNED_BAG = new ExpressionResultBoolean(STATUS_PE_RETURNED_BAG);
private static final Status STATUS_PE_RETURNED_NULL = new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, "Null value from Condition Expression");
private static final ExpressionResultBoolean ERB_RETURNED_NULL = new ExpressionResultBoolean(STATUS_PE_RETURNED_NULL);
private static final Status STATUS_PE_RETURNED_NON_BOOLEAN = new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, "Non-boolean value from Condition Expression");
private static final ExpressionResultBoolean ERB_RETURNED_NON_BOOLEAN = new ExpressionResultBoolean(STATUS_PE_RETURNED_NON_BOOLEAN);
private static final Status STATUS_PE_INVALID_BOOLEAN = new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, "Invalid Boolean value");
private static final ExpressionResultBoolean ERB_INVALID_BOOLEAN = new ExpressionResultBoolean(STATUS_PE_INVALID_BOOLEAN);
private Expression expression;
/**
* Creates a Condition
with the given {@link com.att.research.xacml.api.StatusCode} and String
* status message.
*
* @param statusCodeIn the StatusCode
for the Condition
* @param statusMessageIn the String
status message for the Condition
*/
public Condition(StatusCode statusCodeIn, String statusMessageIn) {
super(statusCodeIn, statusMessageIn);
}
/**
* Creates a Condition
with the given StatusCodeStatusCode
for the Condition
*/
public Condition(StatusCode statusCodeIn) {
super(statusCodeIn);
}
/**
* Creates an empty Condition
*/
public Condition() {
}
/**
* Creates a new Condition
with the given {@link com.att.research.xacmlatt.pdp.policy.Expression} and a default
* OK StatusCode
.
*
* @param expressionIn the Expression
for the Condition
*/
public Condition(Expression expressionIn) {
this.expression = expressionIn;
}
public Expression getExpression() {
return this.expression;
}
public void setExpression(Expression expressionIn) {
this.expression = expressionIn;
}
@Override
protected boolean validateComponent() {
if (this.getExpression() == null) {
this.setStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Missing Expression");
return false;
} else {
this.setStatus(StdStatusCode.STATUS_CODE_OK, null);
return true;
}
}
/**
* Evaluates the Expression
in this Condition
in the given {@link com.att.research.xacmlatt.pdp.eval.EvaluationContext}.
* and validates that the result is a boolean.
*
* @param evaluationContext the EvaluationContext
in which to evaluate this Expression
* @param policyDefaults the {@link com.att.research.xacmlatt.pdp.policy.PolicyDefaults} to use in evaluating this Expression
* @return a {@link com.att.research.xacmlatt.pdp.policy.ExpressionResult}
* @throws EvaluationException
*/
public ExpressionResultBoolean evaluate(EvaluationContext evaluationContext, PolicyDefaults policyDefaults) throws EvaluationException {
if (!this.validate()) {
return new ExpressionResultBoolean(new StdStatus(this.getStatusCode(), this.getStatusMessage()));
}
/*
* Evaluate the expression
*/
ExpressionResult expressionResult = this.getExpression().evaluate(evaluationContext, policyDefaults);
assert(expressionResult != null);
if (!expressionResult.isOk()) {
return new ExpressionResultBoolean(expressionResult.getStatus());
}
/*
* Ensure the result is a single element of type boolean
*/
if (expressionResult.isBag()) {
return ERB_RETURNED_BAG;
}
AttributeValue> attributeValueResult = expressionResult.getValue();
if (attributeValueResult == null) {
return ERB_RETURNED_NULL;
} else if (!DataTypes.DT_BOOLEAN.getId().equals(attributeValueResult.getDataTypeId())) {
return ERB_RETURNED_NON_BOOLEAN;
}
/*
* Otherwise it is a valid condition evaluation
*/
Boolean booleanValue = null;
try {
booleanValue = DataTypes.DT_BOOLEAN.convert(attributeValueResult.getValue());
} catch (DataTypeException ex) {
return new ExpressionResultBoolean(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, ex.getMessage()));
}
if (booleanValue == null) {
return ERB_INVALID_BOOLEAN;
} else {
return (booleanValue.booleanValue() ? ExpressionResultBoolean.ERB_TRUE : ExpressionResultBoolean.ERB_FALSE);
}
}
}