From 027dce8ede3a788f5dbdfdc46106d1b91154f326 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Wed, 10 Mar 2021 16:57:03 +0100 Subject: [PATCH 01/32] tester ut spring webflux (reactive) i norgclient --- build.gradle.kts | 2 + .../innsyn/client/norg/NorgClientImpl.kt | 41 +++++++++++-------- .../innsyn/client/norg/NorgConfig.kt | 22 ++++++++++ .../innsyn/client/norg/NorgClientImplTest.kt | 30 +++++++------- 4 files changed, 63 insertions(+), 32 deletions(-) create mode 100644 src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt diff --git a/build.gradle.kts b/build.gradle.kts index 8d3dd55e3..461e05fdb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -92,6 +92,8 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-data-redis:${Versions.springBoot}") + implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") + // Sosialhjelp-common implementation("no.nav.sosialhjelp:sosialhjelp-common-selftest:${Versions.sosialhjelpCommon}") implementation("no.nav.sosialhjelp:sosialhjelp-common-api:${Versions.sosialhjelpCommon}") diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index 954fabd6a..92b0e20d5 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -1,7 +1,6 @@ package no.nav.sosialhjelp.innsyn.client.norg import no.nav.sosialhjelp.innsyn.common.NorgException -import no.nav.sosialhjelp.innsyn.config.ClientProperties import no.nav.sosialhjelp.innsyn.domain.NavEnhet import no.nav.sosialhjelp.innsyn.redis.RedisService import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils.HEADER_CALL_ID @@ -11,40 +10,42 @@ import no.nav.sosialhjelp.innsyn.utils.logger import no.nav.sosialhjelp.innsyn.utils.mdc.MDCUtils import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.springframework.context.annotation.Profile -import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders -import org.springframework.http.HttpMethod import org.springframework.stereotype.Component -import org.springframework.web.client.HttpStatusCodeException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.WebClientResponseException +import org.springframework.web.reactive.function.client.bodyToMono @Profile("!(mock | local)") @Component class NorgClientImpl( - clientProperties: ClientProperties, - private val restTemplate: RestTemplate, + private val norgWebClient: WebClient, private val redisService: RedisService, ) : NorgClient { - private val baseUrl = clientProperties.norgEndpointUrl - override fun hentNavEnhet(enhetsnr: String): NavEnhet { return hentFraCache(enhetsnr) ?: hentFraNorg(enhetsnr) } private fun hentFraNorg(enhetsnr: String): NavEnhet { - val headers = headers() try { log.debug("Forsøker å hente NAV-enhet $enhetsnr fra NORG2") - val urlTemplate = "$baseUrl/enhet/{enhetsnr}" - val response = restTemplate.exchange(urlTemplate, HttpMethod.GET, HttpEntity(headers), String::class.java, enhetsnr) + + val navEnhet: NavEnhet? = norgWebClient + .get() + .uri("/enhet/{enhetsnr}", enhetsnr) + .headers { it.addAll(headers()) } + .retrieve() + .bodyToMono() + .block() log.info("Hentet NAV-enhet $enhetsnr fra NORG2") - return objectMapper.readValue(response.body!!, NavEnhet::class.java) + + return navEnhet!! .also { lagreTilCache(enhetsnr, it) } - } catch (e: HttpStatusCodeException) { + } catch (e: WebClientResponseException) { log.warn("Noe feilet ved kall mot NORG2 - ${e.statusCode} ${e.statusText}", e) throw NorgException(e.message, e) } catch (e: Exception) { @@ -58,10 +59,16 @@ class NorgClientImpl( override fun ping() { try { - val headers = headers() // samme kall som selftest i soknad-api - restTemplate.exchange("$baseUrl/kodeverk/EnhetstyperNorg", HttpMethod.GET, HttpEntity(headers), String::class.java) - } catch (e: HttpStatusCodeException) { + norgWebClient + .get() + .uri("/kodeverk/EnhetstyperNorg") + .headers { headers() } + .retrieve() + .bodyToMono() + .block() + + } catch (e: WebClientResponseException) { log.warn("Selftest - noe feilet ved kall mot NORG2 - ${e.statusCode} ${e.statusText}", e) throw NorgException(e.message, e) } catch (e: Exception) { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt new file mode 100644 index 000000000..287352ee2 --- /dev/null +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt @@ -0,0 +1,22 @@ +package no.nav.sosialhjelp.innsyn.client.norg + +import no.nav.sosialhjelp.innsyn.config.ClientProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.http.client.HttpClient + +@Configuration +class NorgConfig( + private val clientProperties: ClientProperties, + private val webClientBuilder: WebClient.Builder +) { + + @Bean + fun norgWebClient(): WebClient = + webClientBuilder + .baseUrl(clientProperties.norgEndpointUrl) + .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .build() +} \ No newline at end of file diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt index fd719fa21..4b142e00f 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt @@ -7,7 +7,6 @@ import io.mockk.every import io.mockk.just import io.mockk.mockk import io.mockk.verify -import no.nav.sosialhjelp.innsyn.config.ClientProperties import no.nav.sosialhjelp.innsyn.domain.NavEnhet import no.nav.sosialhjelp.innsyn.redis.RedisService import no.nav.sosialhjelp.innsyn.responses.ok_navenhet @@ -15,15 +14,16 @@ import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.springframework.http.ResponseEntity -import org.springframework.web.client.RestTemplate +import org.springframework.http.HttpHeaders +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.bodyToMono +import java.util.function.Consumer internal class NorgClientImplTest { - private val clientProperties: ClientProperties = mockk(relaxed = true) - private val restTemplate: RestTemplate = mockk() + private val webClient: WebClient = mockk() private val redisService: RedisService = mockk() - private val norgClient = NorgClientImpl(clientProperties, restTemplate, redisService) + private val norgClient = NorgClientImpl(webClient, redisService) private val enhetsnr = "8888" @@ -50,17 +50,17 @@ internal class NorgClientImplTest { @Test fun `skal hente fra Norg og lagre til cache hvis cache er tom`() { - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_navenhet + val navEnhet = objectMapper.readValue(ok_navenhet) every { redisService.get(any(), NavEnhet::class.java) } returns null every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - enhetsnr) - } returns mockResponse + webClient + .get() + .uri("/enhet/{enhetsnr}", enhetsnr) + .headers(any>()) + .retrieve() + .bodyToMono() + .block() + } returns navEnhet val result2 = norgClient.hentNavEnhet(enhetsnr) From b3c4d8bf3579a6559f6c63a79ca392634b8c5c67 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 08:37:55 +0100 Subject: [PATCH 02/32] trenger ikke "it.addAll(headers)" --- .../no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index 92b0e20d5..5baf752f0 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -35,7 +35,7 @@ class NorgClientImpl( val navEnhet: NavEnhet? = norgWebClient .get() .uri("/enhet/{enhetsnr}", enhetsnr) - .headers { it.addAll(headers()) } + .headers { headers() } .retrieve() .bodyToMono() .block() From cf68be3b180739a2addbc718f05d54890dbd7c54 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 09:51:06 +0100 Subject: [PATCH 03/32] accept json --- .../no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index 5baf752f0..10c438f10 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -11,6 +11,7 @@ import no.nav.sosialhjelp.innsyn.utils.mdc.MDCUtils import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.springframework.context.annotation.Profile import org.springframework.http.HttpHeaders +import org.springframework.http.MediaType.APPLICATION_JSON import org.springframework.stereotype.Component import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.WebClientResponseException @@ -79,6 +80,7 @@ class NorgClientImpl( private fun headers(): HttpHeaders { val headers = forwardHeaders() + headers.accept = listOf(APPLICATION_JSON) headers.set(HEADER_CALL_ID, MDCUtils.get(MDCUtils.CALL_ID)) headers.set(HEADER_NAV_APIKEY, System.getenv(NORG2_APIKEY)) return headers From 546026b684de3728290acb039a8183ff2869cd87 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 11:14:45 +0100 Subject: [PATCH 04/32] bruker jetty --- build.gradle.kts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 461e05fdb..b7e4b7899 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -92,7 +92,9 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-data-redis:${Versions.springBoot}") - implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") + implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") { + exclude("org.springframework", "spring-boot-starter-reactor-netty") + } // Sosialhjelp-common implementation("no.nav.sosialhjelp:sosialhjelp-common-selftest:${Versions.sosialhjelpCommon}") From 521bd24f3d41ded3ec24031384132aa52f03a8fe Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 12:03:43 +0100 Subject: [PATCH 05/32] formatting --- .../innsyn/client/norg/NorgClientImpl.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index 10c438f10..ecdb126b1 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -21,8 +21,8 @@ import org.springframework.web.reactive.function.client.bodyToMono @Profile("!(mock | local)") @Component class NorgClientImpl( - private val norgWebClient: WebClient, - private val redisService: RedisService, + private val norgWebClient: WebClient, + private val redisService: RedisService, ) : NorgClient { override fun hentNavEnhet(enhetsnr: String): NavEnhet { @@ -36,7 +36,7 @@ class NorgClientImpl( val navEnhet: NavEnhet? = norgWebClient .get() .uri("/enhet/{enhetsnr}", enhetsnr) - .headers { headers() } + .headers { it.addAll(headers()) } .retrieve() .bodyToMono() .block() @@ -44,7 +44,7 @@ class NorgClientImpl( log.info("Hentet NAV-enhet $enhetsnr fra NORG2") return navEnhet!! - .also { lagreTilCache(enhetsnr, it) } + .also { lagreTilCache(enhetsnr, it) } } catch (e: WebClientResponseException) { log.warn("Noe feilet ved kall mot NORG2 - ${e.statusCode} ${e.statusText}", e) @@ -56,7 +56,7 @@ class NorgClientImpl( } private fun hentFraCache(enhetsnr: String): NavEnhet? = - redisService.get(cacheKey(enhetsnr), NavEnhet::class.java) as NavEnhet? + redisService.get(cacheKey(enhetsnr), NavEnhet::class.java) as NavEnhet? override fun ping() { try { @@ -87,7 +87,9 @@ class NorgClientImpl( } private fun lagreTilCache(enhetsnr: String, navEnhet: NavEnhet) { - redisService.put(cacheKey(enhetsnr), objectMapper.writeValueAsBytes(navEnhet), NAVENHET_CACHE_TIMETOLIVE_SECONDS) + redisService.put(cacheKey(enhetsnr), + objectMapper.writeValueAsBytes(navEnhet), + NAVENHET_CACHE_TIMETOLIVE_SECONDS) } private fun cacheKey(enhetsnr: String): String = "NavEnhet_$enhetsnr" From a5f242c979c53d632d649273a2b7f1331535a8a6 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 12:30:57 +0100 Subject: [PATCH 06/32] spesifiser servletApi version --- build.gradle.kts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b7e4b7899..f7dd8f423 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,6 +32,7 @@ object Versions { const val jetty = "9.4.35.v20201120" const val bouncycastle = "1.67" const val unleash = "3.3.4" + const val servletApi = "3.1.0" // Test only const val junitJupiter = "5.7.0" @@ -92,9 +93,10 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-data-redis:${Versions.springBoot}") - implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") { - exclude("org.springframework", "spring-boot-starter-reactor-netty") - } + implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") + + // servlet-api (override version + implementation("javax.servlet:javax.servlet-api:${Versions.servletApi}") // Sosialhjelp-common implementation("no.nav.sosialhjelp:sosialhjelp-common-selftest:${Versions.sosialhjelpCommon}") From 9764f97b162c50cb23b5cc41ed403e909f695b96 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 12:46:54 +0100 Subject: [PATCH 07/32] Revert "spesifiser servletApi version" This reverts commit a5f242c9 --- build.gradle.kts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f7dd8f423..b7e4b7899 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -32,7 +32,6 @@ object Versions { const val jetty = "9.4.35.v20201120" const val bouncycastle = "1.67" const val unleash = "3.3.4" - const val servletApi = "3.1.0" // Test only const val junitJupiter = "5.7.0" @@ -93,10 +92,9 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-data-redis:${Versions.springBoot}") - implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") - - // servlet-api (override version - implementation("javax.servlet:javax.servlet-api:${Versions.servletApi}") + implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") { + exclude("org.springframework", "spring-boot-starter-reactor-netty") + } // Sosialhjelp-common implementation("no.nav.sosialhjelp:sosialhjelp-common-selftest:${Versions.sosialhjelpCommon}") From 5c4c74b8e3ff1de5f88d359eafdf429f939e4154 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 12:47:50 +0100 Subject: [PATCH 08/32] ping fix --- .../no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index ecdb126b1..c89f2ed72 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -64,7 +64,7 @@ class NorgClientImpl( norgWebClient .get() .uri("/kodeverk/EnhetstyperNorg") - .headers { headers() } + .headers { it.addAll(headers()) } .retrieve() .bodyToMono() .block() From 0af5d9d78817aa6bdc74491cc6861f029ad9e187 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 13:55:20 +0100 Subject: [PATCH 09/32] =?UTF-8?q?errorhandling.=20fjerner=20un=C3=B8dvendi?= =?UTF-8?q?g=20ekstra=20logging=20av=20NorgException=20i=20InnsynException?= =?UTF-8?q?Handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../innsyn/client/norg/NorgClientImpl.kt | 73 ++++++++----------- .../innsyn/common/InnsynExceptionHandler.kt | 1 - .../no/nav/sosialhjelp/innsyn/utils/Utils.kt | 5 ++ .../innsyn/client/norg/NorgClientImplTest.kt | 6 +- 4 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt index c89f2ed72..12e98cd10 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImpl.kt @@ -9,12 +9,13 @@ import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils.forwardHeaders import no.nav.sosialhjelp.innsyn.utils.logger import no.nav.sosialhjelp.innsyn.utils.mdc.MDCUtils import no.nav.sosialhjelp.innsyn.utils.objectMapper +import no.nav.sosialhjelp.innsyn.utils.withStatusCode import org.springframework.context.annotation.Profile import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus import org.springframework.http.MediaType.APPLICATION_JSON import org.springframework.stereotype.Component import org.springframework.web.reactive.function.client.WebClient -import org.springframework.web.reactive.function.client.WebClientResponseException import org.springframework.web.reactive.function.client.bodyToMono @@ -30,52 +31,42 @@ class NorgClientImpl( } private fun hentFraNorg(enhetsnr: String): NavEnhet { - try { - log.debug("Forsøker å hente NAV-enhet $enhetsnr fra NORG2") - - val navEnhet: NavEnhet? = norgWebClient - .get() - .uri("/enhet/{enhetsnr}", enhetsnr) - .headers { it.addAll(headers()) } - .retrieve() - .bodyToMono() - .block() - - log.info("Hentet NAV-enhet $enhetsnr fra NORG2") - - return navEnhet!! - .also { lagreTilCache(enhetsnr, it) } - - } catch (e: WebClientResponseException) { - log.warn("Noe feilet ved kall mot NORG2 - ${e.statusCode} ${e.statusText}", e) - throw NorgException(e.message, e) - } catch (e: Exception) { - log.warn("Noe feilet ved kall mot NORG2", e) - throw NorgException(e.message, e) - } + log.debug("Forsøker å hente NAV-enhet $enhetsnr fra NORG2") + + val navEnhet: NavEnhet? = norgWebClient.get() + .uri("/enhet/{enhetsnr}", enhetsnr) + .headers { it.addAll(headers()) } + .retrieve() + .onStatus(HttpStatus::isError) { it.createException() } + .bodyToMono() + .onErrorMap { + log.warn("Noe feilet ved kall mot NORG2 ${withStatusCode(it)}", it) + NorgException(it.message, it) + } + .block() + + log.info("Hentet NAV-enhet $enhetsnr fra NORG2") + + return navEnhet!! + .also { lagreTilCache(enhetsnr, it) } } private fun hentFraCache(enhetsnr: String): NavEnhet? = redisService.get(cacheKey(enhetsnr), NavEnhet::class.java) as NavEnhet? + // samme kall som selftest i soknad-api override fun ping() { - try { - // samme kall som selftest i soknad-api - norgWebClient - .get() - .uri("/kodeverk/EnhetstyperNorg") - .headers { it.addAll(headers()) } - .retrieve() - .bodyToMono() - .block() - - } catch (e: WebClientResponseException) { - log.warn("Selftest - noe feilet ved kall mot NORG2 - ${e.statusCode} ${e.statusText}", e) - throw NorgException(e.message, e) - } catch (e: Exception) { - log.warn("Selftest - noe feilet ved kall mot NORG2", e) - throw NorgException(e.message, e) - } + norgWebClient.get() + .uri("/kodeverk/EnhetstyperNorg") + .headers { it.addAll(headers()) } + .retrieve() + .onStatus(HttpStatus::isError) { it.createException() } + .bodyToMono() + .onErrorMap { + log.warn("Ping - feilet mot NORG2 ${withStatusCode(it)}", it) + NorgException(it.message, it) + } + .block() } private fun headers(): HttpHeaders { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt index ad53ff7fb..c56ec6f10 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt @@ -71,7 +71,6 @@ class InnsynExceptionHandler : ResponseEntityExceptionHandler() { @ExceptionHandler(NorgException::class) fun handleNorgError(e: NorgException): ResponseEntity { - log.error("Noe feilet ved kall til Norg", e) val error = FrontendErrorMessage(NORG_ERROR, NOE_UVENTET_FEILET) return ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt index 01bc95236..3b786b39e 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt @@ -14,7 +14,9 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import org.slf4j.MDC import org.springframework.core.ParameterizedTypeReference +import org.springframework.http.HttpStatus import org.springframework.web.client.HttpStatusCodeException +import org.springframework.web.reactive.function.client.WebClientResponseException import java.io.IOException import java.sql.Timestamp import java.time.Instant @@ -138,6 +140,9 @@ val ErrorMessage.feilmeldingUtenFnr: String? return this.message?.feilmeldingUtenFnr } +fun withStatusCode(t: Throwable): HttpStatus? = + (t as? WebClientResponseException)?.statusCode + fun runAsyncWithMDC(runnable: Runnable, executor: ExecutorService): CompletableFuture { val previous: Map = MDC.getCopyOfContextMap() return CompletableFuture.runAsync(Runnable { diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt index 4b142e00f..77311eef2 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgClientImplTest.kt @@ -14,10 +14,8 @@ import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.springframework.http.HttpHeaders import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.reactive.function.client.bodyToMono -import java.util.function.Consumer internal class NorgClientImplTest { @@ -56,9 +54,11 @@ internal class NorgClientImplTest { webClient .get() .uri("/enhet/{enhetsnr}", enhetsnr) - .headers(any>()) + .headers(any()) .retrieve() + .onStatus(any(), any()) .bodyToMono() + .onErrorMap(any()) .block() } returns navEnhet From 3fb156d37cf2b3acc2cabeab6079c5b56f121ee0 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Thu, 11 Mar 2021 14:42:47 +0100 Subject: [PATCH 10/32] asdf --- build.gradle.kts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b7e4b7899..9267858f1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -85,6 +85,7 @@ dependencies { // Spring implementation("org.springframework.boot:spring-boot-starter-web:${Versions.springBoot}") + implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-jetty:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-security:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-actuator:${Versions.springBoot}") @@ -92,10 +93,6 @@ dependencies { implementation("org.springframework.boot:spring-boot-starter-validation:${Versions.springBoot}") implementation("org.springframework.boot:spring-boot-starter-data-redis:${Versions.springBoot}") - implementation("org.springframework.boot:spring-boot-starter-webflux:${Versions.springBoot}") { - exclude("org.springframework", "spring-boot-starter-reactor-netty") - } - // Sosialhjelp-common implementation("no.nav.sosialhjelp:sosialhjelp-common-selftest:${Versions.sosialhjelpCommon}") implementation("no.nav.sosialhjelp:sosialhjelp-common-api:${Versions.sosialhjelpCommon}") From db88c68d4b7354cf51907b28893d5679701fd9fd Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 09:40:37 +0100 Subject: [PATCH 11/32] bump sosialhjelp-common (forelopig snapshot-versjon) --- build.gradle.kts | 2 +- .../innsyn/client/fiks/KommuneInfoClientConfig.kt | 6 +++--- .../innsyn/client/idporten/IdPortenClientConfig.kt | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9267858f1..a6a297ee1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ object Versions { const val kotlin = "1.4.21" const val coroutines = "1.4.2" const val springBoot = "2.4.3" - const val sosialhjelpCommon = "1.1c8e196" + const val sosialhjelpCommon = "1.6986bc0-SNAPSHOT" const val logback = "1.2.3" const val logstash = "6.5" const val filformat = "1.2021.03.02-10.58-415c44e55124" diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index 230cf418d..c0adc7f63 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -7,16 +7,16 @@ import no.nav.sosialhjelp.innsyn.config.ClientProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class KommuneInfoClientConfig { @Bean - fun kommuneInfoClient(restTemplate: RestTemplate, clientProperties: ClientProperties): KommuneInfoClient { + fun kommuneInfoClient(clientProperties: ClientProperties): KommuneInfoClient { return KommuneInfoClientImpl( - restTemplate, + WebClient.create(), toFiksProperties(clientProperties) ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 5855950be..6aaedf200 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -7,7 +7,7 @@ import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration @@ -21,9 +21,9 @@ class IdPortenClientConfig( ) { @Bean - fun idPortenClient(restTemplate: RestTemplate): IdPortenClient { + fun idPortenClient(): IdPortenClient { return IdPortenClient( - restTemplate = restTemplate, + webClient = WebClient.create(), idPortenProperties = idPortenProperties() ) } From 64a1bc0f2ca1e1bd94b8b3bd1406fc7b65b327bc Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 10:14:35 +0100 Subject: [PATCH 12/32] formatting --- .../client/fiks/KommuneInfoClientConfig.kt | 12 ++++---- .../client/idporten/IdPortenClientConfig.kt | 30 +++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index c0adc7f63..91657feea 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -16,17 +16,17 @@ class KommuneInfoClientConfig { @Bean fun kommuneInfoClient(clientProperties: ClientProperties): KommuneInfoClient { return KommuneInfoClientImpl( - WebClient.create(), - toFiksProperties(clientProperties) + WebClient.create(), + toFiksProperties(clientProperties) ) } private fun toFiksProperties(clientProperties: ClientProperties): FiksProperties { return FiksProperties( - clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_KOMMUNEINFO, - clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_ALLE_KOMMUNEINFO, - clientProperties.fiksIntegrasjonId, - clientProperties.fiksIntegrasjonpassord + clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_KOMMUNEINFO, + clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_ALLE_KOMMUNEINFO, + clientProperties.fiksIntegrasjonId, + clientProperties.fiksIntegrasjonpassord ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 6aaedf200..3bd85bc65 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -12,31 +12,31 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( - @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, - @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, - @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, - @Value("\${no.nav.sosialhjelp.idporten.config_url}") private val configUrl: String, - @Value("\${no.nav.sosialhjelp.idporten.truststore_type}") private val truststoreType: String, - @Value("\${no.nav.sosialhjelp.idporten.truststore_filepath}") private val truststoreFilepath: String + @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, + @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, + @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, + @Value("\${no.nav.sosialhjelp.idporten.config_url}") private val configUrl: String, + @Value("\${no.nav.sosialhjelp.idporten.truststore_type}") private val truststoreType: String, + @Value("\${no.nav.sosialhjelp.idporten.truststore_filepath}") private val truststoreFilepath: String, ) { @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = WebClient.create(), - idPortenProperties = idPortenProperties() + webClient = WebClient.create(), + idPortenProperties = idPortenProperties() ) } fun idPortenProperties(): IdPortenProperties { return IdPortenProperties( - tokenUrl = tokenUrl, - clientId = clientId, - scope = scope, - configUrl = configUrl, - truststoreType = truststoreType, - truststoreFilepath = truststoreFilepath, - virksomhetSertifikatPath = getenv("VIRKSERT_STI", "/var/run/secrets/nais.io/virksomhetssertifikat") + tokenUrl = tokenUrl, + clientId = clientId, + scope = scope, + configUrl = configUrl, + truststoreType = truststoreType, + truststoreFilepath = truststoreFilepath, + virksomhetSertifikatPath = getenv("VIRKSERT_STI", "/var/run/secrets/nais.io/virksomhetssertifikat") ) } } \ No newline at end of file From 99997a36ae26b96ca98e4c5180b504623ea37088 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 10:43:45 +0100 Subject: [PATCH 13/32] coroutines reactor --- build.gradle.kts | 1 + 1 file changed, 1 insertion(+) diff --git a/build.gradle.kts b/build.gradle.kts index a6a297ee1..5fcfc49c4 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -82,6 +82,7 @@ dependencies { // Coroutines implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.coroutines}") implementation("org.jetbrains.kotlinx:kotlinx-coroutines-slf4j:${Versions.coroutines}") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:${Versions.coroutines}") // Spring implementation("org.springframework.boot:spring-boot-starter-web:${Versions.springBoot}") From 04e3685f20c6001dd714f3f05316914e65c4311c Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 11:32:48 +0100 Subject: [PATCH 14/32] webclient bean --- .../innsyn/client/fiks/KommuneInfoClientConfig.kt | 6 ++++-- .../innsyn/client/idporten/IdPortenClientConfig.kt | 3 ++- .../no/nav/sosialhjelp/innsyn/config/RestConfig.kt | 13 ++++++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index 91657feea..8f0586e4b 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -11,12 +11,14 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration -class KommuneInfoClientConfig { +class KommuneInfoClientConfig( + private val webClient: WebClient +) { @Bean fun kommuneInfoClient(clientProperties: ClientProperties): KommuneInfoClient { return KommuneInfoClientImpl( - WebClient.create(), + webClient, toFiksProperties(clientProperties) ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 3bd85bc65..7127bfddc 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -12,6 +12,7 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( + private val webClient: WebClient, @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, @@ -23,7 +24,7 @@ class IdPortenClientConfig( @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = WebClient.create(), + webClient = webClient, idPortenProperties = idPortenProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt index f32a18559..04cf745cf 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt @@ -7,15 +7,26 @@ import org.springframework.boot.web.client.RestTemplateBuilder import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile +import org.springframework.http.client.reactive.ReactorClientHttpConnector import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter +import reactor.netty.http.client.HttpClient import java.nio.charset.StandardCharsets import java.time.Duration @Configuration -class RestConfig { +class RestConfig( + private val webClientBuilder: WebClient.Builder +) { + + @Bean + fun webClient(): WebClient = + webClientBuilder + .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .build() @Bean fun restTemplate(builder: RestTemplateBuilder): RestTemplate = From 49049e813fb4347e6cdfd2cb4a3c190f1e8df826 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 11:45:37 +0100 Subject: [PATCH 15/32] webclient config --- .../sosialhjelp/innsyn/config/RestConfig.kt | 13 +------------ .../innsyn/config/WebClientConfig.kt | 18 ++++++++++++++++++ .../innsyn/health/checks/StsCheck.kt | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt index 04cf745cf..f32a18559 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/RestConfig.kt @@ -7,26 +7,15 @@ import org.springframework.boot.web.client.RestTemplateBuilder import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.context.annotation.Profile -import org.springframework.http.client.reactive.ReactorClientHttpConnector import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.web.client.RestTemplate -import org.springframework.web.reactive.function.client.WebClient import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter -import reactor.netty.http.client.HttpClient import java.nio.charset.StandardCharsets import java.time.Duration @Configuration -class RestConfig( - private val webClientBuilder: WebClient.Builder -) { - - @Bean - fun webClient(): WebClient = - webClientBuilder - .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) - .build() +class RestConfig { @Bean fun restTemplate(builder: RestTemplateBuilder): RestTemplate = diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt new file mode 100644 index 000000000..e4aa19fae --- /dev/null +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt @@ -0,0 +1,18 @@ +package no.nav.sosialhjelp.innsyn.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.http.client.HttpClient + +@Configuration +class WebClientConfig( + private val webClientBuilder: WebClient.Builder, +) { + @Bean + fun webClient(): WebClient = + webClientBuilder + .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .build() +} \ No newline at end of file diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/health/checks/StsCheck.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/health/checks/StsCheck.kt index c4a581515..5371c1933 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/health/checks/StsCheck.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/health/checks/StsCheck.kt @@ -8,7 +8,7 @@ import no.nav.sosialhjelp.selftest.Importance import org.springframework.context.annotation.Profile import org.springframework.stereotype.Component -@Profile("!mock") +@Profile("!(mock | local)") @Component class StsCheck( clientProperties: ClientProperties, From ee6e4ef13166130f6f458633538340af53fbc281 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 13:17:24 +0100 Subject: [PATCH 16/32] proxy --- .../client/idporten/IdPortenClientConfig.kt | 10 ++++++++-- .../sosialhjelp/innsyn/utils/HttpClientUtil.kt | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/HttpClientUtil.kt diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 7127bfddc..29a2d2dea 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -2,6 +2,7 @@ package no.nav.sosialhjelp.innsyn.client.idporten import no.nav.sosialhjelp.idporten.client.IdPortenClient import no.nav.sosialhjelp.idporten.client.IdPortenProperties +import no.nav.sosialhjelp.innsyn.utils.getReactorClientHttpConnector import no.nav.sosialhjelp.innsyn.utils.getenv import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean @@ -12,7 +13,7 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( - private val webClient: WebClient, + private val webClientBuilder: WebClient.Builder, @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, @@ -21,10 +22,15 @@ class IdPortenClientConfig( @Value("\${no.nav.sosialhjelp.idporten.truststore_filepath}") private val truststoreFilepath: String, ) { + @Value("\${HTTPS_PROXY}") + private lateinit var proxyUrl: String + @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = webClient, + webClient = webClientBuilder + .clientConnector(getReactorClientHttpConnector(proxyUrl)) + .build(), idPortenProperties = idPortenProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/HttpClientUtil.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/HttpClientUtil.kt new file mode 100644 index 000000000..b0829bfed --- /dev/null +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/HttpClientUtil.kt @@ -0,0 +1,17 @@ +package no.nav.sosialhjelp.innsyn.utils + +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import reactor.netty.http.client.HttpClient +import reactor.netty.transport.ProxyProvider +import java.net.URL + +fun getReactorClientHttpConnector(proxyUrl: String): ReactorClientHttpConnector { + val uri = URL(proxyUrl) + + val httpClient: HttpClient = HttpClient.create() + .proxy { proxy -> + proxy.type(ProxyProvider.Proxy.HTTP).host(uri.host).port(uri.port) + } + + return ReactorClientHttpConnector(httpClient) +} \ No newline at end of file From 617141956a08d11cac2cacd144d1ff2b1866a8fb Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 13:23:46 +0100 Subject: [PATCH 17/32] mock idportenclientconfig for test --- .../no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt index b2a0b5ff9..6d83f832c 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt @@ -2,6 +2,7 @@ package no.nav.sosialhjelp.innsyn import com.ninjasquad.springmockk.MockkBean import no.nav.sosialhjelp.idporten.client.IdPortenClient +import no.nav.sosialhjelp.innsyn.client.idporten.IdPortenClientConfig import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles @@ -14,6 +15,9 @@ class ApplicationContextTest { @MockkBean private lateinit var idPortenClient: IdPortenClient + @MockkBean + private lateinit var idPortenClientConfig: IdPortenClientConfig + @MockkBean(name = "stsRestTemplate") private lateinit var stsRestTemplate: RestTemplate From f4723f062ed1dcd0dc7f77d180b713b9118151e1 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 13:58:36 +0100 Subject: [PATCH 18/32] asdf --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5fcfc49c4..39ed2d9c2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ object Versions { const val kotlin = "1.4.21" const val coroutines = "1.4.2" const val springBoot = "2.4.3" - const val sosialhjelpCommon = "1.6986bc0-SNAPSHOT" + const val sosialhjelpCommon = "1.da3e880-SNAPSHOT" const val logback = "1.2.3" const val logstash = "6.5" const val filformat = "1.2021.03.02-10.58-415c44e55124" From 979fe7cbc283f85c1bcf985ed49ec435246bb920 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Fri, 12 Mar 2021 14:56:24 +0100 Subject: [PATCH 19/32] proxiedWebClient for idportenclient og kommuneinfoclient (og fiksclient i fremtiden) --- .../client/fiks/KommuneInfoClientConfig.kt | 11 ++--- .../client/idporten/IdPortenClientConfig.kt | 10 +---- .../innsyn/config/ProxiedWebClientConfig.kt | 41 +++++++++++++++++++ .../innsyn/ApplicationContextTest.kt | 8 +++- 4 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index 8f0586e4b..780a96153 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -12,18 +12,19 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class KommuneInfoClientConfig( - private val webClient: WebClient + private val proxiedWebClient: WebClient, + private val clientProperties: ClientProperties ) { @Bean - fun kommuneInfoClient(clientProperties: ClientProperties): KommuneInfoClient { + fun kommuneInfoClient(): KommuneInfoClient { return KommuneInfoClientImpl( - webClient, - toFiksProperties(clientProperties) + proxiedWebClient, + fiksProperties() ) } - private fun toFiksProperties(clientProperties: ClientProperties): FiksProperties { + private fun fiksProperties(): FiksProperties { return FiksProperties( clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_KOMMUNEINFO, clientProperties.fiksDigisosEndpointUrl + FiksPaths.PATH_ALLE_KOMMUNEINFO, diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 29a2d2dea..3242d552f 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -2,7 +2,6 @@ package no.nav.sosialhjelp.innsyn.client.idporten import no.nav.sosialhjelp.idporten.client.IdPortenClient import no.nav.sosialhjelp.idporten.client.IdPortenProperties -import no.nav.sosialhjelp.innsyn.utils.getReactorClientHttpConnector import no.nav.sosialhjelp.innsyn.utils.getenv import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Bean @@ -13,7 +12,7 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( - private val webClientBuilder: WebClient.Builder, + private val proxiedWebClient: WebClient, @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, @@ -22,15 +21,10 @@ class IdPortenClientConfig( @Value("\${no.nav.sosialhjelp.idporten.truststore_filepath}") private val truststoreFilepath: String, ) { - @Value("\${HTTPS_PROXY}") - private lateinit var proxyUrl: String - @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = webClientBuilder - .clientConnector(getReactorClientHttpConnector(proxyUrl)) - .build(), + webClient = proxiedWebClient, idPortenProperties = idPortenProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt new file mode 100644 index 000000000..f613e1ddd --- /dev/null +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt @@ -0,0 +1,41 @@ +package no.nav.sosialhjelp.innsyn.config + +import no.nav.sosialhjelp.innsyn.utils.getReactorClientHttpConnector +import org.springframework.beans.factory.annotation.Value +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Profile +import org.springframework.http.client.reactive.ReactorClientHttpConnector +import org.springframework.web.reactive.function.client.WebClient +import reactor.netty.http.client.HttpClient + +@Profile("!(mock|mock-alt|local)") +@Configuration +class ProxiedWebClientConfig( + private val webClientBuilder: WebClient.Builder +) { + + @Value("\${HTTPS_PROXY}") + private lateinit var proxyUrl: String + + @Bean + fun proxiedWebClient(): WebClient = + webClientBuilder + .clientConnector(getReactorClientHttpConnector(proxyUrl)) + .build() + +} + +@Profile("mock|mock-alt|local") +@Configuration +class MockProxiedWebClientConfig( + private val webClientBuilder: WebClient.Builder +) { + + @Bean + fun proxiedWebClient(): WebClient = + webClientBuilder + .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .build() + +} \ No newline at end of file diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt index 6d83f832c..9fc56aad6 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt @@ -2,11 +2,12 @@ package no.nav.sosialhjelp.innsyn import com.ninjasquad.springmockk.MockkBean import no.nav.sosialhjelp.idporten.client.IdPortenClient -import no.nav.sosialhjelp.innsyn.client.idporten.IdPortenClientConfig +import no.nav.sosialhjelp.innsyn.config.ProxiedWebClientConfig import org.junit.jupiter.api.Test import org.springframework.boot.test.context.SpringBootTest import org.springframework.test.context.ActiveProfiles import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient @SpringBootTest(classes = [TestApplication::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @ActiveProfiles(profiles = ["no-redis"]) @@ -15,8 +16,11 @@ class ApplicationContextTest { @MockkBean private lateinit var idPortenClient: IdPortenClient + @MockkBean(name = "proxiedWebClient") + private lateinit var proxiedWebClient: WebClient + @MockkBean - private lateinit var idPortenClientConfig: IdPortenClientConfig + private lateinit var proxiedWebClientConfig: ProxiedWebClientConfig @MockkBean(name = "stsRestTemplate") private lateinit var stsRestTemplate: RestTemplate From c748bf90be129df0b16ee07c576fb905c01ada24 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Mon, 15 Mar 2021 09:20:06 +0100 Subject: [PATCH 20/32] non-snapshot sosialhjelp-common --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 39ed2d9c2..c29c08a18 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ object Versions { const val kotlin = "1.4.21" const val coroutines = "1.4.2" const val springBoot = "2.4.3" - const val sosialhjelpCommon = "1.da3e880-SNAPSHOT" + const val sosialhjelpCommon = "1.8f11108" const val logback = "1.2.3" const val logstash = "6.5" const val filformat = "1.2021.03.02-10.58-415c44e55124" From e32711a1ec4b038e673855382a229c4dba4deba5 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Mon, 15 Mar 2021 16:49:30 +0100 Subject: [PATCH 21/32] skriver om fra resttemplate til webclient for fiksclient. Tar i bruk mockwebserver for test --- build.gradle.kts | 2 + .../client/digisosapi/DigisosApiClientImpl.kt | 165 +++++----- .../innsyn/client/fiks/DokumentlagerClient.kt | 67 ++-- .../innsyn/client/fiks/FiksClientImpl.kt | 292 +++++++++--------- .../innsyn/client/fiks/FiksConfig.kt | 19 ++ .../innsyn/client/fiks/FiksPaths.kt | 2 + .../client/fiks/KommuneInfoClientConfig.kt | 4 +- .../client/idporten/IdPortenClientConfig.kt | 4 +- .../innsyn/config/ProxiedWebClientConfig.kt | 6 +- .../no/nav/sosialhjelp/innsyn/utils/Utils.kt | 7 + .../innsyn/ApplicationContextTest.kt | 4 +- .../client/digisosapi/DigisosApiClientTest.kt | 34 +- .../innsyn/client/fiks/FiksClientTest.kt | 260 ++++++++-------- .../innsyn/responses/DigisosSakResponses.kt | 3 +- 14 files changed, 457 insertions(+), 412 deletions(-) create mode 100644 src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt diff --git a/build.gradle.kts b/build.gradle.kts index c29c08a18..c3e54d932 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -37,6 +37,7 @@ object Versions { const val junitJupiter = "5.7.0" const val mockk = "1.10.3" const val springmockk = "2.0.0" + const val mockwebserver = "5.0.0-alpha.2" } plugins { @@ -147,6 +148,7 @@ dependencies { testImplementation("no.nav.security:token-validation-test-support:${Versions.tokenValidation}") testImplementation("org.jetbrains.kotlin:kotlin-test:${Versions.kotlin}") testImplementation("com.ninja-squad:springmockk:${Versions.springmockk}") + testImplementation("com.squareup.okhttp3:mockwebserver3-junit5:${Versions.mockwebserver}") // spesifikke versjoner oppgradert etter ønske fra snyk constraints { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientImpl.kt index 2e22c4eb9..ad4f316db 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientImpl.kt @@ -1,8 +1,6 @@ package no.nav.sosialhjelp.innsyn.client.digisosapi -import com.fasterxml.jackson.module.kotlin.readValue import no.nav.sosialhjelp.api.fiks.exceptions.FiksClientException -import no.nav.sosialhjelp.api.fiks.exceptions.FiksException import no.nav.sosialhjelp.api.fiks.exceptions.FiksServerException import no.nav.sosialhjelp.innsyn.client.fiks.FiksClientImpl import no.nav.sosialhjelp.innsyn.client.fiks.VedleggMetadata @@ -16,17 +14,17 @@ import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils.HEADER_INTEGRASJON_PASSO import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils.forwardHeaders import no.nav.sosialhjelp.innsyn.utils.logger import no.nav.sosialhjelp.innsyn.utils.objectMapper +import no.nav.sosialhjelp.innsyn.utils.typeRef import org.springframework.context.annotation.Profile -import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders.AUTHORIZATION -import org.springframework.http.HttpMethod +import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.stereotype.Component import org.springframework.util.LinkedMultiValueMap -import org.springframework.web.client.HttpClientErrorException -import org.springframework.web.client.HttpServerErrorException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.BodyInserters +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.bodyToMono import java.util.Collections /** @@ -35,109 +33,106 @@ import java.util.Collections @Profile("!(prod-sbs|mock)") @Component class DigisosApiClientImpl( - clientProperties: ClientProperties, - private val restTemplate: RestTemplate, - private val idPortenService: IdPortenService, - private val fiksClientImpl: FiksClientImpl + private val clientProperties: ClientProperties, + private val fiksWebClient: WebClient, + private val idPortenService: IdPortenService, + private val fiksClientImpl: FiksClientImpl, ) : DigisosApiClient { private val testbrukerNatalie = System.getenv("TESTBRUKER_NATALIE") ?: "11111111111" - private val baseUrl = clientProperties.fiksDigisosEndpointUrl - private val fiksIntegrasjonIdKommune = clientProperties.fiksIntegrasjonIdKommune - private val fiksIntegrasjonPassordKommune = clientProperties.fiksIntegrasjonPassordKommune - override fun oppdaterDigisosSak(fiksDigisosId: String?, digisosApiWrapper: DigisosApiWrapper): String? { var id = fiksDigisosId if (fiksDigisosId == null || fiksDigisosId == "001" || fiksDigisosId == "002" || fiksDigisosId == "003") { id = opprettDigisosSak() log.info("Laget ny digisossak: $id") } - val httpEntity = HttpEntity(objectMapper.writeValueAsString(digisosApiWrapper), headers()) - try { - restTemplate.exchange("$baseUrl/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/$id", HttpMethod.POST, httpEntity, String::class.java) - log.info("Postet DigisosSak til Fiks") - return id - } catch (e: HttpClientErrorException) { - log.warn(e.responseBodyAsString) - log.warn("Fiks - oppdaterDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksClientException(e.rawStatusCode, e.message, e) - } catch (e: HttpServerErrorException) { - log.warn(e.responseBodyAsString) - log.warn("Fiks - oppdaterDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksServerException(e.rawStatusCode, e.message, e) - } catch (e: Exception) { - log.error(e.message, e) - throw FiksException(e.message, e) - } + + return fiksWebClient.post() + .uri("/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/$id") + .headers { it.addAll(headers()) } + .body(BodyInserters.fromValue(objectMapper.writeValueAsString(digisosApiWrapper))) + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - oppdaterDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) + FiksClientException(e.rawStatusCode, e.message, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - oppdaterDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) + FiksServerException(e.rawStatusCode, e.message, e) + } + } + .bodyToMono() + .block() + .also { log.info("Postet DigisosSak til Fiks") } } // Brukes for å laste opp Pdf-er fra test-fagsystem i q-miljø override fun lastOppNyeFilerTilFiks(files: List, soknadId: String): List { - val headers = forwardHeaders() - headers.accept = Collections.singletonList(MediaType.APPLICATION_JSON) - headers.set(AUTHORIZATION, BEARER + idPortenService.getToken().token) - headers.set(HEADER_INTEGRASJON_ID, fiksIntegrasjonIdKommune) - headers.set(HEADER_INTEGRASJON_PASSORD, fiksIntegrasjonPassordKommune) - headers.contentType = MediaType.MULTIPART_FORM_DATA - val body = LinkedMultiValueMap() - files.forEachIndexed { fileId, file -> val vedleggMetadata = VedleggMetadata(file.filnavn, file.mimetype, file.storrelse) body.add("vedleggSpesifikasjon:$fileId", fiksClientImpl.createHttpEntityOfString(fiksClientImpl.serialiser(vedleggMetadata), "vedleggSpesifikasjon:$fileId")) body.add("dokument:$fileId", fiksClientImpl.createHttpEntityOfFile(file, "dokument:$fileId")) } - val requestEntity = HttpEntity(body, headers) - try { - val path = "$baseUrl/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/$soknadId/filer" - val response = restTemplate.exchange(path, HttpMethod.POST, requestEntity, String::class.java) - - val opplastingResponse: List = objectMapper.readValue(response.body!!) - log.info("Filer sendt til Fiks") - return opplastingResponse.map { filOpplastingResponse -> filOpplastingResponse.dokumentlagerDokumentId } - - } catch (e: HttpClientErrorException) { - log.warn(e.responseBodyAsString) - log.warn("Opplasting av filer feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksClientException(e.rawStatusCode, e.message, e) - } catch (e: HttpServerErrorException) { - log.warn(e.responseBodyAsString) - log.warn("Opplasting av filer feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksServerException(e.rawStatusCode, e.message, e) - } catch (e: Exception) { - log.warn("Opplasting av filer feilet", e) - throw FiksException(e.message, e) - } - + val opplastingResponseList = fiksWebClient.post() + .uri("/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/$soknadId/filer") + .headers { it.addAll(headers()) } + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(body)) + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - Opplasting av filer feilet - ${e.statusCode} ${e.statusText}", e) + FiksClientException(e.rawStatusCode, e.message, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - Opplasting av filer feilet - ${e.statusCode} ${e.statusText}", e) + FiksServerException(e.rawStatusCode, e.message, e) + } + } + .bodyToMono(typeRef>()) + .block() + log.info("Filer sendt til Fiks") + return opplastingResponseList!!.map { it.dokumentlagerDokumentId } } fun opprettDigisosSak(): String? { - val httpEntity = HttpEntity("", headers()) - try { - val response = restTemplate.exchange("$baseUrl/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/ny?sokerFnr=$testbrukerNatalie", HttpMethod.POST, httpEntity, String::class.java) - log.info("Opprettet sak hos Fiks. Digisosid: ${response.body}") - return response.body?.replace("\"", "") - } catch (e: HttpClientErrorException) { - log.warn("Fiks - opprettDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksClientException(e.rawStatusCode, e.message, e) - } catch (e: HttpServerErrorException) { - log.warn("Fiks - opprettDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksServerException(e.rawStatusCode, e.message, e) - } catch (e: Exception) { - log.error(e.message, e) - throw FiksException(e.message, e) - } + val response = fiksWebClient.post() + .uri("/digisos/api/v1/11415cd1-e26d-499a-8421-751457dfcbd5/ny?sokerFnr=$testbrukerNatalie") + .headers { it.addAll(headers()) } + .body(BodyInserters.fromValue("")) + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - opprettDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) + FiksClientException(e.rawStatusCode, e.message, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - opprettDigisosSak feilet - ${e.statusCode} ${e.statusText}", e) + FiksServerException(e.rawStatusCode, e.message, e) + } + } + .bodyToMono() + .block() + log.info("Opprettet sak hos Fiks. Digisosid: $response") + return response?.replace("\"", "") } private fun headers(): HttpHeaders { val headers = forwardHeaders() - val accessToken = idPortenService.getToken() - headers.accept = Collections.singletonList(MediaType.ALL) - headers.set(HEADER_INTEGRASJON_ID, fiksIntegrasjonIdKommune) - headers.set(HEADER_INTEGRASJON_PASSORD, fiksIntegrasjonPassordKommune) - headers.set(AUTHORIZATION, BEARER + accessToken.token) + headers.accept = Collections.singletonList(MediaType.APPLICATION_JSON) + headers.set(HEADER_INTEGRASJON_ID, clientProperties.fiksIntegrasjonIdKommune) + headers.set(HEADER_INTEGRASJON_PASSORD, clientProperties.fiksIntegrasjonPassordKommune) + headers.set(AUTHORIZATION, BEARER + idPortenService.getToken().token) headers.contentType = MediaType.APPLICATION_JSON return headers } @@ -148,7 +143,7 @@ class DigisosApiClientImpl( } data class FilOpplastingResponse( - val filnavn: String, - val dokumentlagerDokumentId: String, - val storrelse: Long + val filnavn: String, + val dokumentlagerDokumentId: String, + val storrelse: Long, ) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt index 6ca5de425..72d231b6d 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt @@ -1,24 +1,19 @@ package no.nav.sosialhjelp.innsyn.client.fiks import no.nav.sosialhjelp.api.fiks.exceptions.FiksClientException -import no.nav.sosialhjelp.api.fiks.exceptions.FiksException import no.nav.sosialhjelp.api.fiks.exceptions.FiksServerException import no.nav.sosialhjelp.innsyn.config.ClientProperties import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils import no.nav.sosialhjelp.innsyn.utils.logger import org.springframework.context.annotation.Profile -import org.springframework.http.HttpEntity -import org.springframework.http.HttpHeaders -import org.springframework.http.MediaType +import org.springframework.http.HttpStatus import org.springframework.stereotype.Component -import org.springframework.web.client.HttpClientErrorException -import org.springframework.web.client.HttpServerErrorException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.bodyToMono import java.io.ByteArrayInputStream import java.security.cert.CertificateException import java.security.cert.CertificateFactory import java.security.cert.X509Certificate -import java.util.Collections interface DokumentlagerClient { @@ -28,41 +23,37 @@ interface DokumentlagerClient { @Profile("!mock") @Component class DokumentlagerClientImpl( - clientProperties: ClientProperties, - private val restTemplate: RestTemplate + private val clientProperties: ClientProperties, + private val fiksWebClient: WebClient, ) : DokumentlagerClient { - private val baseUrl = clientProperties.fiksDigisosEndpointUrl - private val fiksIntegrasjonid = clientProperties.fiksIntegrasjonId - private val fiksIntegrasjonpassord = clientProperties.fiksIntegrasjonpassord - override fun getDokumentlagerPublicKeyX509Certificate(token: String): X509Certificate { - val headers = IntegrationUtils.forwardHeaders() - headers.accept = Collections.singletonList(MediaType.APPLICATION_JSON) - headers.set(HttpHeaders.AUTHORIZATION, token) - headers.set(IntegrationUtils.HEADER_INTEGRASJON_ID, fiksIntegrasjonid) - headers.set(IntegrationUtils.HEADER_INTEGRASJON_PASSORD, fiksIntegrasjonpassord) - - try { - val response = restTemplate.exchange("$baseUrl/digisos/api/v1/dokumentlager-public-key", org.springframework.http.HttpMethod.GET, HttpEntity(headers), ByteArray::class.java) - log.info("Hentet public key for dokumentlager") - val publicKey = response.body - try { - val certificateFactory = CertificateFactory.getInstance("X.509") + val publicKey = fiksWebClient.get() + .uri(FiksPaths.PATH_DOKUMENTLAGER_PUBLICKEY) + .headers { it.addAll(IntegrationUtils.fiksHeaders(clientProperties, token)) } + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - getDokumentlagerPublicKey feilet - ${e.statusCode} ${e.statusText}", e) + FiksClientException(e.rawStatusCode, e.message, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - getDokumentlagerPublicKey feilet - ${e.statusCode} ${e.statusText}", e) + FiksServerException(e.rawStatusCode, e.message, e) + } + } + .bodyToMono() + .block() - return certificateFactory.generateCertificate(ByteArrayInputStream(publicKey)) as X509Certificate + log.info("Hentet public key for dokumentlager") - } catch (e: CertificateException) { - throw RuntimeException(e) - } - } catch (e: HttpClientErrorException) { - log.warn("Fiks - getDokumentlagerPublicKey feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksClientException(e.rawStatusCode, e.message, e) - } catch (e: HttpServerErrorException) { - log.warn("Fiks - getDokumentlagerPublicKey feilet - ${e.statusCode} ${e.statusText}", e) - throw FiksServerException(e.rawStatusCode, e.message, e) - } catch (e: Exception) { - throw FiksException(e.message, e) + try { + val certificateFactory = CertificateFactory.getInstance("X.509") + return certificateFactory.generateCertificate(ByteArrayInputStream(publicKey!!)) as X509Certificate + } catch (e: CertificateException) { + throw RuntimeException(e) } } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt index 19ca6b88d..c210af46f 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt @@ -5,7 +5,6 @@ import kotlinx.coroutines.runBlocking import no.nav.sbl.soknadsosialhjelp.vedlegg.JsonVedleggSpesifikasjon import no.nav.sosialhjelp.api.fiks.DigisosSak import no.nav.sosialhjelp.api.fiks.exceptions.FiksClientException -import no.nav.sosialhjelp.api.fiks.exceptions.FiksException import no.nav.sosialhjelp.api.fiks.exceptions.FiksNotFoundException import no.nav.sosialhjelp.api.fiks.exceptions.FiksServerException import no.nav.sosialhjelp.innsyn.config.ClientProperties @@ -15,8 +14,8 @@ import no.nav.sosialhjelp.innsyn.utils.IntegrationUtils.fiksHeaders import no.nav.sosialhjelp.innsyn.utils.feilmeldingUtenFnr import no.nav.sosialhjelp.innsyn.utils.lagNavEksternRefId import no.nav.sosialhjelp.innsyn.utils.logger +import no.nav.sosialhjelp.innsyn.utils.messageUtenFnr import no.nav.sosialhjelp.innsyn.utils.objectMapper -import no.nav.sosialhjelp.innsyn.utils.toFiksErrorMessage import no.nav.sosialhjelp.innsyn.utils.typeRef import no.nav.sosialhjelp.kotlin.utils.retry import org.springframework.context.annotation.Profile @@ -24,29 +23,27 @@ import org.springframework.core.io.InputStreamResource import org.springframework.http.ContentDisposition import org.springframework.http.HttpEntity import org.springframework.http.HttpHeaders -import org.springframework.http.HttpMethod import org.springframework.http.HttpStatus import org.springframework.http.MediaType -import org.springframework.http.ResponseEntity import org.springframework.lang.NonNull import org.springframework.stereotype.Component import org.springframework.util.LinkedMultiValueMap -import org.springframework.web.client.HttpClientErrorException -import org.springframework.web.client.HttpServerErrorException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.BodyInserters +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.bodyToMono +import org.springframework.web.reactive.function.client.toEntity +import java.util.function.Predicate @Profile("!mock") @Component class FiksClientImpl( - private val clientProperties: ClientProperties, - private val restTemplate: RestTemplate, - private val retryProperties: FiksRetryProperties, - private val redisService: RedisService + private val clientProperties: ClientProperties, + private val fiksWebClient: WebClient, + private val retryProperties: FiksRetryProperties, + private val redisService: RedisService, ) : FiksClient { - private val baseUrl = clientProperties.fiksDigisosEndpointUrl - override fun hentDigisosSak(digisosId: String, token: String, useCache: Boolean): DigisosSak { return when { useCache -> hentDigisosSakFraCache(digisosId) ?: hentDigisosSakFraFiks(digisosId, token) @@ -55,114 +52,157 @@ class FiksClientImpl( } private fun hentDigisosSakFraCache(digisosId: String): DigisosSak? = - redisService.get(digisosId, DigisosSak::class.java) as DigisosSak? + redisService.get(digisosId, DigisosSak::class.java) as DigisosSak? private fun hentDigisosSakFraFiks(digisosId: String, token: String): DigisosSak { - log.debug("Forsøker å hente digisosSak fra $baseUrl/digisos/api/v1/soknader/$digisosId") - - try { - val headers = fiksHeaders(clientProperties, token) - val urlTemplate = baseUrl + FiksPaths.PATH_DIGISOSSAK - - val response: ResponseEntity = withRetry { - restTemplate.exchange(urlTemplate, HttpMethod.GET, HttpEntity(headers), String::class.java, digisosId) - } - - log.debug("Hentet DigisosSak fra Fiks") - val body = response.body!! - return objectMapper.readValue(body, DigisosSak::class.java) - .also { lagreTilCache(digisosId, it) } - } catch (e: HttpClientErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentDigisosSak feilet - $message - $fiksErrorMessage", e) - if (e.statusCode == HttpStatus.NOT_FOUND) { - throw FiksNotFoundException(message, e) - } - throw FiksClientException(e.rawStatusCode, e.message, e) - } catch (e: HttpServerErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentDigisosSak feilet - $message - $fiksErrorMessage", e) - throw FiksServerException(e.rawStatusCode, message, e) - } catch (e: Exception) { - log.warn("Fiks - hentDigisosSak feilet", e) - throw FiksException(e.message?.feilmeldingUtenFnr, e) + log.debug("Forsøker å hente digisosSak fra /digisos/api/v1/soknader/$digisosId") + + val digisosSak: DigisosSak? = withRetry { + fiksWebClient.get() + .uri(FiksPaths.PATH_DIGISOSSAK, digisosId) + .headers { it.addAll(fiksHeaders(clientProperties, token)) } + .retrieve() + .onStatus(Predicate.isEqual(HttpStatus.NOT_FOUND)) { + it.createException().map { e -> + log.warn("Fiks - hentDigisosSak feilet - ${messageUtenFnr(e)}", e) + FiksNotFoundException(e.message?.feilmeldingUtenFnr, e) + } + } + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - hentDigisosSak feilet - ${messageUtenFnr(e)}", e) + FiksClientException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - hentDigisosSak feilet - ${messageUtenFnr(e)}", e) + FiksServerException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .bodyToMono() + .block() } + log.debug("Hentet DigisosSak fra Fiks") + return digisosSak!! + .also { lagreTilCache(digisosId, it) } } private fun lagreTilCache(id: String, digisosSakEllerDokument: Any) = - redisService.put(id, objectMapper.writeValueAsBytes(digisosSakEllerDokument)) - - override fun hentDokument(digisosId: String, dokumentlagerId: String, requestedClass: Class, token: String): Any { + redisService.put(id, objectMapper.writeValueAsBytes(digisosSakEllerDokument)) + + override fun hentDokument( + digisosId: String, + dokumentlagerId: String, + requestedClass: Class, + token: String, + ): Any { return hentDokumentFraCache(dokumentlagerId, requestedClass) - ?: hentDokumentFraFiks(digisosId, dokumentlagerId, requestedClass, token) + ?: hentDokumentFraFiks(digisosId, dokumentlagerId, requestedClass, token) } private fun hentDokumentFraCache(dokumentlagerId: String, requestedClass: Class): Any? = - redisService.get(dokumentlagerId, requestedClass) - - private fun hentDokumentFraFiks(digisosId: String, dokumentlagerId: String, requestedClass: Class, token: String): Any { - log.debug("Forsøker å hente dokument fra $baseUrl/digisos/api/v1/soknader/nav/$digisosId/dokumenter/$dokumentlagerId") - - try { - val headers = fiksHeaders(clientProperties, token) - val urlTemplate = baseUrl + FiksPaths.PATH_DOKUMENT - val vars = mapOf("digisosId" to digisosId, "dokumentlagerId" to dokumentlagerId) - - val response: ResponseEntity = withRetry { - restTemplate.exchange(urlTemplate, HttpMethod.GET, HttpEntity(headers), String::class.java, vars) - } - - log.debug("Hentet dokument (${requestedClass.simpleName}) fra Fiks, dokumentlagerId=$dokumentlagerId") - return objectMapper.readValue(response.body!!, requestedClass) - .also { lagreTilCache(dokumentlagerId, it) } - } catch (e: HttpClientErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentDokument feilet - $message - $fiksErrorMessage", e) - throw FiksClientException(e.rawStatusCode, message, e) - } catch (e: HttpServerErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentDokument feilet - $message - $fiksErrorMessage", e) - throw FiksServerException(e.rawStatusCode, message, e) - } catch (e: Exception) { - log.warn("Fiks - hentDokument feilet", e) - throw FiksException(e.message?.feilmeldingUtenFnr, e) + redisService.get(dokumentlagerId, requestedClass) + + private fun hentDokumentFraFiks( + digisosId: String, + dokumentlagerId: String, + requestedClass: Class, + token: String, + ): Any { + log.debug("Forsøker å hente dokument fra /digisos/api/v1/soknader/nav/$digisosId/dokumenter/$dokumentlagerId") + + val dokument: Any? = withRetry { + fiksWebClient.get() + .uri(FiksPaths.PATH_DOKUMENT, digisosId, dokumentlagerId) + .headers { it.addAll(fiksHeaders(clientProperties, token)) } + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - hentDokument feilet - ${messageUtenFnr(e)}", e) + FiksClientException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - hentDokument feilet - ${messageUtenFnr(e)}", e) + FiksServerException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .bodyToMono(requestedClass) + .block() } + log.debug("Hentet dokument (${requestedClass.simpleName}) fra Fiks, dokumentlagerId=$dokumentlagerId") + return dokument!! + .also { lagreTilCache(dokumentlagerId, it) } } override fun hentAlleDigisosSaker(token: String): List { - try { - val headers = fiksHeaders(clientProperties, token) - val url = baseUrl + FiksPaths.PATH_ALLE_DIGISOSSAKER - - val response: ResponseEntity> = withRetry { - restTemplate.exchange(url, HttpMethod.GET, HttpEntity(headers), typeRef>()) - } - return response.body.orEmpty() - } catch (e: HttpClientErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentAlleDigisosSaker feilet - $message - $fiksErrorMessage", e) - throw FiksClientException(e.rawStatusCode, message, e) - } catch (e: HttpServerErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Fiks - hentAlleDigisosSaker feilet - $message - $fiksErrorMessage", e) - throw FiksServerException(e.rawStatusCode, message, e) - } catch (e: Exception) { - log.warn("Fiks - hentAlleDigisosSaker feilet", e) - throw FiksException(e.message?.feilmeldingUtenFnr, e) + val digisosSaker: List? = withRetry { + fiksWebClient.get() + .uri(FiksPaths.PATH_ALLE_DIGISOSSAKER) + .headers { it.addAll(fiksHeaders(clientProperties, token)) } + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - hentAlleDigisosSaker feilet - ${messageUtenFnr(e)}", e) + FiksClientException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - hentAlleDigisosSaker feilet - ${messageUtenFnr(e)}", e) + FiksServerException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .bodyToMono(typeRef>()) + .block() } + return digisosSaker!! } - override fun lastOppNyEttersendelse(files: List, vedleggJson: JsonVedleggSpesifikasjon, digisosId: String, token: String) { + override fun lastOppNyEttersendelse( + files: List, + vedleggJson: JsonVedleggSpesifikasjon, + digisosId: String, + token: String, + ) { log.info("Starter sending til FIKS for ettersendelse med ${files.size} filer (inkludert ettersendelse.pdf). Validering, filnavn-endring, generering av ettersendelse.pdf og kryptering er OK.") - val headers = fiksHeaders(clientProperties, token) - headers.contentType = MediaType.MULTIPART_FORM_DATA + val body = createBodyForUpload(vedleggJson, files) + + val digisosSak = hentDigisosSakFraFiks(digisosId, token) + val kommunenummer = digisosSak.kommunenummer + val navEksternRefId = lagNavEksternRefId(digisosSak) + + val responseEntity = fiksWebClient.post() + .uri(FiksPaths.PATH_LAST_OPP_ETTERSENDELSE, kommunenummer, digisosId, navEksternRefId) + .headers { it.addAll(fiksHeaders(clientProperties, token)) } + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(body)) + .retrieve() + .onStatus(HttpStatus::is4xxClientError) { + it.createException().map { e -> + log.warn("Fiks - Opplasting av ettersendelse på $digisosId feilet - ${messageUtenFnr(e)}", e) + FiksClientException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .onStatus(HttpStatus::is5xxServerError) { + it.createException().map { e -> + log.warn("Fiks - Opplasting av ettersendelse på $digisosId feilet - ${messageUtenFnr(e)}", e) + FiksServerException(e.rawStatusCode, e.message?.feilmeldingUtenFnr, e) + } + } + .toEntity() + .block() + + log.info("Sendte ettersendelse til kommune $kommunenummer i Fiks, fikk navEksternRefId $navEksternRefId (statusCode: ${responseEntity!!.statusCodeValue})") + } + + fun createBodyForUpload( + vedleggJson: JsonVedleggSpesifikasjon, + files: List, + ): LinkedMultiValueMap { val body = LinkedMultiValueMap() body.add("vedlegg.json", createHttpEntityOfString(serialiser(vedleggJson), "vedlegg.json")) @@ -171,33 +211,7 @@ class FiksClientImpl( body.add("vedleggSpesifikasjon:$fileId", createHttpEntityOfString(serialiser(vedleggMetadata), "vedleggSpesifikasjon:$fileId")) body.add("dokument:$fileId", createHttpEntityOfFile(file, "dokument:$fileId")) } - - val digisosSak = hentDigisosSakFraFiks(digisosId, token) - val kommunenummer = digisosSak.kommunenummer - val navEksternRefId = lagNavEksternRefId(digisosSak) - - val requestEntity = HttpEntity(body, headers) - try { - val urlTemplate = "$baseUrl/digisos/api/v1/soknader/{kommunenummer}/{digisosId}/{navEksternRefId}" - val vars = mapOf("kommunenummer" to kommunenummer, "digisosId" to digisosId, "navEksternRefId" to navEksternRefId) - val response: ResponseEntity = restTemplate.exchange(urlTemplate, HttpMethod.POST, requestEntity, String::class.java, vars) - - log.info("Sendte ettersendelse til kommune $kommunenummer i Fiks, fikk navEksternRefId $navEksternRefId (statusCode: ${response.statusCodeValue})") - - } catch (e: HttpClientErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Opplasting av ettersendelse på $digisosId feilet - $message - $fiksErrorMessage", e) - throw FiksClientException(e.rawStatusCode, message, e) - } catch (e: HttpServerErrorException) { - val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr - val message = e.message?.feilmeldingUtenFnr - log.warn("Opplasting av ettersendelse på $digisosId feilet - $message - $fiksErrorMessage", e) - throw FiksServerException(e.rawStatusCode, message, e) - } catch (e: Exception) { - log.warn("Opplasting av ettersendelse på $digisosId feilet", e) - throw FiksException(e.message?.feilmeldingUtenFnr, e) - } + return body } fun createHttpEntityOfString(body: String, name: String): HttpEntity { @@ -211,8 +225,8 @@ class FiksClientImpl( private fun createHttpEntity(body: Any, name: String, filename: String?, contentType: String): HttpEntity { val headerMap = LinkedMultiValueMap() val builder: ContentDisposition.Builder = ContentDisposition - .builder("form-data") - .name(name) + .builder("form-data") + .name(name) val contentDisposition: ContentDisposition = if (filename == null) builder.build() else builder.filename(filename).build() headerMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()) @@ -228,13 +242,13 @@ class FiksClientImpl( } } - private fun withRetry(block: () -> ResponseEntity): ResponseEntity { + private fun withRetry(block: () -> T): T { return runBlocking { retry( - attempts = retryProperties.attempts, - initialDelay = retryProperties.initialDelay, - maxDelay = retryProperties.maxDelay, - retryableExceptions = arrayOf(HttpServerErrorException::class) + attempts = retryProperties.attempts, + initialDelay = retryProperties.initialDelay, + maxDelay = retryProperties.maxDelay, + retryableExceptions = arrayOf(FiksServerException::class) ) { block() } @@ -247,7 +261,7 @@ class FiksClientImpl( } data class VedleggMetadata( - val filnavn: String?, - val mimetype: String?, - val storrelse: Long + val filnavn: String?, + val mimetype: String?, + val storrelse: Long, ) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt new file mode 100644 index 000000000..42023d7a1 --- /dev/null +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt @@ -0,0 +1,19 @@ +package no.nav.sosialhjelp.innsyn.client.fiks + +import no.nav.sosialhjelp.innsyn.config.ClientProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.web.reactive.function.client.WebClient + +@Configuration +class FiksConfig ( + private val proxiedWebClientBuilder: WebClient.Builder, + private val clientProperties: ClientProperties +) { + + @Bean + fun fiksWebClient(): WebClient = + proxiedWebClientBuilder + .baseUrl(clientProperties.fiksDigisosEndpointUrl) + .build() +} \ No newline at end of file diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksPaths.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksPaths.kt index 57ce54181..592947833 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksPaths.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksPaths.kt @@ -7,5 +7,7 @@ object FiksPaths { const val PATH_DOKUMENT = "/digisos/api/v1/soknader/{digisosId}/dokumenter/{dokumentlagerId}" const val PATH_KOMMUNEINFO = "/digisos/api/v1/nav/kommuner/{kommunenummer}" const val PATH_ALLE_KOMMUNEINFO = "/digisos/api/v1/nav/kommuner" + const val PATH_LAST_OPP_ETTERSENDELSE = "/digisos/api/v1/soknader/{kommunenummer}/{digisosId}/{navEksternRefId}" + const val PATH_DOKUMENTLAGER_PUBLICKEY = "/digisos/api/v1/dokumentlager-public-key" } \ No newline at end of file diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index 780a96153..2e09b5d78 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -12,14 +12,14 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class KommuneInfoClientConfig( - private val proxiedWebClient: WebClient, + private val proxiedWebClientBuilder: WebClient.Builder, private val clientProperties: ClientProperties ) { @Bean fun kommuneInfoClient(): KommuneInfoClient { return KommuneInfoClientImpl( - proxiedWebClient, + proxiedWebClientBuilder.build(), fiksProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 3242d552f..07b7093bc 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -12,7 +12,7 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( - private val proxiedWebClient: WebClient, + private val proxiedWebClientBuilder: WebClient.Builder, @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, @@ -24,7 +24,7 @@ class IdPortenClientConfig( @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = proxiedWebClient, + webClient = proxiedWebClientBuilder.build(), idPortenProperties = idPortenProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt index f613e1ddd..630e00b0c 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt @@ -19,10 +19,9 @@ class ProxiedWebClientConfig( private lateinit var proxyUrl: String @Bean - fun proxiedWebClient(): WebClient = + fun proxiedWebClientBuilder(): WebClient.Builder = webClientBuilder .clientConnector(getReactorClientHttpConnector(proxyUrl)) - .build() } @@ -33,9 +32,8 @@ class MockProxiedWebClientConfig( ) { @Bean - fun proxiedWebClient(): WebClient = + fun proxiedWebClientBuilder(): WebClient.Builder = webClientBuilder .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) - .build() } \ No newline at end of file diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt index 3b786b39e..823806f47 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt @@ -8,6 +8,8 @@ import no.nav.sbl.soknadsosialhjelp.digisos.soker.filreferanse.JsonDokumentlager import no.nav.sbl.soknadsosialhjelp.digisos.soker.filreferanse.JsonSvarUtFilreferanse import no.nav.sosialhjelp.api.fiks.DigisosSak import no.nav.sosialhjelp.api.fiks.ErrorMessage +import no.nav.sosialhjelp.client.kommuneinfo.feilmeldingUtenFnr +import no.nav.sosialhjelp.client.kommuneinfo.toFiksErrorMessage import no.nav.sosialhjelp.innsyn.config.ClientProperties import no.nav.sosialhjelp.innsyn.utils.mdc.MDCUtils import org.slf4j.Logger @@ -129,6 +131,11 @@ fun T.toFiksErrorMessage(): ErrorMessage? { } } +fun messageUtenFnr(e: WebClientResponseException): String { + val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr + val message = e.message?.feilmeldingUtenFnr + return "$message - $fiksErrorMessage" +} val String.feilmeldingUtenFnr: String? get() { diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt index 9fc56aad6..0adeb3340 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt @@ -16,8 +16,8 @@ class ApplicationContextTest { @MockkBean private lateinit var idPortenClient: IdPortenClient - @MockkBean(name = "proxiedWebClient") - private lateinit var proxiedWebClient: WebClient + @MockkBean(name = "proxiedWebClientBuilder", relaxed = true) + private lateinit var proxiedWebClientBuilder: WebClient.Builder @MockkBean private lateinit var proxiedWebClientConfig: ProxiedWebClientConfig diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientTest.kt index 682488652..c6ac557f4 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/digisosapi/DigisosApiClientTest.kt @@ -1,8 +1,9 @@ package no.nav.sosialhjelp.innsyn.client.digisosapi import io.mockk.coEvery -import io.mockk.every import io.mockk.mockk +import mockwebserver3.MockResponse +import mockwebserver3.MockWebServer import no.nav.sbl.soknadsosialhjelp.digisos.soker.JsonDigisosSoker import no.nav.sosialhjelp.innsyn.client.fiks.FiksClientImpl import no.nav.sosialhjelp.innsyn.config.ClientProperties @@ -10,9 +11,9 @@ import no.nav.sosialhjelp.innsyn.domain.DigisosApiWrapper import no.nav.sosialhjelp.innsyn.domain.SakWrapper import no.nav.sosialhjelp.innsyn.responses.ok_komplett_jsondigisossoker_response import no.nav.sosialhjelp.innsyn.service.idporten.IdPortenService +import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.junit.jupiter.api.Test -import org.springframework.http.ResponseEntity -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient internal class DigisosApiClientTest { @@ -20,23 +21,24 @@ internal class DigisosApiClientTest { @Test fun `Post digisos sak til mock`() { - val restTemplate: RestTemplate = mockk() + val mockWebServer = MockWebServer() + val fiksWebClient = WebClient.create(mockWebServer.url("/").toString()) val idPortenService: IdPortenService = mockk() val fiksClientImpl: FiksClientImpl = mockk() - val digisosApiClient = DigisosApiClientImpl(clientProperties, restTemplate, idPortenService, fiksClientImpl) + val digisosApiClient = DigisosApiClientImpl(clientProperties, fiksWebClient, idPortenService, fiksClientImpl) - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_komplett_jsondigisossoker_response coEvery { idPortenService.getToken().token } returns "Token" - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java) - } returns mockResponse - - digisosApiClient.oppdaterDigisosSak("123123", DigisosApiWrapper(SakWrapper(JsonDigisosSoker()), "")) + + mockWebServer.enqueue( + MockResponse() + .setResponseCode(202) + .setBody("ok") + ) + + val jsonDigisosSoker = + objectMapper.readValue(ok_komplett_jsondigisossoker_response, JsonDigisosSoker::class.java) + + digisosApiClient.oppdaterDigisosSak("123123", DigisosApiWrapper(SakWrapper(jsonDigisosSoker), "")) } } \ No newline at end of file diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt index 02753063d..dfba67cd3 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt @@ -6,8 +6,9 @@ import io.mockk.clearAllMocks import io.mockk.every import io.mockk.just import io.mockk.mockk -import io.mockk.slot import io.mockk.verify +import mockwebserver3.MockResponse +import mockwebserver3.MockWebServer import no.nav.sbl.soknadsosialhjelp.digisos.soker.JsonDigisosSoker import no.nav.sbl.soknadsosialhjelp.vedlegg.JsonVedleggSpesifikasjon import no.nav.sosialhjelp.api.fiks.DigisosSak @@ -21,37 +22,39 @@ import no.nav.sosialhjelp.innsyn.service.pdf.EttersendelsePdfGenerator import no.nav.sosialhjelp.innsyn.service.vedlegg.FilForOpplasting import no.nav.sosialhjelp.innsyn.service.vedlegg.KrypteringService import no.nav.sosialhjelp.innsyn.utils.objectMapper -import no.nav.sosialhjelp.innsyn.utils.typeRef import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatCode import org.assertj.core.api.Assertions.assertThatExceptionOfType +import org.junit.jupiter.api.AfterEach import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test -import org.springframework.http.HttpEntity -import org.springframework.http.HttpMethod +import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus +import org.springframework.http.MediaType import org.springframework.http.ResponseEntity -import org.springframework.util.LinkedMultiValueMap -import org.springframework.web.client.HttpClientErrorException -import org.springframework.web.client.HttpServerErrorException -import org.springframework.web.client.RestTemplate +import org.springframework.web.reactive.function.client.WebClient +import org.springframework.web.reactive.function.client.bodyToMono +import org.springframework.web.reactive.function.client.toEntity import java.io.InputStream internal class FiksClientTest { + private val mockWebServer = MockWebServer() + private val clientProperties: ClientProperties = mockk(relaxed = true) - private val restTemplate: RestTemplate = mockk() + private val fiksWebClient = WebClient.create(mockWebServer.url("/").toString()) private val redisService: RedisService = mockk() private val retryProperties: FiksRetryProperties = mockk() private val ettersendelsePdfGenerator: EttersendelsePdfGenerator = mockk() private val krypteringService: KrypteringService = mockk() - private val fiksClient = FiksClientImpl(clientProperties, restTemplate, retryProperties, redisService) + private val fiksClient = FiksClientImpl(clientProperties, fiksWebClient, retryProperties, redisService) private val id = "123" @BeforeEach fun init() { clearAllMocks() + mockWebServer.start() every { redisService.get(any(), any()) } returns null every { redisService.put(any(), any(), any()) } just Runs @@ -62,18 +65,19 @@ internal class FiksClientTest { every { retryProperties.maxDelay } returns 10 } + @AfterEach + internal fun tearDown() { + mockWebServer.shutdown() + } + @Test fun `GET eksakt 1 DigisosSak`() { - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_digisossak_response - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - id) - } returns mockResponse + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(ok_digisossak_response) + ) val result = fiksClient.hentDigisosSak(id, "Token", false) @@ -94,16 +98,12 @@ internal class FiksClientTest { @Test fun `GET digisosSak fra cache etter put`() { - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_digisossak_response - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - id) - } returns mockResponse + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(ok_digisossak_response) + ) val result1 = fiksClient.hentDigisosSak(id, "Token", true) @@ -111,7 +111,7 @@ internal class FiksClientTest { verify(exactly = 1) { redisService.put(any(), any(), any()) } verify(exactly = 1) { redisService.get(any(), DigisosSak::class.java) } - val digisosSak: DigisosSak = objectMapper.readValue(ok_digisossak_response) + val digisosSak: DigisosSak = objectMapper.readValue(ok_digisossak_response) every { redisService.get(id, DigisosSak::class.java) } returns digisosSak val result = fiksClient.hentDigisosSak(id, "Token", true) @@ -124,61 +124,54 @@ internal class FiksClientTest { @Test fun `GET DigisosSak feiler hvis Fiks gir 500`() { - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - id) - } throws HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "some error") - assertThatExceptionOfType(FiksServerException::class.java) - .isThrownBy { fiksClient.hentDigisosSak(id, "Token", true) } + every { retryProperties.attempts } returns 1 + + mockWebServer.enqueue( + MockResponse() + .setResponseCode(500) + ) + assertThatExceptionOfType(FiksServerException::class.java) + .isThrownBy { fiksClient.hentDigisosSak(id, "Token", true) } } @Test fun `GET alle DigisosSaker skal bruke retry hvis Fiks gir 5xx-feil`() { - every { - restTemplate.exchange( - any(), - any(), - any(), - typeRef>()) - } throws HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR, "some error") + mockWebServer.enqueue( + MockResponse() + .setResponseCode(500) + ) + mockWebServer.enqueue( + MockResponse() + .setResponseCode(500) + ) assertThatExceptionOfType(FiksServerException::class.java).isThrownBy { fiksClient.hentAlleDigisosSaker("Token") } - - verify(atLeast = 2) { restTemplate.exchange(any(), any(), any(), typeRef>()) } + assertThat(mockWebServer.requestCount).isEqualTo(2) } @Test fun `GET alle DigisosSaker skal ikke bruke retry hvis Fiks gir 4xx-feil`() { - every { - restTemplate.exchange( - any(), - any(), - any(), - typeRef>()) - } throws HttpClientErrorException(HttpStatus.BAD_REQUEST, "some error") + mockWebServer.enqueue( + MockResponse() + .setResponseCode(400) + ) assertThatExceptionOfType(FiksClientException::class.java).isThrownBy { fiksClient.hentAlleDigisosSaker("Token") } - verify(exactly = 1) { restTemplate.exchange(any(), any(), any(), typeRef>()) } + assertThat(mockWebServer.requestCount).isEqualTo(1) } @Test fun `GET alle DigisosSaker`() { - val mockListResponse: ResponseEntity> = mockk() val digisosSakOk = objectMapper.readValue(ok_digisossak_response, DigisosSak::class.java) - every { mockListResponse.body } returns listOf(digisosSakOk, digisosSakOk) - every { - restTemplate.exchange( - any(), - any(), - any(), - typeRef>()) - } returns mockListResponse + + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(objectMapper.writeValueAsString(listOf(digisosSakOk, digisosSakOk))) + ) val result = fiksClient.hentAlleDigisosSaker("Token") @@ -188,16 +181,12 @@ internal class FiksClientTest { @Test fun `GET dokument`() { - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_minimal_jsondigisossoker_response - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - any()) - } returns mockResponse + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(ok_minimal_jsondigisossoker_response) + ) val result = fiksClient.hentDokument(id, "dokumentlagerId", JsonDigisosSoker::class.java, "Token") @@ -218,16 +207,12 @@ internal class FiksClientTest { @Test fun `GET dokument fra cache etter put`() { - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_digisossak_response - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - any()) - } returns mockResponse + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(ok_minimal_jsondigisossoker_response) + ) val result1 = fiksClient.hentDokument(id, "dokumentlagerId", JsonDigisosSoker::class.java, "Token") @@ -251,16 +236,12 @@ internal class FiksClientTest { // cache returnerer jsonsoknad, men vi forventer jsondigisossoker every { redisService.get(any(), JsonDigisosSoker::class.java) } returns null - val mockResponse: ResponseEntity = mockk() - every { mockResponse.body } returns ok_minimal_jsondigisossoker_response - every { - restTemplate.exchange( - any(), - any(), - any(), - String::class.java, - any()) - } returns mockResponse + mockWebServer.enqueue( + MockResponse() + .setResponseCode(200) + .setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .setBody(ok_minimal_jsondigisossoker_response) + ) val result2 = fiksClient.hentDokument(id, "dokumentlagerId", JsonDigisosSoker::class.java, "Token") @@ -269,8 +250,11 @@ internal class FiksClientTest { verify(exactly = 1) { redisService.put(any(), any(), any()) } } - @Test + @Test // fikk ikke mockWebServer til å funke her uten å skjønner hvorfor (InputStream-relatert), så gikk for "klassisk" mockk stil fun `POST ny ettersendelse`() { + val webClient: WebClient = mockk() + val clientForPost = FiksClientImpl(clientProperties, webClient, retryProperties, redisService) + val fil1: InputStream = mockk() val fil2: InputStream = mockk() every { fil1.readAllBytes() } returns "test-fil".toByteArray() @@ -280,33 +264,63 @@ internal class FiksClientTest { every { ettersendelsePdfGenerator.generate(any(), any()) } returns ettersendelsPdf every { krypteringService.krypter(any(), any(), any()) } returns fil1 - val mockDigisosSakResponse: ResponseEntity = mockk() - every { mockDigisosSakResponse.body } returns ok_digisossak_response - every { restTemplate.exchange(any(), HttpMethod.GET, any(), String::class.java, id) } returns mockDigisosSakResponse + val files = listOf(FilForOpplasting("filnavn0", "image/png", 1L, fil1), + FilForOpplasting("filnavn1", "image/jpg", 1L, fil2)) + + every { + webClient.get() + .uri(any(), any()) + .headers(any()) + .retrieve() + .onStatus(any(), any()) + .onStatus(any(), any()) + .onStatus(any(), any()) + .bodyToMono() + .block() + } returns objectMapper.readValue(ok_digisossak_response, DigisosSak::class.java) - val slot = slot>>() - val mockFiksResponse: ResponseEntity = mockk() - every { mockFiksResponse.statusCodeValue } returns 202 - every { restTemplate.exchange(any(), HttpMethod.POST, capture(slot), String::class.java, any()) } returns mockFiksResponse + every { + webClient.post() + .uri(any(), any(), any(), any()) + .headers(any()) + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(any()) + .retrieve() + .onStatus(any(), any()) + .onStatus(any(), any()) + .toEntity() + .block() + } returns ResponseEntity(HttpStatus.ACCEPTED) + + assertThatCode { + clientForPost.lastOppNyEttersendelse(files, + JsonVedleggSpesifikasjon(), + id, + "token") + }.doesNotThrowAnyException() + } + + @Test + internal fun `should produce body for upload`() { + val fil1: InputStream = mockk() + val fil2: InputStream = mockk() + every { fil1.readAllBytes() } returns "test-fil".toByteArray() + every { fil2.readAllBytes() } returns "div".toByteArray() val files = listOf(FilForOpplasting("filnavn0", "image/png", 1L, fil1), - FilForOpplasting("filnavn1", "image/jpg", 1L, fil2)) - - assertThatCode { fiksClient.lastOppNyEttersendelse(files, JsonVedleggSpesifikasjon(), id, "token") }.doesNotThrowAnyException() - - val httpEntity = slot.captured - - assertThat(httpEntity.body!!.size == 5) - assertThat(httpEntity.headers["Content-Type"]!![0] == "multipart/form-data") - assertThat(httpEntity.body!!.keys.contains("vedlegg.json")) - assertThat(httpEntity.body!!.keys.contains("vedleggSpesifikasjon:0")) - assertThat(httpEntity.body!!.keys.contains("dokument:0")) - assertThat(httpEntity.body!!.keys.contains("vedleggSpesifikasjon:1")) - assertThat(httpEntity.body!!.keys.contains("dokument:1")) - assertThat(httpEntity.body!!["dokument:0"].toString().contains("InputStream resource")) - assertThat(httpEntity.body!!["dokument:1"].toString().contains("InputStream resource")) - assertThat(httpEntity.body!!["vedlegg.json"].toString().contains("text/plain;charset=UTF-8")) - assertThat(httpEntity.body!!["vedleggSpesifikasjon:0"].toString().contains("text/plain;charset=UTF-8")) - assertThat(httpEntity.body!!["vedleggSpesifikasjon:1"].toString().contains("text/plain;charset=UTF-8")) + FilForOpplasting("filnavn1", "image/jpg", 1L, fil2)) + val body = fiksClient.createBodyForUpload(JsonVedleggSpesifikasjon(), files) + + assertThat(body.size == 5) + assertThat(body.keys.contains("vedlegg.json")) + assertThat(body.keys.contains("vedleggSpesifikasjon:0")) + assertThat(body.keys.contains("dokument:0")) + assertThat(body.keys.contains("vedleggSpesifikasjon:1")) + assertThat(body.keys.contains("dokument:1")) + assertThat(body["dokument:0"].toString().contains("InputStream resource")) + assertThat(body["dokument:1"].toString().contains("InputStream resource")) + assertThat(body["vedlegg.json"].toString().contains("text/plain;charset=UTF-8")) + assertThat(body["vedleggSpesifikasjon:0"].toString().contains("text/plain;charset=UTF-8")) + assertThat(body["vedleggSpesifikasjon:1"].toString().contains("text/plain;charset=UTF-8")) } } \ No newline at end of file diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt index b4b09a8bf..af667521c 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt @@ -53,4 +53,5 @@ val ok_digisossak_response = """ "timestampSistOppdatert": 0 } } -""".trimIndent() \ No newline at end of file +""".trimIndent() + From c5dcf58a4b6ffc59273d7cda3352495c3439b138 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Mon, 15 Mar 2021 17:02:19 +0100 Subject: [PATCH 22/32] asdf --- .../innsyn/config/ProxiedWebClientConfig.kt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt index 630e00b0c..ae647c846 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt @@ -11,29 +11,25 @@ import reactor.netty.http.client.HttpClient @Profile("!(mock|mock-alt|local)") @Configuration -class ProxiedWebClientConfig( - private val webClientBuilder: WebClient.Builder -) { +class ProxiedWebClientConfig { @Value("\${HTTPS_PROXY}") private lateinit var proxyUrl: String @Bean fun proxiedWebClientBuilder(): WebClient.Builder = - webClientBuilder + WebClient.builder() .clientConnector(getReactorClientHttpConnector(proxyUrl)) } @Profile("mock|mock-alt|local") @Configuration -class MockProxiedWebClientConfig( - private val webClientBuilder: WebClient.Builder -) { +class MockProxiedWebClientConfig { @Bean fun proxiedWebClientBuilder(): WebClient.Builder = - webClientBuilder + WebClient.builder() .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) } \ No newline at end of file From 663d6c7a2bf250484a465ac7b4ee0ae07db7e937 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 11:48:46 +0100 Subject: [PATCH 23/32] asdf --- .../sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt | 5 ++++- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt | 5 ++++- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt | 4 ++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt index 72d231b6d..e8ac2c09a 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt @@ -24,7 +24,10 @@ interface DokumentlagerClient { @Component class DokumentlagerClientImpl( private val clientProperties: ClientProperties, - private val fiksWebClient: WebClient, + private val proxiedWebClientBuilder: WebClient.Builder, + private val fiksWebClient: WebClient = proxiedWebClientBuilder + .baseUrl(clientProperties.fiksDigisosEndpointUrl) + .build(), ) : DokumentlagerClient { override fun getDokumentlagerPublicKeyX509Certificate(token: String): X509Certificate { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt index c210af46f..ff2a79b3c 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt @@ -39,7 +39,10 @@ import java.util.function.Predicate @Component class FiksClientImpl( private val clientProperties: ClientProperties, - private val fiksWebClient: WebClient, + private val proxiedWebClientBuilder: WebClient.Builder, + private val fiksWebClient: WebClient = proxiedWebClientBuilder + .baseUrl(clientProperties.fiksDigisosEndpointUrl) + .build(), private val retryProperties: FiksRetryProperties, private val redisService: RedisService, ) : FiksClient { diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt index dfba67cd3..79938cb08 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt @@ -47,7 +47,7 @@ internal class FiksClientTest { private val retryProperties: FiksRetryProperties = mockk() private val ettersendelsePdfGenerator: EttersendelsePdfGenerator = mockk() private val krypteringService: KrypteringService = mockk() - private val fiksClient = FiksClientImpl(clientProperties, fiksWebClient, retryProperties, redisService) + private val fiksClient = FiksClientImpl(clientProperties, WebClient.builder(), fiksWebClient, retryProperties, redisService) private val id = "123" @@ -253,7 +253,7 @@ internal class FiksClientTest { @Test // fikk ikke mockWebServer til å funke her uten å skjønner hvorfor (InputStream-relatert), så gikk for "klassisk" mockk stil fun `POST ny ettersendelse`() { val webClient: WebClient = mockk() - val clientForPost = FiksClientImpl(clientProperties, webClient, retryProperties, redisService) + val clientForPost = FiksClientImpl(clientProperties, WebClient.builder(), webClient, retryProperties, redisService) val fil1: InputStream = mockk() val fil2: InputStream = mockk() From 37b21cc76c18b3629e84ab8aae3d6a32c043b21a Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 12:51:12 +0100 Subject: [PATCH 24/32] hmhm --- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt | 5 +---- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt | 5 +++-- .../innsyn/client/fiks/KommuneInfoClientConfig.kt | 4 ++-- .../innsyn/client/idporten/IdPortenClientConfig.kt | 4 ++-- .../nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt | 6 ++++-- .../no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt | 4 ++-- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt index ff2a79b3c..c210af46f 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientImpl.kt @@ -39,10 +39,7 @@ import java.util.function.Predicate @Component class FiksClientImpl( private val clientProperties: ClientProperties, - private val proxiedWebClientBuilder: WebClient.Builder, - private val fiksWebClient: WebClient = proxiedWebClientBuilder - .baseUrl(clientProperties.fiksDigisosEndpointUrl) - .build(), + private val fiksWebClient: WebClient, private val retryProperties: FiksRetryProperties, private val redisService: RedisService, ) : FiksClient { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt index 42023d7a1..7baae2e66 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt @@ -7,13 +7,14 @@ import org.springframework.web.reactive.function.client.WebClient @Configuration class FiksConfig ( - private val proxiedWebClientBuilder: WebClient.Builder, + private val proxiedWebClient: WebClient, private val clientProperties: ClientProperties ) { @Bean fun fiksWebClient(): WebClient = - proxiedWebClientBuilder + proxiedWebClient + .mutate() .baseUrl(clientProperties.fiksDigisosEndpointUrl) .build() } \ No newline at end of file diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt index 2e09b5d78..780a96153 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/KommuneInfoClientConfig.kt @@ -12,14 +12,14 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class KommuneInfoClientConfig( - private val proxiedWebClientBuilder: WebClient.Builder, + private val proxiedWebClient: WebClient, private val clientProperties: ClientProperties ) { @Bean fun kommuneInfoClient(): KommuneInfoClient { return KommuneInfoClientImpl( - proxiedWebClientBuilder.build(), + proxiedWebClient, fiksProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt index 07b7093bc..3242d552f 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/idporten/IdPortenClientConfig.kt @@ -12,7 +12,7 @@ import org.springframework.web.reactive.function.client.WebClient @Profile("!mock") @Configuration class IdPortenClientConfig( - private val proxiedWebClientBuilder: WebClient.Builder, + private val proxiedWebClient: WebClient, @Value("\${no.nav.sosialhjelp.idporten.token_url}") private val tokenUrl: String, @Value("\${no.nav.sosialhjelp.idporten.client_id}") private val clientId: String, @Value("\${no.nav.sosialhjelp.idporten.scope}") private val scope: String, @@ -24,7 +24,7 @@ class IdPortenClientConfig( @Bean fun idPortenClient(): IdPortenClient { return IdPortenClient( - webClient = proxiedWebClientBuilder.build(), + webClient = proxiedWebClient, idPortenProperties = idPortenProperties() ) } diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt index ae647c846..24bfa8a68 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt @@ -17,9 +17,10 @@ class ProxiedWebClientConfig { private lateinit var proxyUrl: String @Bean - fun proxiedWebClientBuilder(): WebClient.Builder = + fun proxiedWebClient(): WebClient = WebClient.builder() .clientConnector(getReactorClientHttpConnector(proxyUrl)) + .build() } @@ -28,8 +29,9 @@ class ProxiedWebClientConfig { class MockProxiedWebClientConfig { @Bean - fun proxiedWebClientBuilder(): WebClient.Builder = + fun proxiedWebClient(): WebClient = WebClient.builder() .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) + .build() } \ No newline at end of file diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt index 4c376cf19..48c95d76a 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/ApplicationContextTest.kt @@ -16,8 +16,8 @@ class ApplicationContextTest { @MockkBean private lateinit var idPortenClient: IdPortenClient - @MockkBean(name = "proxiedWebClientBuilder", relaxed = true) - private lateinit var proxiedWebClientBuilder: WebClient.Builder + @MockkBean(name = "proxiedWebClient", relaxed = true) + private lateinit var proxiedWebClient: WebClient @MockkBean private lateinit var proxiedWebClientConfig: ProxiedWebClientConfig diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt index 79938cb08..dfba67cd3 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksClientTest.kt @@ -47,7 +47,7 @@ internal class FiksClientTest { private val retryProperties: FiksRetryProperties = mockk() private val ettersendelsePdfGenerator: EttersendelsePdfGenerator = mockk() private val krypteringService: KrypteringService = mockk() - private val fiksClient = FiksClientImpl(clientProperties, WebClient.builder(), fiksWebClient, retryProperties, redisService) + private val fiksClient = FiksClientImpl(clientProperties, fiksWebClient, retryProperties, redisService) private val id = "123" @@ -253,7 +253,7 @@ internal class FiksClientTest { @Test // fikk ikke mockWebServer til å funke her uten å skjønner hvorfor (InputStream-relatert), så gikk for "klassisk" mockk stil fun `POST ny ettersendelse`() { val webClient: WebClient = mockk() - val clientForPost = FiksClientImpl(clientProperties, WebClient.builder(), webClient, retryProperties, redisService) + val clientForPost = FiksClientImpl(clientProperties, webClient, retryProperties, redisService) val fil1: InputStream = mockk() val fil2: InputStream = mockk() From 6770e278880535a9ed54ae834b38ccef78f12ddd Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 13:11:54 +0100 Subject: [PATCH 25/32] max memory size for fiksWebClient --- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt index 7baae2e66..4b039ae4b 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt @@ -6,9 +6,9 @@ import org.springframework.context.annotation.Configuration import org.springframework.web.reactive.function.client.WebClient @Configuration -class FiksConfig ( +class FiksConfig( private val proxiedWebClient: WebClient, - private val clientProperties: ClientProperties + private val clientProperties: ClientProperties, ) { @Bean @@ -16,5 +16,6 @@ class FiksConfig ( proxiedWebClient .mutate() .baseUrl(clientProperties.fiksDigisosEndpointUrl) + .codecs { it.defaultCodecs().maxInMemorySize(16 * 1024 * 1024) } .build() } \ No newline at end of file From 09e74c0ce0d37f86b86913614a06ee4642e19978 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 13:34:30 +0100 Subject: [PATCH 26/32] cleanup --- .../no/nav/sosialhjelp/innsyn/utils/Utils.kt | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt index 823806f47..931971904 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt @@ -16,10 +16,7 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory import org.slf4j.MDC import org.springframework.core.ParameterizedTypeReference -import org.springframework.http.HttpStatus -import org.springframework.web.client.HttpStatusCodeException import org.springframework.web.reactive.function.client.WebClientResponseException -import java.io.IOException import java.sql.Timestamp import java.time.Instant import java.time.LocalDate @@ -57,7 +54,7 @@ fun hentDokumentlagerUrl(clientProperties: ClientProperties, dokumentlagerId: St fun String.toLocalDateTime(): LocalDateTime { return ZonedDateTime.parse(this, ISO_DATE_TIME) - .withZoneSameInstant(ZoneId.of("Europe/Oslo")).toLocalDateTime() + .withZoneSameInstant(ZoneId.of("Europe/Oslo")).toLocalDateTime() } fun String.toLocalDate(): LocalDate = LocalDate.parse(this, ISO_LOCAL_DATE) @@ -75,7 +72,7 @@ fun formatLocalDateTime(dato: LocalDateTime): String { return dato.format(datoFormatter) } -fun soknadsalderIMinutter(tidspunktSendt : LocalDateTime?) : Long { +fun soknadsalderIMinutter(tidspunktSendt: LocalDateTime?): Long { return tidspunktSendt?.until(LocalDateTime.now(), ChronoUnit.MINUTES) ?: -1 } @@ -91,9 +88,9 @@ fun enumNameToLowercase(string: String): String { */ fun lagNavEksternRefId(digisosSak: DigisosSak): String { val previousId: String = digisosSak.ettersendtInfoNAV?.ettersendelser - ?.map { it.navEksternRefId }?.maxByOrNull { it.takeLast(COUNTER_SUFFIX_LENGTH).toLong() } - ?: digisosSak.originalSoknadNAV?.navEksternRefId?.plus("0000") - ?: digisosSak.fiksDigisosId.plus("0000") + ?.map { it.navEksternRefId }?.maxByOrNull { it.takeLast(COUNTER_SUFFIX_LENGTH).toLong() } + ?: digisosSak.originalSoknadNAV?.navEksternRefId?.plus("0000") + ?: digisosSak.fiksDigisosId.plus("0000") val nesteSuffix = lagIdSuffix(previousId) return (previousId.dropLast(COUNTER_SUFFIX_LENGTH).plus(nesteSuffix)) @@ -123,14 +120,6 @@ fun isRunningInProd(): Boolean { return clusterName != null && clusterName.contains("prod") } -fun T.toFiksErrorMessage(): ErrorMessage? { - return try { - objectMapper.readValue(this.responseBodyAsByteArray, ErrorMessage::class.java) - } catch (e: IOException) { - null - } -} - fun messageUtenFnr(e: WebClientResponseException): String { val fiksErrorMessage = e.toFiksErrorMessage()?.feilmeldingUtenFnr val message = e.message?.feilmeldingUtenFnr @@ -147,9 +136,6 @@ val ErrorMessage.feilmeldingUtenFnr: String? return this.message?.feilmeldingUtenFnr } -fun withStatusCode(t: Throwable): HttpStatus? = - (t as? WebClientResponseException)?.statusCode - fun runAsyncWithMDC(runnable: Runnable, executor: ExecutorService): CompletableFuture { val previous: Map = MDC.getCopyOfContextMap() return CompletableFuture.runAsync(Runnable { From 86f8983eb0ea12d9b12ba25bcb8821280c54fe45 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 13:41:23 +0100 Subject: [PATCH 27/32] cleanup --- .../kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt index 931971904..14ff18d4f 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/utils/Utils.kt @@ -126,15 +126,11 @@ fun messageUtenFnr(e: WebClientResponseException): String { return "$message - $fiksErrorMessage" } -val String.feilmeldingUtenFnr: String? - get() { - return this.replace(Regex("""\b[0-9]{11}\b"""), "[FNR]") - } +val String.feilmeldingUtenFnr: String + get() = this.replace(Regex("""\b[0-9]{11}\b"""), "[FNR]") val ErrorMessage.feilmeldingUtenFnr: String? - get() { - return this.message?.feilmeldingUtenFnr - } + get() = this.message?.feilmeldingUtenFnr fun runAsyncWithMDC(runnable: Runnable, executor: ExecutorService): CompletableFuture { val previous: Map = MDC.getCopyOfContextMap() From b6430cd33428e7dc218784da67a66fbf5399905d Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 13:42:42 +0100 Subject: [PATCH 28/32] cleanup --- .../no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt b/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt index af667521c..b4b09a8bf 100644 --- a/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt +++ b/src/test/kotlin/no/nav/sosialhjelp/innsyn/responses/DigisosSakResponses.kt @@ -53,5 +53,4 @@ val ok_digisossak_response = """ "timestampSistOppdatert": 0 } } -""".trimIndent() - +""".trimIndent() \ No newline at end of file From 01783855c61bbae3c91cd63cc5f8a1c520117568 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 14:25:20 +0100 Subject: [PATCH 29/32] =?UTF-8?q?ryddings.=20pr=C3=B8ver=20=C3=A5=20ta=20i?= =?UTF-8?q?=20bruk=20springs=20webClientBuilder,=20som=20skal=20ha=20en=20?= =?UTF-8?q?del=20default=20greier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt | 5 +---- .../no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt | 3 +-- .../no/nav/sosialhjelp/innsyn/client/sts/StsConfig.kt | 5 ++--- .../sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt | 8 ++++---- .../no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt | 7 +++---- 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt index e8ac2c09a..72d231b6d 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/DokumentlagerClient.kt @@ -24,10 +24,7 @@ interface DokumentlagerClient { @Component class DokumentlagerClientImpl( private val clientProperties: ClientProperties, - private val proxiedWebClientBuilder: WebClient.Builder, - private val fiksWebClient: WebClient = proxiedWebClientBuilder - .baseUrl(clientProperties.fiksDigisosEndpointUrl) - .build(), + private val fiksWebClient: WebClient, ) : DokumentlagerClient { override fun getDokumentlagerPublicKeyX509Certificate(token: String): X509Certificate { diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt index 287352ee2..a5eb5ec61 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/norg/NorgConfig.kt @@ -10,11 +10,10 @@ import reactor.netty.http.client.HttpClient @Configuration class NorgConfig( private val clientProperties: ClientProperties, - private val webClientBuilder: WebClient.Builder ) { @Bean - fun norgWebClient(): WebClient = + fun norgWebClient(webClientBuilder: WebClient.Builder): WebClient = webClientBuilder .baseUrl(clientProperties.norgEndpointUrl) .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/sts/StsConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/sts/StsConfig.kt index 3aca1a396..0af272a4a 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/sts/StsConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/sts/StsConfig.kt @@ -15,12 +15,11 @@ import java.util.Base64 @Profile("!(mock | local)") @Configuration class StsConfig ( - private val clientProperties: ClientProperties, - private val webClientBuilder: WebClient.Builder + private val clientProperties: ClientProperties ) { @Bean - fun stsWebClient(): WebClient = + fun stsWebClient(webClientBuilder: WebClient.Builder): WebClient = webClientBuilder .baseUrl(clientProperties.stsTokenEndpointUrl) .defaultHeader(HttpHeaders.AUTHORIZATION, "Basic ${credentials()}") diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt index 24bfa8a68..25e10d8f8 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/ProxiedWebClientConfig.kt @@ -17,8 +17,8 @@ class ProxiedWebClientConfig { private lateinit var proxyUrl: String @Bean - fun proxiedWebClient(): WebClient = - WebClient.builder() + fun proxiedWebClient(webClientBuilder: WebClient.Builder): WebClient = + webClientBuilder .clientConnector(getReactorClientHttpConnector(proxyUrl)) .build() @@ -29,8 +29,8 @@ class ProxiedWebClientConfig { class MockProxiedWebClientConfig { @Bean - fun proxiedWebClient(): WebClient = - WebClient.builder() + fun proxiedWebClient(webClientBuilder: WebClient.Builder): WebClient = + webClientBuilder .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) .build() diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt index e4aa19fae..7711b6ca2 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/config/WebClientConfig.kt @@ -7,11 +7,10 @@ import org.springframework.web.reactive.function.client.WebClient import reactor.netty.http.client.HttpClient @Configuration -class WebClientConfig( - private val webClientBuilder: WebClient.Builder, -) { +class WebClientConfig { + @Bean - fun webClient(): WebClient = + fun webClient(webClientBuilder: WebClient.Builder): WebClient = webClientBuilder .clientConnector(ReactorClientHttpConnector(HttpClient.newConnection())) .build() From 53f0e9fdf920b09f32e1c3be7df11b643b29f3cb Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Tue, 16 Mar 2021 14:53:57 +0100 Subject: [PATCH 30/32] =?UTF-8?q?fjern=20exception=20fra=20error-logging?= =?UTF-8?q?=20i=20exceptionHandler.=20Disse=20loggmeldingene=20gir=20sv?= =?UTF-8?q?=C3=A6rt=20lange=20loggmeldinger=20(for=20lange=20for=20Kibana)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sosialhjelp/innsyn/common/InnsynExceptionHandler.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt index c56ec6f10..81db54f68 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/common/InnsynExceptionHandler.kt @@ -43,28 +43,28 @@ class InnsynExceptionHandler : ResponseEntityExceptionHandler() { @ExceptionHandler(FiksNotFoundException::class) fun handleFiksNotFoundError(e: FiksNotFoundException): ResponseEntity { - log.error("DigisosSak finnes ikke i FIKS ", e) + log.error("DigisosSak finnes ikke i FIKS ") val error = FrontendErrorMessage(FIKS_ERROR, "DigisosSak finnes ikke") return ResponseEntity(error, HttpStatus.NOT_FOUND) } @ExceptionHandler(FiksException::class) fun handleFiksError(e: FiksException): ResponseEntity { - log.error("Noe feilet ved kall til Fiks", e) + log.error("Noe feilet ved kall til Fiks") val error = FrontendErrorMessage(FIKS_ERROR, NOE_UVENTET_FEILET) return ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR) } @ExceptionHandler(FiksClientException::class) fun handleFiksClientError(e: FiksClientException): ResponseEntity { - log.error("Client-feil ved kall til Fiks", e) + log.error("Client-feil ved kall til Fiks") val error = FrontendErrorMessage(FIKS_ERROR, NOE_UVENTET_FEILET) return ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR) } @ExceptionHandler(FiksServerException::class) fun handleFiksServerError(e: FiksServerException): ResponseEntity { - log.error("Server-feil ved kall til Fiks", e) + log.error("Server-feil ved kall til Fiks") val error = FrontendErrorMessage(FIKS_ERROR, NOE_UVENTET_FEILET) return ResponseEntity(error, HttpStatus.INTERNAL_SERVER_ERROR) } From 0eb69998b9f068e6330ffaafc58829c3369189b7 Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Wed, 17 Mar 2021 11:34:19 +0100 Subject: [PATCH 31/32] fiksWebClient konfigurert med objectMapper --- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt index 4b039ae4b..bb486e5ee 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt @@ -1,8 +1,11 @@ package no.nav.sosialhjelp.innsyn.client.fiks import no.nav.sosialhjelp.innsyn.config.ClientProperties +import no.nav.sosialhjelp.innsyn.utils.objectMapper import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration +import org.springframework.http.codec.json.Jackson2JsonDecoder +import org.springframework.http.codec.json.Jackson2JsonEncoder import org.springframework.web.reactive.function.client.WebClient @Configuration @@ -16,6 +19,10 @@ class FiksConfig( proxiedWebClient .mutate() .baseUrl(clientProperties.fiksDigisosEndpointUrl) - .codecs { it.defaultCodecs().maxInMemorySize(16 * 1024 * 1024) } + .codecs { + it.defaultCodecs().maxInMemorySize(16 * 1024 * 1024) + it.customCodecs().register(Jackson2JsonDecoder(objectMapper)) + it.customCodecs().register(Jackson2JsonEncoder(objectMapper)) + } .build() } \ No newline at end of file From 4e335fe56244b6172fd3fe974b1eb269b011e37b Mon Sep 17 00:00:00 2001 From: Martin Tveter Date: Wed, 17 Mar 2021 11:36:45 +0100 Subject: [PATCH 32/32] fiksWebClient konfigurert med objectMapper --- .../no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt index bb486e5ee..023ac1235 100644 --- a/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt +++ b/src/main/kotlin/no/nav/sosialhjelp/innsyn/client/fiks/FiksConfig.kt @@ -21,8 +21,8 @@ class FiksConfig( .baseUrl(clientProperties.fiksDigisosEndpointUrl) .codecs { it.defaultCodecs().maxInMemorySize(16 * 1024 * 1024) - it.customCodecs().register(Jackson2JsonDecoder(objectMapper)) - it.customCodecs().register(Jackson2JsonEncoder(objectMapper)) + it.defaultCodecs().jackson2JsonDecoder(Jackson2JsonDecoder(objectMapper)) + it.defaultCodecs().jackson2JsonEncoder(Jackson2JsonEncoder(objectMapper)) } .build() } \ No newline at end of file