nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From olegz <...@git.apache.org>
Subject [GitHub] nifi pull request: NIFI-1808 Creating general MQTT processors
Date Tue, 17 May 2016 18:43:15 GMT
Github user olegz commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/392#discussion_r63580288
  
    --- Diff: nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/common/AbstractMQTTProcessor.java
---
    @@ -0,0 +1,342 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.nifi.processors.mqtt.common;
    +
    +import org.apache.nifi.components.PropertyDescriptor;
    +import org.apache.nifi.components.PropertyValue;
    +import org.apache.nifi.components.ValidationContext;
    +import org.apache.nifi.components.ValidationResult;
    +import org.apache.nifi.components.Validator;
    +import org.apache.nifi.expression.AttributeExpression;
    +import org.apache.nifi.logging.ProcessorLog;
    +import org.apache.nifi.processor.AbstractSessionFactoryProcessor;
    +import org.apache.nifi.processor.ProcessContext;
    +import org.apache.nifi.processor.ProcessSession;
    +import org.apache.nifi.processor.ProcessSessionFactory;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.processor.util.StandardValidators;
    +import org.apache.nifi.ssl.SSLContextService;
    +import org.eclipse.paho.client.mqttv3.IMqttClient;
    +import org.eclipse.paho.client.mqttv3.MqttClient;
    +import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
    +import org.eclipse.paho.client.mqttv3.MqttException;
    +import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
    +
    +import java.net.URI;
    +import java.net.URISyntaxException;
    +import java.util.ArrayList;
    +import java.util.Collection;
    +import java.util.List;
    +import java.util.Properties;
    +
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_CLEAN_SESSION_FALSE;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_CLEAN_SESSION_TRUE;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_MQTT_VERSION_310;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_MQTT_VERSION_311;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_MQTT_VERSION_AUTO;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_QOS_0;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_QOS_1;
    +import static org.apache.nifi.processors.mqtt.common.MqttConstants.ALLOWABLE_VALUE_QOS_2;
    +
    +public abstract class AbstractMQTTProcessor extends AbstractSessionFactoryProcessor {
    +
    +    protected ProcessorLog logger;
    +    protected IMqttClient mqttClient;
    +    protected final Object mqttClientConnectLock = new Object();
    +    protected volatile String broker;
    +    protected volatile String clientID;
    +    protected MqttConnectOptions connOpts;
    +    protected MemoryPersistence persistence = new MemoryPersistence();
    +    protected long maxTimeout;
    +
    +    public ProcessSessionFactory processSessionFactory;
    +
    +    public static final Validator QoSValidator = new Validator() {
    +
    +        @Override
    +        public ValidationResult validate(String subject, String input, ValidationContext
context) {
    +            Integer inputInt = Integer.parseInt(input);
    +            if (inputInt < 0 || inputInt > 2) {
    +                return new ValidationResult.Builder().subject(subject).valid(false).explanation("QoS
must be an integer between 0 and 2").build();
    +            }
    +            return new ValidationResult.Builder().subject(subject).valid(true).build();
    +        }
    +    };
    +
    +    public static final Validator BrokerValidator = new Validator() {
    +
    +        @Override
    +        public ValidationResult validate(String subject, String input, ValidationContext
context) {
    +            try{
    +                URI brokerURI = new URI(input);
    +                if (!"".equals(brokerURI.getPath())) {
    +                    return new ValidationResult.Builder().subject(subject).valid(false).explanation("the
broker URI cannot have a path. It currently is:" + brokerURI.getPath()).build();
    +                }
    +                if (!("tcp".equals(brokerURI.getScheme()) || "ssl".equals(brokerURI.getScheme())))
{
    +                    return new ValidationResult.Builder().subject(subject).valid(false).explanation("only
the 'tcp' and 'ssl' schemes are supported.").build();
    +                }
    +            } catch (URISyntaxException e) {
    +                return new ValidationResult.Builder().subject(subject).valid(false).explanation("it
is not valid URI syntax.").build();
    +            }
    +            return new ValidationResult.Builder().subject(subject).valid(true).build();
    +        }
    +    };
    +
    +    public static final Validator RetainValidator = new Validator() {
    +
    +        @Override
    +        public ValidationResult validate(String subject, String input, ValidationContext
context) {
    +            if("true".equalsIgnoreCase(input) || "false".equalsIgnoreCase(input)){
    +                return new ValidationResult.Builder().subject(subject).valid(true).build();
    +            } else{
    +                return StandardValidators.createAttributeExpressionLanguageValidator(AttributeExpression.ResultType.BOOLEAN,
false)
    +                        .validate(subject, input, context);
    +            }
    +
    +        }
    +    };
    +
    +    public static final PropertyDescriptor PROP_BROKER_URI = new PropertyDescriptor.Builder()
    +            .name("Broker URI")
    +            .description("The URI to use to connect to the MQTT broker (e.g. tcp://localhost:1883)")
    +            .required(true)
    +            .addValidator(BrokerValidator)
    +            .build();
    +
    +
    +    public static final PropertyDescriptor PROP_CLIENTID = new PropertyDescriptor.Builder()
    +            .name("Client ID")
    +            .description("MQTT client ID to use")
    +            .required(true)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .build();
    +
    +    public static final PropertyDescriptor PROP_USERNAME = new PropertyDescriptor.Builder()
    +            .name("Username")
    +            .description("Username to use when connecting to the broker")
    +            .required(false)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    +            .build();
    +
    +    public static final PropertyDescriptor PROP_PASSWORD = new PropertyDescriptor.Builder()
    +            .name("Password")
    +            .description("Password to use when connecting to the broker")
    +            .required(false)
    +            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
    --- End diff --
    
    A general comment about use of NON_EMPTY_VALIDATOR. Given that this validator will pass
as long as there is something entered including the white space(s), the question I have if
all of the properties that are using it now are OK with that or they require NON_BLANK_VALIDATOR
which will ensure that? I mean the above may all be correct, so I am just asking.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message