Skip to content

Commit

Permalink
follow redirects when fetching WoT TMs via HTTP
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Mar 29, 2022
1 parent 552464b commit 3183295
Showing 1 changed file with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import akka.http.javadsl.model.MediaRanges;
import akka.http.javadsl.model.MediaTypes;
import akka.http.javadsl.model.headers.Accept;
import akka.http.javadsl.model.headers.Location;
import akka.stream.Materializer;
import akka.stream.SystemMaterializer;
import akka.stream.javadsl.Sink;
Expand Down Expand Up @@ -124,8 +125,26 @@ private CompletableFuture<ThingModel> loadThingModelViaHttp(final URL url, final

private CompletionStage<HttpResponse> getThingModelFromUrl(final URL url) {
return httpClient.createSingleHttpRequest(HttpRequest.GET(url.toString()).withHeaders(List.of(ACCEPT_HEADER)))
.thenCompose(response -> {
if (response.status().isRedirection()) {
return response.getHeader(Location.class)
.map(location -> {
try {
LOGGER.debug("Following redirect to location: <{}>", location);
return new URL(location.getUri().toString());
} catch (final MalformedURLException e) {
throw DittoRuntimeException.asDittoRuntimeException(e,
cause -> handleUnexpectedException(cause, url));
}
})
.map(this::getThingModelFromUrl) // recurse following the redirect
.orElseGet(() -> CompletableFuture.completedFuture(response));
} else {
return CompletableFuture.completedFuture(response);
}
})
.thenApply(response -> {
if (!response.status().isSuccess()) {
if (!response.status().isSuccess() || response.status().isRedirection()) {
handleNonSuccessResponse(response, url);
}
return response;
Expand All @@ -142,8 +161,10 @@ private CompletableFuture<ThingModel> mapResponseToThingModel(final HttpResponse
return bodyFuture
.thenApply(ThingModel::fromJson)
.exceptionally(t -> {
LOGGER.warn("Failed to extract ThingModel from body <{}>", bodyFuture);
LOGGER.warn("Failed to extract ThingModel from response <{}> because of <{}: {}>", response,
t.getClass().getSimpleName(), t.getMessage());
throw WotThingModelInvalidException.newBuilder(url)
.cause(t)
.build();
});
}
Expand Down

0 comments on commit 3183295

Please sign in to comment.