mirror of
https://github.com/hapifhir/hapi-fhir.git
synced 2025-02-08 14:05:02 +00:00
Issue-1387: Read version from resource meta when deciding whether to return 304 on read
This commit is contained in:
parent
ef7182228f
commit
bb20c2134b
@ -20,18 +20,6 @@ package ca.uhn.fhir.rest.server.method;
|
|||||||
* #L%
|
* #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.ConfigurationException;
|
||||||
import ca.uhn.fhir.context.FhirContext;
|
import ca.uhn.fhir.context.FhirContext;
|
||||||
import ca.uhn.fhir.model.api.IResource;
|
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.InvalidRequestException;
|
||||||
import ca.uhn.fhir.rest.server.exceptions.NotModifiedException;
|
import ca.uhn.fhir.rest.server.exceptions.NotModifiedException;
|
||||||
import ca.uhn.fhir.util.DateUtils;
|
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 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 {
|
public class ReadMethodBinding extends BaseResourceReturningMethodBinding {
|
||||||
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ReadMethodBinding.class);
|
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);
|
String ifNoneMatch = theRequest.getHeader(Constants.HEADER_IF_NONE_MATCH_LC);
|
||||||
if (StringUtils.isNotBlank(ifNoneMatch)) {
|
if (StringUtils.isNotBlank(ifNoneMatch)) {
|
||||||
ifNoneMatch = ParameterUtil.parseETagValue(ifNoneMatch);
|
ifNoneMatch = ParameterUtil.parseETagValue(ifNoneMatch);
|
||||||
if (responseResource.getIdElement() != null && responseResource.getIdElement().hasVersionIdPart()) {
|
String versionIdPart = responseResource.getIdElement().getVersionIdPart();
|
||||||
if (responseResource.getIdElement().getVersionIdPart().equals(ifNoneMatch)) {
|
if (StringUtils.isBlank(versionIdPart)) {
|
||||||
ourLog.debug("Returning HTTP 304 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch);
|
versionIdPart = responseResource.getMeta().getVersionId();
|
||||||
throw new NotModifiedException("Not Modified");
|
}
|
||||||
}
|
if (ifNoneMatch.equals(versionIdPart)) {
|
||||||
|
ourLog.debug("Returning HTTP 304 because request specified {}={}", Constants.HEADER_IF_NONE_MATCH, ifNoneMatch);
|
||||||
|
throw new NotModifiedException("Not Modified");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import ca.uhn.fhir.rest.api.Constants;
|
|||||||
import ca.uhn.fhir.rest.api.EncodingEnum;
|
import ca.uhn.fhir.rest.api.EncodingEnum;
|
||||||
import ca.uhn.fhir.rest.api.MethodOutcome;
|
import ca.uhn.fhir.rest.api.MethodOutcome;
|
||||||
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
|
import ca.uhn.fhir.rest.server.exceptions.PreconditionFailedException;
|
||||||
|
import ca.uhn.fhir.test.utilities.JettyUtil;
|
||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
@ -33,9 +34,8 @@ import org.junit.*;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import ca.uhn.fhir.test.utilities.JettyUtil;
|
|
||||||
|
|
||||||
public class ETagServerR4Test {
|
public class ETagServerR4Test {
|
||||||
|
|
||||||
@ -58,12 +58,23 @@ public class ETagServerR4Test {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAutomaticNotModified() throws Exception {
|
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");
|
@Test
|
||||||
httpGet.addHeader(Constants.HEADER_IF_NONE_MATCH, "\"222\"");
|
public void testAutomaticNotModifiedFromVersionInMeta() throws Exception {
|
||||||
HttpResponse status = ourClient.execute(httpGet);
|
ourPutVersionInPatientId = false;
|
||||||
assertEquals(Constants.STATUS_HTTP_304_NOT_MODIFIED, status.getStatusLine().getStatusCode());
|
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
|
@Test
|
||||||
@ -99,7 +110,7 @@ public class ETagServerR4Test {
|
|||||||
|
|
||||||
Header cl = status.getFirstHeader(Constants.HEADER_ETAG_LC);
|
Header cl = status.getFirstHeader(Constants.HEADER_ETAG_LC);
|
||||||
assertNotNull(cl);
|
assertNotNull(cl);
|
||||||
assertEquals("W/\"333\"", cl.getValue());
|
assertEquals("W/\"222\"", cl.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -216,7 +227,7 @@ public class ETagServerR4Test {
|
|||||||
patient.setId(theId.withVersion("222"));
|
patient.setId(theId.withVersion("222"));
|
||||||
}
|
}
|
||||||
if (ourPutVersionInPatientMeta) {
|
if (ourPutVersionInPatientMeta) {
|
||||||
patient.getMeta().setVersionId("333");
|
patient.getMeta().setVersionId("222");
|
||||||
}
|
}
|
||||||
return patient;
|
return patient;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user