@Test public void testAnyuriContains() { FunctionArgumentAttributeValue attrV1 = null; FunctionArgumentAttributeValue attrBigger = null; FunctionArgumentAttributeValue attrBiggerNoMatch = null; FunctionArgumentAttributeValue attrCaps = null; FunctionArgumentAttributeValue attrBigString = null; FunctionArgumentAttributeValue attrNull = null; FunctionArgumentAttributeValue attrBlankString = null; FunctionArgumentAttributeValue attrBlankURI = null; FunctionArgumentAttributeValue attrInteger = null; try { String v1 = new String("abc"); URI bigger = new URI("abc.some.stringabc"); URI biggerNoMatch = new URI("Zabc.some.stringabcZ"); String caps = new String("AbC"); String bigString = "thisIsSomeReallyBigStringToMatch"; attrV1 = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(v1)); attrBigger = new FunctionArgumentAttributeValue(DataTypes.DT_ANYURI.createAttributeValue(bigger)); attrBiggerNoMatch = new FunctionArgumentAttributeValue(DataTypes.DT_ANYURI.createAttributeValue(biggerNoMatch)); attrCaps = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(caps)); attrBigString = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(bigString)); attrBlankString = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("")); attrBlankURI = new FunctionArgumentAttributeValue(DataTypes.DT_ANYURI.createAttributeValue("")); attrNull = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(null)); attrInteger = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1234)); } catch (Exception e) { fail("creating attributes e="+ e); } FunctionDefinitionStringFunctions<?,?> fd = (FunctionDefinitionStringFunctions<?,?>) StdFunctions.FD_ANYURI_CONTAINS; assertEquals(XACML3.ID_FUNCTION_ANYURI_CONTAINS, fd.getId()); assertEquals(DataTypes.DT_BOOLEAN.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); arguments.clear(); arguments.add(attrV1); arguments.add(attrBigger); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); Boolean resValue = (Boolean)res.getValue().getValue(); assertEquals(true, resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrBiggerNoMatch); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(false, resValue); arguments.clear(); arguments.add(attrCaps); arguments.add(attrBigger); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(false, resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrBigger); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(false, resValue); arguments.clear(); arguments.add(attrBlankString); arguments.add(attrBigger); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(true, resValue); arguments.clear(); arguments.add(attrV1); arguments.add(attrBlankURI); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(false, resValue); arguments.clear(); arguments.add(attrBlankString); arguments.add(attrBlankURI); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.Boolean.class, res.getValue().getValue().getClass()); resValue = (Boolean)res.getValue().getValue(); assertEquals(true, resValue); arguments.clear(); arguments.add(attrBigger); arguments.add(attrV1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:anyURI-contains Expected data type 'string' saw 'anyURI'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrNull); arguments.add(attrBlankURI); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:anyURI-contains Got null attribute", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrV1); arguments.add(attrNull); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:anyURI-contains Got null attribute", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrInteger); arguments.add(attrBigger); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:anyURI-contains Expected data type 'string' saw 'integer'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); }
@SuppressWarnings("incomplete-switch") @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || (operation == OPERATION.CONCATENATE && arguments.size() < 2) || (operation == OPERATION.SUBSTRING && arguments.size() != 3) || (operation != OPERATION.SUBSTRING && operation != OPERATION.CONCATENATE && arguments.size() != 2) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected " + ((operation == OPERATION.SUBSTRING) ? 3 : (operation == OPERATION.CONCATENATE ? "2 or more " : 2)) + " arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } ExpressionResult expressionResult = null; String firstArgumentAsString = null; String secondArgumentAsString = null; Integer secondArgumentAsInteger = null; Integer thirdArgumentAsInteger = null; if (operation == OPERATION.CONCATENATE) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < arguments.size(); i++) { FunctionArgument functionArgument = arguments.get(i); ConvertedArgument<I> convertedArgument = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } try { String argumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument.getValue()); builder.append(argumentAsString); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, builder.toString() ); expressionResult = ExpressionResult.newSingle(stringResult); return expressionResult; } else if (operation == OPERATION.SUBSTRING) { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<I> convertedArgument0 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } try { firstArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument0.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } functionArgument = arguments.get(1); ConvertedArgument<BigInteger> convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } secondArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (secondArgumentAsInteger < 0 || secondArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "Start point '" + secondArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } functionArgument = arguments.get(2); convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } thirdArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (thirdArgumentAsInteger < -1 || thirdArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } if (thirdArgumentAsInteger != -1 && thirdArgumentAsInteger < secondArgumentAsInteger) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' less than start point '" + secondArgumentAsString + "'" + " for string='" + firstArgumentAsString + "'")); } } else { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<String> convertedArgument0 = new ConvertedArgument<String>(functionArgument, DataTypes.DT_STRING, false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } firstArgumentAsString = convertedArgument0.getValue(); functionArgument = arguments.get(1); ConvertedArgument<I> convertedArgument1 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument1.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument1.getStatus())); } try { secondArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument1.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " " + message )); } } switch (operation) { case STARTS_WITH: if (secondArgumentAsString.startsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case ENDS_WITH: if (secondArgumentAsString.endsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case CONTAINS: if (secondArgumentAsString.contains(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case SUBSTRING: String substring = null; if (thirdArgumentAsInteger == -1) { substring = firstArgumentAsString.substring(secondArgumentAsInteger); } else { substring = firstArgumentAsString.substring(secondArgumentAsInteger, thirdArgumentAsInteger); } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, substring); expressionResult = ExpressionResult.newSingle(stringResult); break; } return expressionResult; }
FunctionDefinitionStringFunctions extends FunctionDefinitionBase<O, I> { @SuppressWarnings("incomplete-switch") @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || (operation == OPERATION.CONCATENATE && arguments.size() < 2) || (operation == OPERATION.SUBSTRING && arguments.size() != 3) || (operation != OPERATION.SUBSTRING && operation != OPERATION.CONCATENATE && arguments.size() != 2) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected " + ((operation == OPERATION.SUBSTRING) ? 3 : (operation == OPERATION.CONCATENATE ? "2 or more " : 2)) + " arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } ExpressionResult expressionResult = null; String firstArgumentAsString = null; String secondArgumentAsString = null; Integer secondArgumentAsInteger = null; Integer thirdArgumentAsInteger = null; if (operation == OPERATION.CONCATENATE) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < arguments.size(); i++) { FunctionArgument functionArgument = arguments.get(i); ConvertedArgument<I> convertedArgument = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } try { String argumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument.getValue()); builder.append(argumentAsString); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, builder.toString() ); expressionResult = ExpressionResult.newSingle(stringResult); return expressionResult; } else if (operation == OPERATION.SUBSTRING) { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<I> convertedArgument0 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } try { firstArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument0.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } functionArgument = arguments.get(1); ConvertedArgument<BigInteger> convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } secondArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (secondArgumentAsInteger < 0 || secondArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "Start point '" + secondArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } functionArgument = arguments.get(2); convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } thirdArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (thirdArgumentAsInteger < -1 || thirdArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } if (thirdArgumentAsInteger != -1 && thirdArgumentAsInteger < secondArgumentAsInteger) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' less than start point '" + secondArgumentAsString + "'" + " for string='" + firstArgumentAsString + "'")); } } else { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<String> convertedArgument0 = new ConvertedArgument<String>(functionArgument, DataTypes.DT_STRING, false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } firstArgumentAsString = convertedArgument0.getValue(); functionArgument = arguments.get(1); ConvertedArgument<I> convertedArgument1 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument1.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument1.getStatus())); } try { secondArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument1.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " " + message )); } } switch (operation) { case STARTS_WITH: if (secondArgumentAsString.startsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case ENDS_WITH: if (secondArgumentAsString.endsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case CONTAINS: if (secondArgumentAsString.contains(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case SUBSTRING: String substring = null; if (thirdArgumentAsInteger == -1) { substring = firstArgumentAsString.substring(secondArgumentAsInteger); } else { substring = firstArgumentAsString.substring(secondArgumentAsInteger, thirdArgumentAsInteger); } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, substring); expressionResult = ExpressionResult.newSingle(stringResult); break; } return expressionResult; } }
FunctionDefinitionStringFunctions extends FunctionDefinitionBase<O, I> { @SuppressWarnings("incomplete-switch") @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || (operation == OPERATION.CONCATENATE && arguments.size() < 2) || (operation == OPERATION.SUBSTRING && arguments.size() != 3) || (operation != OPERATION.SUBSTRING && operation != OPERATION.CONCATENATE && arguments.size() != 2) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected " + ((operation == OPERATION.SUBSTRING) ? 3 : (operation == OPERATION.CONCATENATE ? "2 or more " : 2)) + " arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } ExpressionResult expressionResult = null; String firstArgumentAsString = null; String secondArgumentAsString = null; Integer secondArgumentAsInteger = null; Integer thirdArgumentAsInteger = null; if (operation == OPERATION.CONCATENATE) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < arguments.size(); i++) { FunctionArgument functionArgument = arguments.get(i); ConvertedArgument<I> convertedArgument = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } try { String argumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument.getValue()); builder.append(argumentAsString); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, builder.toString() ); expressionResult = ExpressionResult.newSingle(stringResult); return expressionResult; } else if (operation == OPERATION.SUBSTRING) { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<I> convertedArgument0 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } try { firstArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument0.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } functionArgument = arguments.get(1); ConvertedArgument<BigInteger> convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } secondArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (secondArgumentAsInteger < 0 || secondArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "Start point '" + secondArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } functionArgument = arguments.get(2); convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } thirdArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (thirdArgumentAsInteger < -1 || thirdArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } if (thirdArgumentAsInteger != -1 && thirdArgumentAsInteger < secondArgumentAsInteger) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' less than start point '" + secondArgumentAsString + "'" + " for string='" + firstArgumentAsString + "'")); } } else { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<String> convertedArgument0 = new ConvertedArgument<String>(functionArgument, DataTypes.DT_STRING, false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } firstArgumentAsString = convertedArgument0.getValue(); functionArgument = arguments.get(1); ConvertedArgument<I> convertedArgument1 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument1.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument1.getStatus())); } try { secondArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument1.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " " + message )); } } switch (operation) { case STARTS_WITH: if (secondArgumentAsString.startsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case ENDS_WITH: if (secondArgumentAsString.endsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case CONTAINS: if (secondArgumentAsString.contains(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case SUBSTRING: String substring = null; if (thirdArgumentAsInteger == -1) { substring = firstArgumentAsString.substring(secondArgumentAsInteger); } else { substring = firstArgumentAsString.substring(secondArgumentAsInteger, thirdArgumentAsInteger); } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, substring); expressionResult = ExpressionResult.newSingle(stringResult); break; } return expressionResult; } FunctionDefinitionStringFunctions(Identifier idIn, DataType<O> dataTypeIn, DataType<I> dataTypeArgsIn, OPERATION op); }
FunctionDefinitionStringFunctions extends FunctionDefinitionBase<O, I> { @SuppressWarnings("incomplete-switch") @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || (operation == OPERATION.CONCATENATE && arguments.size() < 2) || (operation == OPERATION.SUBSTRING && arguments.size() != 3) || (operation != OPERATION.SUBSTRING && operation != OPERATION.CONCATENATE && arguments.size() != 2) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected " + ((operation == OPERATION.SUBSTRING) ? 3 : (operation == OPERATION.CONCATENATE ? "2 or more " : 2)) + " arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } ExpressionResult expressionResult = null; String firstArgumentAsString = null; String secondArgumentAsString = null; Integer secondArgumentAsInteger = null; Integer thirdArgumentAsInteger = null; if (operation == OPERATION.CONCATENATE) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < arguments.size(); i++) { FunctionArgument functionArgument = arguments.get(i); ConvertedArgument<I> convertedArgument = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } try { String argumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument.getValue()); builder.append(argumentAsString); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, builder.toString() ); expressionResult = ExpressionResult.newSingle(stringResult); return expressionResult; } else if (operation == OPERATION.SUBSTRING) { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<I> convertedArgument0 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } try { firstArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument0.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } functionArgument = arguments.get(1); ConvertedArgument<BigInteger> convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } secondArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (secondArgumentAsInteger < 0 || secondArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "Start point '" + secondArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } functionArgument = arguments.get(2); convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } thirdArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (thirdArgumentAsInteger < -1 || thirdArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } if (thirdArgumentAsInteger != -1 && thirdArgumentAsInteger < secondArgumentAsInteger) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' less than start point '" + secondArgumentAsString + "'" + " for string='" + firstArgumentAsString + "'")); } } else { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<String> convertedArgument0 = new ConvertedArgument<String>(functionArgument, DataTypes.DT_STRING, false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } firstArgumentAsString = convertedArgument0.getValue(); functionArgument = arguments.get(1); ConvertedArgument<I> convertedArgument1 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument1.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument1.getStatus())); } try { secondArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument1.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " " + message )); } } switch (operation) { case STARTS_WITH: if (secondArgumentAsString.startsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case ENDS_WITH: if (secondArgumentAsString.endsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case CONTAINS: if (secondArgumentAsString.contains(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case SUBSTRING: String substring = null; if (thirdArgumentAsInteger == -1) { substring = firstArgumentAsString.substring(secondArgumentAsInteger); } else { substring = firstArgumentAsString.substring(secondArgumentAsInteger, thirdArgumentAsInteger); } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, substring); expressionResult = ExpressionResult.newSingle(stringResult); break; } return expressionResult; } FunctionDefinitionStringFunctions(Identifier idIn, DataType<O> dataTypeIn, DataType<I> dataTypeArgsIn, OPERATION op); @SuppressWarnings("incomplete-switch") @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments); }
FunctionDefinitionStringFunctions extends FunctionDefinitionBase<O, I> { @SuppressWarnings("incomplete-switch") @Override public ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments) { if (arguments == null || (operation == OPERATION.CONCATENATE && arguments.size() < 2) || (operation == OPERATION.SUBSTRING && arguments.size() != 3) || (operation != OPERATION.SUBSTRING && operation != OPERATION.CONCATENATE && arguments.size() != 2) ) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " Expected " + ((operation == OPERATION.SUBSTRING) ? 3 : (operation == OPERATION.CONCATENATE ? "2 or more " : 2)) + " arguments, got " + ((arguments == null) ? "null" : arguments.size()) )); } ExpressionResult expressionResult = null; String firstArgumentAsString = null; String secondArgumentAsString = null; Integer secondArgumentAsInteger = null; Integer thirdArgumentAsInteger = null; if (operation == OPERATION.CONCATENATE) { StringBuilder builder = new StringBuilder(); for (int i = 0; i < arguments.size(); i++) { FunctionArgument functionArgument = arguments.get(i); ConvertedArgument<I> convertedArgument = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument.getStatus())); } try { String argumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument.getValue()); builder.append(argumentAsString); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, builder.toString() ); expressionResult = ExpressionResult.newSingle(stringResult); return expressionResult; } else if (operation == OPERATION.SUBSTRING) { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<I> convertedArgument0 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } try { firstArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument0.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message )); } functionArgument = arguments.get(1); ConvertedArgument<BigInteger> convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } secondArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (secondArgumentAsInteger < 0 || secondArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "Start point '" + secondArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } functionArgument = arguments.get(2); convertedArgumentInt = new ConvertedArgument<BigInteger>(functionArgument, DataTypes.DT_INTEGER, false); if ( ! convertedArgumentInt.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgumentInt.getStatus())); } thirdArgumentAsInteger = convertedArgumentInt.getValue().intValue(); if (thirdArgumentAsInteger < -1 || thirdArgumentAsInteger > firstArgumentAsString.length()) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' out of range 0-" + firstArgumentAsString.length() + " for string='" + firstArgumentAsString + "'")); } if (thirdArgumentAsInteger != -1 && thirdArgumentAsInteger < secondArgumentAsInteger) { return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + "End point '" + thirdArgumentAsInteger + "' less than start point '" + secondArgumentAsString + "'" + " for string='" + firstArgumentAsString + "'")); } } else { FunctionArgument functionArgument = arguments.get(0); ConvertedArgument<String> convertedArgument0 = new ConvertedArgument<String>(functionArgument, DataTypes.DT_STRING, false); if ( ! convertedArgument0.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument0.getStatus())); } firstArgumentAsString = convertedArgument0.getValue(); functionArgument = arguments.get(1); ConvertedArgument<I> convertedArgument1 = new ConvertedArgument<I>(functionArgument, this.getDataTypeArgs(), false); if ( ! convertedArgument1.isOk()) { return ExpressionResult.newError(getFunctionStatus(convertedArgument1.getStatus())); } try { secondArgumentAsString = this.getDataTypeArgs().toStringValue( convertedArgument1.getValue()); } catch (DataTypeException e) { String message = e.getMessage(); if (e.getCause() != null) { message = e.getCause().getMessage(); } return ExpressionResult.newError(new StdStatus(StdStatusCode.STATUS_CODE_PROCESSING_ERROR, this.getShortFunctionId() + " " + message + " " + message )); } } switch (operation) { case STARTS_WITH: if (secondArgumentAsString.startsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case ENDS_WITH: if (secondArgumentAsString.endsWith(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case CONTAINS: if (secondArgumentAsString.contains(firstArgumentAsString)) { return ER_TRUE; } else { return ER_FALSE; } case SUBSTRING: String substring = null; if (thirdArgumentAsInteger == -1) { substring = firstArgumentAsString.substring(secondArgumentAsInteger); } else { substring = firstArgumentAsString.substring(secondArgumentAsInteger, thirdArgumentAsInteger); } AttributeValue<String> stringResult = new StdAttributeValue<String>(XACML.ID_DATATYPE_STRING, substring); expressionResult = ExpressionResult.newSingle(stringResult); break; } return expressionResult; } FunctionDefinitionStringFunctions(Identifier idIn, DataType<O> dataTypeIn, DataType<I> dataTypeArgsIn, OPERATION op); @SuppressWarnings("incomplete-switch") @Override ExpressionResult evaluate(EvaluationContext evaluationContext, List<FunctionArgument> arguments);  }