diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java index 1bdde3b2649..3b63ede9a31 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/RestfulServerUtils.java @@ -9,9 +9,9 @@ package ca.uhn.fhir.rest.server; * 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. @@ -748,13 +748,19 @@ public class RestfulServerUtils { } if (theServer.getETagSupport() == ETagSupportEnum.ENABLED) { - if (theRequestDetails.getRequestType() == RequestTypeEnum.GET) { - if (fullId != null && fullId.hasVersionIdPart()) { - String versionIdPart = fullId.getVersionIdPart(); - response.addHeader(Constants.HEADER_ETAG, createEtag(versionIdPart)); - } else if (theResource != null && theResource.getMeta() != null && isNotBlank(theResource.getMeta().getVersionId())) { - String versionId = theResource.getMeta().getVersionId(); - response.addHeader(Constants.HEADER_ETAG, createEtag(versionId)); + if (theRequestDetails.getRestOperationType() != null) { + switch (theRequestDetails.getRestOperationType()) { + case CREATE: + case UPDATE: + case READ: + case VREAD: + if (fullId != null && fullId.hasVersionIdPart()) { + String versionIdPart = fullId.getVersionIdPart(); + response.addHeader(Constants.HEADER_ETAG, createEtag(versionIdPart)); + } else if (theResource != null && theResource.getMeta() != null && isNotBlank(theResource.getMeta().getVersionId())) { + String versionId = theResource.getMeta().getVersionId(); + response.addHeader(Constants.HEADER_ETAG, createEtag(versionId)); + } } } } diff --git a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java index 92316a54245..79c088e318e 100644 --- a/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java +++ b/hapi-fhir-structures-dstu2/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu2Test.java @@ -118,38 +118,6 @@ public class UpdateDstu2Test { } - @Test - public void testUpdateReturnsETagAndUpdate() throws Exception { - - Patient patient = new Patient(); - patient.setId("123"); - patient.addIdentifier().setValue("002"); - ourSetLastUpdated = new InstantDt("2002-04-22T11:22:33.022Z"); - - HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Patient/123"); - httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); - - HttpResponse status = ourClient.execute(httpPost); - - String responseContent = IOUtils.toString(status.getEntity().getContent()); - IOUtils.closeQuietly(status.getEntity().getContent()); - - ourLog.info("Response was:\n{}", responseContent); - ourLog.info("Response was:\n{}", status); - - assertThat(responseContent, is(not(emptyString()))); - - Patient actualPatient = (Patient) ourCtx.newXmlParser().parseResource(responseContent); - assertEquals(patient.getId().getIdPart(), actualPatient.getId().getIdPart()); - assertEquals(patient.getIdentifier().get(0).getValue(), actualPatient.getIdentifier().get(0).getValue()); - - assertEquals(200, status.getStatusLine().getStatusCode()); - assertEquals(null, status.getFirstHeader("location")); - assertEquals("http://localhost:" + ourPort + "/Patient/001/_history/002", status.getFirstHeader("content-location").getValue()); - assertEquals("W/\"002\"", status.getFirstHeader(Constants.HEADER_ETAG_LC).getValue()); - assertEquals("Mon, 22 Apr 2002 11:22:33 GMT", status.getFirstHeader(Constants.HEADER_LAST_MODIFIED_LOWERCASE).getValue()); - - } @Test public void testUpdateWithoutConditionalUrl() throws Exception { diff --git a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java index 8d494886d91..d0558c7be38 100644 --- a/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java +++ b/hapi-fhir-structures-dstu3/src/test/java/ca/uhn/fhir/rest/server/UpdateDstu3Test.java @@ -1,6 +1,9 @@ package ca.uhn.fhir.rest.server; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNot.not; +import static org.hamcrest.text.IsEmptyString.emptyString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; @@ -8,6 +11,7 @@ import static org.junit.Assert.assertThat; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; +import ca.uhn.fhir.model.primitive.InstantDt; import org.apache.commons.io.IOUtils; import org.apache.http.HttpResponse; import org.apache.http.client.methods.CloseableHttpResponse; @@ -38,11 +42,46 @@ public class UpdateDstu3Test { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(UpdateDstu3Test.class); private static int ourPort; private static Server ourServer; + private static InstantType ourSetLastUpdated; @Before public void before() { ourConditionalUrl = null; ourId = null; + ourSetLastUpdated = null; + } + + @Test + public void testUpdateReturnsETagAndUpdate() throws Exception { + + Patient patient = new Patient(); + patient.setId("123"); + patient.addIdentifier().setValue("002"); + ourSetLastUpdated = new InstantType("2002-04-22T11:22:33.022Z"); + + HttpPut httpPost = new HttpPut("http://localhost:" + ourPort + "/Patient/123"); + httpPost.setEntity(new StringEntity(ourCtx.newXmlParser().encodeResourceToString(patient), ContentType.create(Constants.CT_FHIR_XML, "UTF-8"))); + + HttpResponse status = ourClient.execute(httpPost); + + String responseContent = IOUtils.toString(status.getEntity().getContent()); + IOUtils.closeQuietly(status.getEntity().getContent()); + + ourLog.info("Response was:\n{}", responseContent); + ourLog.info("Response was:\n{}", status); + + assertThat(responseContent, is(not(emptyString()))); + + Patient actualPatient = (Patient) ourCtx.newXmlParser().parseResource(responseContent); + assertEquals(patient.getIdElement().getIdPart(), actualPatient.getIdElement().getIdPart()); + assertEquals(patient.getIdentifier().get(0).getValue(), actualPatient.getIdentifier().get(0).getValue()); + + assertEquals(200, status.getStatusLine().getStatusCode()); + assertEquals(null, status.getFirstHeader("location")); + assertEquals("http://localhost:" + ourPort + "/Patient/123/_history/002", status.getFirstHeader("content-location").getValue()); + assertEquals("W/\"002\"", status.getFirstHeader(Constants.HEADER_ETAG_LC).getValue()); + assertEquals("Mon, 22 Apr 2002 11:22:33 GMT", status.getFirstHeader(Constants.HEADER_LAST_MODIFIED_LOWERCASE).getValue()); + } @Test @@ -180,7 +219,11 @@ public class UpdateDstu3Test { return new MethodOutcome(id, oo, true); } - return new MethodOutcome(id, oo); + thePatient.getMeta().setLastUpdatedElement(ourSetLastUpdated); + + MethodOutcome retVal = new MethodOutcome(id, oo); + retVal.setResource(thePatient); + return retVal; } }