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

The following examples show how to use org.apache.nifi.components.ValidationContext#getProperty() . 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: StandardSSLContextService.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void verifySslConfig(final ValidationContext validationContext) throws ProcessException {
    final String protocol = validationContext.getProperty(SSL_ALGORITHM).getValue();
    try {
        final PropertyValue keyPasswdProp = validationContext.getProperty(KEY_PASSWORD);
        final char[] keyPassword = keyPasswdProp.isSet() ? keyPasswdProp.getValue().toCharArray() : null;

        final String keystoreFile = validationContext.getProperty(KEYSTORE).getValue();
        if (keystoreFile == null) {
            SslContextFactory.createTrustSslContext(
                    validationContext.getProperty(TRUSTSTORE).getValue(),
                    validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(),
                    validationContext.getProperty(TRUSTSTORE_TYPE).getValue(),
                    protocol);
            return;
        }
        final String truststoreFile = validationContext.getProperty(TRUSTSTORE).getValue();
        if (truststoreFile == null) {
            SslContextFactory.createSslContext(
                    validationContext.getProperty(KEYSTORE).getValue(),
                    validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(),
                    keyPassword,
                    validationContext.getProperty(KEYSTORE_TYPE).getValue(),
                    protocol);
            return;
        }

        SslContextFactory.createSslContext(
                validationContext.getProperty(KEYSTORE).getValue(),
                validationContext.getProperty(KEYSTORE_PASSWORD).getValue().toCharArray(),
                keyPassword,
                validationContext.getProperty(KEYSTORE_TYPE).getValue(),
                validationContext.getProperty(TRUSTSTORE).getValue(),
                validationContext.getProperty(TRUSTSTORE_PASSWORD).getValue().toCharArray(),
                validationContext.getProperty(TRUSTSTORE_TYPE).getValue(),
                org.apache.nifi.security.util.SslContextFactory.ClientAuth.REQUIRED,
                protocol);
    } catch (final Exception e) {
        throw new ProcessException(e);
    }
}
 
Example 3
Source File: GenerateTableFetch.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    List<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));
    final PropertyValue columnForPartitioning = validationContext.getProperty(COLUMN_FOR_VALUE_PARTITIONING);
    // If no EL is present, ensure it's a single column (i.e. no commas in the property value)
    if (columnForPartitioning.isSet() && !columnForPartitioning.isExpressionLanguagePresent() && columnForPartitioning.getValue().contains(",")) {
        results.add(new ValidationResult.Builder().valid(false).explanation(
                COLUMN_FOR_VALUE_PARTITIONING.getDisplayName() + " requires a single column name, but a comma was detected").build());
    }

    return results;
}
 
Example 4
Source File: FetchDistributedMapCache.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    List<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));

    PropertyValue cacheEntryIdentifier = validationContext.getProperty(PROP_CACHE_ENTRY_IDENTIFIER);
    boolean elPresent = false;
    try {
        elPresent = cacheEntryIdentifier.isExpressionLanguagePresent();
    } catch (NullPointerException npe) {
        // Unfortunate workaround to a mock framework bug (NIFI-4590)
    }

    if (elPresent) {
        // This doesn't do a full job of validating against the requirement that Put Cache Value In Attribute must be set if multiple
        // Cache Entry Identifiers are supplied (if Expression Language is used). The user could conceivably have a comma-separated list of EL statements,
        // or a single EL statement with commas inside it but that evaluates to a single item.
        results.add(new ValidationResult.Builder().valid(true).explanation("Contains Expression Language").build());
    } else {
        if (!validationContext.getProperty(FetchDistributedMapCache.PROP_PUT_CACHE_VALUE_IN_ATTRIBUTE).isSet()) {
            String identifierString = cacheEntryIdentifier.getValue();
            if (identifierString.contains(",")) {
                results.add(new ValidationResult.Builder().valid(false)
                        .explanation("Multiple Cache Entry Identifiers specified without Put Cache Value In Attribute set").build());
            }
        }
    }
    return results;
}
 
