Java Code Examples for org.apache.nifi.processor.ProcessContext#getProperty()

The following examples show how to use org.apache.nifi.processor.ProcessContext#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: PublishAMQP.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Will construct AMQP message by extracting its body from the incoming
 * {@link FlowFile}. AMQP Properties will be extracted from the
 * {@link FlowFile} and converted to {@link BasicProperties} to be sent
 * along with the message. Upon success the incoming {@link FlowFile} is
 * transferred to 'success' {@link Relationship} and upon failure FlowFile is
 * penalized and transferred to the 'failure' {@link Relationship}
 * <br>
 * NOTE: Attributes extracted from {@link FlowFile} are considered
 * candidates for AMQP properties if their names are prefixed with
 * {@link AMQPUtils#AMQP_PROP_PREFIX} (e.g., amqp$contentType=text/xml)
 *
 */
@Override
protected void rendezvousWithAmqp(ProcessContext context, ProcessSession processSession) throws ProcessException {
    FlowFile flowFile = processSession.get();
    if (flowFile != null) {
        BasicProperties amqpProperties = this.extractAmqpPropertiesFromFlowFile(flowFile);
        String routingKey = context.getProperty(ROUTING_KEY).evaluateAttributeExpressions(flowFile).getValue();
        if (routingKey == null){
            throw new IllegalArgumentException("Failed to determine 'routing key' with provided value '"
                    + context.getProperty(ROUTING_KEY) + "' after evaluating it as expression against incoming FlowFile.");
        }
        String exchange = context.getProperty(EXCHANGE).evaluateAttributeExpressions(flowFile).getValue();

        byte[] messageContent = this.extractMessage(flowFile, processSession);

        try {
            this.targetResource.publish(messageContent, amqpProperties, routingKey, exchange);
            processSession.transfer(flowFile, REL_SUCCESS);
            processSession.getProvenanceReporter().send(flowFile, this.amqpConnection.toString() + "/E:" + exchange + "/RK:" + routingKey);
        } catch (Exception e) {
            processSession.transfer(processSession.penalize(flowFile), REL_FAILURE);
            this.getLogger().error("Failed while sending message to AMQP via " + this.targetResource, e);
            context.yield();
        }
    }
}
 
Example 2
Source File: PutAccumuloRecord.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Adapted from HBASEUtils. Their approach seemed ideal for what our intent is here.
 * @param columnFamily column family from which to extract the visibility or to execute an expression against
 * @param columnQualifier column qualifier from which to extract the visibility or to execute an expression against
 * @param flowFile flow file being written
 * @param context process context
 * @return
 */
public static String produceVisibility(String columnFamily, String columnQualifier, FlowFile flowFile, ProcessContext context) {
    if (org.apache.commons.lang3.StringUtils.isNotEmpty(columnFamily)) {
        return null;
    }
    String lookupKey = String.format("visibility.%s%s%s", columnFamily, !org.apache.commons.lang3.StringUtils.isNotEmpty(columnQualifier) ? "." : "", columnQualifier);
    String fromAttribute = flowFile.getAttribute(lookupKey);

    if (fromAttribute == null && !org.apache.commons.lang3.StringUtils.isBlank(columnQualifier)) {
        String lookupKeyFam = String.format("visibility.%s", columnFamily);
        fromAttribute = flowFile.getAttribute(lookupKeyFam);
    }

    if (fromAttribute != null) {
        return fromAttribute;
    } else {
        PropertyValue descriptor = context.getProperty(lookupKey);
        if (descriptor == null || !descriptor.isSet()) {
            descriptor = context.getProperty(String.format("visibility.%s", columnFamily));
        }

        String retVal = descriptor != null ? descriptor.evaluateAttributeExpressions(flowFile).getValue() : null;

        return retVal;
    }
}
 
Example 3
Source File: VisibilityUtil.java    From nifi with Apache License 2.0 6 votes vote down vote up
public static String pickVisibilityString(String columnFamily, String columnQualifier, FlowFile flowFile, ProcessContext context) {
    if (StringUtils.isBlank(columnFamily)) {
        return null;
    }
    String lookupKey = String.format("visibility.%s%s%s", columnFamily, !StringUtils.isBlank(columnQualifier) ? "." : "", columnQualifier);
    String fromAttribute = flowFile.getAttribute(lookupKey);

    if (fromAttribute == null && !StringUtils.isBlank(columnQualifier)) {
        String lookupKeyFam = String.format("visibility.%s", columnFamily);
        fromAttribute = flowFile.getAttribute(lookupKeyFam);
    }

    if (fromAttribute != null) {
        return fromAttribute;
    } else {
        PropertyValue descriptor = context.getProperty(lookupKey);
        if (descriptor == null || !descriptor.isSet()) {
            descriptor = context.getProperty(String.format("visibility.%s", columnFamily));
        }

        String retVal = descriptor != null ? descriptor.evaluateAttributeExpressions(flowFile).getValue() : null;

        return retVal;
    }
}
 
Example 4
Source File: ParametersIT.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Test
public void testSensitivePropertyReferenceParameterSupportsEL() {
    final ProcessorNode usernamePassword = createProcessorNode(UsernamePasswordProcessor.class);

    final ParameterReferenceManager referenceManager = new StandardParameterReferenceManager(getFlowController().getFlowManager());
    final ParameterContext parameterContext = new StandardParameterContext(UUID.randomUUID().toString(), "param-context", referenceManager, null);
    parameterContext.setParameters(Collections.singletonMap("pass", new Parameter(new ParameterDescriptor.Builder().name("pass").sensitive(true).build(), "secret")));

    getRootGroup().setParameterContext(parameterContext);

    final Map<String, String> properties = new HashMap<>();
    properties.put("password", "#{pass}");
    usernamePassword.setProperties(properties);

    final ProcessContext processContext = new StandardProcessContext(usernamePassword, getFlowController().getControllerServiceProvider(), getFlowController().getEncryptor(),
        getFlowController().getStateManagerProvider().getStateManager(usernamePassword.getIdentifier()), () -> false);
    final PropertyDescriptor descriptor = usernamePassword.getPropertyDescriptor("password");
    final PropertyValue propertyValue = processContext.getProperty(descriptor);
    final PropertyValue evaluatedPropertyValue = propertyValue.evaluateAttributeExpressions();
    final String evaluatedPassword = evaluatedPropertyValue.getValue();
    assertEquals("secret", evaluatedPassword);
}
 
Example 5
Source File: KafkaProcessorUtils.java    From nifi with Apache License 2.0 6 votes vote down vote up
/**
 * Method used to configure the 'sasl.jaas.config' property based on KAFKA-4259<br />
 * https://cwiki.apache.org/confluence/display/KAFKA/KIP-85%3A+Dynamic+JAAS+configuration+for+Kafka+clients<br />
 * <br />
 * It expects something with the following format: <br />
 * <br />
 * &lt;LoginModuleClass&gt; &lt;ControlFlag&gt; *(&lt;OptionName&gt;=&lt;OptionValue&gt;); <br />
 * ControlFlag = required / requisite / sufficient / optional
 *
 * @param mapToPopulate Map of configuration properties
 * @param context Context
 */
private static void setJaasConfig(Map<String, Object> mapToPopulate, ProcessContext context) {
    String keytab = context.getProperty(USER_KEYTAB) == null ? null : context.getProperty(USER_KEYTAB).evaluateAttributeExpressions().getValue();
    String principal = context.getProperty(USER_PRINCIPAL) == null ? null : context.getProperty(USER_PRINCIPAL).evaluateAttributeExpressions().getValue();

    // If the Kerberos Credentials Service is specified, we need to use its configuration, not the explicit properties for principal/keytab.
    // The customValidate method ensures that only one can be set, so we know that the principal & keytab above are null.
    final KerberosCredentialsService credentialsService = context.getProperty(KERBEROS_CREDENTIALS_SERVICE).asControllerService(KerberosCredentialsService.class);
    if (credentialsService != null) {
        principal = credentialsService.getPrincipal();
        keytab = credentialsService.getKeytab();
    }


    String serviceName = context.getProperty(JAAS_SERVICE_NAME).evaluateAttributeExpressions().getValue();
    if(StringUtils.isNotBlank(keytab) && StringUtils.isNotBlank(principal) && StringUtils.isNotBlank(serviceName)) {
        mapToPopulate.put(SaslConfigs.SASL_JAAS_CONFIG, "com.sun.security.auth.module.Krb5LoginModule required "
                + "useTicketCache=false "
                + "renewTicket=true "
                + "serviceName=\"" + serviceName + "\" "
                + "useKeyTab=true "
                + "keyTab=\"" + keytab + "\" "
                + "principal=\"" + principal + "\";");
    }
}
 
Example 6
Source File: SFTPTransfer.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
public SFTPTransfer(final ProcessContext processContext, final ComponentLog logger) {
    this.ctx = processContext;
    this.logger = logger;

    final PropertyValue disableListing = processContext.getProperty(DISABLE_DIRECTORY_LISTING);
    disableDirectoryListing = disableListing == null ? false : Boolean.TRUE.equals(disableListing.asBoolean());
}
 
Example 7
Source File: PutHDFS.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
@OnScheduled
public void onScheduled(ProcessContext context) throws Exception {
    super.abstractOnScheduled(context);

    // Set umask once, to avoid thread safety issues doing it in onTrigger
    final PropertyValue umaskProp = context.getProperty(UMASK);
    final short dfsUmask;
    if (umaskProp.isSet()) {
        dfsUmask = Short.parseShort(umaskProp.getValue(), 8);
    } else {
        dfsUmask = FsPermission.DEFAULT_UMASK;
    }
    final Configuration conf = getConfiguration();
    FsPermission.setUMask(conf, new FsPermission(dfsUmask));
}
 
Example 8
Source File: AbstractPutHDFSRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected void preProcessConfiguration(Configuration config, ProcessContext context) {
    // Set umask once, to avoid thread safety issues doing it in onTrigger
    final PropertyValue umaskProp = context.getProperty(UMASK);
    final short dfsUmask;
    if (umaskProp.isSet()) {
        dfsUmask = Short.parseShort(umaskProp.getValue(), 8);
    } else {
        dfsUmask = FsPermission.DEFAULT_UMASK;
    }

    FsPermission.setUMask(config, new FsPermission(dfsUmask));
}
 
Example 9
Source File: LookupAttribute.java    From nifi with Apache License 2.0 5 votes vote down vote up
@OnScheduled
public void onScheduled(final ProcessContext context) {
    // Load up all the dynamic properties once for use later in onTrigger
    final Map<PropertyDescriptor, PropertyValue> dynamicProperties = new HashMap<>();
    for (final Map.Entry<PropertyDescriptor, String> e : context.getProperties().entrySet()) {
        final PropertyDescriptor descriptor = e.getKey();
        if (descriptor.isDynamic()) {
            final PropertyValue value = context.getProperty(descriptor);
            dynamicProperties.put(descriptor, value);
        }
    }
    this.dynamicProperties = Collections.unmodifiableMap(dynamicProperties);
}
 
Example 10
Source File: EnforceOrder.java    From nifi with Apache License 2.0 5 votes vote down vote up
private OrderingContext(final ProcessContext processContext, final ProcessSession processSession) {
    this.processContext = processContext;
    this.processSession = processSession;

    orderAttribute = processContext.getProperty(ORDER_ATTRIBUTE).getValue();
    waitTimeoutMillis = processContext.getProperty(WAIT_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS);
    getOrder = flowFile -> Integer.parseInt(flowFile.getAttribute(orderAttribute));


    groupIdentifierProperty = processContext.getProperty(GROUP_IDENTIFIER);

    initOrderProperty = processContext.getProperty(INITIAL_ORDER);
    maxOrderProperty = processContext.getProperty(MAX_ORDER);
}
 
Example 11
Source File: PutHDFS.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected void preProcessConfiguration(final Configuration config, final ProcessContext context) {
    // Set umask once, to avoid thread safety issues doing it in onTrigger
    final PropertyValue umaskProp = context.getProperty(UMASK);
    final short dfsUmask;
    if (umaskProp.isSet()) {
        dfsUmask = Short.parseShort(umaskProp.getValue(), 8);
    } else {
        dfsUmask = FsPermission.getUMask(config).toShort();
    }
    FsPermission.setUMask(config, new FsPermission(dfsUmask));
}
 
Example 12
Source File: AbstractMQTTProcessor.java    From nifi with Apache License 2.0 5 votes vote down vote up
protected void onScheduled(final ProcessContext context){
    broker = context.getProperty(PROP_BROKER_URI).getValue();
    clientID = context.getProperty(PROP_CLIENTID).evaluateAttributeExpressions().getValue();

    if (clientID == null) {
        clientID = UUID.randomUUID().toString();
    }

    connOpts = new MqttConnectOptions();
    connOpts.setCleanSession(context.getProperty(PROP_CLEAN_SESSION).asBoolean());
    connOpts.setKeepAliveInterval(context.getProperty(PROP_KEEP_ALIVE_INTERVAL).asInteger());
    connOpts.setMqttVersion(context.getProperty(PROP_MQTT_VERSION).asInteger());
    connOpts.setConnectionTimeout(context.getProperty(PROP_CONN_TIMEOUT).asInteger());

    PropertyValue sslProp = context.getProperty(PROP_SSL_CONTEXT_SERVICE);
    if (sslProp.isSet()) {
        Properties sslProps = transformSSLContextService((SSLContextService) sslProp.asControllerService());
        connOpts.setSSLProperties(sslProps);
    }

    PropertyValue lastWillTopicProp = context.getProperty(PROP_LAST_WILL_TOPIC);
    if (lastWillTopicProp.isSet()){
        String lastWillMessage = context.getProperty(PROP_LAST_WILL_MESSAGE).getValue();
        PropertyValue lastWillRetain = context.getProperty(PROP_LAST_WILL_RETAIN);
        Integer lastWillQOS = context.getProperty(PROP_LAST_WILL_QOS).asInteger();
        connOpts.setWill(lastWillTopicProp.getValue(), lastWillMessage.getBytes(), lastWillQOS, lastWillRetain.isSet() ? lastWillRetain.asBoolean() : false);
    }


    PropertyValue usernameProp = context.getProperty(PROP_USERNAME);
    if(usernameProp.isSet()) {
        connOpts.setUserName(usernameProp.getValue());
        connOpts.setPassword(context.getProperty(PROP_PASSWORD).getValue().toCharArray());
    }
}
 
Example 13
Source File: PublishAMQP.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Will construct AMQP message by extracting its body from the incoming {@link FlowFile}. AMQP Properties will be extracted from the
 * {@link FlowFile} and converted to {@link BasicProperties} to be sent along with the message. Upon success the incoming {@link FlowFile} is
 * transferred to 'success' {@link Relationship} and upon failure FlowFile is penalized and transferred to the 'failure' {@link Relationship}
 * <br>
 *
 * NOTE: Attributes extracted from {@link FlowFile} are considered
 * candidates for AMQP properties if their names are prefixed with
 * {@link AMQPUtils#AMQP_PROP_PREFIX} (e.g., amqp$contentType=text/xml)
 */
@Override
protected void processResource(final Connection connection, final AMQPPublisher publisher, ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final BasicProperties amqpProperties = extractAmqpPropertiesFromFlowFile(flowFile);
    final String routingKey = context.getProperty(ROUTING_KEY).evaluateAttributeExpressions(flowFile).getValue();
    if (routingKey == null) {
        throw new IllegalArgumentException("Failed to determine 'routing key' with provided value '"
            + context.getProperty(ROUTING_KEY) + "' after evaluating it as expression against incoming FlowFile.");
    }

    final String exchange = context.getProperty(EXCHANGE).evaluateAttributeExpressions(flowFile).getValue();
    final byte[] messageContent = extractMessage(flowFile, session);

    try {
        publisher.publish(messageContent, amqpProperties, routingKey, exchange);
        session.transfer(flowFile, REL_SUCCESS);
        session.getProvenanceReporter().send(flowFile, connection.toString() + "/E:" + exchange + "/RK:" + routingKey);
    } catch (Exception e) {
        session.transfer(session.penalize(flowFile), REL_FAILURE);
        getLogger().error("Failed while sending message to AMQP via " + publisher, e);
    }
}
 
Example 14
Source File: AbstractCassandraProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected void connectToCassandra(ProcessContext context) {
    if (cluster.get() == null) {
        ComponentLog log = getLogger();
        final String contactPointList = context.getProperty(CONTACT_POINTS).getValue();
        final String consistencyLevel = context.getProperty(CONSISTENCY_LEVEL).getValue();
        List<InetSocketAddress> contactPoints = getContactPoints(contactPointList);

        // Set up the client for secure (SSL/TLS communications) if configured to do so
        final SSLContextService sslService =
                context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
        final String rawClientAuth = context.getProperty(CLIENT_AUTH).getValue();
        final SSLContext sslContext;

        if (sslService != null) {
            final SSLContextService.ClientAuth clientAuth;
            if (StringUtils.isBlank(rawClientAuth)) {
                clientAuth = SSLContextService.ClientAuth.REQUIRED;
            } else {
                try {
                    clientAuth = SSLContextService.ClientAuth.valueOf(rawClientAuth);
                } catch (final IllegalArgumentException iae) {
                    throw new ProviderCreationException(String.format("Unrecognized client auth '%s'. Possible values are [%s]",
                            rawClientAuth, StringUtils.join(SslContextFactory.ClientAuth.values(), ", ")));
                }
            }
            sslContext = sslService.createSSLContext(clientAuth);
        } else {
            sslContext = null;
        }

        final String username, password;
        PropertyValue usernameProperty = context.getProperty(USERNAME);
        PropertyValue passwordProperty = context.getProperty(PASSWORD);

        if (usernameProperty != null && passwordProperty != null) {
            username = usernameProperty.getValue();
            password = passwordProperty.getValue();
        } else {
            username = null;
            password = null;
        }

        // Create the cluster and connect to it
        Cluster newCluster = createCluster(contactPoints, sslContext, username, password);
        PropertyValue keyspaceProperty = context.getProperty(KEYSPACE);
        final Session newSession;
        if (keyspaceProperty != null) {
            newSession = newCluster.connect(keyspaceProperty.getValue());
        } else {
            newSession = newCluster.connect();
        }
        newCluster.getConfiguration().getQueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel));
        Metadata metadata = newCluster.getMetadata();
        log.info("Connected to Cassandra cluster: {}", new Object[]{metadata.getClusterName()});
        cluster.set(newCluster);
        cassandraSession.set(newSession);
    }
}
 
