nandorsoma commented on code in PR #6225:
URL: https://github.com/apache/nifi/pull/6225#discussion_r952888865


##########
nifi-nar-bundles/nifi-mqtt-bundle/nifi-mqtt-processors/src/main/java/org/apache/nifi/processors/mqtt/adapters/HiveMqV5ClientAdapter.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * 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.adapters;
+
+import com.hivemq.client.mqtt.datatypes.MqttQos;
+import com.hivemq.client.mqtt.mqtt5.Mqtt5BlockingClient;
+import com.hivemq.client.mqtt.mqtt5.Mqtt5Client;
+import com.hivemq.client.mqtt.mqtt5.message.connect.Mqtt5Connect;
+import com.hivemq.client.mqtt.mqtt5.message.connect.Mqtt5ConnectBuilder;
+import org.apache.nifi.processors.mqtt.common.MqttConnectionProperties;
+import org.apache.nifi.processors.mqtt.common.NifiMqttCallback;
+import org.apache.nifi.processors.mqtt.common.NifiMqttClient;
+import org.apache.nifi.processors.mqtt.common.NifiMqttException;
+import org.apache.nifi.processors.mqtt.common.NifiMqttMessage;
+
+import javax.net.ssl.KeyManagerFactory;
+import javax.net.ssl.TrustManagerFactory;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyStore;
+import java.security.KeyStoreException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.util.Arrays;
+import java.util.Objects;
+
+public class HiveMqV5ClientAdapter implements NifiMqttClient {
+
+    private final Mqtt5Client mqtt5Client;
+
+    private NifiMqttCallback callback;
+
+    public HiveMqV5ClientAdapter(Mqtt5BlockingClient mqtt5BlockingClient) {
+        this.mqtt5Client = mqtt5BlockingClient;
+    }
+
+    @Override
+    public boolean isConnected() {
+        return mqtt5Client.getState().isConnected();
+    }
+
+    @Override
+    public void connect(MqttConnectionProperties connectionProperties) throws 
NifiMqttException {
+        final Mqtt5ConnectBuilder connectBuilder = Mqtt5Connect.builder()
+                .keepAlive(connectionProperties.getKeepAliveInterval());
+
+        final boolean cleanSession = connectionProperties.isCleanSession();
+        connectBuilder.cleanStart(cleanSession);
+        if (!cleanSession) {
+            
connectBuilder.sessionExpiryInterval(connectionProperties.getSessionExpiryInterval());
+        }
+
+        final String lastWillTopic = connectionProperties.getLastWillTopic();
+        if (lastWillTopic != null) {
+            connectBuilder.willPublish()
+                    .topic(lastWillTopic)
+                    
.payload(connectionProperties.getLastWillMessage().getBytes())
+                    .retain(connectionProperties.getLastWillRetain())
+                    
.qos(MqttQos.fromCode(connectionProperties.getLastWillQOS()))
+                    .applyWillPublish();
+        }
+
+        // checking for presence of password because username can be null
+        final char[] password = connectionProperties.getPassword();
+        if (password != null) {
+            connectBuilder.simpleAuth()
+                    .username(connectionProperties.getUsername())
+                    .password(toBytes(password))
+                    .applySimpleAuth();

Review Comment:
   Mqtt v5 allows to use just password without username: 
https://www.hivemq.com/blog/mqtt5-essentials-part2-foundational-changes-in-the-protocol/
 (see Using passwords without usernames)
   But as we agreed, now I'm adding username to the check and when needed we 
will create a separate field on the processor to allow authentication just with 
password.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@nifi.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to