Example 5
Source File: TransformXml.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    final List<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));

    PropertyValue filename = validationContext.getProperty(XSLT_FILE_NAME);
    PropertyValue controller = validationContext.getProperty(XSLT_CONTROLLER);
    PropertyValue key = validationContext.getProperty(XSLT_CONTROLLER_KEY);

    if((filename.isSet() && controller.isSet())
            || (!filename.isSet() && !controller.isSet())) {
        results.add(new ValidationResult.Builder()
                .valid(false)
                .subject(this.getClass().getSimpleName())
                .explanation("Exactly one of the \"XSLT file name\" and \"XSLT controller\" properties must be defined.")
                .build());
    }

    if(controller.isSet() && !key.isSet()) {
        results.add(new ValidationResult.Builder()
                .valid(false)
                .subject(XSLT_CONTROLLER_KEY.getDisplayName())
                .explanation("If using \"XSLT controller\", the XSLT controller key property must be defined.")
                .build());
    }

    if(controller.isSet()) {
        final LookupService<String> lookupService = validationContext.getProperty(XSLT_CONTROLLER).asControllerService(StringLookupService.class);
        final Set<String> requiredKeys = lookupService.getRequiredKeys();
        if (requiredKeys == null || requiredKeys.size() != 1) {
            results.add(new ValidationResult.Builder()
                    .valid(false)
                    .subject(XSLT_CONTROLLER.getDisplayName())
                    .explanation("This processor requires a key-value lookup service supporting exactly one required key, was: " +
                        (requiredKeys == null ? "null" : String.valueOf(requiredKeys.size())))
                    .build());
        }
    }

    return results;
}
 
Example 6
Source File: AbstractJMSProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
ConnectionFactoryConfigValidator(ValidationContext validationContext) {
    this.validationContext = validationContext;

    connectionFactoryServiceProperty = validationContext.getProperty(CF_SERVICE);
    jndiInitialContextFactoryProperty = validationContext.getProperty(JndiJmsConnectionFactoryProperties.JNDI_INITIAL_CONTEXT_FACTORY);
    jmsConnectionFactoryImplProperty = validationContext.getProperty(JMSConnectionFactoryProperties.JMS_CONNECTION_FACTORY_IMPL);
}
 
Example 7
Source File: DistributeLoad.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>();
    if (doCustomValidate.getAndSet(false)) {
        String distStrat = validationContext.getProperty(DISTRIBUTION_STRATEGY).getValue();
        if (distStrat.equals(STRATEGY_LOAD_DISTRIBUTION_SERVICE)) {
            // make sure Hostnames and Controller service are set
            PropertyValue propDesc = validationContext.getProperty(HOSTNAMES);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder().subject(HOSTNAMES.getName())
                        .explanation("Must specify Hostnames when using 'Load Distribution Strategy'").valid(false).build());
            }
            propDesc = validationContext.getProperty(LOAD_DISTRIBUTION_SERVICE_TEMPLATE);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder()
                        .subject(LOAD_DISTRIBUTION_SERVICE_TEMPLATE.getName())
                        .explanation("Must specify 'Load Distribution Service ID' when using 'Load Distribution Strategy'")
                        .valid(false).build());
            }
            if (results.isEmpty()) {
                int numRels = validationContext.getProperty(NUM_RELATIONSHIPS).asInteger();
                String hostNamesValue = validationContext.getProperty(HOSTNAMES).getValue();
                String[] hostNames = hostNamesValue.split("(?:,+|;+|\\s+)");
                int numHosts = 0;
                for (String hostName : hostNames) {
                    if (StringUtils.isNotBlank(hostName)) {
                        hostNames[numHosts++] = hostName;
                    }
                }
                if (numHosts > numRels) {
                    results.add(new ValidationResult.Builder()
                            .subject("Number of Relationships and Hostnames")
                            .explanation("Number of Relationships must be equal to, or greater than, the number of host names")
                            .valid(false).build());
                } else {
                    // create new relationships with descriptions of hostname
                    Set<Relationship> relsWithDesc = new TreeSet<>();
                    for (int i = 0; i < numHosts; i++) {
                        relsWithDesc.add(new Relationship.Builder().name(String.valueOf(i + 1))
                                .description(hostNames[i]).build());
                    }
                    // add add'l rels if configuration requires it...it probably shouldn't
                    for (int i = numHosts + 1; i <= numRels; i++) {
                        relsWithDesc.add(createRelationship(i));
                    }
                    relationshipsRef.set(Collections.unmodifiableSet(relsWithDesc));
                }
            }
        }
    }
    return results;
}
 
