From d1f16d1218afc9d9f58e12ed97d7ba0dc1b9b348 Mon Sep 17 00:00:00 2001 From: Clayton Bodendein Date: Tue, 28 Nov 2017 13:37:23 -0600 Subject: [PATCH 1/2] Fixes issue #799 by adding logic to handle the R4 and DSTU2_1 contexts in ca.uhn.fhir.jaxrs.server.util.JaxRsRequest. Also changed the locations in the hapi-fhir-jaxrsserver-base module where it was doing if-else branches based on the FhirVersionEnum to now use switch statements instead. --- .../AbstractJaxRsConformanceProvider.java | 114 ++++++++++-------- .../fhir/jaxrs/server/util/JaxRsRequest.java | 75 +++++++++--- 2 files changed, 122 insertions(+), 67 deletions(-) diff --git a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsConformanceProvider.java b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsConformanceProvider.java index 1b7f328e6e9..52d73b8cfa7 100644 --- a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsConformanceProvider.java +++ b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/AbstractJaxRsConformanceProvider.java @@ -34,8 +34,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.apache.commons.lang3.StringUtils; -import org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider; -import org.hl7.fhir.dstu3.model.CapabilityStatement; import org.hl7.fhir.instance.model.api.IBaseResource; import org.slf4j.LoggerFactory; @@ -67,10 +65,10 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv /** the conformance. It is created once during startup */ private org.hl7.fhir.r4.model.CapabilityStatement myR4CapabilityStatement; - private CapabilityStatement myDstu3CapabilityStatement; + private org.hl7.fhir.dstu3.model.CapabilityStatement myDstu3CapabilityStatement; private org.hl7.fhir.dstu2016may.model.Conformance myDstu2_1Conformance; - private ca.uhn.fhir.model.dstu2.resource.Conformance myDstu2Conformance; private org.hl7.fhir.instance.model.Conformance myDstu2Hl7OrgConformance; + private ca.uhn.fhir.model.dstu2.resource.Conformance myDstu2Conformance; /** * Constructor allowing the description, servername and server to be set @@ -127,26 +125,35 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv HardcodedServerAddressStrategy hardcodedServerAddressStrategy = new HardcodedServerAddressStrategy(); hardcodedServerAddressStrategy.setValue(getBaseForServer()); serverConfiguration.setServerAddressStrategy(hardcodedServerAddressStrategy); - if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.R4)) { - org.hl7.fhir.r4.hapi.rest.server.ServerCapabilityStatementProvider serverCapabilityStatementProvider = new org.hl7.fhir.r4.hapi.rest.server.ServerCapabilityStatementProvider(serverConfiguration); - serverCapabilityStatementProvider.initializeOperations(); - myR4CapabilityStatement = serverCapabilityStatementProvider.getServerConformance(null); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { - ServerCapabilityStatementProvider serverCapabilityStatementProvider = new ServerCapabilityStatementProvider(serverConfiguration); - serverCapabilityStatementProvider.initializeOperations(); - myDstu3CapabilityStatement = serverCapabilityStatementProvider.getServerConformance(null); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_1)) { - org.hl7.fhir.dstu2016may.hapi.rest.server.ServerConformanceProvider serverCapabilityStatementProvider = new org.hl7.fhir.dstu2016may.hapi.rest.server.ServerConformanceProvider(serverConfiguration); - serverCapabilityStatementProvider.initializeOperations(); - myDstu2_1Conformance = serverCapabilityStatementProvider.getServerConformance(null); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) { - ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider serverCapabilityStatementProvider = new ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider(serverConfiguration); - serverCapabilityStatementProvider.initializeOperations(); - myDstu2Conformance = serverCapabilityStatementProvider.getServerConformance(null); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_HL7ORG)) { - org.hl7.fhir.instance.conf.ServerConformanceProvider serverCapabilityStatementProvider = new org.hl7.fhir.instance.conf.ServerConformanceProvider(serverConfiguration); - serverCapabilityStatementProvider.initializeOperations(); - myDstu2Hl7OrgConformance = serverCapabilityStatementProvider.getServerConformance(null); + FhirVersionEnum fhirContextVersion = super.getFhirContext().getVersion().getVersion(); + switch (fhirContextVersion) { + case R4: + org.hl7.fhir.r4.hapi.rest.server.ServerCapabilityStatementProvider r4ServerCapabilityStatementProvider = new org.hl7.fhir.r4.hapi.rest.server.ServerCapabilityStatementProvider(serverConfiguration); + r4ServerCapabilityStatementProvider.initializeOperations(); + myR4CapabilityStatement = r4ServerCapabilityStatementProvider.getServerConformance(null); + break; + case DSTU3: + org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider dstu3ServerCapabilityStatementProvider = new org.hl7.fhir.dstu3.hapi.rest.server.ServerCapabilityStatementProvider(serverConfiguration); + dstu3ServerCapabilityStatementProvider.initializeOperations(); + myDstu3CapabilityStatement = dstu3ServerCapabilityStatementProvider.getServerConformance(null); + break; + case DSTU2_1: + org.hl7.fhir.dstu2016may.hapi.rest.server.ServerConformanceProvider dstu2_1ServerConformanceProvider = new org.hl7.fhir.dstu2016may.hapi.rest.server.ServerConformanceProvider(serverConfiguration); + dstu2_1ServerConformanceProvider.initializeOperations(); + myDstu2_1Conformance = dstu2_1ServerConformanceProvider.getServerConformance(null); + break; + case DSTU2_HL7ORG: + org.hl7.fhir.instance.conf.ServerConformanceProvider dstu2Hl7OrgServerConformanceProvider = new org.hl7.fhir.instance.conf.ServerConformanceProvider(serverConfiguration); + dstu2Hl7OrgServerConformanceProvider.initializeOperations(); + myDstu2Hl7OrgConformance = dstu2Hl7OrgServerConformanceProvider.getServerConformance(null); + break; + case DSTU2: + ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider dstu2ServerConformanceProvider = new ca.uhn.fhir.rest.server.provider.dstu2.ServerConformanceProvider(serverConfiguration); + dstu2ServerConformanceProvider.initializeOperations(); + myDstu2Conformance = dstu2ServerConformanceProvider.getServerConformance(null); + break; + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); } } @@ -181,20 +188,26 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv IRestfulResponse response = request.build().getResponse(); response.addHeader(Constants.HEADER_CORS_ALLOW_ORIGIN, "*"); - IBaseResource conformance = null; - if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.R4)) { - conformance = myR4CapabilityStatement; -// return (Response) response.returnResponse(ParseAction.create(myDstu3CapabilityStatement), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName()); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { - conformance = myDstu3CapabilityStatement; -// return (Response) response.returnResponse(ParseAction.create(myDstu3CapabilityStatement), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName()); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_1)) { - conformance = myDstu2_1Conformance; - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) { - conformance = myDstu2Conformance; -// return (Response) response.returnResponse(ParseAction.create(myDstu2CapabilityStatement), Constants.STATUS_HTTP_200_OK, true, null, getResourceType().getSimpleName()); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_HL7ORG)) { - conformance = myDstu2Hl7OrgConformance; + IBaseResource conformance; + FhirVersionEnum fhirContextVersion = super.getFhirContext().getVersion().getVersion(); + switch (fhirContextVersion) { + case R4: + conformance = myR4CapabilityStatement; + break; + case DSTU3: + conformance = myDstu3CapabilityStatement; + break; + case DSTU2_1: + conformance = myDstu2_1Conformance; + break; + case DSTU2_HL7ORG: + conformance = myDstu2Hl7OrgConformance; + break; + case DSTU2: + conformance = myDstu2Conformance; + break; + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); } if (conformance != null) { @@ -279,18 +292,21 @@ public abstract class AbstractJaxRsConformanceProvider extends AbstractJaxRsProv @SuppressWarnings("unchecked") @Override public Class getResourceType() { - if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.R4)) { - return Class.class.cast(org.hl7.fhir.r4.model.CapabilityStatement.class); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU3)) { - return Class.class.cast(CapabilityStatement.class); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_1)) { - return Class.class.cast(org.hl7.fhir.dstu2016may.model.Conformance.class); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2)) { - return Class.class.cast(ca.uhn.fhir.model.dstu2.resource.Conformance.class); - } else if (super.getFhirContext().getVersion().getVersion().equals(FhirVersionEnum.DSTU2_HL7ORG)) { - return Class.class.cast(org.hl7.fhir.instance.model.Conformance.class); + FhirVersionEnum fhirContextVersion = super.getFhirContext().getVersion().getVersion(); + switch (fhirContextVersion) { + case R4: + return Class.class.cast(org.hl7.fhir.r4.model.CapabilityStatement.class); + case DSTU3: + return Class.class.cast(org.hl7.fhir.dstu3.model.CapabilityStatement.class); + case DSTU2_1: + return Class.class.cast(org.hl7.fhir.dstu2016may.model.Conformance.class); + case DSTU2_HL7ORG: + return Class.class.cast(org.hl7.fhir.instance.model.Conformance.class); + case DSTU2: + return Class.class.cast(ca.uhn.fhir.model.dstu2.resource.Conformance.class); + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); } - return null; } } diff --git a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/util/JaxRsRequest.java b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/util/JaxRsRequest.java index 091bfeee655..aa53e82fe54 100644 --- a/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/util/JaxRsRequest.java +++ b/hapi-fhir-jaxrsserver-base/src/main/java/ca/uhn/fhir/jaxrs/server/util/JaxRsRequest.java @@ -28,12 +28,11 @@ import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import org.apache.commons.lang3.StringUtils; -import org.hl7.fhir.dstu3.model.IdType; +import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.jaxrs.server.AbstractJaxRsProvider; -import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.api.*; import ca.uhn.fhir.rest.api.server.IRestfulResponse; import ca.uhn.fhir.rest.api.server.RequestDetails; @@ -93,28 +92,68 @@ public class JaxRsRequest extends RequestDetails { FhirVersionEnum fhirContextVersion = myServer.getFhirContext().getVersion().getVersion(); if (StringUtils.isNotBlank(myVersion)) { - if (FhirVersionEnum.DSTU3.equals(fhirContextVersion) || FhirVersionEnum.DSTU2_HL7ORG.equals(fhirContextVersion)) { - result.setId( - new IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); - } else if (FhirVersionEnum.DSTU2.equals(fhirContextVersion)) { - result.setId( - new IdDt(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); - } + switch (fhirContextVersion) { + case R4: + result.setId(new org.hl7.fhir.r4.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); + break; + case DSTU3: + result.setId(new org.hl7.fhir.dstu3.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); + break; + case DSTU2_1: + result.setId(new org.hl7.fhir.dstu2016may.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); + break; + case DSTU2_HL7ORG: + result.setId(new org.hl7.fhir.instance.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); + break; + case DSTU2: + result.setId(new ca.uhn.fhir.model.primitive.IdDt(myServer.getBaseForRequest(), UrlUtil.unescape(myId), UrlUtil.unescape(myVersion))); + break; + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); + } } else if (StringUtils.isNotBlank(myId)) { - if (FhirVersionEnum.DSTU3.equals(fhirContextVersion) || FhirVersionEnum.DSTU2_HL7ORG.equals(fhirContextVersion)) { - result.setId(new IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); - } else if (FhirVersionEnum.DSTU2.equals(fhirContextVersion)) { - result.setId(new IdDt(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); - } + switch (fhirContextVersion) { + case R4: + result.setId(new org.hl7.fhir.r4.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); + break; + case DSTU3: + result.setId(new org.hl7.fhir.dstu3.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); + break; + case DSTU2_1: + result.setId(new org.hl7.fhir.dstu2016may.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); + break; + case DSTU2_HL7ORG: + result.setId(new org.hl7.fhir.instance.model.IdType(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); + break; + case DSTU2: + result.setId(new ca.uhn.fhir.model.primitive.IdDt(myServer.getBaseForRequest(), UrlUtil.unescape(myId))); + break; + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); + } } if (myRestOperation == RestOperationTypeEnum.UPDATE) { String contentLocation = result.getHeader(Constants.HEADER_CONTENT_LOCATION); if (contentLocation != null) { - if (FhirVersionEnum.DSTU3.equals(fhirContextVersion) || FhirVersionEnum.DSTU2_HL7ORG.equals(fhirContextVersion)) { - result.setId(new IdType(contentLocation)); - } else if (FhirVersionEnum.DSTU2.equals(fhirContextVersion)) { - result.setId(new IdDt(contentLocation)); + switch (fhirContextVersion) { + case R4: + result.setId(new org.hl7.fhir.r4.model.IdType(contentLocation)); + break; + case DSTU3: + result.setId(new org.hl7.fhir.dstu3.model.IdType(contentLocation)); + break; + case DSTU2_1: + result.setId(new org.hl7.fhir.dstu2016may.model.IdType(contentLocation)); + break; + case DSTU2_HL7ORG: + result.setId(new org.hl7.fhir.instance.model.IdType(contentLocation)); + break; + case DSTU2: + result.setId(new ca.uhn.fhir.model.primitive.IdDt(contentLocation)); + break; + default: + throw new ConfigurationException("Unsupported Fhir version: " + fhirContextVersion); } } } From b81e89604cfae66245cf9155681d5a90217a375a Mon Sep 17 00:00:00 2001 From: Clayton Bodendein Date: Wed, 29 Nov 2017 15:50:10 -0600 Subject: [PATCH 2/2] Related to issue #799, adding DSTU2_1 and R4 contexts to the FhirContext#(FhirVersionEnum, Collection>) method --- .../src/main/java/ca/uhn/fhir/context/FhirContext.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/FhirContext.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/FhirContext.java index 106d9c6589e..3b2c26cf26a 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/FhirContext.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/FhirContext.java @@ -149,8 +149,12 @@ public class FhirContext { myVersion = FhirVersionEnum.DSTU2.getVersionImplementation(); } else if (FhirVersionEnum.DSTU2_HL7ORG.isPresentOnClasspath()) { myVersion = FhirVersionEnum.DSTU2_HL7ORG.getVersionImplementation(); + } else if (FhirVersionEnum.DSTU2_1.isPresentOnClasspath()) { + myVersion = FhirVersionEnum.DSTU2_1.getVersionImplementation(); } else if (FhirVersionEnum.DSTU3.isPresentOnClasspath()) { myVersion = FhirVersionEnum.DSTU3.getVersionImplementation(); + } else if (FhirVersionEnum.R4.isPresentOnClasspath()) { + myVersion = FhirVersionEnum.R4.getVersionImplementation(); } else { throw new IllegalStateException(getLocalizer().getMessage(FhirContext.class, "noStructures")); }