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.CamelContext;
21  import org.apache.camel.Endpoint;
22  import org.apache.camel.impl.UriEndpointComponent;
23  import org.openehealth.ipf.commons.ihe.core.atna.AuditStrategy;
24  import org.openehealth.ipf.commons.ihe.fhir.AbstractPlainProvider;
25  import org.openehealth.ipf.commons.ihe.fhir.DefaultFhirRegistry;
26  import org.openehealth.ipf.commons.ihe.fhir.audit.FhirAuditDataset;
27  import org.openehealth.ipf.commons.ihe.fhir.FhirInteractionId;
28  import org.openehealth.ipf.commons.ihe.fhir.FhirTransactionConfiguration;
29  import org.openehealth.ipf.platform.camel.ihe.atna.AuditableComponent;
30  import org.openehealth.ipf.platform.camel.ihe.core.InterceptableComponent;
31  import org.openehealth.ipf.platform.camel.ihe.core.Interceptor;
32  
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Map;
36  
37  /**
38   * Abstract FHIR Camel component
39   *
40   * @author Christian Ohr
41   * @since 3.1
42   */
43  public abstract class FhirComponent<AuditDatasetType extends FhirAuditDataset>
44          extends UriEndpointComponent implements AuditableComponent<AuditDatasetType>, InterceptableComponent {
45  
46      private FhirInteractionId fhirInteractionId;
47  
48  
49      public FhirComponent(FhirInteractionId fhirInteractionId) {
50          super(FhirEndpoint.class);
51          this.fhirInteractionId = fhirInteractionId;
52      }
53  
54      public FhirComponent(CamelContext context, FhirInteractionId fhirInteractionId) {
55          super(context, FhirEndpoint.class);
56          this.fhirInteractionId = fhirInteractionId;
57      }
58  
59      /**
60       * Connects the URL specified on the endpoint to the specified processor.
61       *
62       * @param consumer         the consumer
63       * @param resourceProvider the resource provider
64       * @throws Exception can be thrown
65       */
66      public void connect(FhirConsumer<AuditDatasetType> consumer, AbstractPlainProvider resourceProvider) throws Exception {
67          String name = consumer.getEndpoint().getInterceptableConfiguration().getServletName();
68          DefaultFhirRegistry.getFhirRegistry(name).register(resourceProvider);
69      }
70  
71      /**
72       * Disconnects the URL specified on the endpoint from the specified processor.
73       *
74       * @param consumer the consumer
75       * @throws Exception can be thrown
76       */
77      public void disconnect(FhirConsumer<AuditDatasetType> consumer, AbstractPlainProvider resourceProvider) throws Exception {
78          String name = consumer.getEndpoint().getInterceptableConfiguration().getServletName();
79          DefaultFhirRegistry.getFhirRegistry(name).unregister(resourceProvider);
80      }
81  
82      public FhirContext initializeFhirContext() {
83          return getFhirTransactionConfiguration().initializeFhirContext();
84      }
85  
86      protected FhirEndpointConfiguration<AuditDatasetType> createConfig(String remaining, Map<String, Object> parameters) throws Exception {
87          return new FhirEndpointConfiguration<>(this, remaining, parameters);
88      }
89  
90      @Override
91      protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
92          FhirEndpointConfiguration<AuditDatasetType> config = createConfig(remaining, parameters);
93          // Component configuration determines if lazy loading is allowed or not. Otherwise the endpoint has
94          // the choice to do so.
95          if (!fhirInteractionId.getFhirTransactionConfiguration().supportsLazyLoading() &&
96                  parameters.containsKey(FhirEndpointConfiguration.LAZY_LOAD_BUNDLES) &&
97                  Boolean.parseBoolean((String) parameters.get(FhirEndpointConfiguration.LAZY_LOAD_BUNDLES))) {
98              throw new IllegalArgumentException("The FHIR component " + getClass().getSimpleName() +
99                      " is configured to not support lazy-loading of bundles, but the endpoint requested to do so.");
100         }
101         return doCreateEndpoint(uri, config);
102     }
103 
104     @Override
105     public List<Interceptor<?>> getAdditionalConsumerInterceptors() {
106         return Collections.emptyList();
107     }
108 
109     @Override
110     public List<Interceptor<?>> getAdditionalProducerInterceptors() {
111         return Collections.emptyList();
112     }
113 
114     /**
115      * Returns a new endpoint instance
116      *
117      * @param uri    the endpoint URI
118      * @param config FhirEndpointConfiguration
119      * @return a new endpoint instance
120      */
121     protected abstract FhirEndpoint<?, ?> doCreateEndpoint(String uri, FhirEndpointConfiguration<AuditDatasetType> config);
122 
123     /**
124      * @return static component-specific configuration
125      */
126     public FhirTransactionConfiguration<AuditDatasetType> getFhirTransactionConfiguration() {
127         return fhirInteractionId.getFhirTransactionConfiguration();
128     }
129 
130     @Override
131     public AuditStrategy<AuditDatasetType> getServerAuditStrategy() {
132         return getFhirTransactionConfiguration().getServerAuditStrategy();
133     }
134 
135     @Override
136     public AuditStrategy<AuditDatasetType> getClientAuditStrategy() {
137         return getFhirTransactionConfiguration().getClientAuditStrategy();
138     }
139 
140     public FhirInteractionId getInteractionId() {
141         return fhirInteractionId;
142     }
143 
144     /**
145      * Sets the FHIR interactionID. Prefer setting the interactionId
146      * @param fhirInteractionId
147      */
148     public void setFhirInteractionId(FhirInteractionId fhirInteractionId) {
149         this.fhirInteractionId = fhirInteractionId;
150     }
151 }