Skip to content

Commit

Permalink
fix: add readTimeout to http connections
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Apr 22, 2024
1 parent 28ca0b8 commit fdb5916
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
19 changes: 10 additions & 9 deletions src/main/kotlin/no/fdk/userapi/adapter/AltinnAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL
import java.net.URI

private val logger = LoggerFactory.getLogger(AltinnAdapter::class.java)

@Service
class AltinnAdapter(private val hostProperties: HostProperties) {
private val sixtySeconds = 60000
private val twentySeconds = 20000

private fun altinnStream(url: URL): InputStream =
with(url.openConnection() as HttpURLConnection) {
connectTimeout = sixtySeconds
private fun altinnStream(uri: URI): InputStream =
with(uri.toURL().openConnection() as HttpURLConnection) {
connectTimeout = twentySeconds
readTimeout = twentySeconds
connect()
if (HttpStatus.resolve(responseCode)?.is2xxSuccessful == true) {
inputStream
Expand All @@ -31,9 +32,9 @@ class AltinnAdapter(private val hostProperties: HostProperties) {
}

private fun getReportees(socialSecurityNumber: String, serviceCode: String): List<AltinnSubject?> {
val url = URL("${hostProperties.altinnProxyHost}/api/serviceowner/reportees?ForceEIAuthentication&subject=$socialSecurityNumber&servicecode=$serviceCode&serviceedition=1&\$top=1000")
val uri = URI("${hostProperties.altinnProxyHost}/api/serviceowner/reportees?ForceEIAuthentication&subject=$socialSecurityNumber&servicecode=$serviceCode&serviceedition=1&\$top=1000")
return try {
jacksonObjectMapper().readValue(altinnStream(url))
jacksonObjectMapper().readValue(altinnStream(uri))
} catch (ex: Exception) {
logger.error("Unable to get reportees from Altinn", ex)
emptyList()
Expand All @@ -50,8 +51,8 @@ class AltinnAdapter(private val hostProperties: HostProperties) {

fun getRights(socialSecurityNumber: String, orgNumber: String): AltinnRightsResponse? =
try {
val url = URL("${hostProperties.altinnProxyHost}/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=${socialSecurityNumber}&reportee=${orgNumber}&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27")
jacksonObjectMapper().readValue(altinnStream(url))
val uri = URI("${hostProperties.altinnProxyHost}/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=${socialSecurityNumber}&reportee=${orgNumber}&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27")
jacksonObjectMapper().readValue(altinnStream(uri))
} catch (ex: Exception) {
logger.error("Unable to get rights from Altinn", ex)
null
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/no/fdk/userapi/adapter/TermsAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.slf4j.LoggerFactory
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import java.net.HttpURLConnection
import java.net.URL
import java.net.URI

private val logger = LoggerFactory.getLogger(TermsAdapter::class.java)

Expand All @@ -18,11 +18,12 @@ class TermsAdapter(
private val fiveSeconds = 5555

fun orgAcceptedTermsVersion(organization: String): String {
val url = URL("${hostProperties.termsHost}/terms/org/$organization/version")
val uri = URI("${hostProperties.termsHost}/terms/org/$organization/version")
try {
with(url.openConnection() as HttpURLConnection) {
with(uri.toURL().openConnection() as HttpURLConnection) {
setRequestProperty("X-API-KEY", securityProperties.userApiKey)
connectTimeout = fiveSeconds
readTimeout = fiveSeconds
connect()
if (HttpStatus.resolve(responseCode)?.is2xxSuccessful == true) {
inputStream.bufferedReader().use {
Expand Down
36 changes: 19 additions & 17 deletions src/test/kotlin/no/fdk/userapi/utils/ContractMock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package no.fdk.userapi.utils
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.client.WireMock.aResponse
import com.github.tomakehurst.wiremock.client.WireMock.get
import com.github.tomakehurst.wiremock.client.WireMock.ok
import com.github.tomakehurst.wiremock.client.WireMock.okJson
import com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo
import org.slf4j.LoggerFactory
import java.io.IOException
Expand All @@ -15,7 +17,7 @@ fun startMockServer() {
if(!mockserver.isRunning) {
mockserver.stubFor(
get(urlEqualTo("/ping"))
.willReturn(aResponse().withStatus(200)))
.willReturn(ok()))

mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=12345678901&servicecode=5755&serviceedition=1&\$top=1000"))
Expand All @@ -25,42 +27,42 @@ fun startMockServer() {
.willReturn(aResponse().withStatus(404)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5755&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5756&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=10987654321&servicecode=5977&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_0)))
.willReturn(okJson(ALTINN_PERSON_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&servicecode=5755&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/reportees?ForceEIAuthentication&subject=11223344556&servicecode=5756&serviceedition=1&\$top=1000"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_PERSON_1)))
.willReturn(okJson(ALTINN_PERSON_1)))

mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=10987654321&reportee=920210023&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_0)))
.willReturn(okJson(ALTINN_RIGHTS_0)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=11223344556&reportee=910258028&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_1)))
.willReturn(okJson(ALTINN_RIGHTS_1)))
mockserver.stubFor(
get(urlEqualTo("/altinn/api/serviceowner/authorization/rights?ForceEIAuthentication&subject=11223344556&reportee=123456789&%24filter=ServiceCode%20eq%20%275755%27%20or%20ServiceCode%20eq%20%275756%27%20or%20ServiceCode%20eq%20%275977%27"))
.willReturn(aResponse().withStatus(200).withBody(ALTINN_RIGHTS_2)))
.willReturn(okJson(ALTINN_RIGHTS_2)))

mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/920210023/version"))
.willReturn(aResponse().withStatus(200).withBody("1.2.3")))
.willReturn(ok("1.2.3")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/910258028/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.0")))
.willReturn(ok("1.0.0")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/123456789/version"))
.willReturn(aResponse().withStatus(404)))
Expand All @@ -70,16 +72,16 @@ fun startMockServer() {
.willReturn(aResponse().withStatus(404)))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/987592567/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.1")))
.willReturn(ok("1.0.1")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/923954791/version"))
.willReturn(aResponse().withStatus(200).withBody("12.16.11")))
.willReturn(ok("12.16.11")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/974760673/version"))
.willReturn(aResponse().withStatus(200).withBody("1.0.1")))
.willReturn(ok("1.0.1")))
mockserver.stubFor(
get(urlEqualTo("/terms/terms/org/974761076/version"))
.willReturn(aResponse().withStatus(200).withBody("1.1.1")))
.willReturn(ok("1.1.1")))

mockserver.start()
}
Expand Down

0 comments on commit fdb5916

Please sign in to comment.