View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.platform.camel.ihe.fhir.core.intercept.producer;
18  
19  import ca.uhn.fhir.rest.client.api.IClientInterceptor;
20  import ca.uhn.fhir.rest.client.api.IHttpRequest;
21  import ca.uhn.fhir.rest.client.api.IHttpResponse;
22  import org.apache.camel.Exchange;
23  import org.openehealth.ipf.platform.camel.ihe.fhir.core.FhirEndpoint;
24  import org.openehealth.ipf.platform.camel.ihe.fhir.core.HapiClientInterceptorFactory;
25  
26  import java.io.IOException;
27  import java.util.Map;
28  
29  import static org.openehealth.ipf.commons.ihe.core.Constants.*;
30  
31  /**
32   * HAPI interceptor that adds tracing information from the Camel message headers, if available,
33   * to the HTTP request headers
34   *
35   * @author Christian Ohr
36   * @since 3.2
37   */
38  public class ProducerTracingInterceptor implements IClientInterceptor, HapiClientInterceptorFactory {
39  
40      private final Map<String, Object> headers;
41  
42      private ProducerTracingInterceptor(Map<String, Object> headers) {
43          this.headers = headers;
44      }
45  
46      @Override
47      public IClientInterceptor newInstance(FhirEndpoint endpoint, Exchange exchange) {
48          return new ProducerTracingInterceptor(exchange.getIn().getHeaders());
49      }
50  
51      @Override
52      public void interceptRequest(IHttpRequest request) {
53          if (headers.containsKey(TRACE_ID)) {
54              request.addHeader(TRACE_ID, headers.get(TRACE_ID).toString());
55          }
56          if (headers.containsKey(SPAN_ID)) {
57              request.addHeader(SPAN_ID, headers.get(SPAN_ID).toString());
58          }
59          if (headers.containsKey(PARENT_SPAN_ID)) {
60              request.addHeader(PARENT_SPAN_ID, headers.get(PARENT_SPAN_ID).toString());
61          }
62          if (headers.containsKey(SAMPLED)) {
63              request.addHeader(SAMPLED, headers.get(SAMPLED).toString());
64          }
65          if (headers.containsKey(FLAGS)) {
66              request.addHeader(FLAGS, headers.get(FLAGS).toString());
67          }
68      }
69  
70      @Override
71      public void interceptResponse(IHttpResponse theResponse) throws IOException {
72          // nothing to do here?
73      }
74  }