From 025e2d35e25cdcc1f695ce6206b1482feaee0d85 Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Sun, 1 Aug 2021 14:28:51 +0100 Subject: [PATCH] BAEL-5054 make sure all files are added BAEL-5054 organise packages BAEL-5054 move project to correct folder BAEL-5044 remove old folder --- spring-5-reactive-client/pom.xml | 2 +- .../com/baeldung/reactive/Application.java | 12 +++ .../reactive/controller/UploadController.java | 47 ++++++++++++ .../service/ReactiveUploadService.java | 69 +++++++++++++++++ .../src/main/resources/application.properties | 2 +- webclient_upload/pom.xml | 76 ------------------- 6 files changed, 130 insertions(+), 78 deletions(-) create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java create mode 100644 spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java delete mode 100644 webclient_upload/pom.xml diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 136f31b49e..6a0a2d6fe3 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -133,7 +133,7 @@ org.springframework.boot spring-boot-maven-plugin - com.baeldung.Spring5Application + com.baeldung.reactive.Application JAR diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java new file mode 100644 index 0000000000..65e497b519 --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.reactive; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} + diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java new file mode 100644 index 0000000000..71cea35685 --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/controller/UploadController.java @@ -0,0 +1,47 @@ +package com.baeldung.reactive.controller; + + +import com.baeldung.reactive.service.ReactiveUploadService; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import reactor.core.publisher.Mono; + + +@RestController +public class UploadController { + final ReactiveUploadService uploadService; + + public UploadController(ReactiveUploadService uploadService) { + this.uploadService = uploadService; + } + + @PostMapping(path = "/upload") + @ResponseBody + public Mono uploadPdf(@RequestParam("file") final MultipartFile multipartFile) { + return uploadService.uploadPdf(multipartFile.getResource()); + } + + @PostMapping(path = "/upload/multipart") + @ResponseBody + public Mono uploadMultipart(@RequestParam("file") final MultipartFile multipartFile) { + return uploadService.uploadMultipart(multipartFile); + } + + + /** + * Fake upload endpoint returning "OK" HttpStatus + * @return "OK" HttpStatus + */ + @PostMapping(path = "/external/upload") + @ResponseBody + public HttpStatus externalUpload() { + return HttpStatus.OK; + } + + @GetMapping("/trixi") + public String returnTrixi() { + return "Trixi"; + + } +} diff --git a/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java new file mode 100644 index 0000000000..e31674282b --- /dev/null +++ b/spring-5-reactive-client/src/main/java/com/baeldung/reactive/service/ReactiveUploadService.java @@ -0,0 +1,69 @@ +package com.baeldung.reactive.service; + + +import org.springframework.core.io.Resource; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.client.MultipartBodyBuilder; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriComponentsBuilder; +import reactor.core.publisher.Mono; + +import java.net.URI; + +@Service +public class ReactiveUploadService { + + private final WebClient webClient; + private static final String EXTERNAL_UPLOAD_URL = "http://localhost:8080/external/upload"; + + public ReactiveUploadService() { + this.webClient = WebClient.create(); + } + + + public Mono uploadPdf(final Resource resource){ + + final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); + Mono httpStatusMono = webClient.post() + .uri(url) + .contentType(MediaType.APPLICATION_PDF) + .body(BodyInserters.fromResource(resource)) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + System.out.println("Failed to upload pdf. " + response.statusCode()); + } + return null; + }); + return httpStatusMono; + } + + + public Mono uploadMultipart(final MultipartFile multipartFile){ + final URI url = UriComponentsBuilder.fromHttpUrl(EXTERNAL_UPLOAD_URL).build().toUri(); + + final MultipartBodyBuilder builder = new MultipartBodyBuilder(); + builder.part("file", multipartFile.getResource()); + + Mono httpStatusMono = webClient.post() + .uri(url) + .contentType(MediaType.MULTIPART_FORM_DATA) + .body(BodyInserters.fromMultipartData(builder.build())) + .exchangeToMono(response -> { + if (response.statusCode().equals(HttpStatus.OK)) { + return response.bodyToMono(HttpStatus.class).thenReturn(response.statusCode()); + } else { + System.out.println("Failed to upload pdf. " + response.statusCode()); + } + return null; + }); + return httpStatusMono; + } + + +} diff --git a/spring-5-reactive-client/src/main/resources/application.properties b/spring-5-reactive-client/src/main/resources/application.properties index 05033054b1..7859537230 100644 --- a/spring-5-reactive-client/src/main/resources/application.properties +++ b/spring-5-reactive-client/src/main/resources/application.properties @@ -1,5 +1,5 @@ logging.level.root=INFO -server.port=8081 +server.port=8080 logging.level.reactor.netty.http.client.HttpClient=DEBUG \ No newline at end of file diff --git a/webclient_upload/pom.xml b/webclient_upload/pom.xml deleted file mode 100644 index 69f7f78cd1..0000000000 --- a/webclient_upload/pom.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - - 4.0.0 - - com.baeldung - webclient_upload - 1.0-SNAPSHOT - jar - - - - org.springframework.boot - spring-boot-starter-parent - 2.5.0 - - - webclient_upload Maven Webapp - - 16 - 1.0.1.RELEASE - - - - - - - org.projectreactor - reactor-spring - ${reactor-spring.version} - - - - - - - - org.springframework.boot - spring-boot-starter - - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-webflux - - - org.projectreactor - reactor-spring - - - org.springframework.boot - spring-boot-starter-test - test - - - io.rest-assured - json-schema-validator - test - - - org.springframework.boot - spring-boot-starter-web - - - - -