WIP: WebClient
authorKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 15:29:10 +0000 (16:29 +0100)
committerKai Moritz <kai@jupl.de>
Thu, 21 Nov 2019 15:29:10 +0000 (16:29 +0100)
src/main/java/de/juplo/facebook/errors/GraphApiException.java
src/main/java/de/juplo/facebook/errors/GraphApiExchangeFilterFunction.java

index a696f6e..27f359f 100644 (file)
@@ -8,12 +8,14 @@ import com.fasterxml.jackson.databind.SerializationFeature;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.function.Supplier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.core.io.buffer.DataBufferUtils;
 import org.springframework.http.HttpHeaders;
 import org.springframework.http.HttpStatus;
+import org.springframework.web.reactive.function.BodyExtractors;
 import org.springframework.web.reactive.function.client.ClientResponse;
+import reactor.core.publisher.Mono;
 
 
 
@@ -45,15 +47,17 @@ public class GraphApiException extends RuntimeException
 
 
 
-  public static Supplier<GraphApiException> create(ClientResponse response)
+  public static Mono<GraphApiException> create(ClientResponse response)
   {
-    HttpStatus status = response.statusCode();
-    HttpHeaders headers = response.headers().asHttpHeaders();
-    return
-        () -> response
-            .bodyToMono(InputStream.class)
-            .map(is -> create(status , headers, is))
-            .block();
+               return DataBufferUtils.join(response.body(BodyExtractors.toDataBuffers()))
+                               .map(dataBuffer -> {
+                                       byte[] bytes = new byte[dataBuffer.readableByteCount()];
+                                       dataBuffer.read(bytes);
+                                       DataBufferUtils.release(dataBuffer);
+                                       return bytes;
+                               })
+                               .defaultIfEmpty(new byte[0])
+                               .map(bytes -> create(response.statusCode(), response.headers().asHttpHeaders(), bytes));
   }
 
   public static GraphApiException create(
index 53bdd0b..6642e38 100644 (file)
@@ -35,7 +35,7 @@ public class GraphApiExchangeFilterFunction implements ExchangeFilterFunction
             {
               return
                   HttpStatus.Series.CLIENT_ERROR.equals(response.statusCode().series())
-                      ? Mono.error(GraphApiException.create(response))
+                      ? Mono.error(GraphApiException.create(response).block())
                       : Mono.just(response);
             });
   }