Issue-1387: Read version from resource meta when deciding whether to return 304 on read

This commit is contained in:
Stig Rohde Døssing 2019-07-18 14:48:14 +02:00 committed by James Agnew
parent ef7182228f
commit bb20c2134b
2 changed files with 38 additions and 27 deletions

View File

@ -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");
}
}
}

View File

@ -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;
}