|
1 | 1 | package pl.allegro.tech.hermes.client.restclient;
|
2 | 2 |
|
3 |
| -import org.springframework.web.client.RestClient; |
4 |
| -import pl.allegro.tech.hermes.client.HermesMessage; |
5 |
| -import pl.allegro.tech.hermes.client.HermesResponse; |
6 |
| -import pl.allegro.tech.hermes.client.HermesSender; |
| 3 | +import static java.util.stream.Collectors.toMap; |
| 4 | +import static pl.allegro.tech.hermes.client.HermesResponseBuilder.hermesResponse; |
7 | 5 |
|
8 | 6 | import java.net.URI;
|
9 | 7 | import java.nio.charset.StandardCharsets;
|
10 | 8 | import java.util.Map;
|
11 | 9 | import java.util.TreeMap;
|
12 | 10 | import java.util.concurrent.CompletableFuture;
|
13 |
| - |
14 |
| -import static java.util.stream.Collectors.toMap; |
15 |
| -import static pl.allegro.tech.hermes.client.HermesResponseBuilder.hermesResponse; |
16 |
| - |
| 11 | +import org.springframework.web.client.RestClient; |
| 12 | +import pl.allegro.tech.hermes.client.HermesMessage; |
| 13 | +import pl.allegro.tech.hermes.client.HermesResponse; |
| 14 | +import pl.allegro.tech.hermes.client.HermesSender; |
17 | 15 |
|
18 | 16 | public class RestClientHermesSender implements HermesSender {
|
19 | 17 |
|
20 |
| - private final RestClient restClient; |
21 |
| - |
| 18 | + private final RestClient restClient; |
22 | 19 |
|
23 |
| - public RestClientHermesSender(RestClient restClient) { |
24 |
| - this.restClient = restClient; |
25 |
| - } |
| 20 | + public RestClientHermesSender(RestClient restClient) { |
| 21 | + this.restClient = restClient; |
| 22 | + } |
26 | 23 |
|
27 |
| - @Override |
28 |
| - public CompletableFuture<HermesResponse> send(URI uri, HermesMessage message) { |
29 |
| - CompletableFuture<HermesResponse> future = new CompletableFuture<>(); |
| 24 | + @Override |
| 25 | + public CompletableFuture<HermesResponse> send(URI uri, HermesMessage message) { |
| 26 | + CompletableFuture<HermesResponse> future = new CompletableFuture<>(); |
30 | 27 |
|
31 |
| - var received = restClient |
32 |
| - .post() |
33 |
| - .uri(uri) |
34 |
| - .headers(httpHeaders -> httpHeaders.setAll(message.getHeaders())) |
35 |
| - .body(message.getBody()) |
36 |
| - .exchange((request, response) -> { |
37 |
| - if (response.getStatusCode().is2xxSuccessful()) { |
38 |
| - return hermesResponse(message) |
39 |
| - .withBody(new String(response.getBody().readAllBytes(), StandardCharsets.UTF_8)) |
40 |
| - .withHttpStatus(response.getStatusCode().value()) |
41 |
| - .withHeaderSupplier(header -> convertToCaseInsensitiveMap(response.getHeaders().toSingleValueMap()).get(header)) |
42 |
| - .build(); |
43 |
| - } else { |
44 |
| - return hermesResponse(message) |
45 |
| - .withBody("") |
46 |
| - .withHttpStatus(response.getStatusCode().value()) |
47 |
| - .withHeaderSupplier(header -> convertToCaseInsensitiveMap(response.getHeaders().toSingleValueMap()).get(header)) |
48 |
| - .build(); |
49 |
| - } |
| 28 | + var received = |
| 29 | + restClient |
| 30 | + .post() |
| 31 | + .uri(uri) |
| 32 | + .headers(httpHeaders -> httpHeaders.setAll(message.getHeaders())) |
| 33 | + .body(message.getBody()) |
| 34 | + .exchange( |
| 35 | + (request, response) -> { |
| 36 | + if (response.getStatusCode().is2xxSuccessful()) { |
| 37 | + return hermesResponse(message) |
| 38 | + .withBody( |
| 39 | + new String(response.getBody().readAllBytes(), StandardCharsets.UTF_8)) |
| 40 | + .withHttpStatus(response.getStatusCode().value()) |
| 41 | + .withHeaderSupplier( |
| 42 | + header -> |
| 43 | + convertToCaseInsensitiveMap( |
| 44 | + response.getHeaders().toSingleValueMap()) |
| 45 | + .get(header)) |
| 46 | + .build(); |
| 47 | + } else { |
| 48 | + return hermesResponse(message) |
| 49 | + .withBody("") |
| 50 | + .withHttpStatus(response.getStatusCode().value()) |
| 51 | + .withHeaderSupplier( |
| 52 | + header -> |
| 53 | + convertToCaseInsensitiveMap( |
| 54 | + response.getHeaders().toSingleValueMap()) |
| 55 | + .get(header)) |
| 56 | + .build(); |
| 57 | + } |
50 | 58 | });
|
51 | 59 |
|
52 |
| - future.complete(received); |
53 |
| - return future; |
54 |
| - } |
| 60 | + future.complete(received); |
| 61 | + return future; |
| 62 | + } |
55 | 63 |
|
56 |
| - private TreeMap<String, String> convertToCaseInsensitiveMap(Map<String, String> hashMap) { |
57 |
| - return hashMap.entrySet().stream() |
58 |
| - .collect(toMap( |
59 |
| - Map.Entry::getKey, |
60 |
| - Map.Entry::getValue, |
61 |
| - (oldVal, newVal) -> newVal, |
62 |
| - () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER) |
63 |
| - )); |
64 |
| - } |
| 64 | + private TreeMap<String, String> convertToCaseInsensitiveMap(Map<String, String> hashMap) { |
| 65 | + return hashMap.entrySet().stream() |
| 66 | + .collect( |
| 67 | + toMap( |
| 68 | + Map.Entry::getKey, |
| 69 | + Map.Entry::getValue, |
| 70 | + (oldVal, newVal) -> newVal, |
| 71 | + () -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER))); |
| 72 | + } |
65 | 73 | }
|
0 commit comments