From cac44c736ce5a33b3dabec783f3f82bd93232b81 Mon Sep 17 00:00:00 2001 From: Sebastien Riviere Date: Thu, 19 Jan 2017 14:54:17 +0100 Subject: [PATCH] Add the possibility to use the serverBaseUrl to generate the url of extension, if %BASE_SERVER_URL% is found --- .../java/ca/uhn/fhir/parser/BaseParser.java | 4 +++ .../java/ca/uhn/fhir/parser/JsonParser.java | 10 +++---- .../java/ca/uhn/fhir/parser/XmlParser.java | 8 ++--- .../uhn/fhir/parser/CustomPatientDstu2.java | 29 +++++++++++++++++++ .../uhn/fhir/parser/JsonParserDstu2Test.java | 20 ++++++++++++- .../uhn/fhir/parser/XmlParserDstu2Test.java | 17 +++++++++++ .../uhn/fhir/parser/CustomPatientDstu3.java | 29 +++++++++++++++++++ .../uhn/fhir/parser/JsonParserDstu3Test.java | 18 ++++++++++++ .../uhn/fhir/parser/XmlParserDstu3Test.java | 17 +++++++++++ 9 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu2.java create mode 100644 hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu3.java diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java index 3c764c9d313..d0cc5b81dd8 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/BaseParser.java @@ -931,6 +931,10 @@ public abstract class BaseParser implements IParser { return retVal; } + protected String getExtensionUrl(final String extensionUrl) { + return StringUtils.isNotBlank(extensionUrl) && StringUtils.isNotBlank(myServerBaseUrl) ? extensionUrl.replace("%BASE_SERVER_URL%", myServerBaseUrl) : extensionUrl; + } + /** * Used for DSTU2 only */ diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java index 6f8868c4f02..8ea6378b91b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/JsonParser.java @@ -1405,7 +1405,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { getErrorHandler().missingRequiredElement(new ParseLocation(parentElementName), "url"); url = null; } else { - url = jsonElement.getAsString(); + url = getExtensionUrl(jsonElement.getAsString()); } theState.enteringNewElementExtension(null, url, theIsModifier); for (String next : nextExtObj.keySet()) { @@ -1758,8 +1758,8 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { public int compareTo(HeldExtension theArg0) { String url1 = myDef != null ? myDef.getExtensionUrl() : myUndeclaredExtension.getUrl(); String url2 = theArg0.myDef != null ? theArg0.myDef.getExtensionUrl() : theArg0.myUndeclaredExtension.getUrl(); - url1 = defaultString(url1); - url2 = defaultString(url2); + url1 = defaultString(getExtensionUrl(url1)); + url2 = defaultString(getExtensionUrl(url2)); return url1.compareTo(url2); } @@ -1771,7 +1771,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { writeCommentsPreAndPost(myValue, theEventWriter); - JsonParser.write(theEventWriter, "url", myDef.getExtensionUrl()); + JsonParser.write(theEventWriter, "url", getExtensionUrl(myDef.getExtensionUrl())); /* * This makes sure that even if the extension contains a reference to a contained @@ -1797,7 +1797,7 @@ public class JsonParser extends BaseParser implements IJsonLikeParser { private void writeUndeclaredExtension(RuntimeResourceDefinition theResDef, IBaseResource theResource, JsonLikeWriter theEventWriter, IBaseExtension ext) throws IOException { IBase value = ext.getValue(); - String extensionUrl = ext.getUrl(); + final String extensionUrl = getExtensionUrl(ext.getUrl()); theEventWriter.beginObject(); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java index a0fbff92853..f1835e905bd 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/parser/XmlParser.java @@ -655,11 +655,11 @@ public class XmlParser extends BaseParser implements IParser { String childName = childNameAndDef.getChildName(); BaseRuntimeElementDefinition childDef = childNameAndDef.getChildDef(); - String extensionUrl = nextChild.getExtensionUrl(); + String extensionUrl = getExtensionUrl(nextChild.getExtensionUrl()); if (nextValue instanceof IBaseExtension && myContext.getVersion().getVersion() == FhirVersionEnum.DSTU1) { // This is called for the Query resource in DSTU1 only - extensionUrl = ((IBaseExtension) nextValue).getUrl(); + extensionUrl = getExtensionUrl(((IBaseExtension) nextValue).getUrl()); encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extensionUrl, theContainedResource, nextChildElem); } else if (extensionUrl != null && childName.equals("extension") == false) { @@ -671,7 +671,7 @@ public class XmlParser extends BaseParser implements IParser { continue; } } - encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, extension.getUrl(), theContainedResource, nextChildElem); + encodeChildElementToStreamWriter(theResource, theEventWriter, nextValue, childName, childDef, getExtensionUrl(extension.getUrl()), theContainedResource, nextChildElem); } else if (nextChild instanceof RuntimeChildNarrativeDefinition && theContainedResource) { // suppress narratives from contained resources } else { @@ -902,7 +902,7 @@ public class XmlParser extends BaseParser implements IParser { theEventWriter.writeAttribute("id", elementId); } - String url = next.getUrl(); + String url = getExtensionUrl(next.getUrl()); theEventWriter.writeAttribute("url", url); if (next.getValue() != null) { diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu2.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu2.java new file mode 100644 index 00000000000..89a0e4a0ba0 --- /dev/null +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu2.java @@ -0,0 +1,29 @@ +package ca.uhn.fhir.parser; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.Description; +import ca.uhn.fhir.model.api.annotation.Extension; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import ca.uhn.fhir.model.dstu2.resource.Patient; +import ca.uhn.fhir.model.primitive.BooleanDt; + +@ResourceDef(name = "Patient", profile = "Patient") +public class CustomPatientDstu2 extends Patient { + + + private static final long serialVersionUID = 1L; + + @Child(name = "homeless", order = 1) + @Extension(url = "%BASE_SERVER_URL%/StructureDefinition/homeless", definedLocally = true, isModifier = false) + @Description(shortDefinition = "The patient being homeless, true if homeless") + private BooleanDt homeless; + + + public BooleanDt getHomeless() { + return homeless; + } + + public void setHomeless(final BooleanDt homeless) { + this.homeless = homeless; + } +} \ No newline at end of file diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java index 567b6c4fc89..456796e10a0 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/JsonParserDstu2Test.java @@ -9,6 +9,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -48,6 +49,7 @@ import ca.uhn.fhir.model.dstu2.valueset.IdentifierUseEnum; import ca.uhn.fhir.model.dstu2.valueset.MaritalStatusCodesEnum; import ca.uhn.fhir.model.dstu2.valueset.ObservationStatusEnum; import ca.uhn.fhir.model.dstu2.valueset.UnknownContentCodeEnum; +import ca.uhn.fhir.model.primitive.BooleanDt; import ca.uhn.fhir.model.primitive.CodeDt; import ca.uhn.fhir.model.primitive.DateDt; import ca.uhn.fhir.model.primitive.DateTimeDt; @@ -1871,5 +1873,21 @@ public class JsonParserDstu2Test { assertEquals("2011-01-01", condition.getDateRecordedElement().getValueAsString()); } - + + /** + * Test for the url generated based on the server config + */ + @Test + public void testGeneratedUrls() { + final IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); + jsonParser.setServerBaseUrl("http://myserver.com"); + + final CustomPatientDstu2 patient = new CustomPatientDstu2(); + patient.setHomeless(new BooleanDt(true)); + + final String parsedPatient = jsonParser.encodeResourceToString(patient); + + assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/Patient")); + assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/homeless")); + } } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java index 441acc63334..b2f2419959b 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserDstu2Test.java @@ -2726,6 +2726,23 @@ public class XmlParserDstu2Test { assertEquals("Patient", reincarnatedPatient.getId().getResourceType()); } + /** + * Test for the url generated based on the server config + */ + @Test + public void testGeneratedUrls() { + final IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); + xmlParser.setServerBaseUrl("http://myserver.com"); + + final CustomPatientDstu2 patient = new CustomPatientDstu2(); + patient.setHomeless(new BooleanDt(true)); + + final String parsedPatient = xmlParser.encodeResourceToString(patient); + + assertTrue(parsedPatient.contains("")); + assertTrue(parsedPatient.contains("")); + } + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu3.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu3.java new file mode 100644 index 00000000000..66812ad51e2 --- /dev/null +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/CustomPatientDstu3.java @@ -0,0 +1,29 @@ +package ca.uhn.fhir.parser; + +import ca.uhn.fhir.model.api.annotation.Child; +import ca.uhn.fhir.model.api.annotation.Description; +import ca.uhn.fhir.model.api.annotation.Extension; +import ca.uhn.fhir.model.api.annotation.ResourceDef; +import org.hl7.fhir.dstu3.model.BooleanType; +import org.hl7.fhir.dstu3.model.Patient; + +@ResourceDef(name = "Patient", profile = "Patient") +public class CustomPatientDstu3 extends Patient { + + + private static final long serialVersionUID = 1L; + + @Child(name = "homeless", order = 1) + @Extension(url = "%BASE_SERVER_URL%/StructureDefinition/homeless", definedLocally = true, isModifier = false) + @Description(shortDefinition = "The patient being homeless, true if homeless") + private BooleanType homeless; + + + public BooleanType getHomeless() { + return homeless; + } + + public void setHomeless(final BooleanType homeless) { + this.homeless = homeless; + } +} \ No newline at end of file diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java index bc5ac21e26f..e587cfcca0d 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/JsonParserDstu3Test.java @@ -40,6 +40,7 @@ import org.hl7.fhir.dstu3.model.Attachment; import org.hl7.fhir.dstu3.model.AuditEvent; import org.hl7.fhir.dstu3.model.Basic; import org.hl7.fhir.dstu3.model.Binary; +import org.hl7.fhir.dstu3.model.BooleanType; import org.hl7.fhir.dstu3.model.Bundle; import org.hl7.fhir.dstu3.model.Bundle.BundleEntryComponent; import org.hl7.fhir.dstu3.model.Bundle.BundleType; @@ -2176,6 +2177,23 @@ public class JsonParserDstu3Test { assertTrue(result.isSuccessful()); } + /** + * Test for the url generated based on the server config + */ + @Test + public void testGeneratedUrls() { + final IParser jsonParser = ourCtx.newJsonParser().setPrettyPrint(true); + jsonParser.setServerBaseUrl("http://myserver.com"); + + final CustomPatientDstu3 patient = new CustomPatientDstu3(); + patient.setHomeless(new BooleanType(true)); + + final String parsedPatient = jsonParser.encodeResourceToString(patient); + + assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/Patient")); + assertTrue(parsedPatient.contains("http://myserver.com/StructureDefinition/homeless")); + } + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest(); diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java index 1b00e00eca8..2668d5d5073 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/parser/XmlParserDstu3Test.java @@ -3139,6 +3139,23 @@ public class XmlParserDstu3Test { } + /** + * Test for the url generated based on the server config + */ + @Test + public void testGeneratedUrls() { + final IParser xmlParser = ourCtx.newXmlParser().setPrettyPrint(true); + xmlParser.setServerBaseUrl("http://myserver.com"); + + final CustomPatientDstu3 patient = new CustomPatientDstu3(); + patient.setHomeless(new BooleanType(true)); + + final String parsedPatient = xmlParser.encodeResourceToString(patient); + + assertTrue(parsedPatient.contains("")); + assertTrue(parsedPatient.contains("")); + } + @AfterClass public static void afterClassClearContext() { TestUtil.clearAllStaticFieldsForUnitTest();