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;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.rest.client.api.IGenericClient;
21  import ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor;
22  import ca.uhn.fhir.rest.gclient.IClientExecutable;
23  import org.apache.camel.Endpoint;
24  import org.apache.camel.Exchange;
25  import org.apache.camel.Message;
26  import org.apache.camel.impl.DefaultProducer;
27  import org.apache.camel.spi.HeaderFilterStrategy;
28  import org.openehealth.ipf.commons.ihe.fhir.ClientRequestFactory;
29  import org.openehealth.ipf.commons.ihe.fhir.audit.FhirAuditDataset;
30  import org.openehealth.ipf.commons.ihe.fhir.SslAwareApacheRestfulClientFactory;
31  import org.openehealth.ipf.commons.ihe.fhir.translation.FhirSecurityInformation;
32  import org.openehealth.ipf.platform.camel.core.util.Exchanges;
33  
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * @author Christian Ohr
39   * @since 3.1
40   */
41  public class FhirProducer<AuditDatasetType extends FhirAuditDataset> extends DefaultProducer {
42  
43      private IGenericClient client;
44      private HeaderFilterStrategy headerFilterStrategy;
45  
46      public FhirProducer(Endpoint endpoint) {
47          super(endpoint);
48      }
49  
50      protected synchronized IGenericClient getClient(Exchange exchange) {
51          if (client == null) {
52              FhirContext context = getEndpoint().getContext();
53              SslAwareApacheRestfulClientFactory clientFactory = (SslAwareApacheRestfulClientFactory)context.getRestfulClientFactory();
54              FhirEndpointConfiguration<AuditDatasetType> config = getEndpoint().getInterceptableConfiguration();
55              FhirSecurityInformation securityInformation = config.getSecurityInformation();
56              clientFactory.setSecurityInformation(securityInformation);
57  
58              // For the producer, the path is supposed to be the server URL
59              String path = config.getPath();
60  
61              path = (config.getSecurityInformation().isSecure() ? "https://" : "http://") + path;
62              client = clientFactory.newGenericClient(path);
63  
64              if (securityInformation != null && securityInformation.getUsername() != null) {
65                  client.registerInterceptor(new BasicAuthInterceptor(securityInformation.getUsername(), securityInformation.getPassword()));
66              }
67  
68              // deploy user-defined HAPI interceptors
69              List<HapiClientInterceptorFactory> factories = config.getHapiClientInterceptorFactories();
70              if (factories != null) {
71                  for (HapiClientInterceptorFactory factory : factories) {
72                      client.registerInterceptor(factory.newInstance(getEndpoint(), exchange));
73                  }
74              }
75  
76          }
77          return client;
78      }
79  
80      @Override
81      public FhirEndpoint<AuditDatasetType, FhirComponent<AuditDatasetType>> getEndpoint() {
82          return (FhirEndpoint<AuditDatasetType, FhirComponent<AuditDatasetType>>)super.getEndpoint();
83      }
84  
85      /**
86       * Processes the exchange. Body and Headers are forwarded to
87       * {@link ClientRequestFactory#getClientExecutable(IGenericClient, Object, Map)}, so that
88       * the actual query can be dynamically constructed from the exchange.
89       *
90       * @param exchange Camel exchange
91       * @throws Exception
92       */
93      @Override
94      public void process(Exchange exchange) {
95          ClientRequestFactory<?> requestFactory = getEndpoint().getClientRequestFactory();
96          IClientExecutable<?, ?> executableClient = requestFactory.getClientExecutable(
97                  getClient(exchange),
98                  exchange.getIn().getBody(),
99                  exchange.getIn().getHeaders());
100         Object result = executableClient.execute();
101         Message resultMessage = Exchanges.resultMessage(exchange);
102         resultMessage.setBody(result);
103     }
104 
105 
106 }