Java Code Examples for org.apache.nifi.components.ValidationContext#isExpressionLanguagePresent()

The following examples show how to use org.apache.nifi.components.ValidationContext#isExpressionLanguagePresent() . 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: StandardValidators.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    final ValidationResult.Builder builder = new ValidationResult.Builder();
    builder.subject(subject).input(input);
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return builder.valid(true).explanation("Contains Expression Language").build();
    }

    try {
        FlowFile.KeyValidator.validateKey(input);
        builder.valid(true);
    } catch (final IllegalArgumentException e) {
        builder.valid(false).explanation(e.getMessage());
    }

    return builder.build();
}
 
Example 2
Source File: UpdateAttribute.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        final AttributeExpression.ResultType resultType = context.newExpressionLanguageCompiler().getResultType(input);
        if (!resultType.equals(AttributeExpression.ResultType.STRING)) {
            return new ValidationResult.Builder()
                    .subject(subject)
                    .input(input)
                    .valid(false)
                    .explanation("Expected property to to return type " + AttributeExpression.ResultType.STRING +
                            " but expression returns type " + resultType)
                    .build();
        }
        return new ValidationResult.Builder()
                .subject(subject)
                .input(input)
                .valid(true)
                .explanation("Property returns type " + AttributeExpression.ResultType.STRING)
                .build();
    }

    return DPV_RE_VALIDATOR.validate(subject, input, context);
}
 
Example 3
Source File: StandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    final ValidationResult.Builder builder = new ValidationResult.Builder();
    builder.subject("Property Name").input(subject);
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return builder.valid(true).explanation("Contains Expression Language").build();
    }

    try {
        FlowFile.KeyValidator.validateKey(subject);
        builder.valid(true);
    } catch (final IllegalArgumentException e) {
        builder.valid(false).explanation(e.getMessage());
    }

    return builder.build();
}
 
Example 4
Source File: StandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static Validator createDataSizeBoundsValidator(final long minBytesInclusive, final long maxBytesInclusive) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
            }

            final ValidationResult vr = DATA_SIZE_VALIDATOR.validate(subject, input, context);
            if (!vr.isValid()) {
                return vr;
            }
            final long dataSizeBytes = DataUnit.parseDataSize(input, DataUnit.B).longValue();
            if (dataSizeBytes < minBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be smaller than " + minBytesInclusive + " bytes").build();
            }
            if (dataSizeBytes > maxBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be larger than " + maxBytesInclusive + " bytes").build();
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };

}
 
Example 5
Source File: StandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static Validator createLongValidator(final long minimum, final long maximum, final boolean inclusive) {
    return new Validator() {
        @Override
        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
            }

            String reason = null;
            try {
                final long longVal = Long.parseLong(input);
                if (longVal < minimum || (!inclusive && longVal == minimum) | longVal > maximum || (!inclusive && longVal == maximum)) {
                    reason = "Value must be between " + minimum + " and " + maximum + " (" + (inclusive ? "inclusive" : "exclusive") + ")";
                }
            } catch (final NumberFormatException e) {
                reason = "not a valid integer";
            }

            return new ValidationResult.Builder().subject(subject).input(input).explanation(reason).valid(reason == null).build();
        }

    };
}
 
Example 6
Source File: AbstractKiteProcessor.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String uri, ValidationContext context) {
    Configuration conf = getConfiguration(context.getProperty(CONF_XML_FILES).evaluateAttributeExpressions().getValue());
    String error = null;

    if(StringUtils.isBlank(uri)) {
        return new ValidationResult.Builder().subject(subject).input(uri).explanation("Schema cannot be null.").valid(false).build();
    }

    final boolean elPresent = context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(uri);
    if (!elPresent) {
        try {
            getSchema(uri, conf);
        } catch (SchemaNotFoundException e) {
            error = e.getMessage();
        }
    }
    return new ValidationResult.Builder()
            .subject(subject)
            .input(uri)
            .explanation(error)
            .valid(error == null)
            .build();
}
 