Example 15
Source File: AbstractMQTTProcessor.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
protected void buildClient(ProcessContext context){
    try {
        broker = context.getProperty(PROP_BROKER_URI).getValue();
        clientID = context.getProperty(PROP_CLIENTID).getValue();

        connOpts = new MqttConnectOptions();
        connOpts.setCleanSession(context.getProperty(PROP_CLEAN_SESSION).asBoolean());
        connOpts.setKeepAliveInterval(context.getProperty(PROP_KEEP_ALIVE_INTERVAL).asInteger());
        connOpts.setMqttVersion(context.getProperty(PROP_MQTT_VERSION).asInteger());
        connOpts.setConnectionTimeout(context.getProperty(PROP_CONN_TIMEOUT).asInteger());

        PropertyValue sslProp = context.getProperty(PROP_SSL_CONTEXT_SERVICE);
        if (sslProp.isSet()) {
            Properties sslProps = transformSSLContextService((SSLContextService) sslProp.asControllerService());
            connOpts.setSSLProperties(sslProps);
        }

        PropertyValue lastWillTopicProp = context.getProperty(PROP_LAST_WILL_TOPIC);
        if (lastWillTopicProp.isSet()){
            String lastWillMessage = context.getProperty(PROP_LAST_WILL_MESSAGE).getValue();
            PropertyValue lastWillRetain = context.getProperty(PROP_LAST_WILL_RETAIN);
            Integer lastWillQOS = context.getProperty(PROP_LAST_WILL_QOS).asInteger();
            connOpts.setWill(lastWillTopicProp.getValue(), lastWillMessage.getBytes(), lastWillQOS, lastWillRetain.isSet() ? lastWillRetain.asBoolean() : false);
        }


        PropertyValue usernameProp = context.getProperty(PROP_USERNAME);
        if(usernameProp.isSet()) {
            connOpts.setUserName(usernameProp.getValue());
            connOpts.setPassword(context.getProperty(PROP_PASSWORD).getValue().toCharArray());
        }

        mqttClientConnectLock.writeLock().lock();
        try{
            mqttClient = getMqttClient(broker, clientID, persistence);

        } finally {
            mqttClientConnectLock.writeLock().unlock();
        }
    } catch(MqttException me) {
        logger.error("Failed to initialize the connection to the  " + me.getMessage());
    }
}