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.boot.fhir;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.narrative.INarrativeGenerator;
21  import ca.uhn.fhir.rest.server.ApacheProxyAddressStrategy;
22  import ca.uhn.fhir.rest.server.IPagingProvider;
23  import ca.uhn.fhir.rest.server.IServerAddressStrategy;
24  import ca.uhn.fhir.rest.server.IServerConformanceProvider;
25  import org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider;
26  import org.hl7.fhir.dstu3.model.CapabilityStatement;
27  import org.openehealth.ipf.boot.atna.IpfAtnaAutoConfiguration;
28  import org.openehealth.ipf.commons.ihe.fhir.IpfFhirServlet;
29  import org.openehealth.ipf.commons.ihe.fhir.support.DefaultNamingSystemServiceImpl;
30  import org.openehealth.ipf.commons.ihe.fhir.support.NamingSystemService;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.boot.autoconfigure.AutoConfigureAfter;
33  import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
34  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
35  import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
36  import org.springframework.boot.context.properties.EnableConfigurationProperties;
37  import org.springframework.boot.web.servlet.ServletRegistrationBean;
38  import org.springframework.cache.CacheManager;
39  import org.springframework.context.annotation.Bean;
40  import org.springframework.context.annotation.Configuration;
41  import org.springframework.core.io.Resource;
42  
43  import java.io.IOException;
44  import java.io.InputStreamReader;
45  import java.nio.charset.StandardCharsets;
46  import java.util.Map;
47  
48  
49  @Configuration
50  @AutoConfigureAfter(IpfAtnaAutoConfiguration.class)
51  @EnableConfigurationProperties(IpfFhirConfigurationProperties.class)
52  public class IpfFhirAutoConfiguration {
53  
54      @Autowired
55      private IpfFhirConfigurationProperties config;
56  
57      @Bean
58      public FhirContext fhirContext() {
59          return new FhirContext(config.getFhirVersion());
60      }
61  
62      @Bean
63      @ConditionalOnMissingBean(NamingSystemService.class)
64      public NamingSystemService namingSystemService(FhirContext fhirContext) throws IOException {
65          DefaultNamingSystemServiceImpl namingSystemService = new DefaultNamingSystemServiceImpl(fhirContext);
66          for (Resource resource : config.getNamingSystems()) {
67              namingSystemService.addNamingSystemsFromXml(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8));
68          }
69          return namingSystemService;
70      }
71  
72      @Bean
73      @ConditionalOnMissingBean(name = "fhirServletRegistration")
74      @ConditionalOnWebApplication
75      public ServletRegistrationBean fhirServletRegistration(IpfFhirServlet camelFhirServlet) {
76          String urlMapping = config.getFhirMapping();
77          ServletRegistrationBean registration = new ServletRegistrationBean(camelFhirServlet, urlMapping);
78          IpfFhirConfigurationProperties.Servlet servletProperties = config.getServlet();
79          registration.setLoadOnStartup(servletProperties.getLoadOnStartup());
80          registration.setName(servletProperties.getName());
81          for (Map.Entry<String, String> entry : servletProperties.getInit().entrySet()) {
82              registration.addInitParameter(entry.getKey(), entry.getValue());
83          }
84          return registration;
85      }
86  
87      @Bean
88      @ConditionalOnMissingBean(IServerConformanceProvider.class)
89      public IServerConformanceProvider<CapabilityStatement> serverConformanceProvider() {
90          return new ServerCapabilityStatementProvider();
91      }
92  
93      @Bean
94      @ConditionalOnMissingBean(IServerAddressStrategy.class)
95      public IServerAddressStrategy serverAddressStrategy() {
96          return ApacheProxyAddressStrategy.forHttp();
97      }
98  
99      @Bean
100     @ConditionalOnMissingBean(INarrativeGenerator.class)
101     public INarrativeGenerator narrativeGenerator() {
102         return null;
103     }
104 
105     @Bean
106     @ConditionalOnMissingBean(IPagingProvider.class)
107     @ConditionalOnProperty("ipf.fhir.caching")
108     public IPagingProvider pagingProvider(CacheManager cacheManager, FhirContext fhirContext) {
109         IpfFhirConfigurationProperties.Servlet servletProperties = config.getServlet();
110         CachingPagingProvider pagingProvider = new CachingPagingProvider(cacheManager, fhirContext);
111         pagingProvider.setDefaultPageSize(servletProperties.getDefaultPageSize());
112         pagingProvider.setMaximumPageSize(servletProperties.getMaxPageSize());
113         pagingProvider.setDistributed(servletProperties.isDistributedPagingProvider());
114         return pagingProvider;
115     }
116 
117     @Bean
118     @ConditionalOnMissingBean(IpfFhirServlet.class)
119     @ConditionalOnWebApplication
120     public IpfFhirServlet fhirServlet(
121             IServerConformanceProvider<CapabilityStatement> serverConformanceProvider,
122             @Autowired(required = false) IPagingProvider pagingProvider,
123             IServerAddressStrategy serverAddressStrategy,
124             INarrativeGenerator narrativeGenerator) {
125         IpfFhirServlet fhirServlet = new IpfFhirServlet(config.getFhirVersion());
126         IpfFhirConfigurationProperties.Servlet servletProperties = config.getServlet();
127 
128         fhirServlet.setLogging(servletProperties.isLogging());
129         fhirServlet.setPrettyPrint(servletProperties.isPrettyPrint());
130         fhirServlet.setResponseHighlighting(servletProperties.isResponseHighlighting());
131         fhirServlet.setStrictErrorHandler(servletProperties.isStrict());
132         fhirServlet.setServerConformanceProvider(serverConformanceProvider);
133         fhirServlet.setServerAddressStrategy(serverAddressStrategy);
134         if (narrativeGenerator != null) {
135             fhirServlet.setNarrativeGenerator(narrativeGenerator);
136         }
137         if (pagingProvider != null) {
138             fhirServlet.setPagingProvider(pagingProvider);
139         }
140         return fhirServlet;
141     }
142 
143 }