diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java index 20f694e62b1..7b95cb667e2 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/config/BaseDstu2Config.java @@ -20,6 +20,16 @@ package ca.uhn.fhir.jpa.config; * #L% */ +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.dao.*; +import ca.uhn.fhir.jpa.term.HapiTerminologySvcDstu2; +import ca.uhn.fhir.jpa.term.IHapiTerminologySvc; +import ca.uhn.fhir.model.dstu2.composite.MetaDt; +import ca.uhn.fhir.validation.IValidatorModule; +import org.hl7.fhir.instance.hapi.validation.DefaultProfileValidationSupport; +import org.hl7.fhir.instance.hapi.validation.FhirInstanceValidator; +import org.hl7.fhir.instance.hapi.validation.ValidationSupportChain; +import org.hl7.fhir.instance.validation.IResourceValidator.BestPracticeWarningLevel; import org.springframework.beans.factory.annotation.Autowire; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -27,17 +37,6 @@ import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Primary; import org.springframework.transaction.annotation.EnableTransactionManagement; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jpa.dao.FulltextSearchSvcImpl; -import ca.uhn.fhir.jpa.dao.IFhirSystemDao; -import ca.uhn.fhir.jpa.dao.IFulltextSearchSvc; -import ca.uhn.fhir.jpa.dao.ISearchParamRegistry; -import ca.uhn.fhir.jpa.dao.SearchParamExtractorDstu2; -import ca.uhn.fhir.jpa.dao.SearchParamRegistryDstu2; -import ca.uhn.fhir.jpa.term.HapiTerminologySvcDstu2; -import ca.uhn.fhir.jpa.term.IHapiTerminologySvc; -import ca.uhn.fhir.model.dstu2.composite.MetaDt; - @Configuration @EnableTransactionManagement public class BaseDstu2Config extends BaseConfig { @@ -75,6 +74,15 @@ public class BaseDstu2Config extends BaseConfig { return retVal; } + @Bean(name = "myInstanceValidatorDstu2") + @Lazy + public IValidatorModule instanceValidatorDstu2() { + FhirInstanceValidator retVal = new FhirInstanceValidator(); + retVal.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning); + retVal.setValidationSupport(new ValidationSupportChain(new DefaultProfileValidationSupport(), jpaValidationSupportDstu2())); + return retVal; + } + @Bean(autowire = Autowire.BY_TYPE) public IFulltextSearchSvc searchDao() { FulltextSearchSvcImpl searchDao = new FulltextSearchSvcImpl(); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2.java index 62ae1312e66..7df0c6be92a 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoDstu2.java @@ -1,43 +1,5 @@ package ca.uhn.fhir.jpa.dao; -import static org.apache.commons.lang3.StringUtils.isNotBlank; - -import java.util.ArrayList; - -/* - * #%L - * HAPI FHIR JPA Server - * %% - * Copyright (C) 2014 - 2017 University Health Network - * %% - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * #L% - */ - -import java.util.Collections; -import java.util.List; - -import org.hl7.fhir.instance.hapi.validation.DefaultProfileValidationSupport; -import org.hl7.fhir.instance.hapi.validation.FhirInstanceValidator; -import org.hl7.fhir.instance.hapi.validation.IValidationSupport; -import org.hl7.fhir.instance.hapi.validation.ValidationSupportChain; -import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.instance.model.api.IIdType; -import org.hl7.fhir.instance.validation.IResourceValidator.BestPracticeWarningLevel; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; - import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.context.RuntimeSearchParam; import ca.uhn.fhir.jpa.entity.ResourceTable; @@ -64,6 +26,38 @@ import ca.uhn.fhir.validation.FhirValidator; import ca.uhn.fhir.validation.IValidationContext; import ca.uhn.fhir.validation.IValidatorModule; import ca.uhn.fhir.validation.ValidationResult; +import org.hl7.fhir.instance.hapi.validation.IValidationSupport; +import org.hl7.fhir.instance.model.api.IBaseOperationOutcome; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; + +/* + * #%L + * HAPI FHIR JPA Server + * %% + * Copyright (C) 2014 - 2017 University Health Network + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ public class FhirResourceDaoDstu2 extends BaseHapiFhirResourceDao { @@ -71,6 +65,9 @@ public class FhirResourceDaoDstu2 extends BaseHapiFhirResou @Qualifier("myJpaValidationSupportDstu2") private IValidationSupport myJpaValidationSupport; + @Autowired() + @Qualifier("myInstanceValidatorDstu2") + private IValidatorModule myInstanceValidator; @Override protected List getIncludeValues(FhirTerser theTerser, Include theInclude, IBaseResource theResource, RuntimeResourceDefinition theResourceDef) { @@ -124,10 +121,7 @@ public class FhirResourceDaoDstu2 extends BaseHapiFhirResou FhirValidator validator = getContext().newValidator(); - FhirInstanceValidator val = new FhirInstanceValidator(); - val.setBestPracticeWarningLevel(BestPracticeWarningLevel.Warning); - val.setValidationSupport(new ValidationSupportChain(new DefaultProfileValidationSupport(), myJpaValidationSupport)); - validator.registerValidatorModule(val); + validator.registerValidatorModule(myInstanceValidator); validator.registerValidatorModule(new IdChecker(theMode)); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/config/TestDstu2Config.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/config/TestDstu2Config.java index 51560436e1f..824133f0921 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/config/TestDstu2Config.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/config/TestDstu2Config.java @@ -1,21 +1,20 @@ package ca.uhn.fhir.jpa.config; -import java.util.Properties; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor; +import ca.uhn.fhir.validation.ResultSeverityEnum; import org.apache.commons.dbcp2.BasicDataSource; import org.hibernate.jpa.HibernatePersistenceProvider; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; +import org.springframework.context.annotation.Lazy; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; -import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jpa.dao.DaoConfig; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import java.util.Properties; @Configuration @EnableTransactionManagement() @@ -65,4 +64,19 @@ public class TestDstu2Config extends BaseJavaConfigDstu2 { return extraProperties; } + /** + * Bean which validates incoming requests + */ + @Bean + @Lazy + public RequestValidatingInterceptor requestValidatingInterceptor() { + RequestValidatingInterceptor requestValidator = new RequestValidatingInterceptor(); + requestValidator.setFailOnSeverity(ResultSeverityEnum.ERROR); + requestValidator.setAddResponseHeaderOnSeverity(null); + requestValidator.setAddResponseOutcomeHeaderOnSeverity(ResultSeverityEnum.INFORMATION); + requestValidator.addValidatorModule(instanceValidatorDstu2()); + + return requestValidator; + } + } diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TdlDstu2Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TdlDstu2Config.java index 51ffedf1a03..43d286f5905 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TdlDstu2Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TdlDstu2Config.java @@ -1,10 +1,14 @@ package ca.uhn.fhirtest.config; -import java.util.Properties; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - +import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptorDstu2; +import ca.uhn.fhir.rest.api.RestOperationTypeEnum; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; +import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor; +import ca.uhn.fhir.rest.server.interceptor.ResponseValidatingInterceptor; +import ca.uhn.fhir.validation.ResultSeverityEnum; +import ca.uhn.fhirtest.interceptor.TdlSecurityInterceptor; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.time.DateUtils; import org.hibernate.dialect.DerbyTenSevenDialect; @@ -13,18 +17,16 @@ import org.springframework.beans.factory.annotation.Autowire; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Lazy; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; -import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; -import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptorDstu2; -import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; -import ca.uhn.fhirtest.interceptor.TdlSecurityInterceptor; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import java.util.Properties; @Configuration @Import(CommonConfig.class) @@ -113,6 +115,49 @@ public class TdlDstu2Config extends BaseJavaConfigDstu2 { return extraProperties; } + /** + * Bean which validates incoming requests + */ + @Bean + @Lazy + public RequestValidatingInterceptor requestValidatingInterceptor() { + RequestValidatingInterceptor requestValidator = new RequestValidatingInterceptor(); + requestValidator.setFailOnSeverity(null); + requestValidator.setAddResponseHeaderOnSeverity(null); + requestValidator.setAddResponseOutcomeHeaderOnSeverity(ResultSeverityEnum.INFORMATION); + requestValidator.addValidatorModule(instanceValidatorDstu2()); + requestValidator.setIgnoreValidatorExceptions(true); + + return requestValidator; + } + + /** + * Bean which validates outgoing responses + */ + @Bean + @Lazy + public ResponseValidatingInterceptor responseValidatingInterceptor() { + ResponseValidatingInterceptor responseValidator = new ResponseValidatingInterceptor(); + responseValidator.setResponseHeaderValueNoIssues("Validation did not detect any issues"); + responseValidator.setFailOnSeverity(null); + responseValidator.setAddResponseHeaderOnSeverity(null); + responseValidator.setAddResponseOutcomeHeaderOnSeverity(ResultSeverityEnum.INFORMATION); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.METADATA); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.EXTENDED_OPERATION_INSTANCE); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.EXTENDED_OPERATION_SERVER); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.EXTENDED_OPERATION_TYPE); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.GET_PAGE); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.HISTORY_INSTANCE); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.HISTORY_SYSTEM); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.HISTORY_TYPE); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.SEARCH_SYSTEM); + responseValidator.addExcludeOperationType(RestOperationTypeEnum.SEARCH_TYPE); + responseValidator.addValidatorModule(instanceValidatorDstu2()); + responseValidator.setIgnoreValidatorExceptions(true); + + return responseValidator; + } + @Bean(autowire=Autowire.BY_TYPE) public IServerInterceptor subscriptionSecurityInterceptor() { return new SubscriptionsRequireManualActivationInterceptorDstu2(); diff --git a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java index cb8997c90aa..4fc19e053f1 100644 --- a/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java +++ b/hapi-fhir-jpaserver-uhnfhirtest/src/main/java/ca/uhn/fhirtest/config/TestDstu2Config.java @@ -1,30 +1,28 @@ package ca.uhn.fhirtest.config; -import java.util.Properties; - -import javax.persistence.EntityManagerFactory; -import javax.sql.DataSource; - +import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; +import ca.uhn.fhir.jpa.dao.DaoConfig; +import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; +import ca.uhn.fhir.rest.server.interceptor.RequestValidatingInterceptor; +import ca.uhn.fhir.validation.ResultSeverityEnum; +import ca.uhn.fhirtest.interceptor.PublicSecurityInterceptor; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.lang3.time.DateUtils; import org.hibernate.dialect.PostgreSQL94Dialect; import org.hibernate.jpa.HibernatePersistenceProvider; -import org.springframework.beans.factory.annotation.Autowire; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Lazy; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; -import ca.uhn.fhir.jpa.config.BaseJavaConfigDstu2; -import ca.uhn.fhir.jpa.dao.DaoConfig; -import ca.uhn.fhir.jpa.util.SubscriptionsRequireManualActivationInterceptorDstu2; -import ca.uhn.fhir.rest.server.interceptor.IServerInterceptor; -import ca.uhn.fhirtest.interceptor.PublicSecurityInterceptor; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import java.util.Properties; @Configuration @Import(CommonConfig.class) @@ -114,6 +112,22 @@ public class TestDstu2Config extends BaseJavaConfigDstu2 { return extraProperties; } + /** + * Bean which validates incoming requests + */ + @Bean + @Lazy + public RequestValidatingInterceptor requestValidatingInterceptor() { + RequestValidatingInterceptor requestValidator = new RequestValidatingInterceptor(); + requestValidator.setFailOnSeverity(null); + requestValidator.setAddResponseHeaderOnSeverity(null); + requestValidator.setAddResponseOutcomeHeaderOnSeverity(ResultSeverityEnum.INFORMATION); + requestValidator.addValidatorModule(instanceValidatorDstu2()); + requestValidator.setIgnoreValidatorExceptions(true); + + return requestValidator; + } + // @Bean(autowire = Autowire.BY_TYPE) // public IServerInterceptor subscriptionSecurityInterceptor() { // return new SubscriptionsRequireManualActivationInterceptorDstu2(); diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 16ac5de552d..7dbc2ada03b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -26,6 +26,11 @@ Web testing UI displayed an error when a transaction was pasted into the UI for a DSTU2 server. Thanks to Suresh Kumar for reporting! + + hapi-fhir-jpaserver-base now exposes a + FhirInstanceValidator bean named "myInstanceValidatorDstu2"]]> + for DSTU2. A similar bean for DSTU3 was previously implemented. +