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

The following examples show how to use org.apache.nifi.components.ValidationContext#isExpressionLanguageSupported() . 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: ValidateCsv.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext context) {

    PropertyValue schemaProp = context.getProperty(SCHEMA);
    String schema = schemaProp.getValue();
    String subject = SCHEMA.getName();

    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(schema)) {
        return Collections.singletonList(new ValidationResult.Builder().subject(subject).input(schema).explanation("Expression Language Present").valid(true).build());
    }
    // If no Expression Language is present, try parsing the schema
    try {
        this.parseSchema(schema);
    } catch (Exception e) {
        final List<ValidationResult> problems = new ArrayList<>(1);
        problems.add(new ValidationResult.Builder().subject(subject)
                .input(schema)
                .valid(false)
                .explanation("Error while parsing the schema: " + e.getMessage())
                .build());
        return problems;
    }
    return super.customValidate(context);
}
 
Example 2
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 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 {
        final int intVal = Integer.parseInt(value);

        if (intVal < 0) {
            reason = "value is negative";
        }
    } catch (final NumberFormatException e) {
        reason = "value is not a valid integer";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
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) {
    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("Data Size cannot be null")
                .build();
    }
    if (DATA_SIZE_PATTERN.matcher(input.toUpperCase()).matches()) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
    } else {
        return new ValidationResult.Builder()
                .subject(subject).input(input)
                .valid(false)
                .explanation("Must be of format <Data Size> <Data Unit> where <Data Size>"
                        + " is a non-negative integer and <Data Unit> is a supported Data"
                        + " Unit, such as: B, KB, MB, GB, TB")
                .build();
    }
}
 
Example 4
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) {
    String evaluatedInput = input;
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        try {
            PropertyValue propertyValue = context.newPropertyValue(input);
            evaluatedInput = (propertyValue == null) ? input : propertyValue.evaluateAttributeExpressions().getValue();
        } catch (final Exception e) {
            return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid expression").valid(false).build();
        }
    }

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

    return new ValidationResult.Builder().subject(subject).input(evaluatedInput).explanation(reason).valid(reason == null).build();
}
 
Example 5
Source File: StandardValidators.java    From localization_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 6
Source File: StandardValidators.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private static Validator createURLValidator() {
    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();
            }

            try {
                final String evaluatedInput = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();
                new URL(evaluatedInput);
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid URL").valid(true).build();
            } catch (final Exception e) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid URL").valid(false).build();
            }
        }
    };
}
 
Example 7
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) {
    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("Data Size cannot be null")
                .build();
    }
    if (Pattern.compile(DataUnit.DATA_SIZE_REGEX).matcher(input.toUpperCase()).matches()) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
    } else {
        return new ValidationResult.Builder()
                .subject(subject).input(input)
                .valid(false)
                .explanation("Must be of format <Data Size> <Data Unit> where <Data Size>"
                        + " is a non-negative integer and <Data Unit> is a supported Data"
                        + " Unit, such as: B, KB, MB, GB, TB")
                .build();
    }
}
 
Example 8
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) {
    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();
    }
    if (Pattern.compile(FormatUtils.TIME_DURATION_REGEX).matcher(input.toLowerCase()).matches()) {
        return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
    } else {
        return new ValidationResult.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")
                .build();
    }
}
 
Example 9
Source File: ConvertAvroSchema.java    From localization_nifi with Apache License 2.0 6 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 = "";
    if (!AVRO_FIELDNAME_PATTERN.matcher(subject).matches()) {
        reason = subject + " is not a valid Avro fieldname";
    }
    if (!AVRO_FIELDNAME_PATTERN.matcher(value).matches()) {
        reason = reason + value + " is not a valid Avro fieldname";
    }

    return new ValidationResult.Builder().subject(subject).input(value)
            .explanation(reason).valid(reason.equals("")).build();
}
 
Example 10
Source File: AbstractKiteProcessor.java    From localization_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).getValue());
    String error = null;

    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 11
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(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 12
Source File: StandardValidators.java    From nifi with Apache License 2.0 6 votes vote down vote up
private static Validator createURLValidator() {
    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();
            }

            try {
                final String evaluatedInput = context.newPropertyValue(input).evaluateAttributeExpressions().getValue();
                new URL(evaluatedInput);
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid URL").valid(true).build();
            } catch (final Exception e) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Not a valid URL").valid(false).build();
            }
        }
    };
}
 
Example 13
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) {
    String message = "not set";
    boolean isValid = true;

    if (uri.trim().isEmpty()) {
        isValid = false;
    } else {
        final boolean elPresent = context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(uri);
        if (!elPresent) {
            try {
                new URIBuilder(URI.create(uri)).build();
            } catch (RuntimeException e) {
                message = e.getMessage();
                isValid = false;
            }
        }
    }

    return new ValidationResult.Builder()
            .subject(subject)
            .input(uri)
            .explanation("Dataset URI is invalid: " + message)
            .valid(isValid)
            .build();
}
 
Example 14
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 15
Source File: AbstractHTMLProcessor.java    From nifi with Apache License 2.0 6 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 {
        Document doc = Jsoup.parse("<html></html>");
        doc.select(value);
    } catch (final Selector.SelectorParseException e) {
        reason = "\"" + value + "\" is an invalid CSS selector";
    }

    return new ValidationResult.Builder().subject(subject).input(value).explanation(reason).valid(reason == null).build();
}
 
Example 16
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 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 {
        final int intVal = Integer.parseInt(value);

        if (intVal <= 0) {
            reason = "not a positive 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 17
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)) {
        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 18
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) {
    // expression language
    if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
        return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
    }
    // not empty
    ValidationResult nonEmptyValidatorResult = StandardValidators.NON_EMPTY_VALIDATOR.validate(subject, input, context);
    if (!nonEmptyValidatorResult.isValid()) {
        return nonEmptyValidatorResult;
    }
    // check format
    final List<String> hostnamePortList = Arrays.asList(input.split(","));
    for (String hostnamePort : hostnamePortList) {
        String[] addresses = hostnamePort.split(":");
        // Protect against invalid input like http://127.0.0.1:9300 (URL scheme should not be there)
        if (addresses.length != 2) {
            return new ValidationResult.Builder().subject(subject).input(input).explanation(
                    "Must be in hostname:port form (no scheme such as http://").valid(false).build();
        }

        // Validate the port
        String port = addresses[1].trim();
        ValidationResult portValidatorResult = StandardValidators.PORT_VALIDATOR.validate(subject, port, context);
        if (!portValidatorResult.isValid()) {
            return portValidatorResult;
        }
    }
    return new ValidationResult.Builder().subject(subject).input(input).explanation("Valid cluster definition").valid(true).build();
}
 
Example 19
Source File: RecordPathValidator.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()
            .input(input)
            .subject(subject)
            .valid(true)
            .explanation("Property uses Expression Language so no further validation is possible")
            .build();
    }

    try {
        RecordPath.compile(input);
        return new ValidationResult.Builder()
            .input(input)
            .subject(subject)
            .valid(true)
            .explanation("Valid RecordPath")
            .build();
    } catch (final RecordPathException e) {
        return new ValidationResult.Builder()
            .input(input)
            .subject(subject)
            .valid(false)
            .explanation("Property Value is not a valid RecordPath value: " + e.getMessage())
            .build();
    }
}
 
Example 20
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 {
        NumberFormat.getInstance().parse(value);
    } catch (ParseException e) {
        reason = "not a valid Number";
    }

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