diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ReadMethodBinding.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ReadMethodBinding.java index ab7099a5a3c..3bc0a03380e 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ReadMethodBinding.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/method/ReadMethodBinding.java @@ -20,18 +20,6 @@ package ca.uhn.fhir.rest.server.method; * #L% */ -import static org.apache.commons.lang3.StringUtils.isNotBlank; - -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; - -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.Validate; -import org.hl7.fhir.instance.model.api.IBaseResource; -import org.hl7.fhir.instance.model.api.IIdType; - import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; @@ -53,8 +41,18 @@ import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.rest.server.exceptions.NotModifiedException; import ca.uhn.fhir.util.DateUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; +import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IIdType; import javax.annotation.Nonnull; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import static org.apache.commons.lang3.StringUtils.isNotBlank; public class ReadMethodBinding extends BaseResourceReturningMethodBinding { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ReadMethodBinding.class); @@ -172,11 +170,13 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding { String ifNoneMatch = theRequest.getHeader(Constants.HEADER_IF_NONE_MATCH_LC); if (StringUtils.isNotBlank(ifNoneMatch)) { ifNoneMatch = ParameterUtil.parseETagValue(ifNoneMatch); - if (responseResource.getIdElement() != null && responseResource.getIdElement().hasVersionIdPart()) { - if (responseResource.getIdElement().getVersionIdPart().equals(ifNoneMatch)) { - ourLog.debug("Returning HTTP 304 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch); - throw new NotModifiedException("Not Modified"); - } + String versionIdPart = responseResource.getIdElement().getVersionIdPart(); + if (StringUtils.isBlank(versionIdPart)) { + versionIdPart = responseResource.getMeta().getVersionId(); + } + if (ifNoneMatch.equals(versionIdPart)) { + ourLog.debug("Returning HTTP 304 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch); + throw new NotModifiedException("Not Modified"); } } } diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java index 2d5e96373f8..ef8e07061cc 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/ETagServerR4Test.java @@ -10,6 +10,7 @@ import ca.uhn.fhir.rest.api.Constants; import ca.uhn.fhir.rest.api.EncodingEnum; import ca.uhn.fhir.rest.api.MethodOutcome; import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException; +import ca.uhn.fhir.test.utilities.JettyUtil; import com.google.common.base.Charsets; import org.apache.commons.io.IOUtils; import org.apache.http.Header; @@ -33,9 +34,8 @@ import org.junit.*; import java.util.Date; import java.util.concurrent.TimeUnit; -import static org.junit.Assert.*; - -import ca.uhn.fhir.test.utilities.JettyUtil; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; public class ETagServerR4Test { @@ -58,12 +58,23 @@ public class ETagServerR4Test { @Test public void testAutomaticNotModified() throws Exception { - ourLastModifiedDate = new InstantDt("2012-11-25T02:34:45.2222Z").getValue(); + doTestAutomaticNotModified(); + } - HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); - httpGet.addHeader(Constants.HEADER_IF_NONE_MATCH, "\"222\""); - HttpResponse status = ourClient.execute(httpGet); - assertEquals(Constants.STATUS_HTTP_304_NOT_MODIFIED, status.getStatusLine().getStatusCode()); + @Test + public void testAutomaticNotModifiedFromVersionInMeta() throws Exception { + ourPutVersionInPatientId = false; + ourPutVersionInPatientMeta = true; + doTestAutomaticNotModified(); + } + + private void doTestAutomaticNotModified() throws Exception { + ourLastModifiedDate = new InstantDt("2012-11-25T02:34:45.2222Z").getValue(); + + HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/Patient/2"); + httpGet.addHeader(Constants.HEADER_IF_NONE_MATCH, "\"222\""); + HttpResponse status = ourClient.execute(httpGet); + assertEquals(Constants.STATUS_HTTP_304_NOT_MODIFIED, status.getStatusLine().getStatusCode()); } @Test @@ -99,7 +110,7 @@ public class ETagServerR4Test { Header cl = status.getFirstHeader(Constants.HEADER_ETAG_LC); assertNotNull(cl); - assertEquals("W/\"333\"", cl.getValue()); + assertEquals("W/\"222\"", cl.getValue()); } @Test @@ -216,7 +227,7 @@ public class ETagServerR4Test { patient.setId(theId.withVersion("222")); } if (ourPutVersionInPatientMeta) { - patient.getMeta().setVersionId("333"); + patient.getMeta().setVersionId("222"); } return patient; }