View Javadoc
1   /*
2    * Copyright 2018 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.commons.ihe.fhir.support;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import org.hl7.fhir.dstu3.hapi.validation.DefaultProfileValidationSupport;
21  import org.hl7.fhir.dstu3.model.StructureDefinition;
22  import org.hl7.fhir.instance.model.api.IBaseResource;
23  
24  import java.io.InputStream;
25  import java.util.Optional;
26  import java.util.Scanner;
27  
28  /**
29   * Validation loader that first tries to load a custom structure definition before falling back to the
30   * default.
31   *
32   * @author Christian Ohr
33   * @since 3.4
34   */
35  public class CustomValidationSupport extends DefaultProfileValidationSupport {
36  
37      static final String HTTP_HL7_ORG_FHIR_STRUCTURE_DEFINITION = "http://hl7.org/fhir/StructureDefinition/";
38      private String prefix = "profiles/";
39  
40      public CustomValidationSupport() {
41      }
42  
43      public CustomValidationSupport(String prefix) {
44          this.prefix = prefix;
45      }
46  
47      @Override
48      public <T extends IBaseResource> T fetchResource(FhirContext fhirContext, Class<T> clazz, String uri) {
49          if (uri.startsWith(HTTP_HL7_ORG_FHIR_STRUCTURE_DEFINITION)) {
50              return (T) findProfile(fhirContext, uri.substring(HTTP_HL7_ORG_FHIR_STRUCTURE_DEFINITION.length()))
51                      .orElseGet(() -> super.fetchResource(fhirContext, clazz, uri));
52          } else {
53              return super.fetchResource(fhirContext, clazz, uri);
54          }
55      }
56  
57      private <T extends IBaseResource> Optional<T> findProfile(FhirContext fhirContext, String resourceName) {
58          String path = prefix + resourceName + ".xml";
59          InputStream is = getClass().getClassLoader().getResourceAsStream(path);
60          if (is != null) {
61              String profileText = new Scanner(getClass().getClassLoader().getResourceAsStream(path), "UTF-8").useDelimiter("\\A").next();
62              T structureDefinition = (T) fhirContext.newXmlParser().parseResource(StructureDefinition.class, profileText);
63              return Optional.of(structureDefinition);
64          }
65          return Optional.empty();
66      }
67  
68      public void setPrefix(String prefix) {
69          this.prefix = prefix;
70      }
71  }