View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * Creates a List of Headers containing the keys and values in the RequestContext that have a mapping indicating
33   * they should be propogated to the service being called.
34   *
35   * This class is designed to be used by Spring as part of the REST Template configuration when calling a REST service.
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  }