From 4c254dc37e6b29bd9aa872384ed3f6b60b1c20ac Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Wed, 20 Nov 2019 16:02:48 +0100 Subject: [PATCH] WIP: WebClient --- pom.xml | 3 +- .../facebook/errors/GraphApiException.java | 16 +++++++++ .../GraphApiExchangeFilterFunction.java | 36 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java diff --git a/pom.xml b/pom.xml index be37746..9bc175d 100644 --- a/pom.xml +++ b/pom.xml @@ -70,7 +70,8 @@ org.springframework - spring-webmvc + spring-webflux + true org.springframework.boot diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiException.java b/src/main/java/de/juplo/facebook/errors/GraphApiException.java index a781c3b..2528e7e 100644 --- a/src/main/java/de/juplo/facebook/errors/GraphApiException.java +++ b/src/main/java/de/juplo/facebook/errors/GraphApiException.java @@ -10,8 +10,12 @@ import java.io.IOException; import java.io.InputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import org.springframework.http.ReactiveHttpInputMessage; +import org.springframework.web.reactive.function.BodyExtractor.Context; +import org.springframework.web.reactive.function.client.ClientResponse; @@ -42,6 +46,18 @@ public class GraphApiException extends RuntimeException } + + public static GraphApiException create(ClientResponse response) + { + return + response.body((ReactiveHttpInputMessage message, Context context) -> + { + DataBuffer buffer = message.getBody().blockFirst(); + InputStream is = message.getBody().blockFirst().asInputStream(); + return create(response.statusCode(), message.getHeaders(), is); + }); + } + public static GraphApiException create( HttpStatus status, HttpHeaders headers, diff --git a/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java b/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java new file mode 100644 index 0000000..0b583f4 --- /dev/null +++ b/src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java @@ -0,0 +1,36 @@ +package de.juplo.facebook.errors; + + +import org.springframework.http.HttpStatus; +import org.springframework.web.reactive.function.client.ClientRequest; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import org.springframework.web.reactive.function.client.ExchangeFunction; +import reactor.core.publisher.Mono; + + +/** + * An {@link ExchangeFilterFunction} + * @author Kai Moritz + */ +public class GraphApiExchangeFilterFunction implements ExchangeFilterFunction +{ + public static GraphApiExchangeFilterFunction INSTANCE = + new GraphApiExchangeFilterFunction(); + + + @Override + public Mono filter(ClientRequest request, ExchangeFunction next) + { + return + next + .exchange(request) + .flatMap(response -> + { + return + HttpStatus.Series.CLIENT_ERROR.equals(response.statusCode().series()) + ? Mono.error(GraphApiException.create(response)) + : Mono.just(response); + }); + } +} -- 2.20.1