ElasticsearchClient.scala 4.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * generic OAI Server - agnostic in relation to the data repository
 * initially created for memobase project
 *
 * Copyright (C) 2021  UB Basel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

22
23


24
25
26
package modules

import com.typesafe.config.Config
27
28
29
import javax.inject.{Inject, Singleton}
import org.apache.http.{Header, HttpHost}
import org.apache.http.message.BasicHeader
30
import org.elasticsearch.client.{RestClient, RestHighLevelClient}
31
32
33
34
35
36
37
38
39
40
import org.swissbib.memobase.oai.common.util.{OaiCommonConfig, OaiConfig, OaiConfigMetadataPrefixes, OaiConfigSets, OaiIdentifyConfig}
import play.Environment
import play.api.inject.ApplicationLifecycle
import scala.jdk.CollectionConverters._
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future

@Singleton
class ElasticsearchClient @Inject()(
                              lifecycle: ApplicationLifecycle,
41
42
                              val config: Config,
                              val env: Environment
43
44
45
46
47
48
                            ) extends ElasticsearchComponent {
  lifecycle.addStopHook(() => {
    //Future.successful(client.get.close())
    Future.successful(client.getOrElse(Option.empty))
  })

49
  override val index: String = getEnv("ELASTICSEARCH_INDEX")
50

51
  //val client: Option[RestHighLevelClient] = connect()
52
  override val client: Option[RestHighLevelClient] = connect()
53
54
55

  //val oai = loadOaiConfig()

56
57
58
  override def listLength: Int = getEnv("RESPONSE_LISTLENGTH").toIntOption.getOrElse(30)

  override def resumptionTokenTTL: Long = getEnv("RESUMPTION_TOKEN_TTL").toLongOption.getOrElse(3)
59

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  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
84
85
    val oaiConfigSet = config.getObjectList("oaiconfigs.sets").asScala.toSeq
    val oaiConfigPrefixes = config.getObjectList("oaiconfigs.metadataPrefix").asScala.toSeq
86
87
88
89
90
91
92
93
94
95
96

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

  }

  private def connect(): Option[RestHighLevelClient] = {
    val hosts = new ArrayBuffer[HttpHost]
97
98
99
    val configuredHosts = getEnv("ELASTICSEARCH_HOSTS").split(";")
    val configuredPort = getEnv("ELASTICSEARCH_PORT").toIntOption.getOrElse(8080)
    configuredHosts.foreach(
100
      value => {
101
        hosts += new HttpHost(value, configuredPort)
102
103
      }
    )
104
105
    val headers = Array(new BasicHeader("cluster.name", getEnv("ELASTICSEARCH_CLUSTER"))
      .asInstanceOf[Header])
106
107
108
    Option(new RestHighLevelClient(RestClient.builder(hosts.toArray : _*).setDefaultHeaders(headers)))
  }

109
110
111
112
113
114
115
116
117
118
119
  private def getEnv(value: String): String = {
    val envValue: Option[String] = sys.env.get(value)
    if (envValue.isDefined) {
      envValue.get
    } else {
      Option(System.getProperties.get(value)) match {
        case Some(value) => value.toString
        case None => throw new Exception("Environment variable " + value + " not available")
      }
    }
  }
120
121
122


}