Move all file in fr.dcproject.
This commit is contained in:
6
src/main/kotlin/fr/dcproject/common/utils/DateTime.kt
Normal file
6
src/main/kotlin/fr/dcproject/common/utils/DateTime.kt
Normal file
@@ -0,0 +1,6 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import org.joda.time.DateTime
|
||||
import org.joda.time.format.ISODateTimeFormat
|
||||
|
||||
fun DateTime.toIso(): String = ISODateTimeFormat.dateTime().print(this)
|
||||
29
src/main/kotlin/fr/dcproject/common/utils/Elastic.kt
Normal file
29
src/main/kotlin/fr/dcproject/common/utils/Elastic.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import com.jayway.jsonpath.JsonPath
|
||||
import com.jayway.jsonpath.PathNotFoundException
|
||||
import org.apache.http.util.EntityUtils
|
||||
import org.elasticsearch.client.Response
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
fun Response.contentToString(): String {
|
||||
return EntityUtils.toString(this.entity)
|
||||
}
|
||||
|
||||
fun Response.getField(jsonPath: String): Int? {
|
||||
return try {
|
||||
JsonPath.read(this.contentToString(), jsonPath)
|
||||
} catch (e: PathNotFoundException) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun String.getJsonField(jsonPath: String): Int? {
|
||||
return try {
|
||||
JsonPath.read(this, jsonPath)
|
||||
} catch (e: PathNotFoundException) {
|
||||
LoggerFactory.getLogger("fr.dcproject.utils.getJsonField")
|
||||
.warn("No value for Json path ${JsonPath.compile(jsonPath).path}")
|
||||
null
|
||||
}
|
||||
}
|
||||
10
src/main/kotlin/fr/dcproject/common/utils/LoggerDelegate.kt
Normal file
10
src/main/kotlin/fr/dcproject/common/utils/LoggerDelegate.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import kotlin.properties.ReadOnlyProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
internal class LoggerDelegate<in R : Any> : ReadOnlyProperty<R, Logger> {
|
||||
override fun getValue(thisRef: R, property: KProperty<*>): Logger = LoggerFactory.getLogger(thisRef.javaClass.packageName)
|
||||
}
|
||||
23
src/main/kotlin/fr/dcproject/common/utils/Request.kt
Normal file
23
src/main/kotlin/fr/dcproject/common/utils/Request.kt
Normal file
@@ -0,0 +1,23 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.application.log
|
||||
import io.ktor.features.BadRequestException
|
||||
import io.ktor.request.receive
|
||||
import kotlin.reflect.typeOf
|
||||
|
||||
/**
|
||||
* Receives content for this request.
|
||||
* @param type instance of `KClass` specifying type to be received.
|
||||
* @return instance of [T] received from this call, or `null` if content cannot be transformed to the requested type..
|
||||
*/
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
public suspend inline fun <reified T : Any> ApplicationCall.receiveOrBadRequest(message: String = "Bad Request, wrong body request"): T {
|
||||
return try {
|
||||
receive<T>(typeOf<T>())
|
||||
} catch (cause: MissingKotlinParameterException) {
|
||||
application.log.debug("Conversion failed, throw bad exeption", cause)
|
||||
throw BadRequestException(message, cause)
|
||||
}
|
||||
}
|
||||
7
src/main/kotlin/fr/dcproject/common/utils/Resources.kt
Normal file
7
src/main/kotlin/fr/dcproject/common/utils/Resources.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
fun String.readResource(callback: (String) -> Unit = {}): String {
|
||||
val content = callback::class.java.getResource(this).readText()
|
||||
callback(content)
|
||||
return content
|
||||
}
|
||||
11
src/main/kotlin/fr/dcproject/common/utils/Uuid.kt
Normal file
11
src/main/kotlin/fr/dcproject/common/utils/Uuid.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
fun String.toUUID(): UUID = UUID.fromString(this.trim())
|
||||
|
||||
fun List<String?>.toUUID(): List<UUID> = this
|
||||
.filterNotNull()
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
.map { UUID.fromString(it) }
|
||||
28
src/main/kotlin/fr/dcproject/common/utils/waitElastic.kt
Normal file
28
src/main/kotlin/fr/dcproject/common/utils/waitElastic.kt
Normal file
@@ -0,0 +1,28 @@
|
||||
package fr.dcproject.common.utils
|
||||
|
||||
import org.elasticsearch.client.Request
|
||||
import org.elasticsearch.client.RestClient
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
fun RestClient.waitElasticsearchIsUp() {
|
||||
val logger: Logger = LoggerFactory.getLogger("fr.dcproject.elasticsearch")
|
||||
val request = Request("GET", "/_cluster/health")
|
||||
repeat(5 * 60 / 2) { // 5 minutes
|
||||
runCatching {
|
||||
performRequest(request).statusLine.statusCode
|
||||
}.onSuccess {
|
||||
if (it == 200) {
|
||||
logger.debug("Elasticsearch is Ready! Continue...")
|
||||
return
|
||||
} else {
|
||||
logger.debug("sleep 2s and retry...")
|
||||
Thread.sleep(2000)
|
||||
}
|
||||
}.onFailure {
|
||||
logger.debug("${it.message}, sleep 2s and retry...")
|
||||
Thread.sleep(2000)
|
||||
}
|
||||
}
|
||||
error("Elasticsearch is not ready")
|
||||
}
|
||||
Reference in New Issue
Block a user