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 org.apache.camel.Consumer;
21 import org.apache.camel.ExchangePattern;
22 import org.apache.camel.Processor;
23 import org.apache.camel.Producer;
24 import org.apache.camel.impl.DefaultEndpoint;
25 import org.openehealth.ipf.commons.audit.AuditContext;
26 import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategy;
27 import org.openehealth.ipf.commons.ihe.fhir.AbstractPlainProvider;
28 import org.openehealth.ipf.commons.ihe.fhir.ClientRequestFactory;
29 import org.openehealth.ipf.commons.ihe.fhir.audit.FhirAuditDataset;
30 import org.openehealth.ipf.platform.camel.ihe.atna.AuditableEndpoint;
31 import org.openehealth.ipf.platform.camel.ihe.core.InterceptableEndpoint;
32 import org.openehealth.ipf.platform.camel.ihe.core.Interceptor;
33 import org.openehealth.ipf.platform.camel.ihe.fhir.core.intercept.consumer.ConsumerAuditInterceptor;
34 import org.openehealth.ipf.platform.camel.ihe.fhir.core.intercept.producer.ProducerAuditInterceptor;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39
40
41
42
43
44
45 public abstract class FhirEndpoint<AuditDatasetType extends FhirAuditDataset, ComponentType extends FhirComponent<AuditDatasetType>>
46 extends DefaultEndpoint
47 implements InterceptableEndpoint<FhirEndpointConfiguration<AuditDatasetType>, ComponentType>, AuditableEndpoint<AuditDatasetType> {
48
49 private final FhirEndpointConfiguration<AuditDatasetType> config;
50 private final ComponentType fhirComponent;
51
52 public FhirEndpoint(String uri, ComponentType fhirComponent, FhirEndpointConfiguration<AuditDatasetType> config) {
53 super(uri, fhirComponent);
54 this.fhirComponent = fhirComponent;
55 this.config = config;
56 this.setExchangePattern(ExchangePattern.InOut);
57 }
58
59 @Override
60 public ComponentType getInterceptableComponent() {
61 return fhirComponent;
62 }
63
64 @Override
65 public Producer doCreateProducer() {
66 return new FhirProducer<AuditDatasetType>(this);
67 }
68
69
70
71
72
73
74
75 public void connect(FhirConsumer<AuditDatasetType> consumer) throws Exception {
76 for (AbstractPlainProvider provider : getResourceProviders()) {
77
78 provider.setConsumer(consumer);
79 fhirComponent.connect(consumer, provider);
80 }
81
82 }
83
84
85
86
87
88
89
90 public void disconnect(FhirConsumer<AuditDatasetType> consumer) throws Exception {
91 for (AbstractPlainProvider provider : getResourceProviders()) {
92 provider.unsetConsumer(consumer);
93 fhirComponent.disconnect(consumer, provider);
94 }
95 }
96
97 public FhirContext getContext() {
98 return getInterceptableConfiguration().getContext();
99 }
100
101
102
103
104
105
106
107 @Override
108 public List<Interceptor> createInitialConsumerInterceptorChain() {
109 List<Interceptor> initialChain = new ArrayList<>();
110 if (isAudit()) {
111 initialChain.add(new ConsumerAuditInterceptor<>(getAuditContext()));
112 }
113 return initialChain;
114 }
115
116
117
118
119
120
121
122 @Override
123 public List<Interceptor> createInitialProducerInterceptorChain() {
124 List<Interceptor> initialChain = new ArrayList<>();
125 if (isAudit()) {
126 initialChain.add(new ProducerAuditInterceptor<>(getAuditContext()));
127 }
128 return initialChain;
129 }
130
131 @Override
132 public boolean isSingleton() {
133 return true;
134 }
135
136 @Override
137 public FhirEndpointConfiguration<AuditDatasetType> getInterceptableConfiguration() {
138 return config;
139 }
140
141 @Override
142 public AuditStrategy<AuditDatasetType> getClientAuditStrategy() {
143 return fhirComponent.getClientAuditStrategy();
144 }
145
146 @Override
147 public AuditStrategy<AuditDatasetType> getServerAuditStrategy() {
148 return fhirComponent.getServerAuditStrategy();
149 }
150
151 @Override
152 public AuditContext getAuditContext() {
153 return getInterceptableConfiguration().getAuditContext();
154 }
155
156 @Override
157 public Consumer doCreateConsumer(Processor processor) {
158 return new FhirConsumer<>(this, processor);
159 }
160
161
162
163 private List<? extends AbstractPlainProvider> getResourceProviders() {
164 List<? extends AbstractPlainProvider> providers = config.getResourceProvider();
165 if (providers == null) {
166 providers = fhirComponent.getFhirTransactionConfiguration().getStaticResourceProvider();
167 }
168 return providers;
169 }
170
171 public ClientRequestFactory<?> getClientRequestFactory() {
172 ClientRequestFactory<?> factory = config.getClientRequestFactory();
173 if (factory == null) {
174 factory = fhirComponent.getFhirTransactionConfiguration().getStaticClientRequestFactory();
175 }
176 return factory;
177 }
178
179 }