ElasticsearchClient.scala 5.2 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
package modules

26
27
import com.typesafe.config.{Config, ConfigValue, ConfigValueFactory}

28
29
30
import javax.inject.{Inject, Singleton}
import org.apache.http.{Header, HttpHost}
import org.apache.http.message.BasicHeader
31
import org.elasticsearch.client.{RestClient, RestHighLevelClient}
32
import org.swissbib.memobase.oai.common.util.{OAIMetadataPrefix, OAISet, OaiCommonConfig, OaiConfig, OaiConfigMetadataPrefixes, OaiConfigSets, OaiIdentifyConfig}
33
34
import play.Environment
import play.api.inject.ApplicationLifecycle
35
36

import java.util
37
38
39
40
41
42
43
import scala.jdk.CollectionConverters._
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.Future

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

52
  override val index: String = getEnv("ELASTICSEARCH_INDEX")
53

54
  //val client: Option[RestHighLevelClient] = connect()
55
  override val client: Option[RestHighLevelClient] = connect()
56
57
58

  //val oai = loadOaiConfig()

59
60
61
  override def listLength: Int = getEnv("RESPONSE_LISTLENGTH").toIntOption.getOrElse(30)

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

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  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
87
88
89
90
91
92
93
94
95
96
97
98
    val oaiSets = config.getObjectList("oaiconfigs.sets").asScala.toSeq.map(
      o => {
        OAISet(
        spec = o.get("spec").unwrapped().toString,
        name = o.get("name").unwrapped().toString,
        includedSets = Option(o.getOrDefault("includedsets",null))
          .map(_.unwrapped().asInstanceOf[util.ArrayList[String]].asScala.toList),
        field = Option(o.getOrDefault("field",null)).map(_.unwrapped().toString),
        fieldValue = Option(o.getOrDefault("value",null)).map(_.unwrapped().toString)
        )
      }
    )
99
    val oaiConfigPrefixes = config.getObjectList("oaiconfigs.metadataPrefix").asScala.toSeq
100

101
102
103
104
105
106
107
108
109
    val oaiPrefixes = config.getObjectList("oaiconfigs.metadataPrefix").asScala.toSeq.map(
      o =>
        OAIMetadataPrefix (metadataPrefix = o.get("metadataPrefix").unwrapped().toString,
          namespace = o.get("metadataNamespace").unwrapped().toString,
          schema = o.get("schema").unwrapped().toString)
    )



110
111
112
    OaiConfig(
      commonConfig,
      identifyConfig,
113
114
      oaiSets.toList,
      oaiPrefixes.toList )
115
116
117
118
119

  }

  private def connect(): Option[RestHighLevelClient] = {
    val hosts = new ArrayBuffer[HttpHost]
120
121
122
    val configuredHosts = getEnv("ELASTICSEARCH_HOSTS").split(";")
    val configuredPort = getEnv("ELASTICSEARCH_PORT").toIntOption.getOrElse(8080)
    configuredHosts.foreach(
123
      value => {
124
        hosts += new HttpHost(value, configuredPort)
125
126
      }
    )
127
128
    val headers = Array(new BasicHeader("cluster.name", getEnv("ELASTICSEARCH_CLUSTER"))
      .asInstanceOf[Header])
129
130
131
    Option(new RestHighLevelClient(RestClient.builder(hosts.toArray : _*).setDefaultHeaders(headers)))
  }

132
133
134
135
136
137
138
139
140
141
142
  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")
      }
    }
  }
143
144
145


}