Example 7
Source File: JsonValidator.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
    ObjectMapper mapper = new ObjectMapper();
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
    }

    try {
        Class clz = input.startsWith("[") ? List.class : Map.class;
        mapper.readValue(input, clz);
    } catch (Exception e) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(false)
                .explanation(subject + " is not a valid JSON representation due to " + e.getLocalizedMessage())
                .build();
    }

    return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
}
 
Example 8
Source File: PutCloudWatchMetric.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return (new ValidationResult.Builder()).subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
    } else {
        String reason = null;

        try {
            Double.parseDouble(input);
        } catch (NumberFormatException e) {
            reason = "not a valid Double";
        }

        return (new ValidationResult.Builder()).subject(subject).input(input).explanation(reason).valid(reason == null).build();
    }
}
 
Example 9
Source File: YandexTranslate.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    if ((StringUtils.isBlank(input))) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).explanation("No Language Input Present").build();
    }

    if (context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).explanation("Expression Language Present").build();
    }

    if (Languages.getLanguageMap().keySet().contains(input.toLowerCase())) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
    }

    return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(input + " is not a language that is supported by Yandex").build();
}
 
Example 10
Source File: StandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
    }

    if (input == null) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Time Period cannot be null").build();
    }
    final String lowerCase = input.toLowerCase();
    final boolean validSyntax = pattern.matcher(lowerCase).matches();
    final ValidationResult.Builder builder = new ValidationResult.Builder();
    if (validSyntax) {
        final long nanos = FormatUtils.getTimeDuration(lowerCase, TimeUnit.NANOSECONDS);

        if (nanos < minNanos || nanos > maxNanos) {
            builder.subject(subject).input(input).valid(false)
                    .explanation("Must be in the range of " + minValueEnglish + " to " + maxValueEnglish);
        } else {
            builder.subject(subject).input(input).valid(true);
        }
    } else {
        builder.subject(subject).input(input).valid(false)
                .explanation("Must be of format <duration> <TimeUnit> where <duration> is a non-negative "
                        + "integer and TimeUnit is a supported Time Unit, such as: nanos, millis, secs, mins, hrs, days");
    }
    return builder.build();
}
 
Example 11
Source File: StandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
    }

    String reason = null;
    try {
        Integer.parseInt(value);
    } catch (final NumberFormatException e) {
        reason = "not a valid integer";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
Example 12
Source File: StandardValidators.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
    }

    String reason = null;
    try {
        Long.parseLong(value);
    } catch (final NumberFormatException e) {
        reason = "not a valid Long";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
Example 13
Source File: StandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
    }
    return StandardValidators.NON_EMPTY_VALIDATOR.validate(subject, input, context);
}
 
Example 14
Source File: StandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        final ResultType resultType = context.newExpressionLanguageCompiler().getResultType(value);
        if (!resultType.equals(ResultType.STRING)) {
            return new ValidationResult.Builder()
                    .subject(subject)
                    .input(value)
                    .valid(false)
                    .explanation("Expected Attribute Query to return type " + ResultType.STRING + " but query returns type " + resultType)
                    .build();
        }

        return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
    }

    String reason = null;
    try {
        if (!Charset.isSupported(value)) {
            reason = "Character Set is not supported by this JVM.";
        }
    } catch (final UnsupportedCharsetException uce) {
        reason = "Character Set is not supported by this JVM.";
    } catch (final IllegalArgumentException iae) {
        reason = "Character Set value cannot be null.";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
Example 15
Source File: StandardValidators.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(value)) {
        return new ValidationResult.Builder().subject(subject).input(value).explanation("Expression Language Present").valid(true).build();
    }

    String reason = null;
    try {
        Integer.parseInt(value);
    } catch (final NumberFormatException e) {
        reason = "not a valid integer";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
Example 16
Source File: QueryRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    if (context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder()
            .input(input)
            .subject(subject)
            .valid(true)
            .explanation("Expression Language Present")
            .build();
    }

    final String substituted = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();

    final Config config = SqlParser.configBuilder()
        .setLex(Lex.MYSQL_ANSI)
        .build();

    final SqlParser parser = SqlParser.create(substituted, config);
    try {
        parser.parseStmt();
        return new ValidationResult.Builder()
            .subject(subject)
            .input(input)
            .valid(true)
            .build();
    } catch (final Exception e) {
        return new ValidationResult.Builder()
            .subject(subject)
            .input(input)
            .valid(false)
            .explanation("Not a valid SQL Statement: " + e.getMessage())
            .build();
    }
}
 
