From 571a41f1be781ec285642fe2e814ce121a070a25 Mon Sep 17 00:00:00 2001 From: Saeed Rezaee Date: Wed, 24 Jan 2024 11:56:13 +0100 Subject: [PATCH] Prioritize the target token in okHttp Refactored the code Signed-off-by: Saeed Rezaee --- .../org/eclipse/hara/ddi/api/DdiClientDefaultImpl.kt | 11 +++++------ .../HawkbitAuthenticationRequestInterceptor.kt | 5 +---- .../org/eclipse/hara/ddiclient/api/HaraClientData.kt | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/api/DdiClientDefaultImpl.kt b/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/api/DdiClientDefaultImpl.kt index b81c895..c82fcf7 100644 --- a/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/api/DdiClientDefaultImpl.kt +++ b/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/api/DdiClientDefaultImpl.kt @@ -21,7 +21,6 @@ import org.eclipse.hara.ddi.security.Authentication import org.eclipse.hara.ddi.security.HawkbitAuthenticationRequestInterceptor import java.io.InputStream import java.net.HttpURLConnection -import java.util.HashSet import java.util.concurrent.Executors import okhttp3.OkHttpClient import org.eclipse.hara.ddiclient.api.HaraClientData @@ -132,14 +131,14 @@ class DdiClientDefaultImpl private constructor(private val ddiRestApi: DdiRestAp val LOG = LoggerFactory.getLogger(DdiClient::class.java)!! fun of(haraClientData: HaraClientData, httpBuilder:OkHttpClient.Builder): DdiClientDefaultImpl { - val authentications = HashSet() + val authentications = mutableListOf() with(haraClientData) { - if (gatewayToken != null) { - authentications.add(Authentication.newInstance(Authentication.AuthenticationType.GATEWAY_TOKEN_AUTHENTICATION, gatewayToken!!)) - } - if (targetToken != null) { + if (!targetToken.isNullOrBlank()) { authentications.add(Authentication.newInstance(Authentication.AuthenticationType.TARGET_TOKEN_AUTHENTICATION, targetToken!!)) } + if (!gatewayToken.isNullOrBlank()) { + authentications.add(Authentication.newInstance(Authentication.AuthenticationType.GATEWAY_TOKEN_AUTHENTICATION, gatewayToken!!)) + } httpBuilder.interceptors().add(0, HawkbitAuthenticationRequestInterceptor(authentications)) val ddiRestApi = Retrofit.Builder() .baseUrl(serverUrl) diff --git a/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/security/HawkbitAuthenticationRequestInterceptor.kt b/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/security/HawkbitAuthenticationRequestInterceptor.kt index e38ba27..d653bd7 100644 --- a/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/security/HawkbitAuthenticationRequestInterceptor.kt +++ b/ddi-consumer/src/main/kotlin/org/eclipse/hara/ddi/security/HawkbitAuthenticationRequestInterceptor.kt @@ -11,7 +11,6 @@ package org.eclipse.hara.ddi.security import java.io.IOException -import java.util.ArrayList import java.util.Objects import okhttp3.Interceptor import okhttp3.Response @@ -19,14 +18,12 @@ import okhttp3.Response /** * @author Daniele Sergio */ -class HawkbitAuthenticationRequestInterceptor(authentications: Set) : Interceptor { +class HawkbitAuthenticationRequestInterceptor(private val authentications: List) : Interceptor { - private val authentications: List private var authenticationUse = 0 init { Objects.requireNonNull(authentications) - this.authentications = ArrayList(authentications) } @Throws(IOException::class) diff --git a/hara-ddiclient-api/src/main/kotlin/org/eclipse/hara/ddiclient/api/HaraClientData.kt b/hara-ddiclient-api/src/main/kotlin/org/eclipse/hara/ddiclient/api/HaraClientData.kt index bdbe709..46b8657 100644 --- a/hara-ddiclient-api/src/main/kotlin/org/eclipse/hara/ddiclient/api/HaraClientData.kt +++ b/hara-ddiclient-api/src/main/kotlin/org/eclipse/hara/ddiclient/api/HaraClientData.kt @@ -38,7 +38,7 @@ data class HaraClientData constructor( notEmpty(controllerId, "controllerId") notEmpty(serverUrl, "serverUrl") validUrl(serverUrl, "serverUrl") - if ((gatewayToken == null || gatewayToken.isBlank()) && (targetToken == null || targetToken.isBlank())) { + if (gatewayToken.isNullOrBlank() && targetToken.isNullOrBlank()) { throw IllegalStateException("gatewayToken and targetToken cannot both be empty") } }