Skip to content

Commit 06f8841

Browse files
Replaces usage of ofInputStream with ofString in clients where applicable (#783)
* Replaces usage of ofInputStream with ofString in client where applicable Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de> * Removes experimental POM entry --------- Signed-off-by: Frank Schnicke <frank.schnicke@iese.fraunhofer.de>
1 parent dae8588 commit 06f8841

8 files changed

Lines changed: 196 additions & 382 deletions

File tree

  • basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/client/internal
  • basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/internal
  • basyx.aasregistry/basyx.aasregistry-client-native/templates/libraries/native
  • basyx.aasrepository/basyx.aasrepository-client/src/main/java/org/eclipse/digitaltwin/basyx/aasrepository/client/internal
  • basyx.aasservice/basyx.aasservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasservice/client/internal
  • basyx.submodelregistry/basyx.submodelregistry-client-native/templates/libraries/native
  • basyx.submodelrepository/basyx.submodelrepository-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelrepository/client/internal
  • basyx.submodelservice/basyx.submodelservice-client/src/main/java/org/eclipse/digitaltwin/basyx/submodelservice/client/internal

basyx.aasdiscoveryservice/basyx.aasdiscoveryservice-client/src/main/java/org/eclipse/digitaltwin/basyx/aasdiscoveryservice/client/internal/AssetAdministrationShellDiscoveryApi.java

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
package org.eclipse.digitaltwin.basyx.aasdiscoveryservice.client.internal;
2626

2727
import java.io.IOException;
28-
import java.io.InputStream;
2928
import java.net.URI;
3029
import java.net.http.HttpClient;
3130
import java.net.http.HttpRequest;
@@ -63,8 +62,6 @@ public class AssetAdministrationShellDiscoveryApi {
6362
private final String baseUri;
6463
private final Consumer<HttpRequest.Builder> requestInterceptor;
6564
private final Duration readTimeout;
66-
private final Consumer<HttpResponse<InputStream>> responseInterceptor;
67-
private final Consumer<HttpResponse<String>> asyncResponseInterceptor;
6865
private TokenManager tokenManager;
6966

7067
public AssetAdministrationShellDiscoveryApi() {
@@ -82,8 +79,6 @@ public AssetAdministrationShellDiscoveryApi(ApiClient apiClient) {
8279
this.baseUri = apiClient.getBaseUri();
8380
this.requestInterceptor = apiClient.getRequestInterceptor();
8481
this.readTimeout = apiClient.getReadTimeout();
85-
this.responseInterceptor = apiClient.getResponseInterceptor();
86-
this.asyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
8782
}
8883

8984
public AssetAdministrationShellDiscoveryApi(String baseUri) {
@@ -103,9 +98,11 @@ public CursorResult<List<String>> getAllAssetAdministrationShellIdsByAssetLink(L
10398
public ApiResponse<Base64UrlEncodedCursorResult<List<String>>> getAllAssetAdministrationShellIdsByAssetLinkWithHttpInfo(List<AssetLink> assetIds, Integer limit, String cursor) throws ApiException {
10499
HttpRequest.Builder requestBuilder = getAllShellIdsRequestBuilder(assetIds, limit, cursor);
105100
try {
106-
HttpResponse<InputStream> response = httpClient.send(requestBuilder.build(), HttpResponse.BodyHandlers.ofInputStream());
107-
if (responseInterceptor != null) responseInterceptor.accept(response);
108-
if (response.statusCode() / 100 != 2) throw getApiException("getAllAssetAdministrationShellIdsByAssetLink", response);
101+
HttpResponse<String> response = httpClient.send(requestBuilder.build(),
102+
HttpResponse.BodyHandlers.ofString());
103+
if (response.statusCode() / 100 != 2) {
104+
throw getApiException("getAllAssetAdministrationShellIdsByAssetLink", response);
105+
}
109106
return new ApiResponse<>(
110107
response.statusCode(),
111108
response.headers().map(),
@@ -136,14 +133,20 @@ private HttpRequest.Builder getAllShellIdsRequestBuilder(List<AssetLink> assetId
136133
queryParams.addAll(ApiClient.parameterToPairs("cursor", cursor));
137134

138135
StringJoiner query = new StringJoiner("&");
139-
for (Pair p : queryParams) query.add(p.getName() + "=" + p.getValue());
136+
for (Pair p : queryParams) {
137+
query.add(p.getName() + "=" + p.getValue());
138+
}
140139

141140
builder.uri(URI.create(baseUri + path + (query.length() > 0 ? "?" + query : "")));
142141
builder.header("Accept", "application/json");
143142
addAuthorizationHeaderIfAuthIsEnabled(builder);
144143
builder.method("GET", HttpRequest.BodyPublishers.noBody());
145-
if (readTimeout != null) builder.timeout(readTimeout);
146-
if (requestInterceptor != null) requestInterceptor.accept(builder);
144+
if (readTimeout != null) {
145+
builder.timeout(readTimeout);
146+
}
147+
if (requestInterceptor != null) {
148+
requestInterceptor.accept(builder);
149+
}
147150
return builder;
148151
}
149152

@@ -153,14 +156,18 @@ public List<SpecificAssetId> getAllAssetLinksById(String aasIdentifier) throws A
153156
builder.header("Accept", "application/json");
154157
addAuthorizationHeaderIfAuthIsEnabled(builder);
155158
builder.method("GET", HttpRequest.BodyPublishers.noBody());
156-
if (readTimeout != null) builder.timeout(readTimeout);
157-
if (requestInterceptor != null) requestInterceptor.accept(builder);
159+
if (readTimeout != null) {
160+
builder.timeout(readTimeout);
161+
}
162+
if (requestInterceptor != null) {
163+
requestInterceptor.accept(builder);
164+
}
158165

159166
try {
160-
HttpResponse<InputStream> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
161-
if (responseInterceptor != null) responseInterceptor.accept(response);
162-
if (response.statusCode() / 100 != 2)
163-
throw getApiException("getAllAssetLinksById", response);
167+
HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString());
168+
if (response.statusCode() / 100 != 2) {
169+
throw getApiException("getAllAssetLinksById", response);
170+
}
164171
return objectMapper.readValue(response.body(), new TypeReference<List<SpecificAssetId>>() {});
165172
} catch (IOException | InterruptedException e) {
166173
Thread.currentThread().interrupt();
@@ -182,14 +189,18 @@ public List<SpecificAssetId> postAllAssetLinksById(String aasIdentifier, List<Sp
182189
throw new ApiException(e);
183190
}
184191

185-
if (readTimeout != null) builder.timeout(readTimeout);
186-
if (requestInterceptor != null) requestInterceptor.accept(builder);
192+
if (readTimeout != null) {
193+
builder.timeout(readTimeout);
194+
}
195+
if (requestInterceptor != null) {
196+
requestInterceptor.accept(builder);
197+
}
187198

188199
try {
189-
HttpResponse<InputStream> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
190-
if (responseInterceptor != null) responseInterceptor.accept(response);
191-
if (response.statusCode() / 100 != 2)
192-
throw getApiException("postAllAssetLinksById", response);
200+
HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString());
201+
if (response.statusCode() / 100 != 2) {
202+
throw getApiException("postAllAssetLinksById", response);
203+
}
193204
return objectMapper.readValue(response.body(), new TypeReference<List<SpecificAssetId>>() {});
194205
} catch (IOException | InterruptedException e) {
195206
Thread.currentThread().interrupt();
@@ -203,14 +214,18 @@ public void deleteAllAssetLinksById(String aasIdentifier) throws ApiException {
203214
builder.header("Accept", "application/json");
204215
addAuthorizationHeaderIfAuthIsEnabled(builder);
205216
builder.method("DELETE", HttpRequest.BodyPublishers.noBody());
206-
if (readTimeout != null) builder.timeout(readTimeout);
207-
if (requestInterceptor != null) requestInterceptor.accept(builder);
217+
if (readTimeout != null) {
218+
builder.timeout(readTimeout);
219+
}
220+
if (requestInterceptor != null) {
221+
requestInterceptor.accept(builder);
222+
}
208223

209224
try {
210-
HttpResponse<InputStream> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
211-
if (responseInterceptor != null) responseInterceptor.accept(response);
212-
if (response.statusCode() / 100 != 2)
213-
throw getApiException("deleteAllAssetLinksById", response);
225+
HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString());
226+
if (response.statusCode() / 100 != 2) {
227+
throw getApiException("deleteAllAssetLinksById", response);
228+
}
214229
} catch (IOException | InterruptedException e) {
215230
Thread.currentThread().interrupt();
216231
throw new ApiException(e);
@@ -223,24 +238,29 @@ public Object getDescription() throws ApiException {
223238
builder.header("Accept", "application/json");
224239
addAuthorizationHeaderIfAuthIsEnabled(builder);
225240
builder.method("GET", HttpRequest.BodyPublishers.noBody());
226-
if (readTimeout != null) builder.timeout(readTimeout);
227-
if (requestInterceptor != null) requestInterceptor.accept(builder);
241+
if (readTimeout != null) {
242+
builder.timeout(readTimeout);
243+
}
244+
if (requestInterceptor != null) {
245+
requestInterceptor.accept(builder);
246+
}
228247

229248
try {
230-
HttpResponse<InputStream> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
231-
if (responseInterceptor != null) responseInterceptor.accept(response);
232-
if (response.statusCode() / 100 != 2)
233-
throw getApiException("getDescription", response);
249+
HttpResponse<String> response = httpClient.send(builder.build(), HttpResponse.BodyHandlers.ofString());
250+
if (response.statusCode() / 100 != 2) {
251+
throw getApiException("getDescription", response);
252+
}
234253
return objectMapper.readValue(response.body(), Object.class);
235254
} catch (IOException | InterruptedException e) {
236255
Thread.currentThread().interrupt();
237256
throw new ApiException(e);
238257
}
239258
}
240259

241-
private ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
242-
String body = response.body() == null ? null : new String(response.body().readAllBytes());
243-
return new ApiException(response.statusCode(), operationId + " call failed with: " + response.statusCode() + " - " + (body != null ? body : "[no body]"), response.headers(), body);
260+
private ApiException getApiException(String operationId, HttpResponse<String> response) throws IOException {
261+
return new ApiException(response.statusCode(),
262+
operationId + " call failed with: " + response.statusCode() + " - " + response.body(),
263+
response.headers(), response.body());
244264
}
245265

246266
private void addAuthorizationHeaderIfAuthIsEnabled(HttpRequest.Builder builder) {

basyx.aasenvironment/basyx.aasenvironment-client/src/main/java/org/eclipse/digitaltwin/basyx/aasenvironment/client/internal/SerializationApi.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@
4444
import org.eclipse.digitaltwin.basyx.core.exceptions.AccessTokenRetrievalException;
4545
import org.eclipse.digitaltwin.basyx.http.Base64UrlEncodedIdentifier;
4646
import org.springframework.core.io.ByteArrayResource;
47-
import org.springframework.core.io.FileSystemResource;
4847
import org.springframework.core.io.Resource;
4948

50-
import com.fasterxml.jackson.core.type.TypeReference;
5149
import com.fasterxml.jackson.databind.ObjectMapper;
5250

5351
@jakarta.annotation.Generated(value = "org.openapitools.codegen.languages.JavaClientCodegen", date = "2024-12-02T23:26:18.943744+01:00[Europe/Berlin]")
@@ -103,10 +101,10 @@ public SerializationApi(ApiClient apiClient) {
103101
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
104102
}
105103

106-
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
107-
String body = response.body() == null ? null : new String(response.body().readAllBytes());
108-
String message = formatExceptionMessage(operationId, response.statusCode(), body);
109-
return new ApiException(response.statusCode(), message, response.headers(), body);
104+
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
105+
String body = response.body() == null ? null : new String(response.body().readAllBytes());
106+
String message = formatExceptionMessage(operationId, response.statusCode(), body);
107+
return new ApiException(response.statusCode(), message, response.headers(), body);
110108
}
111109

112110
private String formatExceptionMessage(String operationId, int statusCode, String body) {

basyx.aasregistry/basyx.aasregistry-client-native/templates/libraries/native/api.mustache

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ public class {{classname}} {
5959
private final String memberVarBaseUri;
6060
private final {{#fullJavaUtil}}java.util.function.{{/fullJavaUtil}}Consumer<HttpRequest.Builder> memberVarInterceptor;
6161
private final Duration memberVarReadTimeout;
62-
private final {{#fullJavaUtil}}java.util.function.{{/fullJavaUtil}}Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
63-
private final {{#fullJavaUtil}}java.util.function.{{/fullJavaUtil}}Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
6462
private TokenManager tokenManager;
6563

6664
public {{classname}}() {
@@ -101,8 +99,6 @@ public class {{classname}} {
10199
memberVarBaseUri = apiClient.getBaseUri();
102100
memberVarInterceptor = apiClient.getRequestInterceptor();
103101
memberVarReadTimeout = apiClient.getReadTimeout();
104-
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
105-
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
106102
}
107103
{{#asyncNative}}
108104

@@ -113,10 +109,9 @@ public class {{classname}} {
113109
{{/asyncNative}}
114110
{{^asyncNative}}
115111

116-
protected ApiException getApiException(String operationId, HttpResponse<InputStream> response) throws IOException {
117-
String body = response.body() == null ? null : new String(response.body().readAllBytes());
118-
String message = formatExceptionMessage(operationId, response.statusCode(), body);
119-
return new ApiException(response.statusCode(), message, response.headers(), body);
112+
protected ApiException getApiException(String operationId, HttpResponse<String> response) throws IOException {
113+
String message = formatExceptionMessage(operationId, response.statusCode(), response.body());
114+
return new ApiException(response.statusCode(), message, response.headers(), response.body());
120115
}
121116
{{/asyncNative}}
122117

@@ -273,12 +268,9 @@ public class {{classname}} {
273268
{{^asyncNative}}
274269
HttpRequest.Builder localVarRequestBuilder = {{operationId}}RequestBuilder({{#allParams}}{{paramName}}{{^-last}}, {{/-last}}{{/allParams}});
275270
try {
276-
HttpResponse<InputStream> localVarResponse = memberVarHttpClient.send(
271+
HttpResponse<String> localVarResponse = memberVarHttpClient.send(
277272
localVarRequestBuilder.build(),
278-
HttpResponse.BodyHandlers.ofInputStream());
279-
if (memberVarResponseInterceptor != null) {
280-
memberVarResponseInterceptor.accept(localVarResponse);
281-
}
273+
HttpResponse.BodyHandlers.ofString());
282274
try {
283275
if (localVarResponse.statusCode()/ 100 != 2) {
284276
throw getApiException("{{operationId}}", localVarResponse);
@@ -311,13 +303,6 @@ public class {{classname}} {
311303
);
312304
{{/vendorExtensions.x-java-text-plain-string}}
313305
} finally {
314-
{{^returnType}}
315-
// Drain the InputStream
316-
while (localVarResponse.body().read() != -1) {
317-
// Ignore
318-
}
319-
localVarResponse.body().close();
320-
{{/returnType}}
321306
}
322307
} catch (IOException e) {
323308
throw new ApiException(e);

0 commit comments

Comments
 (0)