import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Mono;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
private final GetTransferUseCase getTransferUseCase;
private final MessagingService messagingService;
private final TransferConsumer consumer;
+ private final WebClient webClient;
@PostMapping(
path = "",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
- public DeferredResult<ResponseEntity<?>> transfer(
- HttpServletRequest request,
- @Valid @RequestBody TransferDTO transferDTO)
+ public DeferredResult<ResponseEntity<?>> transfer(@Valid @RequestBody TransferDTO transferDTO)
{
DeferredResult<ResponseEntity<?>> result = new DeferredResult<>();
- getTransferUseCase
- .get(transferDTO.getId())
- .map(transfer ->
+ Long id = transferDTO.getId();
+
+ consumer
+ .uriForKey(id.toString())
+ .map(uri ->
+ webClient.get()
+ .uri(uri + PATH + "/" + id)
+ .accept(MediaType.APPLICATION_JSON)
+ .retrieve()
+ .onStatus(status -> true, bar -> Mono.empty())
+ .toBodilessEntity()
+ .blockOptional()
+ .flatMap(resp ->
+ resp.getStatusCode().is2xxSuccessful()
+ ? Optional.of(Boolean.TRUE)
+ : Optional.<Boolean>empty()))
+ .or(() -> Optional.of(getTransferUseCase.get(transferDTO.getId()).map(transfer -> Boolean.TRUE)))
+ .flatMap(optional -> optional)
+ .map($ ->
CompletableFuture.completedFuture(
ResponseEntity
.ok()