From a007c8dd20be0a1be313f0273d8534109f368f34 Mon Sep 17 00:00:00 2001 From: Paul Warren Date: Wed, 5 Oct 2022 21:03:19 -0700 Subject: [PATCH] Getting a resource's lastModified is safe - not all resources support lastModified --- .../rest/controllers/StoreRestController.java | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/spring-content-rest/src/main/java/internal/org/springframework/content/rest/controllers/StoreRestController.java b/spring-content-rest/src/main/java/internal/org/springframework/content/rest/controllers/StoreRestController.java index c5964dc70..39a3a46ed 100644 --- a/spring-content-rest/src/main/java/internal/org/springframework/content/rest/controllers/StoreRestController.java +++ b/spring-content-rest/src/main/java/internal/org/springframework/content/rest/controllers/StoreRestController.java @@ -1,6 +1,8 @@ package internal.org.springframework.content.rest.controllers; +import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.FileSystemNotFoundException; import java.util.Date; import javax.servlet.http.HttpServletRequest; @@ -84,12 +86,7 @@ public void getContent(HttpServletRequest request, StoreResource storeResource = (StoreResource)resource; - long lastModified = -1; - try { - lastModified = storeResource.lastModified(); - } catch (IOException e) {} - - if(new ServletWebRequest(request, response).checkNotModified(storeResource.getETag() != null ? storeResource.getETag().toString() : null, lastModified)) { + if(new ServletWebRequest(request, response).checkNotModified(storeResource.getETag() != null ? storeResource.getETag().toString() : null, resolveLastModified(storeResource))) { return; } @@ -184,7 +181,7 @@ public void deleteContent(@RequestHeader HttpHeaders headers, HttpServletRespons if (!resource.exists()) { throw new ResourceNotFoundException(); } else { - HeaderUtils.evaluateHeaderConditions(headers, storeResource.getETag() != null ? storeResource.getETag().toString() : null, new Date(storeResource.lastModified())); + HeaderUtils.evaluateHeaderConditions(headers, storeResource.getETag() != null ? storeResource.getETag().toString() : null, new Date(resolveLastModified(storeResource))); } ContentService contentService = contentServiceFactory.getContentService(storeResource); @@ -205,7 +202,7 @@ protected void handleMultipart(HttpServletRequest request, HttpServletResponse r boolean isNew = false; if (target.exists()) { - HeaderUtils.evaluateHeaderConditions(headers, targetETag != null ? targetETag.toString() : null, new Date(target.lastModified())); + HeaderUtils.evaluateHeaderConditions(headers, targetETag != null ? targetETag.toString() : null, new Date(resolveLastModified(target))); } else { isNew = true; } @@ -220,6 +217,15 @@ protected void handleMultipart(HttpServletRequest request, HttpServletResponse r } } + protected static long resolveLastModified(StoreResource storeResource) { + long lastModified = -1L; + try { + lastModified = storeResource.lastModified(); + } catch (FileSystemNotFoundException fnfe) { + } catch (IOException e) {} + return lastModified; + } + @Override public void afterPropertiesSet() throws Exception { @@ -236,4 +242,4 @@ public void afterPropertiesSet() throws Exception { contentServiceFactory = new ContentServiceFactory(config, repositories, repoInvokerFactory, stores, mappingContext, byteRangeRestRequestHandler); } - } \ No newline at end of file +} \ No newline at end of file