Dont overwrite absolute IDs in the server

This commit is contained in:
James Agnew 2014-11-18 14:29:57 -05:00
parent 62ef60e096
commit 7ca4bc30d2
3 changed files with 77 additions and 35 deletions

View File

@ -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()) {
StringBuilder b = new StringBuilder();
b.append(theServerBase);
if (b.length() > 0 && b.charAt(b.length() - 1) != '/') {
b.append('/');
}
b.append(def.getName());
b.append('/');
String resId = theResource.getId().getIdPart();
b.append(resId);
entry.getLinkSelf().setValue(theResource.getId().getValue());
entry.getId().setValue(theResource.getId().toVersionless().getValue());
entry.getId().setValue(b.toString());
} else if (StringUtils.isNotBlank(theResource.getId().getValue())) {
if (isNotBlank(theResource.getId().getVersionIdPart())) {
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(Constants.PARAM_HISTORY);
b.append('/');
b.append(theResource.getId().getVersionIdPart());
} else {
IdDt versionId = (IdDt) ResourceMetadataKeyEnum.VERSION_ID.get(theResource);
if (versionId != null) {
String resId = theResource.getId().getIdPart();
b.append(resId);
entry.getId().setValue(b.toString());
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);

View File

@ -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<Patient> 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<Patient> getResourceType() {
return Patient.class;

View File

@ -120,6 +120,12 @@
DateDt left precision value as null in the constructor
<![CDATA[DateDt(Date)]]>.
</action>
<action type="fix">
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!
</action>
</release>
<release version="0.7" date="2014-Oct-23">
<action type="add" issue="30">