-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestTemplateConfig.java
76 lines (64 loc) · 3.04 KB
/
RestTemplateConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.mangkyu.currency.exchanger.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.*;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.web.client.RestTemplate;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.UUID;
import java.util.stream.Collectors;
@Configuration
class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplateBuilder()
.setConnectTimeout(Duration.ofSeconds(5))
.setReadTimeout(Duration.ofSeconds(5))
.additionalInterceptors(clientHttpRequestInterceptor(), new LoggingInterceptor())
.requestFactory(() -> new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()))
.build();
}
public ClientHttpRequestInterceptor clientHttpRequestInterceptor() {
return (request, body, execution) -> {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(3));
try {
return retryTemplate.execute(context -> execution.execute(request, body));
} catch (Throwable throwable) {
throw new RuntimeException(throwable);
}
};
}
@Slf4j
static class LoggingInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest req, byte[] body, ClientHttpRequestExecution ex) throws IOException {
final String requestSessionKey = makeRequestSessionKey();
printRequest(requestSessionKey, req, body);
ClientHttpResponse response = ex.execute(req, body);
printResponse(requestSessionKey, response);
return response;
}
private String makeRequestSessionKey() {
return UUID.randomUUID().toString();
}
private void printRequest(final String sessionNumber, final HttpRequest req, final byte[] body) {
log.info("[{}] URI: {}, Method: {}, Headers:{}, Body:{} ",
sessionNumber, req.getURI(), req.getMethod(), req.getHeaders(), new String(body, StandardCharsets.UTF_8));
}
private void printResponse(final String sessionNumber, final ClientHttpResponse res) throws IOException {
String body = new BufferedReader(new InputStreamReader(res.getBody(), StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining("\n"));
log.info("[{}] Status: {}, Headers:{}, Body:{} ",
sessionNumber, res.getStatusCode(), res.getHeaders(), body);
}
}
}