Skip to content

Commit

Permalink
Merge pull request #446 from ks-no/New-MaskinportException-Temorarily…
Browse files Browse the repository at this point in the history
…Unavailable

Added new MaskinportenException for temporarily timeout response from maskinporten
  • Loading branch information
181182 authored Nov 7, 2023
2 parents 7482002 + 7432880 commit 6f7d5dc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import net.jodah.expiringmap.ExpiringMap
import net.jodah.expiringmap.ExpiringValue
import no.ks.fiks.maskinporten.error.MaskinportenClientTokenRequestException
import no.ks.fiks.maskinporten.error.MaskinportenTokenRequestException
import no.ks.fiks.maskinporten.error.MaskinportenTokenTemporarilyUnavailableException
import org.apache.hc.client5.http.config.RequestConfig
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder
Expand Down Expand Up @@ -223,17 +224,35 @@ class Maskinportenklient(
log.debug { "Access token response received in $timeUsed ms with status $responseCode" }
logConnectionManager(timeUsed, connectionManager)
return if (HttpStatus.SC_OK == responseCode) {
classicHttpResponse.entity.content?.use { contentStream -> contentStream.bufferedReader().use { it.readText() } }
} else {
val errorFromMaskinporten: String = classicHttpResponse.entity.content.use { errorContentStream ->
errorContentStream.bufferedReader().use { it.readText() }
classicHttpResponse.entity.content?.use { contentStream ->
contentStream.bufferedReader().use { it.readText() }
}
} else {
val errorFromMaskinporten: String =
classicHttpResponse.entity.content.use { errorContentStream ->
errorContentStream.bufferedReader().use { it.readText() }
}
log.warn { "Failed to get token: $errorFromMaskinporten" }
val exceptionMessage = "Http response code: $responseCode, url: '$tokenEndpointUrlString', message: '$errorFromMaskinporten'"
val exceptionMessage =
"Http response code: $responseCode, url: '$tokenEndpointUrlString', message: '$errorFromMaskinporten'"
if (responseCode >= HttpStatus.SC_BAD_REQUEST && responseCode < HttpStatus.SC_INTERNAL_SERVER_ERROR) {
throw MaskinportenClientTokenRequestException(exceptionMessage, responseCode, errorFromMaskinporten)
throw MaskinportenClientTokenRequestException(
exceptionMessage,
responseCode,
errorFromMaskinporten
)
} else if (responseCode == HttpStatus.SC_SERVICE_UNAVAILABLE) {
throw MaskinportenTokenTemporarilyUnavailableException(
exceptionMessage,
responseCode,
errorFromMaskinporten
)
} else {
throw MaskinportenTokenRequestException(exceptionMessage, responseCode, errorFromMaskinporten)
throw MaskinportenTokenRequestException(
exceptionMessage,
responseCode,
errorFromMaskinporten
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package no.ks.fiks.maskinporten.error

class MaskinportenTokenTemporarilyUnavailableException(message: String?, statusCode: Int, maskinportenError: String) : MaskinportenTokenRequestException(message, statusCode, maskinportenError)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.netty.handler.codec.http.HttpMethod;
import no.ks.fiks.maskinporten.error.MaskinportenClientTokenRequestException;
import no.ks.fiks.maskinporten.error.MaskinportenTokenRequestException;
import no.ks.fiks.maskinporten.error.MaskinportenTokenTemporarilyUnavailableException;
import no.ks.fiks.virksomhetsertifikat.Sertifikat;
import no.ks.fiks.virksomhetsertifikat.SertifikatType;
import no.ks.fiks.virksomhetsertifikat.VirksomhetSertifikater;
Expand Down Expand Up @@ -238,6 +239,30 @@ void getAccessTokenFails() {
}
}

@DisplayName("Generate access token fails. Maskinporten returns temporarily unavailable.")
@Test
void getAccessTokenFailsTemporarilyUnavailable() {
try (final ClientAndServer client = ClientAndServer.startClientAndServer()) {
client.when(
request()
.withSecure(false)
.withMethod(HttpMethod.POST.name())
.withPath("/token")
.withBody(
params(
param("grant_type", JWT_BEARER_GRANT)
)
),
Times.exactly(1))
.respond(response()
.withBody("FAILURE WAS AN OPTION AFTER ALL")
.withStatusCode(HttpStatusCode.SERVICE_UNAVAILABLE_503.code()));
final Maskinportenklient maskinportenklient = createClient(String.format("http://localhost:%s/token", client.getLocalPort()));
final MaskinportenTokenTemporarilyUnavailableException exception = catchThrowableOfType(() -> maskinportenklient.getAccessToken(SCOPE), MaskinportenTokenTemporarilyUnavailableException.class);
assertThat(exception.getStatusCode()).isEqualTo(HttpStatusCode.SERVICE_UNAVAILABLE_503.code());
}
}

@DisplayName("Generate access token fails due to a connection failure")
@Timeout(value = 60)
@Test
Expand Down

0 comments on commit 6f7d5dc

Please sign in to comment.