Support conditional updates in ServletWebRequest

Prior to this commit, `ServletWebRequest.checkNotModified` would only
support conditional GET/HEAD requests with "If-Modified-Since" and/or
"If-None-Match" request headers. In those cases, the server would return
"HTTP 304 Not Modified" responses if the resource didn't change.

This commit adds support for conditional update requests, such as
POST/PUT/DELETE requests with "If-Unmodified-Since" request headers.
If the underlying resource has been modified since the specified date,
the server will return a "409 Precondition failed" response status
to prevent concurrent updates.

Even if the modification status of the resource is reversed here
(modified vs. not modified), we're keeping here the same intent for the
return value, which signals if the response requires more processing or
if the handler method can return immediately:

```
if (request.checkNotModified(lastModified)) {
  // shortcut exit - no further processing necessary
  return null;
}
```

Issue: SPR-13863
This commit is contained in:
Brian Clozel
2016-03-01 14:37:29 +01:00
parent b3abd3b809
commit 0d6f80052d
4 changed files with 115 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.web.context.request;
import static org.junit.Assert.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
@@ -32,8 +34,6 @@ import org.junit.runners.Parameterized.Parameters;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import static org.junit.Assert.*;
/**
* Parameterized tests for ServletWebRequest
* @author Juergen Hoeller
@@ -293,4 +293,28 @@ public class ServletWebRequestHttpMethodsTests {
assertEquals(dateFormat.format(epochTime), servletResponse.getHeader("Last-Modified"));
}
@Test
public void checkNotModifiedTimestampConditionalPut() throws Exception {
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.setMethod("PUT");
servletRequest.addHeader("If-UnModified-Since", currentEpoch);
assertFalse(request.checkNotModified(oneMinuteAgo));
assertEquals(200, servletResponse.getStatus());
assertEquals(null, servletResponse.getHeader("Last-Modified"));
}
@Test
public void checkNotModifiedTimestampConditionalPutConflict() throws Exception {
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.setMethod("PUT");
servletRequest.addHeader("If-UnModified-Since", oneMinuteAgo);
assertTrue(request.checkNotModified(currentEpoch));
assertEquals(412, servletResponse.getStatus());
assertEquals(null, servletResponse.getHeader("Last-Modified"));
}
}