From 7ca4bc30d227bce8730510222dcd29db1d6adc89 Mon Sep 17 00:00:00 2001 From: James Agnew Date: Tue, 18 Nov 2014 14:29:57 -0500 Subject: [PATCH] Dont overwrite absolute IDs in the server --- .../java/ca/uhn/fhir/model/api/Bundle.java | 77 ++++++++++--------- .../fhir/rest/server/ServerFeaturesTest.java | 29 +++++++ src/changes/changes.xml | 6 ++ 3 files changed, 77 insertions(+), 35 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java index 28e9415c8be..8e70a3666ed 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/api/Bundle.java @@ -117,56 +117,63 @@ public class Bundle extends BaseBundle /* implements IElement */{ entry.getTitle().setValue(def.getName() + " " + StringUtils.defaultString(theResource.getId().getValue(), "(no ID)")); } - if (theResource.getId() != null && StringUtils.isNotBlank(theResource.getId().getValue())) { + if (theResource.getId() != null) { + if (theResource.getId().isAbsolute()) { + + entry.getLinkSelf().setValue(theResource.getId().getValue()); + entry.getId().setValue(theResource.getId().toVersionless().getValue()); + + } else if (StringUtils.isNotBlank(theResource.getId().getValue())) { - StringBuilder b = new StringBuilder(); - b.append(theServerBase); - if (b.length() > 0 && b.charAt(b.length() - 1) != '/') { + StringBuilder b = new StringBuilder(); + b.append(theServerBase); + if (b.length() > 0 && b.charAt(b.length() - 1) != '/') { + b.append('/'); + } + b.append(def.getName()); b.append('/'); - } - b.append(def.getName()); - b.append('/'); - String resId = theResource.getId().getIdPart(); - b.append(resId); + String resId = theResource.getId().getIdPart(); + b.append(resId); - entry.getId().setValue(b.toString()); + entry.getId().setValue(b.toString()); - if (isNotBlank(theResource.getId().getVersionIdPart())) { - b.append('/'); - b.append(Constants.PARAM_HISTORY); - b.append('/'); - b.append(theResource.getId().getVersionIdPart()); - } else { - IdDt versionId = (IdDt) ResourceMetadataKeyEnum.VERSION_ID.get(theResource); - if (versionId != null) { + if (isNotBlank(theResource.getId().getVersionIdPart())) { b.append('/'); b.append(Constants.PARAM_HISTORY); b.append('/'); - b.append(versionId.getValue()); + b.append(theResource.getId().getVersionIdPart()); + } else { + IdDt versionId = (IdDt) ResourceMetadataKeyEnum.VERSION_ID.get(theResource); + if (versionId != null) { + b.append('/'); + b.append(Constants.PARAM_HISTORY); + b.append('/'); + b.append(versionId.getValue()); + } } - } - String qualifiedId = b.toString(); - entry.getLinkSelf().setValue(qualifiedId); + String qualifiedId = b.toString(); + entry.getLinkSelf().setValue(qualifiedId); - // String resourceType = theContext.getResourceDefinition(theResource).getName(); + // String resourceType = theContext.getResourceDefinition(theResource).getName(); - String linkSearch = ResourceMetadataKeyEnum.LINK_SEARCH.get(theResource); - if (isNotBlank(linkSearch)) { - if (!UrlUtil.isAbsolute(linkSearch)) { - linkSearch = (theServerBase + "/" + linkSearch); + String linkSearch = ResourceMetadataKeyEnum.LINK_SEARCH.get(theResource); + if (isNotBlank(linkSearch)) { + if (!UrlUtil.isAbsolute(linkSearch)) { + linkSearch = (theServerBase + "/" + linkSearch); + } + entry.getLinkSearch().setValue(linkSearch); } - entry.getLinkSearch().setValue(linkSearch); - } - String linkAlternate = ResourceMetadataKeyEnum.LINK_ALTERNATE.get(theResource); - if (isNotBlank(linkAlternate)) { - if (!UrlUtil.isAbsolute(linkAlternate)) { - linkSearch = (theServerBase + "/" + linkAlternate); + String linkAlternate = ResourceMetadataKeyEnum.LINK_ALTERNATE.get(theResource); + if (isNotBlank(linkAlternate)) { + if (!UrlUtil.isAbsolute(linkAlternate)) { + linkSearch = (theServerBase + "/" + linkAlternate); + } + entry.getLinkAlternate().setValue(linkSearch); } - entry.getLinkAlternate().setValue(linkSearch); - } + } } InstantDt published = ResourceMetadataKeyEnum.PUBLISHED.get(theResource); diff --git a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesTest.java b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesTest.java index b8cd559dd57..0435977c5f9 100644 --- a/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesTest.java +++ b/hapi-fhir-structures-dstu/src/test/java/ca/uhn/fhir/rest/server/ServerFeaturesTest.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.composite.HumanNameDt; import ca.uhn.fhir.model.dstu.composite.IdentifierDt; @@ -223,6 +224,25 @@ public class ServerFeaturesTest { } + @Test + public void testSearchReturnWithAbsoluteIdSpecified() throws Exception { + + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/?_query=findPatientsWithAbsoluteIdSpecified"); + httpGet.addHeader("Accept", Constants.CT_FHIR_XML + "; pretty=true"); + CloseableHttpResponse status = ourClient.execute(httpGet); + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + + assertEquals(200, status.getStatusLine().getStatusCode()); + + Bundle bundle = servlet.getFhirContext().newXmlParser().parseBundle(responseContent); + assertEquals(1,bundle.size()); + + assertEquals("http://absolute.com/Patient/123", bundle.getEntries().get(0).getId().getValue()); + assertEquals("http://absolute.com/Patient/123/_history/22", bundle.getEntries().get(0).getLinkSelf().getValue()); + + } + @Test public void testSearchWithWildcardRetVal() throws Exception { @@ -345,6 +365,15 @@ public class ServerFeaturesTest { return Collections.singletonList(p); } + @Search(queryName = "findPatientsWithAbsoluteIdSpecified") + public List findPatientsWithAbsoluteIdSpecified() { + Patient p = new Patient(); + p.addIdentifier().setSystem("foo"); + p.setId("http://absolute.com/Patient/123/_history/22"); + return Collections.singletonList(p); + } + + @Override public Class getResourceType() { return Patient.class; diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 36d44ae8a52..52a96f7f8a8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -120,6 +120,12 @@ DateDt left precision value as null in the constructor . + + RESTful server now doesn't overwrite resource IDs if they are absolute. In other words, if + a server's Resource Provider returns a resource with ID "Patient/123" it will be translated to + "[base url]/Patient/123" but if the RP returns ID "http://foo/Patient/123" the ID will be + returned exactly as is. Thanks to Bill de Beaubien for the suggestion! +