Example 8
Source File: DistributeLoad.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>();
    if (doCustomValidate.getAndSet(false)) {
        String distStrat = validationContext.getProperty(DISTRIBUTION_STRATEGY).getValue();
        if (distStrat.equals(STRATEGY_LOAD_DISTRIBUTION_SERVICE)) {
            // make sure Hostnames and Controller service are set
            PropertyValue propDesc = validationContext.getProperty(HOSTNAMES);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder().subject(HOSTNAMES.getName())
                        .explanation("Must specify Hostnames when using 'Load Distribution Strategy'").valid(false).build());
            }
            propDesc = validationContext.getProperty(LOAD_DISTRIBUTION_SERVICE_TEMPLATE);
            if (null == propDesc || null == propDesc.getValue() || propDesc.getValue().isEmpty()) {
                results.add(new ValidationResult.Builder()
                        .subject(LOAD_DISTRIBUTION_SERVICE_TEMPLATE.getName())
                        .explanation("Must specify 'Load Distribution Service ID' when using 'Load Distribution Strategy'")
                        .valid(false).build());
            }
            if (results.isEmpty()) {
                int numRels = validationContext.getProperty(NUM_RELATIONSHIPS).asInteger();
                String hostNamesValue = validationContext.getProperty(HOSTNAMES).getValue();
                String[] hostNames = hostNamesValue.split("(?:,+|;+|\\s+)");
                int numHosts = 0;
                for (String hostName : hostNames) {
                    if (StringUtils.isNotBlank(hostName)) {
                        hostNames[numHosts++] = hostName;
                    }
                }
                if (numHosts > numRels) {
                    results.add(new ValidationResult.Builder()
                            .subject("Number of Relationships and Hostnames")
                            .explanation("Number of Relationships must be equal to, or greater than, the number of host names")
                            .valid(false).build());
                } else {
                    // create new relationships with descriptions of hostname
                    Set<Relationship> relsWithDesc = new TreeSet<>();
                    for (int i = 0; i < numHosts; i++) {
                        relsWithDesc.add(new Relationship.Builder().name(String.valueOf(i + 1))
                                .description(hostNames[i]).build());
                    }
                    // add add'l rels if configuration requires it...it probably shouldn't
                    for (int i = numHosts + 1; i <= numRels; i++) {
                        relsWithDesc.add(createRelationship(i));
                    }
                    relationshipsRef.set(Collections.unmodifiableSet(relsWithDesc));
                }
            }
        }
    }
    return results;
}
 
Example 9
Source File: StandardS3EncryptionService.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    Collection<ValidationResult> validationResults = new ArrayList<>();

    String encryptionStrategyName = validationContext.getProperty(ENCRYPTION_STRATEGY).getValue();
    String encryptionStrategyDisplayName = ENCRYPTION_STRATEGY_ALLOWABLE_VALUES.get(encryptionStrategyName).getDisplayName();
    PropertyValue encryptionValueProperty = validationContext.getProperty(ENCRYPTION_VALUE);
    String encryptionValue = encryptionValueProperty.evaluateAttributeExpressions().getValue();

    switch (encryptionStrategyName) {
        case STRATEGY_NAME_NONE:
        case STRATEGY_NAME_SSE_S3:
            if (encryptionValueProperty.isSet()) {
                validationResults.add(new ValidationResult.Builder()
                        .subject(ENCRYPTION_VALUE.getDisplayName())
                        .valid(false)
                        .explanation("the property cannot be specified for encryption strategy " + encryptionStrategyDisplayName)
                        .build()
                );
            }
            break;
        case STRATEGY_NAME_SSE_KMS:
        case STRATEGY_NAME_CSE_KMS:
            if (StringUtils.isEmpty(encryptionValue)) {
                validationResults.add(new ValidationResult.Builder()
                        .subject(ENCRYPTION_VALUE.getDisplayName())
                        .valid(false)
                        .explanation("a non-empty Key ID must be specified for encryption strategy " + encryptionStrategyDisplayName)
                        .build()
                );
            }
            break;
        case STRATEGY_NAME_SSE_C:
        case STRATEGY_NAME_CSE_C:
            if (StringUtils.isEmpty(encryptionValue)) {
                validationResults.add(new ValidationResult.Builder()
                        .subject(ENCRYPTION_VALUE.getDisplayName())
                        .valid(false)
                        .explanation("a non-empty Key Material must be specified for encryption strategy " + encryptionStrategyDisplayName)
                        .build()
                );
            } else {
                S3EncryptionStrategy encryptionStrategy = NAMED_STRATEGIES.get(encryptionStrategyName);
                String keyIdOrMaterial = validationContext.getProperty(ENCRYPTION_VALUE).evaluateAttributeExpressions().getValue();

                validationResults.add(encryptionStrategy.validateKey(keyIdOrMaterial));
            }
            break;
    }

    return validationResults;
}