Github user takezoe commented on a diff in the pull request:

    
https://github.com/apache/incubator-predictionio/pull/372#discussion_r112356702
  
    --- Diff: 
storage/elasticsearch/src/main/scala/org/apache/predictionio/data/storage/elasticsearch/StorageClient.scala
 ---
    @@ -18,27 +18,66 @@
     package org.apache.predictionio.data.storage.elasticsearch
     
     import org.apache.http.HttpHost
    +import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
    +import org.apache.http.impl.client.BasicCredentialsProvider
    +import org.apache.http.impl.nio.client.HttpAsyncClientBuilder
     import org.apache.predictionio.data.storage.BaseStorageClient
     import org.apache.predictionio.data.storage.StorageClientConfig
     import org.apache.predictionio.data.storage.StorageClientException
     import org.elasticsearch.client.RestClient
    +import org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback
     
     import grizzled.slf4j.Logging
     
    -case class ESClient(hosts: Seq[HttpHost]) {
    +case class ESClient(
    +    hosts: Seq[HttpHost],
    +    basicAuth: Option[(String, String)] = None) {
    +
       def open(): RestClient = {
         try {
    -      RestClient.builder(hosts: _*).build()
    +      var builder = RestClient.builder(hosts: _*)
    +      builder = basicAuth match {
    +        case Some((username, password)) => 
builder.setHttpClientConfigCallback(
    +          new BasicAuthProvider(username, password))
    +        case None                       => builder}
    +      builder.build()
         } catch {
           case e: Throwable =>
             throw new StorageClientException(e.getMessage, e)
         }
       }
     }
     
    -class StorageClient(val config: StorageClientConfig) extends 
BaseStorageClient
    -    with Logging {
    +class StorageClient(val config: StorageClientConfig)
    +  extends BaseStorageClient with Logging {
    +
       override val prefix = "ES"
     
    -  val client = ESClient(ESUtils.getHttpHosts(config))
    +  val usernamePassword = (
    +    config.properties.get("USERNAME"),
    +    config.properties.get("PASSWORD"))
    +  val optionalBasicAuth: Option[(String, String)] = usernamePassword match 
{
    +    case (Some(username), Some(password)) => Some(username, password)
    +    case (Some(username), None)           => Some(username, "")
    +    case (None, Some(password))           => Some("", password)
    +    case (None, None)                     => None}
    --- End diff --
    
    This pattern match can be simpler like:
    
    ```scala
    usernamePassword match {
      case (None, None)         => None
      case (username, password) => Some((username.getOrElse(""), 
password.getOrElse("")))
    }
    ```
    
    and `Some(username, password)` causes auto tuppling. It's warned by Scala 
compiler to avoid unintended conversion to arguments to tuple.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to