@Test public void testStringSubstring() { String bigString = new String("abc some string abc"); FunctionArgumentAttributeValue attrBigString = null; FunctionArgumentAttributeValue attrNull = null; FunctionArgumentAttributeValue attrBlank = null; FunctionArgumentAttributeValue attrDouble = null; FunctionArgumentAttributeValue attrInteger0 = null; FunctionArgumentAttributeValue attrInteger1 = null; FunctionArgumentAttributeValue attrIntegerM1 = null; FunctionArgumentAttributeValue attrInteger8 = null; FunctionArgumentAttributeValue attrInteger19 = null; FunctionArgumentAttributeValue attrInteger20 = null; try { attrBigString = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(bigString)); attrBlank = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue("")); attrNull = new FunctionArgumentAttributeValue(DataTypes.DT_STRING.createAttributeValue(null)); attrInteger0 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(0)); attrInteger1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(1)); attrIntegerM1 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(-1)); attrInteger8 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(8)); attrInteger19 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(19)); attrInteger20 = new FunctionArgumentAttributeValue(DataTypes.DT_INTEGER.createAttributeValue(20)); attrDouble = new FunctionArgumentAttributeValue(DataTypes.DT_DOUBLE.createAttributeValue(123.4)); } catch (Exception e) { fail("creating attributes e="+ e); } FunctionDefinitionStringFunctions<?,?> fd = (FunctionDefinitionStringFunctions<?,?>) StdFunctions.FD_STRING_SUBSTRING; assertEquals(XACML3.ID_FUNCTION_STRING_SUBSTRING, fd.getId()); assertEquals(DataTypes.DT_STRING.getId(), fd.getDataTypeId()); assertFalse(fd.returnsBag()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger1); arguments.add(attrInteger8); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); String resValue = (String)res.getValue().getValue(); assertEquals("bc some", resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger0); arguments.add(attrInteger8); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); resValue = (String)res.getValue().getValue(); assertEquals("abc some", resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrInteger19); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); resValue = (String)res.getValue().getValue(); assertEquals(" string abc", resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrIntegerM1); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); resValue = (String)res.getValue().getValue(); assertEquals(" string abc", resValue); arguments.clear(); arguments.add(attrBigString); arguments.add(attrIntegerM1); arguments.add(attrInteger8); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Start point '-1' out of range 0-19 for string='abc some string abc'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrInteger20); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring End point '20' out of range 0-19 for string='abc some string abc'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring End point '1' less than start point 'null' for string='abc some string abc'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrInteger8); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); resValue = (String)res.getValue().getValue(); assertEquals("", resValue); arguments.clear(); arguments.add(attrBlank); arguments.add(attrInteger0); arguments.add(attrInteger0); res = fd.evaluate(null, arguments); assertTrue(res.isOk()); assertEquals(java.lang.String.class, res.getValue().getValue().getClass()); resValue = (String)res.getValue().getValue(); assertEquals("", resValue); arguments.clear(); arguments.add(attrDouble); arguments.add(attrInteger8); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Expected data type 'string' saw 'double'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrDouble); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Expected data type 'integer' saw 'double'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrDouble); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Expected data type 'integer' saw 'double'", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); arguments.add(attrInteger1); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Expected 3 arguments, got 4", res.getStatus().getStatusMessage()); assertEquals("urn:oasis:names:tc:xacml:1.0:status:processing-error", res.getStatus().getStatusCode().getStatusCodeValue().stringValue()); arguments.clear(); arguments.add(attrBigString); arguments.add(attrInteger8); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Expected 3 arguments, got 2", 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(attrInteger8); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring 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(attrBigString); arguments.add(attrNull); arguments.add(attrInteger1); res = fd.evaluate(null, arguments); assertFalse(res.isOk()); assertEquals("function:string-substring Got null attribute", 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);  }