Skip to content

Commit

Permalink
Split common module into several smaller modules (!1135)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelprinz authored and MarcelKonrad committed Dec 10, 2024
1 parent 1475a91 commit 79393a2
Show file tree
Hide file tree
Showing 95 changed files with 309 additions and 188 deletions.
22 changes: 22 additions & 0 deletions common/datatypes/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// JVM Test Suite is still incubating, but expected to be stable soon, so disabling the warning.
@file:Suppress("UnstableApiUsage")

plugins {
id("org.orkg.gradle.kotlin-library")
}

dependencies {
api("org.eclipse.rdf4j:rdf4j-common-io") // for RFC 3987 compliant IRIs
implementation(project(":common:string-utils"))
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
dependencies {
implementation("io.kotest:kotest-assertions-shared")
implementation("org.junit.jupiter:junit-jupiter-params")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package org.orkg.common

import java.io.IOException
import java.math.BigInteger
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.security.MessageDigest
import java.time.Duration
import java.time.MonthDay
import java.time.YearMonth
import java.time.chrono.IsoChronology
Expand All @@ -18,29 +11,6 @@ import java.time.temporal.ChronoField
import java.util.*
import java.util.regex.Pattern
import org.eclipse.rdf4j.common.net.ParsedIRI
import org.orkg.common.exceptions.ServiceUnavailable
import org.springframework.http.CacheControl
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity

fun String.toSnakeCase(): String =
if (this.isEmpty()) this else StringBuilder().also {
this.forEach { c ->
when (c) {
in 'A'..'Z' -> {
it.append("_")
it.append(c.lowercase())
}

else -> {
it.append(c)
}
}
}
}.toString()

fun <T> T.withCacheControl(duration: Duration): ResponseEntity<T> =
ResponseEntity.ok().cacheControl(CacheControl.maxAge(duration)).body(this)

/**
* Checks whether the given string is a [RFC 3987](https://www.ietf.org/rfc/rfc3987.txt) compliant IRI.
Expand Down Expand Up @@ -284,19 +254,6 @@ fun String.isValidDateTimeStamp(): Boolean {
return true
}

fun <T> HttpClient.send(httpRequest: HttpRequest, serviceName: String, successCallback: (String) -> T): T? {
try {
val response = send(httpRequest, HttpResponse.BodyHandlers.ofString())
return when (response.statusCode()) {
HttpStatus.OK.value() -> successCallback(response.body())
HttpStatus.NOT_FOUND.value() -> null
else -> throw ServiceUnavailable.create(serviceName, response.statusCode(), response.body())
}
} catch (e: IOException) {
throw ServiceUnavailable.create(serviceName, e)
}
}

fun <T : Any> mutableSetOfNotNull(vararg elements: T?): MutableSet<T> {
val result = mutableSetOf<T>()
elements.forEach {
Expand All @@ -319,13 +276,3 @@ fun String.isValidBoolean(): Boolean =
"true", "1", "false", "0" -> true
else -> false
}

/**
* Calculate the MD5 of a string.
*
* @return The MD5 in hexadecimal, zero-prefixed to 32 characters.
*/
val String.md5: String
get() = BigInteger(1, MessageDigest.getInstance("MD5").digest(this.toByteArray()))
.toString(16)
.padStart(32, '0')
3 changes: 3 additions & 0 deletions common/functional/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("org.orkg.gradle.kotlin-library")
}
3 changes: 3 additions & 0 deletions common/identifiers/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins {
id("org.orkg.gradle.kotlin-library")
}
26 changes: 26 additions & 0 deletions common/pagination/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
id("org.orkg.gradle.input-adapter-spring-web") // for RestDocs configuration
id("org.orkg.gradle.kotlin-library")
}

dependencies {
api("org.springframework.data:spring-data-commons")
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
dependencies {
implementation(testFixtures(project(":testing:spring")))
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.springframework.boot:spring-boot-test")
implementation("org.springframework.boot:spring-boot-test-autoconfigure")
implementation("org.springframework.restdocs:spring-restdocs-core")
implementation("org.springframework:spring-test")
implementation("org.springframework:spring-web")
implementation("org.springframework.restdocs:spring-restdocs-mockmvc")
runtimeOnly("com.jayway.jsonpath:json-path")
}
}
}
}
4 changes: 2 additions & 2 deletions common/serialization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ plugins {
}

