From 938397f1516fc87b8a323e52e5a9217a95c4975b Mon Sep 17 00:00:00 2001 From: Mohammad Ghazanfar Ali Danish <62088117+mdanish98@users.noreply.github.com> Date: Wed, 31 Jul 2024 08:12:13 +0200 Subject: [PATCH] Fixes token retrieval mechanism issue in Authorized version of Connected client versions in AAS/SM Service (#366) Signed-off-by: Mohammad Ghazanfar Ali Danish --- .../client/ConnectedAasManager.java | 2 +- .../client/AuthorizedConnectedAasService.java | 14 +-- .../AssetAdministrationShellServiceApi.java | 103 ++++++++++++------ .../AuthorizedConnectedSubmodelService.java | 15 +-- .../client/internal/SubmodelServiceApi.java | 99 +++++++++++------ 5 files changed, 138 insertions(+), 95 deletions(-) diff --git a/basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManager.java b/basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManager.java index a9ab86892..ab0a5f74d 100644 --- a/basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManager.java +++ b/basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/ConnectedAasManager.java @@ -145,7 +145,7 @@ public ConnectedSubmodelService getSubmodelService(String identifier) { public List getAllSubmodels(String shellIdentifier) { AssetAdministrationShell shell = getAasService(shellIdentifier).getAAS(); List submodelReferences = shell.getSubmodels(); - return submodelReferences.parallelStream() + return submodelReferences.stream() .map(this::extractSubmodelIdentifierFromReference) .map(this::getSubmodelService) .collect(Collectors.toList()); diff --git a/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/AuthorizedConnectedAasService.java b/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/AuthorizedConnectedAasService.java index 270f8d1b4..9f1f2ed47 100644 --- a/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/AuthorizedConnectedAasService.java +++ b/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/AuthorizedConnectedAasService.java @@ -25,9 +25,6 @@ package org.eclipse.digitaltwin.basyx.aasservice.client; -import java.io.IOException; -import java.net.http.HttpRequest; - import org.eclipse.digitaltwin.basyx.aasservice.client.internal.AssetAdministrationShellServiceApi; import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager; @@ -46,16 +43,7 @@ public class AuthorizedConnectedAasService extends ConnectedAasService { * @param tokenManager */ public AuthorizedConnectedAasService(String repoUrl, TokenManager tokenManager) { - super(new AssetAdministrationShellServiceApi(repoUrl, getRequestBuilder(tokenManager))); - } - - private static HttpRequest.Builder getRequestBuilder(TokenManager tokenManager) { - try { - return HttpRequest.newBuilder() - .header("Authorization", "Bearer " + tokenManager.getAccessToken()); - } catch (IOException e) { - throw new RuntimeException("Unable to request access token"); - } + super(new AssetAdministrationShellServiceApi(repoUrl, tokenManager)); } } diff --git a/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal/AssetAdministrationShellServiceApi.java b/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal/AssetAdministrationShellServiceApi.java index 4b444cc5b..af304ec72 100644 --- a/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal/AssetAdministrationShellServiceApi.java +++ b/basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal/AssetAdministrationShellServiceApi.java @@ -57,6 +57,8 @@ import org.eclipse.digitaltwin.basyx.client.internal.ApiException; import org.eclipse.digitaltwin.basyx.client.internal.ApiResponse; import org.eclipse.digitaltwin.basyx.client.internal.Pair; +import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager; +import org.eclipse.digitaltwin.basyx.core.exceptions.AccessTokenRetrievalException; import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; import org.eclipse.digitaltwin.basyx.http.description.ServiceDescription; import org.eclipse.digitaltwin.basyx.http.pagination.Base64UrlEncodedCursorResult; @@ -73,36 +75,33 @@ public class AssetAdministrationShellServiceApi { private final Consumer memberVarInterceptor; private final Duration memberVarReadTimeout; private final Consumer> memberVarResponseInterceptor; - private HttpRequest.Builder httpRequestBuilder; + private TokenManager tokenManager; public AssetAdministrationShellServiceApi() { this(new ApiClient()); - this.httpRequestBuilder = HttpRequest.newBuilder(); } - public AssetAdministrationShellServiceApi(HttpRequest.Builder httpRequestBuilder) { - this(); - this.httpRequestBuilder = httpRequestBuilder; -} + public AssetAdministrationShellServiceApi(TokenManager tokenManager) { + this(new ApiClient()); + this.tokenManager = tokenManager; + } public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri) { this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri)); - this.httpRequestBuilder = HttpRequest.newBuilder(); } - public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri, HttpRequest.Builder httpRequestBuilder) { - this(mapper, baseUri); - this.httpRequestBuilder = httpRequestBuilder; -} + public AssetAdministrationShellServiceApi(ObjectMapper mapper, String baseUri, TokenManager tokenManager) { + this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri)); + this.tokenManager = tokenManager; + } public AssetAdministrationShellServiceApi(String baseUri) { this(new ApiClient(HttpClient.newBuilder(), new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create()), baseUri)); - this.httpRequestBuilder = HttpRequest.newBuilder(); } - public AssetAdministrationShellServiceApi(String baseUri, HttpRequest.Builder httpRequestBuilder) { - this(baseUri); - this.httpRequestBuilder = httpRequestBuilder; + public AssetAdministrationShellServiceApi(String baseUri, TokenManager tokenManager) { + this(new ApiClient(HttpClient.newBuilder(), new JsonMapperFactory().create(new SimpleAbstractTypeResolverFactory().create()), baseUri)); + this.tokenManager = tokenManager; } @@ -201,13 +200,15 @@ private HttpRequest.Builder deleteSubmodelReferenceByIdRequestBuilder(String sub throw new ApiException(400, "Missing the required parameter 'submodelIdentifier' when calling deleteSubmodelReferenceById"); } + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/submodel-refs/{submodelIdentifier}".replace("{submodelIdentifier}", ApiClient.urlEncode(submodelIdentifier.toString())); - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -284,13 +285,15 @@ public ApiResponse deleteThumbnailWithHttpInfoNoUrlEncoding() throws ApiEx private HttpRequest.Builder deleteThumbnailRequestBuilder() throws ApiException { + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/asset-information/thumbnail"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -348,9 +351,9 @@ private ApiResponse>> getAllSubmode private HttpRequest.Builder getAllSubmodelReferencesRequestBuilder(Integer limit, String cursor) throws ApiException { + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/submodel-refs"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); List localVarQueryParams = new ArrayList<>(); StringJoiner localVarQueryStringJoiner = new StringJoiner("&"); @@ -369,6 +372,8 @@ private HttpRequest.Builder getAllSubmodelReferencesRequestBuilder(Integer limit } localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -440,11 +445,13 @@ public ApiResponse getAssetAdministrationShellWithHttp private HttpRequest.Builder getAssetAdministrationShellRequestBuilder() throws ApiException { - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); - + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + localVarRequestBuilder.uri(URI.create(memberVarBaseUri)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -523,6 +530,8 @@ private HttpRequest.Builder getAssetAdministrationShellReferenceRequestBuilder() localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -594,13 +603,15 @@ public ApiResponse getAssetInformationWithHttpInfoNoUrlEncodin private HttpRequest.Builder getAssetInformationRequestBuilder() throws ApiException { + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/asset-information"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -679,6 +690,8 @@ private HttpRequest.Builder getDescriptionRequestBuilder() throws ApiException { localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -748,13 +761,15 @@ public ApiResponse getThumbnailWithHttpInfoNoUrlEncoding() throws ApiExcep private HttpRequest.Builder getThumbnailRequestBuilder() throws ApiException { + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/asset-information/thumbnail"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/octet-stream, application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -833,14 +848,16 @@ private HttpRequest.Builder postSubmodelReferenceRequestBuilder(Reference refere throw new ApiException(400, "Missing the required parameter 'reference' when calling postSubmodelReference"); } + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/submodel-refs"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(reference); @@ -935,6 +952,8 @@ private HttpRequest.Builder putAssetAdministrationShellRequestBuilder(AssetAdmin localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(assetAdministrationShell); @@ -1021,14 +1040,16 @@ private HttpRequest.Builder putAssetInformationRequestBuilder(AssetInformation a throw new ApiException(400, "Missing the required parameter 'assetInformation' when calling putAssetInformation"); } + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); + String localVarPath = "/asset-information"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(assetInformation); @@ -1087,11 +1108,8 @@ private ApiResponse putThumbnailWithHttpInfoNoUrlEncoding(String fileName, } private HttpRequest.Builder putThumbnailRequestBuilder(String fileName, ContentType contentType, InputStream inputStream) throws ApiException { - + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/asset-information/thumbnail"; - - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); - localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); @@ -1118,6 +1136,8 @@ private HttpRequest.Builder putThumbnailRequestBuilder(String fileName, ContentT HttpRequest.BodyPublisher formDataPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> Channels.newInputStream(pipe.source())); localVarRequestBuilder.header("Content-Type", entity.getContentType().getValue()).method("PUT", formDataPublisher); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); @@ -1140,5 +1160,16 @@ private static String extractFileName(HttpHeaders headers) { private static String buildUniqueFilename() { return UUID.randomUUID().toString(); } + + private void addAuthorizationHeaderIfAuthIsEnabled(HttpRequest.Builder localVarRequestBuilder) { + if (tokenManager != null) { + try { + localVarRequestBuilder.header("Authorization", "Bearer " + tokenManager.getAccessToken()); + } catch (IOException e) { + e.printStackTrace(); + throw new AccessTokenRetrievalException("Unable to request access token"); + } + } + } -} +} \ No newline at end of file diff --git a/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/AuthorizedConnectedSubmodelService.java b/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/AuthorizedConnectedSubmodelService.java index 549d02881..0cdff6f7d 100644 --- a/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/AuthorizedConnectedSubmodelService.java +++ b/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/AuthorizedConnectedSubmodelService.java @@ -25,9 +25,6 @@ package org.eclipse.digitaltwin.basyx.submodelservice.client; -import java.io.IOException; -import java.net.http.HttpRequest; - import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager; import org.eclipse.digitaltwin.basyx.submodelservice.client.internal.SubmodelServiceApi; @@ -46,17 +43,7 @@ public class AuthorizedConnectedSubmodelService extends ConnectedSubmodelService * @param tokenManager */ public AuthorizedConnectedSubmodelService(String repoUrl, TokenManager tokenManager) { - super(new SubmodelServiceApi(repoUrl, getRequestBuilder(tokenManager))); - } - - private static HttpRequest.Builder getRequestBuilder(TokenManager tokenManager) { - try { - return HttpRequest.newBuilder() - .header("Authorization", "Bearer " + tokenManager.getAccessToken()); - } catch (IOException e) { - e.printStackTrace(); - throw new RuntimeException("Unable to request access token"); - } + super(new SubmodelServiceApi(repoUrl, tokenManager)); } } diff --git a/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/internal/SubmodelServiceApi.java b/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/internal/SubmodelServiceApi.java index 73eb5af24..90c7568f1 100644 --- a/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/internal/SubmodelServiceApi.java +++ b/basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/internal/SubmodelServiceApi.java @@ -56,6 +56,8 @@ import org.eclipse.digitaltwin.basyx.client.internal.ApiException; import org.eclipse.digitaltwin.basyx.client.internal.ApiResponse; import org.eclipse.digitaltwin.basyx.client.internal.Pair; +import org.eclipse.digitaltwin.basyx.client.internal.authorization.TokenManager; +import org.eclipse.digitaltwin.basyx.core.exceptions.AccessTokenRetrievalException; import org.eclipse.digitaltwin.basyx.core.pagination.CursorResult; import org.eclipse.digitaltwin.basyx.http.pagination.Base64UrlEncodedCursorResult; import org.eclipse.digitaltwin.basyx.submodelservice.value.SubmodelElementValue; @@ -73,38 +75,36 @@ public class SubmodelServiceApi { private final Consumer memberVarInterceptor; private final Duration memberVarReadTimeout; private final Consumer> memberVarResponseInterceptor; - private final Consumer> memberVarAsyncResponseInterceptor; - private HttpRequest.Builder httpRequestBuilder; + private final Consumer> memberVarAsyncResponseInterceptor; + + private TokenManager tokenManager; public SubmodelServiceApi() { this(new ApiClient()); - this.httpRequestBuilder = HttpRequest.newBuilder(); } - public SubmodelServiceApi(HttpRequest.Builder httpRequestBuilder) { - this(); - this.httpRequestBuilder = httpRequestBuilder; + public SubmodelServiceApi(TokenManager tokenManager) { + this(new ApiClient()); + this.tokenManager = tokenManager; } public SubmodelServiceApi(ObjectMapper mapper, String baseUri) { this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri)); - this.httpRequestBuilder = HttpRequest.newBuilder(); } - public SubmodelServiceApi(ObjectMapper mapper, String baseUri, HttpRequest.Builder httpRequestBuilder) { - this(mapper, baseUri); - this.httpRequestBuilder = httpRequestBuilder; + public SubmodelServiceApi(ObjectMapper mapper, String baseUri, TokenManager tokenManager) { + this(new ApiClient(HttpClient.newBuilder(), mapper, baseUri)); + this.tokenManager = tokenManager; } - public SubmodelServiceApi(String baseUri) { + public SubmodelServiceApi(String baseUri) { this(new ApiClient(HttpClient.newBuilder(), new SubmodelSpecificJsonMapperFactory().create(), baseUri)); - this.httpRequestBuilder = HttpRequest.newBuilder(); - } + } - public SubmodelServiceApi(String baseUri, HttpRequest.Builder httpRequestBuilder) { - this(baseUri); - this.httpRequestBuilder = httpRequestBuilder; - } + public SubmodelServiceApi(String baseUri, TokenManager tokenManager) { + this(new ApiClient(HttpClient.newBuilder(), new SubmodelSpecificJsonMapperFactory().create(), baseUri)); + this.tokenManager = tokenManager; + } @@ -208,7 +208,7 @@ public ApiResponse getSubmodelWithHttpInfoNoUrlEncoding(String level, private HttpRequest.Builder getSubmodelRequestBuilder(String level, String extent) throws ApiException { - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = ""; @@ -232,6 +232,8 @@ private HttpRequest.Builder getSubmodelRequestBuilder(String level, String exten } localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -330,7 +332,7 @@ private HttpRequest.Builder getSubmodelElementByPathRequestBuilder(String idShor throw new ApiException(400, "Missing the required parameter 'idShortPath' when calling getSubmodelElementByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -354,6 +356,8 @@ private HttpRequest.Builder getSubmodelElementByPathRequestBuilder(String idShor } localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -455,7 +459,7 @@ private HttpRequest.Builder getSubmodelElementByPathValueOnlyRequestBuilder(Stri throw new ApiException(400, "Missing the required parameter 'idShortPath' when calling getSubmodelElementByPathValueOnly"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/$value".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -479,6 +483,8 @@ private HttpRequest.Builder getSubmodelElementByPathValueOnlyRequestBuilder(Stri } localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -596,7 +602,7 @@ private HttpRequest.Builder patchSubmodelElementByPathValueOnlyRequestBuilder(St throw new ApiException(400, "Missing the required parameter 'getSubmodelElementsValueResult' when calling patchSubmodelElementByPathValueOnly"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/$value".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -623,6 +629,8 @@ private HttpRequest.Builder patchSubmodelElementByPathValueOnlyRequestBuilder(St localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(getSubmodelElementsValueResult); @@ -707,7 +715,7 @@ private HttpRequest.Builder postSubmodelElementRequestBuilder(SubmodelElement su throw new ApiException(400, "Missing the required parameter 'submodelElement' when calling postSubmodelElement"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements"; @@ -715,6 +723,8 @@ private HttpRequest.Builder postSubmodelElementRequestBuilder(SubmodelElement su localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(submodelElement); @@ -812,7 +822,7 @@ private HttpRequest.Builder postSubmodelElementByPathRequestBuilder(String idSho throw new ApiException(400, "Missing the required parameter 'submodelElement' when calling postSubmodelElementByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -820,6 +830,8 @@ private HttpRequest.Builder postSubmodelElementByPathRequestBuilder(String idSho localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(submodelElement); @@ -927,7 +939,7 @@ private HttpRequest.Builder putSubmodelElementByPathRequestBuilder(String idShor throw new ApiException(400, "Missing the required parameter 'submodelElement' when calling putSubmodelElementByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -950,6 +962,8 @@ private HttpRequest.Builder putSubmodelElementByPathRequestBuilder(String idShor localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(submodelElement); @@ -1038,13 +1052,15 @@ private HttpRequest.Builder deleteSubmodelElementByPathRequestBuilder(String idS throw new ApiException(400, "Missing the required parameter 'idShortPath' when calling deleteSubmodelElementByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -1109,7 +1125,7 @@ private ApiResponse>> getAllS private HttpRequest.Builder getAllSubmodelElementsRequestBuilder(Integer limit, String cursor, String level, String extent) throws ApiException { - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements"; @@ -1137,6 +1153,8 @@ private HttpRequest.Builder getAllSubmodelElementsRequestBuilder(Integer limit, } localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -1198,7 +1216,7 @@ private HttpRequest.Builder putFileByPathRequestBuilder(String idShortPath, Stri if (inputStream == null) throw new ApiException(400, "Missing the required parameter 'inputStream' when calling putFileByPath"); - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/attachment".replace("{idShortPath}", ApiClient.urlEncode(idShortPath)); @@ -1225,6 +1243,8 @@ private HttpRequest.Builder putFileByPathRequestBuilder(String idShortPath, Stri formDataPublisher = HttpRequest.BodyPublishers.ofInputStream(() -> Channels.newInputStream(pipe.source())); localVarRequestBuilder.header("Content-Type", entity.getContentType().getValue()).method("PUT", formDataPublisher); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); @@ -1307,13 +1327,15 @@ private HttpRequest.Builder deleteFileByPathRequestBuilder(String idShortPath) t throw new ApiException(400, "Missing the required parameter 'idShortPath' when calling deleteFileByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/attachment".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -1374,13 +1396,15 @@ private HttpRequest.Builder getFileByPathRequestBuilder(String idShortPath) thro throw new ApiException(400, "Missing the required parameter 'idShortPath' when calling getFileByPath"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/attachment".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/octet-stream, application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { @@ -1471,7 +1495,7 @@ private HttpRequest.Builder invokeOperationRequestBuilder(String idShortPath, Op throw new ApiException(400, "Missing the required parameter 'operationRequest' when calling invokeOperation"); } - HttpRequest.Builder localVarRequestBuilder = this.httpRequestBuilder.copy(); + HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/submodel-elements/{idShortPath}/invoke".replace("{idShortPath}", ApiClient.urlEncode(idShortPath.toString())); @@ -1479,6 +1503,8 @@ private HttpRequest.Builder invokeOperationRequestBuilder(String idShortPath, Op localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); + + addAuthorizationHeaderIfAuthIsEnabled(localVarRequestBuilder); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(operationRequest); @@ -1507,4 +1533,15 @@ private static String extractFileName(HttpHeaders headers) { private static String buildUniqueFilename() { return UUID.randomUUID().toString(); } -} + + private void addAuthorizationHeaderIfAuthIsEnabled(HttpRequest.Builder localVarRequestBuilder) { + if (tokenManager != null) { + try { + localVarRequestBuilder.header("Authorization", "Bearer " + tokenManager.getAccessToken()); + } catch (IOException e) { + e.printStackTrace(); + throw new AccessTokenRetrievalException("Unable to request access token"); + } + } + } +} \ No newline at end of file