Skip to content

Commit 63b1d39

Browse files
committed
[CSAPI] Add client-side support for bearer token
1 parent 648c68a commit 63b1d39

File tree

1 file changed

+56
-24
lines changed

1 file changed

+56
-24
lines changed

sensorhub-service-consys/src/main/java/org/sensorhub/impl/service/consys/client/ConSysApiClient.java

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public class ConSysApiClient
123123
protected Authenticator authenticator;
124124
protected HttpClient http;
125125
protected URI endpoint;
126+
protected String token;
126127

127128

128129
protected ConSysApiClient() {}
@@ -1103,27 +1104,35 @@ protected <T> CompletableFuture<T> sendGetRequest(URI collectionUri, ResourceFor
11031104
if (!isHttpClientAvailable)
11041105
return sendGetRequestFallback(collectionUri, format, bodyMapper);
11051106

1106-
var req = HttpRequest.newBuilder()
1107+
var builder = HttpRequest.newBuilder()
11071108
.uri(collectionUri)
11081109
.GET()
1109-
.header(HttpHeaders.ACCEPT, format.getMimeType())
1110-
.build();
1111-
1110+
.header(HttpHeaders.ACCEPT, format.getMimeType());
1111+
1112+
if (token != null)
1113+
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
1114+
1115+
var req = builder.build();
11121116
BodyHandler<T> bodyHandler = resp -> {
11131117
BodySubscriber<byte[]> upstream = BodySubscribers.ofByteArray();
11141118
return BodySubscribers.mapping(upstream, body -> {
1115-
var is = new ByteArrayInputStream(body);
1116-
return bodyMapper.apply(is);
1119+
if (resp.statusCode() == 200) {
1120+
var is = new ByteArrayInputStream(body);
1121+
return bodyMapper.apply(is);
1122+
} else {
1123+
var error = new String(body);
1124+
throw new CompletionException("HTTP error " + resp.statusCode() + ": " + error, null);
1125+
}
11171126
});
11181127
};
11191128

11201129
return http.sendAsync(req, bodyHandler)
1121-
.thenApply(resp -> {
1122-
if (resp.statusCode() == 200)
1123-
return resp.body();
1124-
else
1125-
throw new CompletionException("HTTP error " + resp.statusCode(), null);
1126-
});
1130+
.thenApply(resp -> {
1131+
if (resp.statusCode() == 200)
1132+
return resp.body();
1133+
else
1134+
throw new CompletionException("HTTP error " + resp.statusCode(), null);
1135+
});
11271136
}
11281137

11291138

@@ -1143,6 +1152,8 @@ protected <T> CompletableFuture<T> sendGetRequestFallback(URI collectionUri, Res
11431152
connection = (HttpURLConnection) url.openConnection();
11441153
connection.setRequestMethod("GET");
11451154
connection.setRequestProperty(HttpHeaders.ACCEPT, format.getMimeType());
1155+
if (token != null)
1156+
connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
11461157

11471158
int responseCode = connection.getResponseCode();
11481159
if (responseCode == 200) {
@@ -1168,13 +1179,16 @@ protected CompletableFuture<String> sendPostRequest(URI collectionUri, ResourceF
11681179
if (!isHttpClientAvailable)
11691180
return sendPostRequestFallback(collectionUri, format, body);
11701181

1171-
var req = HttpRequest.newBuilder()
1182+
var builder = HttpRequest.newBuilder()
11721183
.uri(collectionUri)
11731184
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
11741185
.header(HttpHeaders.ACCEPT, ResourceFormat.JSON.getMimeType())
1175-
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType())
1176-
.build();
1177-
1186+
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1187+
1188+
if (token != null)
1189+
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
1190+
1191+
var req = builder.build();
11781192
return http.sendAsync(req, BodyHandlers.ofString())
11791193
.thenApply(resp -> {
11801194
if (resp.statusCode() == 201 || resp.statusCode() == 303) {
@@ -1205,6 +1219,8 @@ protected CompletableFuture<String> sendPostRequestFallback(URI collectionUri, R
12051219
connection.setRequestMethod("POST");
12061220
connection.setRequestProperty(HttpHeaders.ACCEPT, ResourceFormat.JSON.getMimeType());
12071221
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1222+
if (token != null)
1223+
connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
12081224
connection.setDoOutput(true);
12091225

12101226
try (OutputStream os = connection.getOutputStream()) {
@@ -1237,13 +1253,16 @@ protected CompletableFuture<Integer> sendPutRequest(URI collectionUri, ResourceF
12371253
if (!isHttpClientAvailable)
12381254
return sendPutRequestFallback(collectionUri, format, body);
12391255

1240-
var req = HttpRequest.newBuilder()
1256+
var builder = HttpRequest.newBuilder()
12411257
.uri(collectionUri)
12421258
.PUT(HttpRequest.BodyPublishers.ofByteArray(body))
12431259
.header(HttpHeaders.ACCEPT, ResourceFormat.JSON.getMimeType())
1244-
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType())
1245-
.build();
1246-
1260+
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1261+
1262+
if (token != null)
1263+
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
1264+
1265+
var req = builder.build();
12471266
return http.sendAsync(req, BodyHandlers.ofString())
12481267
.thenApply(HttpResponse::statusCode);
12491268
}
@@ -1266,6 +1285,8 @@ protected CompletableFuture<Integer> sendPutRequestFallback(URI collectionUri, R
12661285
connection.setRequestMethod("PUT");
12671286
connection.setRequestProperty(HttpHeaders.ACCEPT, ResourceFormat.JSON.getMimeType());
12681287
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1288+
if (token != null)
1289+
connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
12691290
connection.setDoOutput(true);
12701291

12711292
try (OutputStream os = connection.getOutputStream()) {
@@ -1289,12 +1310,15 @@ protected CompletableFuture<Set<String>> sendBatchPostRequest(URI collectionUri,
12891310
if (!isHttpClientAvailable)
12901311
return sendBatchPostRequestFallback(collectionUri, format, body);
12911312

1292-
var req = HttpRequest.newBuilder()
1313+
var builder = HttpRequest.newBuilder()
12931314
.uri(collectionUri)
12941315
.POST(HttpRequest.BodyPublishers.ofByteArray(body))
1295-
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType())
1296-
.build();
1297-
1316+
.header(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1317+
1318+
if (token != null)
1319+
builder.header(HttpHeaders.AUTHORIZATION, "Bearer " + token);
1320+
1321+
var req = builder.build();
12981322
return http.sendAsync(req, BodyHandlers.ofString())
12991323
.thenApply(Lambdas.checked(resp -> {
13001324
if (resp.statusCode() == 201 || resp.statusCode() == 303) {
@@ -1331,6 +1355,8 @@ protected CompletableFuture<Set<String>> sendBatchPostRequestFallback(URI collec
13311355
connection = (HttpURLConnection) url.openConnection();
13321356
connection.setRequestMethod("POST");
13331357
connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, format.getMimeType());
1358+
if (token != null)
1359+
connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
13341360
connection.setDoOutput(true);
13351361

13361362
try (OutputStream os = connection.getOutputStream()) {
@@ -1442,6 +1468,12 @@ protected void skipToCollectionItems(JsonReader reader) throws IOException
14421468
}
14431469
}
14441470

1471+
1472+
public void setAuthToken(String token)
1473+
{
1474+
this.token = token;
1475+
}
1476+
14451477

14461478

14471479
/* Builder stuff */

0 commit comments

Comments
 (0)