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 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   * Generic FHIR endpoint
41   *
42   * @author Christian Ohr
43   * @since 3.1
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       * Called when a {@link FhirConsumer} is started. Registers the resource provider
71       *
72       * @param consumer FhirConsumer
73       * @throws Exception if resource provider could not be registered
74       */
75      public void connect(FhirConsumer<AuditDatasetType> consumer) throws Exception {
76          for (AbstractPlainProvider provider : getResourceProviders()) {
77              // Make consumer known to provider
78              provider.setConsumer(consumer);
79              fhirComponent.connect(consumer, provider);
80          }
81  
82      }
83  
84      /**
85       * Called when a {@link FhirConsumer} is stopped. Unregisters the resource provider
86       *
87       * @param consumer FhirConsumer
88       * @throws Exception if resource provider could not be unregistered
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      * Returns a list of interceptors that are default for FHIR consumers. Subclasses
103      * can add additional interceptors that are required for a concrete FHIR endpoint.
104      *
105      * @return list of default interceptors
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      * Returns a list of interceptors that are default for FHIR producers. Subclasses
118      * can add additional interceptors that are required for a concrete FHIR endpoint.
119      *
120      * @return list of default interceptors
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     // Private stuff
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 }