diff --git a/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/Headers.groovy b/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/Headers.groovy new file mode 100644 index 0000000..490e771 --- /dev/null +++ b/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/Headers.groovy @@ -0,0 +1,9 @@ +package pl.allegro.tech.workshops.testsparallelexecution.support + +final class Headers { + + private Headers() {} + + public static final String REQUEST_ID = 'X-request-id' + +} diff --git a/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/RestClient.groovy b/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/RestClient.groovy index 0ad9c89..f4e8860 100644 --- a/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/RestClient.groovy +++ b/commons-rest/src/main/groovy/pl/allegro/tech/workshops/testsparallelexecution/support/RestClient.groovy @@ -2,29 +2,49 @@ package pl.allegro.tech.workshops.testsparallelexecution.support import org.springframework.boot.test.web.client.TestRestTemplate import org.springframework.http.HttpEntity -import org.springframework.http.HttpMethod +import org.springframework.http.HttpHeaders import org.springframework.http.ResponseEntity +import static org.springframework.http.HttpMethod.DELETE +import static org.springframework.http.HttpMethod.GET +import static org.springframework.http.HttpMethod.POST import static org.springframework.http.HttpMethod.PUT class RestClient { String url TestRestTemplate restTemplate - ResponseEntity get(String path, Class responseType) { - restTemplate.getForEntity("$url$path", responseType) + ResponseEntity get(String path, Class responseType, Map headers = [:]) { + restTemplate.exchange("$url$path", GET, createHttpEntity(headers), responseType) } - ResponseEntity post(String path, Object request, Class responseType) { - restTemplate.postForEntity("$url$path", request, responseType) + ResponseEntity post(String path, Object request, Class responseType, Map headers = [:]) { + restTemplate.exchange("$url$path", POST, createHttpEntity(request, headers), responseType) } - ResponseEntity put(String path, Object request, Class responseType) { - restTemplate.exchange("$url$path", PUT, new HttpEntity(request), responseType) + ResponseEntity put(String path, Object request, Class responseType, Map headers = [:]) { + restTemplate.exchange("$url$path", PUT, createHttpEntity(request, headers), responseType) } - ResponseEntity delete(String path) { - restTemplate.exchange("$url$path", HttpMethod.DELETE, HttpEntity.EMPTY, Void) + ResponseEntity delete(String path, Map headers = [:]) { + restTemplate.exchange("$url$path", DELETE, createHttpEntity(headers), Void) } + private static HttpEntity createHttpEntity(Object request, Map headers) { + new HttpEntity(request, createHeaders(headers)) + } + + private static HttpEntity createHttpEntity(Map headers = [:]) { + new HttpEntity(createHeaders(headers)) + } + + private static HttpHeaders createHeaders(Map headers) { + new HttpHeaders().tap { + headers.each { header -> + set(header.key, header.value) + } + } + } + + } diff --git a/part2.2-rest/src/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/ExternalEmailClient.java b/part2.2-rest/src/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/ExternalEmailClient.java index 63210ec..ede35c2 100644 --- a/part2.2-rest/src/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/ExternalEmailClient.java +++ b/part2.2-rest/src/main/java/pl/allegro/tech/workshops/testsparallelexecution/email/rest/ExternalEmailClient.java @@ -2,11 +2,20 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpRequest; +import org.springframework.http.client.ClientHttpRequestExecution; +import org.springframework.http.client.ClientHttpRequestInterceptor; +import org.springframework.http.client.ClientHttpResponse; import org.springframework.retry.policy.MaxAttemptsRetryPolicy; import org.springframework.retry.support.RetryTemplate; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import org.springframework.web.context.request.RequestAttributes; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import java.io.IOException; import java.time.Duration; @Component @@ -21,6 +30,7 @@ public ExternalEmailClient(RestTemplateBuilder builder, @Value("${application.se .rootUri(serviceUrl) .setConnectTimeout(Duration.ofMillis(300)) .setReadTimeout(Duration.ofMillis(500)) + .additionalInterceptors(new PassRequestIdHeaderRequestInterceptor()) .build(); this.retryTemplate = new RetryTemplate(); this.retryTemplate.setRetryPolicy(new MaxAttemptsRetryPolicy(2)); @@ -36,4 +46,16 @@ public Email read(String id) { return retryTemplate.execute(context -> restTemplate.getForEntity("/external-api-service/emails/" + id, Email.class).getBody()); } + private static class PassRequestIdHeaderRequestInterceptor implements ClientHttpRequestInterceptor { + @Override + public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { + RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); + if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) { + String requestId = ((ServletRequestAttributes) requestAttributes).getRequest().getHeader("X-request-id"); + HttpHeaders headers = request.getHeaders(); + headers.set("X-request-id", requestId); + } + return execution.execute(request, body); + } + } }