dependencies {
api(project(":common"))

api(project(":common:identifiers"))
implementation(project(":common:spring-webmvc"))
api("com.fasterxml.jackson.core:jackson-core")
api("jakarta.validation:jakarta.validation-api")
api("org.eclipse.rdf4j:rdf4j-common-io")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ plugins {
}

dependencies {
api(project(":common:identifiers"))
implementation(project(":common:string-utils"))
api("org.neo4j.driver:neo4j-java-driver")
api("org.springframework.data:spring-data-commons")
api("org.springframework.security:spring-security-core") // for AccessDeniedException, UserDetails
api("org.springframework:spring-context")
api("org.springframework:spring-core")
api("org.springframework:spring-web")
api("org.springframework:spring-webmvc")
api("org.eclipse.rdf4j:rdf4j-common-io") // for RFC 3987 compliant IRIs
implementation("com.fasterxml.jackson.core:jackson-core")
implementation("org.apache.tomcat.embed:tomcat-embed-core") // for HttpServletRequest
implementation("org.slf4j:jcl-over-slf4j") // for org.apache.commons.logging.LogFactory in ResponseEntityExceptionHandler
Expand All @@ -26,20 +27,15 @@ testing {
suites {
val test by getting(JvmTestSuite::class) {
dependencies {
implementation(project(":common"))
implementation(project(":common:serialization"))

implementation(testFixtures(project(":testing:spring")))

implementation("io.kotest:kotest-assertions-shared")
implementation("org.hamcrest:hamcrest")
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.junit.jupiter:junit-jupiter-params")
implementation("org.springframework.boot:spring-boot-test")
implementation("org.springframework.boot:spring-boot-test-autoconfigure")
implementation("org.springframework.restdocs:spring-restdocs-core")
implementation("org.springframework:spring-test")
implementation("org.assertj:assertj-core")
implementation("org.springframework.restdocs:spring-restdocs-mockmvc")
runtimeOnly("com.jayway.jsonpath:json-path")
compileOnly("eu.michael-simons.neo4j:neo4j-migrations-spring-boot-starter")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package org.orkg.common.configuration
package org.orkg.common

import org.orkg.common.MediaTypeCapabilities
import org.orkg.common.MediaTypeCapabilityRegistry
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.core.MethodParameter
import org.springframework.core.annotation.AnnotatedElementUtils
import org.springframework.data.web.config.EnableSpringDataWebSupport
import org.springframework.data.web.config.EnableSpringDataWebSupport.PageSerializationMode.DIRECT
import org.springframework.http.MediaType
import org.springframework.util.MimeTypeUtils
import org.springframework.web.accept.ContentNegotiationManager
Expand All @@ -16,21 +10,6 @@ import org.springframework.web.bind.support.WebDataBinderFactory
import org.springframework.web.context.request.NativeWebRequest
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.method.support.ModelAndViewContainer
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
@EnableSpringDataWebSupport(pageSerializationMode = DIRECT)
class PagedSerializationConfiguration

@Configuration
@Import(MediaTypeCapabilityRegistry::class)
class WebMvcConfiguration(
private val mediaTypeCapabilityRegistry: MediaTypeCapabilityRegistry,
) : WebMvcConfigurer {
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) {
resolvers.add(MediaTypeCapabilitiesResolver(mediaTypeCapabilityRegistry))
}
}

class MediaTypeCapabilitiesResolver(
private val mediaTypeCapabilityRegistry: MediaTypeCapabilityRegistry,
Expand Down Expand Up @@ -67,8 +46,8 @@ class MediaTypeCapabilitiesResolver(
}
MimeTypeUtils.sortBySpecificity(mediaTypesToUse)
return mediaTypesToUse.firstOrNull { it.isConcrete }
?.let { MediaTypeCapabilities.parse(it, mediaTypeCapabilityRegistry.getSupportedCapabilities(it)) }
?: MediaTypeCapabilities.EMPTY
?.let { MediaTypeCapabilities.Companion.parse(it, mediaTypeCapabilityRegistry.getSupportedCapabilities(it)) }
?: MediaTypeCapabilities.Companion.EMPTY
}

private fun getMostSpecificMediaType(acceptType: MediaType, produceType: MediaType): MediaType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.orkg.common

import java.io.IOException
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Duration
import org.orkg.common.exceptions.ServiceUnavailable
import org.springframework.http.CacheControl
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity

fun <T> T.withCacheControl(duration: Duration): ResponseEntity<T> =
ResponseEntity.ok().cacheControl(CacheControl.maxAge(duration)).body(this)

fun <T> HttpClient.send(httpRequest: HttpRequest, serviceName: String, successCallback: (String) -> T): T? {
try {
val response = send(httpRequest, HttpResponse.BodyHandlers.ofString())
return when (response.statusCode()) {
HttpStatus.OK.value() -> successCallback(response.body())
HttpStatus.NOT_FOUND.value() -> null
else -> throw ServiceUnavailable.create(serviceName, response.statusCode(), response.body())
}
} catch (e: IOException) {
throw ServiceUnavailable.create(serviceName, e)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.orkg.common.configuration

import org.springframework.context.annotation.Configuration
import org.springframework.data.web.config.EnableSpringDataWebSupport

@Configuration
@EnableSpringDataWebSupport(pageSerializationMode = EnableSpringDataWebSupport.PageSerializationMode.DIRECT)
class PagedSerializationConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.orkg.common.configuration

import org.orkg.common.MediaTypeCapabilitiesResolver
import org.orkg.common.MediaTypeCapabilityRegistry
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer

@Configuration
@Import(MediaTypeCapabilityRegistry::class)
class WebMvcConfiguration(
private val mediaTypeCapabilityRegistry: MediaTypeCapabilityRegistry,
) : WebMvcConfigurer {
override fun addArgumentResolvers(resolvers: MutableList<HandlerMethodArgumentResolver>) {
resolvers.add(MediaTypeCapabilitiesResolver(mediaTypeCapabilityRegistry))
}
}
27 changes: 0 additions & 27 deletions common/src/test/kotlin/org/orkg/common/TimeEqualsContractTest.kt

This file was deleted.

17 changes: 17 additions & 0 deletions common/string-utils/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// JVM Test Suite is still incubating, but expected to be stable soon, so disabling the warning.
@file:Suppress("UnstableApiUsage")

plugins {
id("org.orkg.gradle.kotlin-library")
}

testing {
suites {
val test by getting(JvmTestSuite::class) {
dependencies {
implementation("org.junit.jupiter:junit-jupiter-api")
implementation("org.assertj:assertj-core")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.orkg.common

import java.math.BigInteger
import java.security.MessageDigest

fun String.toSnakeCase(): String =
if (this.isEmpty()) this else StringBuilder().also {
this.forEach { c ->
when (c) {
in 'A'..'Z' -> {
it.append("_")
it.append(c.lowercase())
}

else -> {
it.append(c)
}
}
}
}.toString()

/**
* Calculate the MD5 of a string.
*
* @return The MD5 in hexadecimal, zero-prefixed to 32 characters.
*/
val String.md5: String
get() = BigInteger(1, MessageDigest.getInstance("MD5").digest(this.toByteArray()))
.toString(16)
.padStart(32, '0')
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {
api("com.fasterxml.jackson.core:jackson-databind")
api("org.keycloak:keycloak-admin-client")
api(project(":community:community-ports-output"))
implementation(project(":common"))
implementation(project(":common:identifiers"))
implementation(project(":community:community-core-model"))
implementation("org.springframework:spring-web")
implementation("com.fasterxml.jackson.core:jackson-core")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// JVM Test Suite is still incubating, but expected to be stable soon, so disabling the warning.
@file:Suppress("UnstableApiUsage")

plugins {
id("org.orkg.gradle.input-adapter-spring-web")
id("org.orkg.gradle.kotlin-library")
Expand All @@ -10,7 +7,8 @@ dependencies {
api("com.fasterxml.jackson.core:jackson-annotations")
api("org.springframework:spring-web")
api("jakarta.validation:jakarta.validation-api")
api(project(":common"))
api(project(":common:identifiers"))
api(project(":common:spring-webmvc"))
api(project(":community:community-core-model"))
api(project(":community:community-ports-input-legacy"))
}
Loading

0 comments on commit 79393a2

Please sign in to comment.