ElasticsearchClient.scala 3.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package modules

import java.io.IOException
import java.util

import com.typesafe.config.{Config, ConfigObject}
import javax.inject.{Inject, Singleton}
import org.apache.http.{Header, HttpHost}
import org.apache.http.message.BasicHeader
import org.elasticsearch.action.admin.cluster.storedscripts.PutStoredScriptRequest
import org.elasticsearch.client.{RequestOptions, RestClient, RestHighLevelClient}
import org.elasticsearch.common.bytes.BytesReference
import org.elasticsearch.common.xcontent.{XContentFactory, XContentType}
import org.swissbib.memobase.oai.common.util.{OaiCommonConfig, OaiConfig, OaiConfigMetadataPrefixes, OaiConfigSets, OaiIdentifyConfig}
import play.Environment
import play.api.inject.ApplicationLifecycle
import utilities.FileUtil
import scala.jdk.CollectionConverters._

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future

@Singleton
class ElasticsearchClient @Inject()(
                              lifecycle: ApplicationLifecycle,
26
27
                              val config: Config,
                              val env: Environment
28
29
30
31
32
33
                            ) extends ElasticsearchComponent {
  lifecycle.addStopHook(() => {
    //Future.successful(client.get.close())
    Future.successful(client.getOrElse(Option.empty))
  })

34
35
  override val index: String = config.getString("elasticsearch.index")

36
  //val client: Option[RestHighLevelClient] = connect()
37
  override val client: Option[RestHighLevelClient] = connect()
38
39
40

  //val oai = loadOaiConfig()

41

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  override val oaiConfig: OaiConfig = loadOaiConfig()
  //uploadTemplates()

  private def loadOaiConfig(): OaiConfig = {

    val sI = config.getObject("oaiconfigs.identify").toConfig
          //todo: make this as implicit Conversion
    val identifyConfig = OaiIdentifyConfig(
          earliestDatestamp = sI.getString("earliestDatestamp"),
          deletedRecord = sI.getString("deletedRecord"),
          adminEmail = sI.getString("adminEmail"),
          repositoryName = sI.getString("repositoryName"),
          baseUrl = sI.getString("baseURL"),
          granularity = sI.getString("granularity"),
          protocolVersion = sI.getString("protocolVersion"))

    val sC = config.getObject("oaiconfigs.common").toConfig
    val commonConfig = OaiCommonConfig(
      xsi_schemaLocation = sC.getString("xsi_schemaLocation")
    )


    //collection conversions
    //https://stackoverflow.com/questions/8301947/what-is-the-difference-between-javaconverters-and-javaconversions-in-scala
66
67
    val oaiConfigSet = config.getObjectList("oaiconfigs.sets").asScala.toSeq
    val oaiConfigPrefixes = config.getObjectList("oaiconfigs.metadataPrefix").asScala.toSeq
68
69
70
71
72
73
74
75
76
77
78

    OaiConfig(
      commonConfig,
      identifyConfig,
      OaiConfigSets(oaiConfigSet),
      OaiConfigMetadataPrefixes(oaiConfigPrefixes) )

  }

  private def connect(): Option[RestHighLevelClient] = {
    val hosts = new ArrayBuffer[HttpHost]
79
    config.getStringList("elasticsearch.hosts").forEach(
80
81
82
83
84
      value => {
        val hostPort = value.split(":")
        hosts += new HttpHost(hostPort(0), hostPort(1).toInt)
      }
    )
85
    val headers = Array(new BasicHeader("cluster.name", config.getString("elasticsearch.cluster")).asInstanceOf[Header])
86
87
88
89
90
91
    Option(new RestHighLevelClient(RestClient.builder(hosts.toArray : _*).setDefaultHeaders(headers)))
  }



}