1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.audit.rest;
18
19 import java.io.IOException;
20 import java.util.Map;
21
22 import org.apache.logging.log4j.ThreadContext;
23 import org.apache.logging.log4j.audit.request.RequestContextMapping;
24 import org.apache.logging.log4j.audit.request.RequestContextMappings;
25 import org.springframework.http.HttpHeaders;
26 import org.springframework.http.HttpRequest;
27 import org.springframework.http.client.ClientHttpRequestExecution;
28 import org.springframework.http.client.ClientHttpRequestInterceptor;
29 import org.springframework.http.client.ClientHttpResponse;
30
31
32
33
34
35
36
37
38 public class RequestContextHeaderInterceptor implements ClientHttpRequestInterceptor {
39
40 private RequestContextMappings mappings = null;
41
42 public RequestContextHeaderInterceptor(RequestContextMappings mappings) {
43 this.mappings = mappings;
44 }
45
46 @Override
47 public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] body,
48 ClientHttpRequestExecution clientHttpRequestExecution) throws IOException {
49 Map<String, String> map = ThreadContext.getImmutableContext();
50 HttpHeaders headers = httpRequest.getHeaders();
51 for (Map.Entry<String, String> entry : map.entrySet()) {
52 RequestContextMapping mapping = mappings.getMapping(entry.getKey());
53 if (mapping != null && !mapping.isLocal()) {
54 String key = mappings.getHeaderPrefix() + mapping.getFieldName();
55 if (!headers.containsKey(key)) {
56 headers.add(key, entry.getValue());
57 }
58 }
59 }
60 return clientHttpRequestExecution.execute(httpRequest, body);
61 }
62 }