<commit_before>/*
 * Copyright 2017 Datamountaineer.
 *
 * Licensed 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 com.datamountaineer.streamreactor.connect.mqtt.connection

import com.datamountaineer.streamreactor.connect.mqtt.config.{ MqttSinkSettings, MqttSourceSettings}
import com.datamountaineer.streamreactor.connect.mqtt.source.MqttSSLSocketFactory
import com.typesafe.scalalogging.slf4j.StrictLogging
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import org.eclipse.paho.client.mqttv3.{MqttCallback, MqttClient, MqttConnectOptions}

object MqttClientConnectionFn extends StrictLogging {
  def apply(callback: MqttCallback)(implicit settings: MqttSourceSettings): MqttClient = {
    {
      val options = buildBaseClient(settings.connectionTimeout,
                                    settings.keepAliveInterval,
                                    settings.cleanSession,
                                    settings.password,
                                    settings.sslCertFile,
                                    settings.sslCACertFile,
                                    settings.sslCertKeyFile
                                  )

      val c = new MqttClient(settings.connection, settings.clientId, new MemoryPersistence())
      c.setCallback(callback)

      logger.info(s"Connecting to ${settings.connection}")
      c.connect(options)
      logger.info(s"Connected to ${settings.connection} as ${settings.clientId}")
      c
    }
  }

  def apply(settings: MqttSinkSettings) : MqttClient = {
    val options = buildBaseClient(settings.connectionTimeout,
                                  settings.keepAliveInterval,
                                  settings.cleanSession,
                                  settings.password,
                                  settings.sslCertFile,
                                  settings.sslCACertFile,
                                  settings.sslCertKeyFile
                                )

    val c = new MqttClient(settings.connection, settings.clientId, new MemoryPersistence())
    logger.info(s"Connecting to ${settings.connection}")
    c.connect(options)
    logger.info(s"Connected to ${settings.connection} as ${settings.clientId}")
    c
  }

  def buildBaseClient(connectionTimeout: Int,
                      keepAliveInterval: Int,
                      cleanSession: Boolean,
                      password: Option[String],
                      sslCertFile: Option[String],
                      sslCACertFile: Option[String],
                      sslCertKeyFile: Option[String]
                     ) : MqttConnectOptions = {
    val options = new MqttConnectOptions()
    options.setConnectionTimeout(connectionTimeout)
    options.setKeepAliveInterval(keepAliveInterval)
    options.setCleanSession(cleanSession)
    password.foreach(p => options.setPassword(p.toCharArray))
    options.setAutomaticReconnect(true)

    sslCertFile.foreach { _ =>
      options.setSocketFactory(
        MqttSSLSocketFactory(sslCACertFile.get, sslCertFile.get, sslCertKeyFile.get, "")
      )
    }

    options
  }
}<commit_msg>Fix in mqtt connector when use user/password auth (#376)<commit_after>/*
 * Copyright 2017 Datamountaineer.
 *
 * Licensed 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 com.datamountaineer.streamreactor.connect.mqtt.connection

import com.datamountaineer.streamreactor.connect.mqtt.config.{ MqttSinkSettings, MqttSourceSettings}
import com.datamountaineer.streamreactor.connect.mqtt.source.MqttSSLSocketFactory
import com.typesafe.scalalogging.slf4j.StrictLogging
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence
import org.eclipse.paho.client.mqttv3.{MqttCallback, MqttClient, MqttConnectOptions}

object MqttClientConnectionFn extends StrictLogging {
  def apply(callback: MqttCallback)(implicit settings: MqttSourceSettings): MqttClient = {
    {
      val options = buildBaseClient(settings.connectionTimeout,
                                    settings.keepAliveInterval,
                                    settings.cleanSession,
                                    settings.user,
                                    settings.password,
                                    settings.sslCertFile,
                                    settings.sslCACertFile,
                                    settings.sslCertKeyFile
                                  )

      val c = new MqttClient(settings.connection, settings.clientId, new MemoryPersistence())
      c.setCallback(callback)

      logger.info(s"Connecting to ${settings.connection}")
      c.connect(options)
      logger.info(s"Connected to ${settings.connection} as ${settings.clientId}")
      c
    }
  }

  def apply(settings: MqttSinkSettings) : MqttClient = {
    val options = buildBaseClient(settings.connectionTimeout,
                                  settings.keepAliveInterval,
                                  settings.cleanSession,
                                  settings.user,
                                  settings.password,
                                  settings.sslCertFile,
                                  settings.sslCACertFile,
                                  settings.sslCertKeyFile
                                )

    val c = new MqttClient(settings.connection, settings.clientId, new MemoryPersistence())
    logger.info(s"Connecting to ${settings.connection}")
    c.connect(options)
    logger.info(s"Connected to ${settings.connection} as ${settings.clientId}")
    c
  }

  def buildBaseClient(connectionTimeout: Int,
                      keepAliveInterval: Int,
                      cleanSession: Boolean,
                      username: Option[String],
                      password: Option[String],
                      sslCertFile: Option[String],
                      sslCACertFile: Option[String],
                      sslCertKeyFile: Option[String]
                     ) : MqttConnectOptions = {
    val options = new MqttConnectOptions()
    options.setConnectionTimeout(connectionTimeout)
    options.setKeepAliveInterval(keepAliveInterval)
    options.setCleanSession(cleanSession)
    username.foreach(n => options.setUserName(n))
    password.foreach(p => options.setPassword(p.toCharArray))
    options.setAutomaticReconnect(true)

    sslCertFile.foreach { _ =>
      options.setSocketFactory(
        MqttSSLSocketFactory(sslCACertFile.get, sslCertFile.get, sslCertKeyFile.get, "")
      )
    }

    options
  }
}
{'fraction_non_alphanumeric': 0.07668670674682698, 'fraction_numerical': 0.0033400133600534404, 'mean_word_length': 2.136154168412233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5331625', 'n_tokens_mistral': 1958, 'n_tokens_neox': 1876, 'n_words': 408}