import java.util.Map;
import javax.servlet.ServletException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.RestfulServer;
@Configuration
@EnableAutoConfiguration
//comp scan to find package of resource providers; if you want to auto detect them
@ComponentScan(basePackageClasses = { path.to.package.resourceproviers.myPatientProvider.class })
public class FhirServerV1Config {
@Autowired
private ApplicationContext appContext;
@Bean
public FhirServer fhirServlet(FhirContext fhirCtx) {
return new FhirServer(fhirCtx) {
private static final long serialVersionUID = 1L;
@Override
protected void initialize() throws ServletException {
//register resource providers manually or by discovering them through component scanning
Map<String, IResourceProvider> resourceProviders = appContext.getBeansOfType(IResourceProvider.class);
setResourceProviders(resourceProviders.values());
Map<String, IServerBaseAware> serverAwares = appContext.getBeansOfType(IServerBaseAware.class);
for (IServerBaseAware serverAware : serverAwares.values())
serverAware.setServerBase(this);
}
};
}
@Bean
public ServletRegistrationBean fhirServletRegistrationBean(RestfulServer fhirServlet) {
ServletRegistrationBean s = new ServletRegistrationBean(fhirServlet, "/fhir/mybase/*");
s.setName("my server");
return s;
}
/**
* This enables CORS support
*/
@Bean
public FilterRegistrationBean corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
bean.setOrder(0);
return bean;
}
}