From 6534d8d0fcc02b4cd51965b048170a8f8915db30 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Mon, 4 Nov 2024 12:38:13 -0300 Subject: [PATCH] WICKET-7024 skip cache update if resource is not found --- .../PackageResourceReferenceTest.java | 19 +++++++++++++++++++ .../mapper/BasicResourceReferenceMapper.java | 10 ++++++++-- .../request/resource/PackageResource.java | 4 ++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/PackageResourceReferenceTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/PackageResourceReferenceTest.java index a909ad38f4..8b7e05aac3 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/PackageResourceReferenceTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/PackageResourceReferenceTest.java @@ -443,6 +443,25 @@ public void decodeStyleFromUrl() assertThat(tester.getLastResponseAsString(), not(containsString("blue"))); } + @Test + public void doNotFindNullResourceInTheCache() + { + IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); + when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/z.css", + "orange", null, null, null, false)).thenReturn(null); + + tester.getApplication().getResourceSettings() + .setResourceStreamLocator(new CachingResourceStreamLocator(resourceStreamLocator)); + + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/z.css?-orange"); + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/z.css?-orange"); + + verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, + "org/apache/wicket/core/request/resource/z.css", "orange", null, null, null, false); + } + @Test public void doNotFindResourceInTheCache() { diff --git a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java index 23f34ab351..73edb9c610 100755 --- a/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java @@ -132,11 +132,17 @@ public IRequestHandler mapRequest(Request request) if (scope != null && scope.getPackage() != null) { - attributes = PackageResource.sanitize(attributes, scope, name.toString()); + ResourceReference.UrlAttributes sanitized = PackageResource.sanitize(attributes, scope, name.toString()); + boolean createIfNotFound = false; + if (sanitized != null) + { + attributes = sanitized; + createIfNotFound = true; + } ResourceReference res = getContext().getResourceReferenceRegistry() .getResourceReference(scope, name.toString(), attributes.getLocale(), - attributes.getStyle(), attributes.getVariation(), true, true); + attributes.getStyle(), attributes.getVariation(), true, createIfNotFound); if (res != null) { diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java index 58d5bc3534..49df1dd8ae 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java @@ -871,7 +871,7 @@ public PackageResource readBuffered(boolean readBuffered) /** * @return UrlAttributes with an existent locale/style/variation if a resource is bound to the - * scope+name + * scope+name, otherwise returns null */ public static ResourceReference.UrlAttributes sanitize( ResourceReference.UrlAttributes urlAttributes, Class scope, String name) @@ -880,7 +880,7 @@ public static ResourceReference.UrlAttributes sanitize( urlAttributes.getStyle(), urlAttributes.getVariation(), false); if (filesystemMatch == null) { - return urlAttributes; + return null; } try {