Example 17
Source File: YandexTranslate.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
    if (context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).explanation("Expression Language Present").build();
    }

    if (Languages.getLanguageMap().keySet().contains(input.toLowerCase())) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
    }

    return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(input + " is not a language that is supported by Yandex").build();
}
 
Example 18
Source File: AbstractDatabaseFetchProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    // For backwards-compatibility, keep track of whether the table name and max-value column properties are dynamic (i.e. has expression language)
    isDynamicTableName = validationContext.isExpressionLanguagePresent(validationContext.getProperty(TABLE_NAME).getValue());
    isDynamicMaxValues = validationContext.isExpressionLanguagePresent(validationContext.getProperty(MAX_VALUE_COLUMN_NAMES).getValue());

    return super.customValidate(validationContext);
}
 
Example 19
Source File: ListenerProperties.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public ValidationResult validate(String subject, String input, ValidationContext context) {
    ValidationResult result = new ValidationResult.Builder()
            .subject("Local Network Interface").valid(true).input(input).build();
    if (interfaceSet.contains(input.toLowerCase())) {
        return result;
    }

    String message;
    String realValue = input;
    try {
        if (context.isExpressionLanguagePresent(input)) {
            AttributeExpression ae = context.newExpressionLanguageCompiler().compile(input);
            realValue = ae.evaluate();
        }

        if (interfaceSet.contains(realValue.toLowerCase())) {
            return result;
        }

        message = realValue + " is not a valid network name. Valid names are " + interfaceSet.toString();

    } catch (IllegalArgumentException e) {
        message = "Not a valid AttributeExpression: " + e.getMessage();
    }
    result = new ValidationResult.Builder().subject("Local Network Interface")
            .valid(false).input(input).explanation(message).build();

    return result;
}
 
Example 20
Source File: ConvertAvroSchema.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
public ValidationResult validate(String subject, String uri,
        ValidationContext context) {
    Configuration conf = getConfiguration(context.getProperty(
            CONF_XML_FILES).getValue());
    String inputUri = context.getProperty(INPUT_SCHEMA).getValue();
    String error = null;

    final boolean elPresent = context
            .isExpressionLanguageSupported(subject)
            && context.isExpressionLanguagePresent(uri);
    if (!elPresent) {
        try {
            Schema outputSchema = getSchema(uri, conf);
            Schema inputSchema = getSchema(inputUri, conf);
            // Get the explicitly mapped fields. This is identical to
            // logic in onTrigger, but ValidationContext and
            // ProcessContext share no ancestor, so we cannot generalize
            // the code.
            Map<String, String> fieldMapping = new HashMap<>();
            for (final Map.Entry<PropertyDescriptor, String> entry : context
                    .getProperties().entrySet()) {
                if (entry.getKey().isDynamic()) {
                    fieldMapping.put(entry.getKey().getName(),
                            entry.getValue());
                }
            }
            AvroRecordConverter converter = new AvroRecordConverter(
                    inputSchema, outputSchema, fieldMapping);
            Collection<String> unmappedFields = converter
                    .getUnmappedFields();
            if (unmappedFields.size() > 0) {
                error = "The following fields are unmapped: "
                        + unmappedFields;
            }

        } catch (SchemaNotFoundException e) {
            error = e.getMessage();
        }
    }
    return new ValidationResult.Builder().subject(subject).input(uri)
            .explanation(error).valid(error == null).build();
}