1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
39
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
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
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
87
88
89
90
91
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 }