WIP: WebClient
authorKai Moritz <kai@jupl.de>
Wed, 20 Nov 2019 15:02:48 +0000 (16:02 +0100)
committerKai Moritz <kai@jupl.de>
Wed, 20 Nov 2019 23:30:09 +0000 (00:30 +0100)
pom.xml
src/main/java/de/juplo/facebook/errors/GraphApiException.java
src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index be37746..9bc175d 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -70,7 +70,8 @@
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
-      <artifactId>spring-webmvc</artifactId>
+      <artifactId>spring-webflux</artifactId>
+      <optional>true</optional>
     </dependency>
     <dependency>
       <groupId>org.springframework.boot</groupId>
index a781c3b..2528e7e 100644 (file)
@@ -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 (file)
index 0000000..0b583f4
--- /dev/null
@@ -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<ClientResponse> 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);
+        });
+  }
+}