From ec48219d59e8ec35a7d9607dd421d6df3e23f92d Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Wed, 30 Oct 2024 17:58:57 -0300 Subject: [PATCH 01/16] WICKET-7122 update wicket-examples bean archive descriptor --- .../src/main/resources/META-INF/beans.xml | 45 +++++++++---------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/wicket-examples/src/main/resources/META-INF/beans.xml b/wicket-examples/src/main/resources/META-INF/beans.xml index cf01807ece1..73ad431fc10 100644 --- a/wicket-examples/src/main/resources/META-INF/beans.xml +++ b/wicket-examples/src/main/resources/META-INF/beans.xml @@ -1,23 +1,22 @@ - - - - \ No newline at end of file + + + \ No newline at end of file From 1ec7cf9eca9c6ed4082d30677ec455cd930215c6 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Sat, 26 Oct 2024 00:54:31 -0300 Subject: [PATCH 02/16] WICKET-7024 decode PackageResourceReference URL attributes only inside requests for it --- .../PackageResourceReferenceTest.java | 72 +++++++++++++++++++ .../wicket/core/request/resource/a_blue.css | 3 + .../wicket/core/request/resource/a_orange.css | 3 + .../resource/PackageResourceReference.java | 4 +- 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_blue.css create mode 100644 wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_orange.css 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 bf8cd883983..14a1b2a2057 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 @@ -16,6 +16,10 @@ */ package org.apache.wicket.core.request.resource; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.StringContains.containsString; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -26,7 +30,10 @@ import java.util.Locale; import org.apache.wicket.Application; +import org.apache.wicket.MarkupContainer; import org.apache.wicket.ThreadContext; +import org.apache.wicket.markup.IMarkupResourceStreamProvider; +import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; import org.apache.wicket.protocol.http.mock.MockHttpServletResponse; import org.apache.wicket.request.Request; @@ -43,6 +50,8 @@ import org.apache.wicket.request.resource.ResourceReference.UrlAttributes; import org.apache.wicket.response.ByteArrayResponse; import org.apache.wicket.util.io.IOUtils; +import org.apache.wicket.util.resource.IResourceStream; +import org.apache.wicket.util.resource.StringResourceStream; import org.apache.wicket.util.tester.WicketTestCase; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -396,4 +405,67 @@ void noRequestCycle() assertEquals(variations[1], resource.getResourceStream().getVariation()); } + @Test + public void getResouceWithNoStyle() + { + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css"); + + assertThat(tester.getLastResponseAsString(), not(containsString("color"))); + } + + @Test + public void getStyleFromSession() + { + tester.getSession().setStyle("blue"); + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css"); + + assertThat(tester.getLastResponseAsString(), containsString("blue")); + } + + @Test + public void decodeStyleFromUrl() + { + tester.getSession().setStyle("blue"); + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-orange"); + + assertThat(tester.getLastResponseAsString(), containsString("orange")); + } + + /** + * @see https://issues.apache.org/jira/browse/WICKET-7024 + */ + @Test + public void notDecodeStyleFromUrl() + { + tester.executeUrl( + "wicket/bookmarkable/org.apache.wicket.core.request.resource.PackageResourceReferenceTest$TestPage?0-1.0-resumeButton&_=1730041277224"); + + TestPage page = (TestPage)tester.getLastRenderedPage(); + + assertThat(page.resource.getStyle(), is(not("1.0"))); + } + + public static class TestPage extends WebPage implements IMarkupResourceStreamProvider + { + CssPackageResource resource; + + @Override + protected void onConfigure() + { + super.onConfigure(); + resource = (CssPackageResource)new PackageResourceReference(scope, "a.css") + .getResource(); + } + + @Override + public IResourceStream getMarkupResourceStream(MarkupContainer container, + Class containerClass) + { + return new StringResourceStream(""); + } + } + } diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_blue.css b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_blue.css new file mode 100644 index 00000000000..6c4aa1a395a --- /dev/null +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_blue.css @@ -0,0 +1,3 @@ +.a{ + color: blue; +} \ No newline at end of file diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_orange.css b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_orange.css new file mode 100644 index 00000000000..3282726ba6f --- /dev/null +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/a_orange.css @@ -0,0 +1,3 @@ +.a{ + color: orange; +} \ No newline at end of file diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java index f10a01cb83b..67634e10da6 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java @@ -26,6 +26,7 @@ import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator; import org.apache.wicket.request.Url; import org.apache.wicket.request.cycle.RequestCycle; +import org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler; import org.apache.wicket.resource.ResourceUtil; import org.apache.wicket.util.lang.Generics; import org.apache.wicket.util.lang.Packages; @@ -114,7 +115,8 @@ public PackageResource getResource() RequestCycle requestCycle = RequestCycle.get(); UrlAttributes urlAttributes = null; - if (requestCycle != null) + if (requestCycle != null + && requestCycle.getActiveRequestHandler() instanceof ResourceReferenceRequestHandler) { //resource attributes (locale, style, variation) might be encoded in the URL final Url url = requestCycle.getRequest().getUrl(); From 2ca9206d3aa0c30f1871d440223bb0f6b6f5725f Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Mon, 28 Oct 2024 13:15:49 -0300 Subject: [PATCH 03/16] WICKET-7024 test legibility improvement --- .../core/request/resource/PackageResourceReferenceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 14a1b2a2057..657423f8775 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 @@ -432,6 +432,7 @@ public void decodeStyleFromUrl() "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-orange"); assertThat(tester.getLastResponseAsString(), containsString("orange")); + assertThat(tester.getLastResponseAsString(), not(containsString("blue"))); } /** @@ -445,7 +446,7 @@ public void notDecodeStyleFromUrl() TestPage page = (TestPage)tester.getLastRenderedPage(); - assertThat(page.resource.getStyle(), is(not("1.0"))); + assertThat(page.resource.getStyle(), not(is("1.0"))); } public static class TestPage extends WebPage implements IMarkupResourceStreamProvider From 38cb92c1091d6006ff7d0ab6e71d91a9a3518394 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Tue, 29 Oct 2024 11:21:28 -0300 Subject: [PATCH 04/16] WICKET-7024 URL parameters sanitization --- .../CachingResourceStreamLocatorTest.java | 14 +++---- .../locator/IResourceStreamLocator.java | 24 +++++++++++ .../locator/ResourceStreamLocator.java | 15 +++++++ .../caching/CachingResourceStreamLocator.java | 17 +++++++- .../request/resource/PackageResource.java | 9 ++-- .../resource/PackageResourceReference.java | 41 ++++++++++++++++--- 6 files changed, 102 insertions(+), 18 deletions(-) diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java index fd15e29cb3c..6b1f55c71ff 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java @@ -168,7 +168,7 @@ void fileResource() FileResourceStream frs = new FileResourceStream(new File(".")); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true)).thenReturn(frs); + "extension", true, true)).thenReturn(frs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -178,7 +178,7 @@ void fileResource() // there is a file resource with that Key so expect just one call to the delegate verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension", true); + null, "extension", true, true); } /** @@ -192,7 +192,7 @@ void fileResourceDifferentExtensions() FileResourceStream frs = new FileResourceStream(new File(".")); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true)).thenReturn(frs); + "extension", true, true)).thenReturn(frs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -203,9 +203,9 @@ void fileResourceDifferentExtensions() // there is a file resource with that Key so expect just one call to the delegate verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension", true); + null, "extension", true, true); verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension2", true); + null, "extension2", true, true); } /** @@ -244,7 +244,7 @@ void lightweightResource() StringResourceStream srs = new StringResourceStream("anything"); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true)).thenReturn(srs); + "extension", true, true)).thenReturn(srs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -255,6 +255,6 @@ void lightweightResource() // lightweight resource streams should not be cached so expect just a call to the delegate // for each call to the caching locator verify(resourceStreamLocator, times(2)).locate(String.class, "path", "style", "variation", - null, "extension", true); + null, "extension", true, true); } } diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java index c0a34e38b99..0521845d24e 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java @@ -64,10 +64,34 @@ public interface IResourceStreamLocator * @param strict * whether the specified attributes must match exactly * @return The resource or null + * @deprecated */ IResourceStream locate(Class clazz, String path, String style, String variation, Locale locale, String extension, boolean strict); + /** + * Locate a resource by combining the given path, style, variation, locale and extension + * parameters. The exact search order depends on the implementation. + * + * @param clazz + * The class loader for delegating the loading of the resource + * @param path + * The path of the resource + * @param style + * Any resource style, such as a skin style (see {@link org.apache.wicket.Session}) + * @param variation + * The component's variation (of the style) + * @param locale + * The locale of the resource to load + * @param extension + * A comma separate list of extensions + * @param strict + * whether the specified attributes must match exactly + * @return The resource or null + */ + IResourceStream locate(Class clazz, String path, String style, String variation, + Locale locale, String extension, boolean strict, boolean updateCache); + /** * Markup resources and Properties files both need to iterate over different combinations of * locale, style, etc.. And though no single locate(..) method exists which is used by both, diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java index 55a15c449cb..f59e6ec1386 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java @@ -140,10 +140,25 @@ public IResourceStream locate(final Class clazz, final String path) * @see org.apache.wicket.core.util.resource.locator.IResourceStreamLocator#locate(java.lang.Class, * java.lang.String, java.lang.String, java.lang.String, java.util.Locale, * java.lang.String, boolean) + * @deprecated */ @Override public IResourceStream locate(final Class clazz, String path, final String style, final String variation, Locale locale, final String extension, final boolean strict) + { + return locate(clazz, path, style, variation, locale, extension, strict, true); + } + + /** + * + * @see org.apache.wicket.core.util.resource.locator.IResourceStreamLocator#locate(java.lang.Class, + * java.lang.String, java.lang.String, java.lang.String, java.util.Locale, + * java.lang.String, boolean) + */ + @Override + public IResourceStream locate(final Class clazz, String path, final String style, + final String variation, Locale locale, final String extension, final boolean strict, + boolean updateCache) { // If path contains a locale, then it'll replace the locale provided to this method PathLocale data = ResourceUtils.getLocaleFromFilename(path); diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java index f9b36243200..6eed118ab93 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java @@ -109,9 +109,19 @@ else if (stream instanceof UrlResourceStream) } } + /** + * @deprecated + */ @Override public IResourceStream locate(Class scope, String path, String style, String variation, Locale locale, String extension, boolean strict) + { + return locate(scope, path, style, variation, locale, extension, strict, true); + } + + @Override + public IResourceStream locate(Class scope, String path, String style, String variation, + Locale locale, String extension, boolean strict, boolean updateCache) { CacheKey key = new CacheKey(scope.getName(), path, extension, locale, style, variation, strict); IResourceStreamReference resourceStreamReference = cache.get(key); @@ -119,9 +129,12 @@ public IResourceStream locate(Class scope, String path, String style, String final IResourceStream result; if (resourceStreamReference == null) { - result = delegate.locate(scope, path, style, variation, locale, extension, strict); + result = delegate.locate(scope, path, style, variation, locale, extension, strict, updateCache); - updateCache(key, result); + if (updateCache) + { + updateCache(key, result); + } } else { 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 7a8a8079e7c..63d9eda1859 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 @@ -531,8 +531,8 @@ private ResourceResponse sendResourceError(ResourceResponse resourceResponse, in @Override public IResourceStream getResourceStream() { - return internalGetResourceStream(getCurrentStyle(), getCurrentLocale()); - } + return internalGetResourceStream(getCurrentStyle(), getCurrentLocale(), isCachingEnabled()); + } /** * @return whether {@link org.apache.wicket.resource.ITextResourceCompressor} can be used to @@ -552,13 +552,13 @@ public void setCompress(boolean compress) this.compress = compress; } - private IResourceStream internalGetResourceStream(final String style, final Locale locale) + private IResourceStream internalGetResourceStream(final String style, final Locale locale, boolean updateCache) { IResourceStreamLocator resourceStreamLocator = Application.get() .getResourceSettings() .getResourceStreamLocator(); IResourceStream resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, - style, variation, locale, null, false); + style, variation, locale, null, false, updateCache); String realPath = absolutePath; if (resourceStream instanceof IFixedLocationResourceStream) @@ -855,4 +855,5 @@ public PackageResource readBuffered(boolean readBuffered) this.readBuffered = readBuffered; return this; } + } diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java index 67634e10da6..83014337b53 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java @@ -18,6 +18,7 @@ import static org.apache.wicket.util.resource.ResourceUtils.MIN_POSTFIX_DEFAULT_AS_EXTENSION; +import java.io.IOException; import java.util.Locale; import java.util.concurrent.ConcurrentMap; @@ -123,11 +124,41 @@ public PackageResource getResource() urlAttributes = ResourceUtil.decodeResourceReferenceAttributes(url); } - final String currentVariation = getCurrentVariation(urlAttributes); - final String currentStyle = getCurrentStyle(urlAttributes); - final Locale currentLocale = getCurrentLocale(urlAttributes); - final Class scope = getScope(); - final String name = getName(); + String currentVariation = getCurrentVariation(urlAttributes); + String currentStyle = getCurrentStyle(urlAttributes); + Locale currentLocale = getCurrentLocale(urlAttributes); + Class scope = getScope(); + String name = getName(); + + if (urlAttributes != null) // sanitize + { + PackageResource urlResource = new PackageResource(scope, name, currentLocale, + currentStyle, currentVariation); + urlResource.setCachingEnabled(false); + IResourceStream filesystemMatch = urlResource.getResourceStream(); + + ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name, + currentLocale, currentStyle, currentVariation); + + ResourceReference.Key filesystemKey = new ResourceReference.Key(scope.getName(), name, + filesystemMatch.getLocale(), filesystemMatch.getStyle(), + filesystemMatch.getVariation()); + + if (!urlKey.equals(filesystemKey)) + { + currentLocale = filesystemKey.getLocale(); + currentStyle = filesystemKey.getStyle(); + currentVariation = filesystemKey.getVariation(); + } + try + { + filesystemMatch.close(); + } + catch (IOException e) + { + log.error("failed to close", e); + } + } if (CSS_EXTENSION.equals(extension)) { From 755a63ac249f3864c3a7692cce4712b6b358a7ee Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Fri, 1 Nov 2024 00:35:36 -0300 Subject: [PATCH 05/16] WICKET-7024 squash to the last commit (WIP) - test cases - code refactoring - WIP: the fix works only to mounted resources, next step is to make it work with the BasicResourceReferenceMapper --- .../PackageResourceReferenceTest.java | 52 +++++++++++- .../CachingResourceStreamLocatorTest.java | 14 ++-- .../mapper/BasicResourceReferenceMapper.java | 7 +- .../locator/IResourceStreamLocator.java | 24 ------ .../locator/ResourceStreamLocator.java | 15 ---- .../caching/CachingResourceStreamLocator.java | 3 +- .../request/resource/PackageResource.java | 80 ++++++++++++++++++- .../resource/PackageResourceReference.java | 35 +------- 8 files changed, 142 insertions(+), 88 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 657423f8775..922a192e220 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 @@ -24,6 +24,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; import java.io.IOException; import java.io.InputStream; @@ -32,6 +34,9 @@ import org.apache.wicket.Application; import org.apache.wicket.MarkupContainer; import org.apache.wicket.ThreadContext; +import org.apache.wicket.core.util.resource.UrlResourceStream; +import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator; +import org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator; import org.apache.wicket.markup.IMarkupResourceStreamProvider; import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.protocol.http.mock.MockHttpServletRequest; @@ -50,10 +55,12 @@ import org.apache.wicket.request.resource.ResourceReference.UrlAttributes; import org.apache.wicket.response.ByteArrayResponse; import org.apache.wicket.util.io.IOUtils; +import org.apache.wicket.util.resource.FileResourceStream; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.StringResourceStream; import org.apache.wicket.util.tester.WicketTestCase; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** @@ -62,6 +69,7 @@ class PackageResourceReferenceTest extends WicketTestCase { private static Class scope = PackageResourceReferenceTest.class; + private static final Locale defaultLocale = Locale.CHINA; private static final Locale[] locales = { null, new Locale("en"), new Locale("en", "US") }; private static final String[] styles = { null, "style" }; private static final String[] variations = { null, "var" }; @@ -406,7 +414,7 @@ void noRequestCycle() } @Test - public void getResouceWithNoStyle() + public void getResourceWithNoStyle() { tester.executeUrl( "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css"); @@ -435,6 +443,48 @@ public void decodeStyleFromUrl() assertThat(tester.getLastResponseAsString(), not(containsString("blue"))); } + @Test + @Disabled + public void doNotFindResourceInTheCache() + { + IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); + when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", + "yellow", null, defaultLocale, null, false)).thenReturn( + new UrlResourceStream(scope.getResource("a.css"))); + + tester.getApplication().getResourceSettings() + .setResourceStreamLocator(new CachingResourceStreamLocator(resourceStreamLocator)); + + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-yellow"); + tester.executeUrl( + "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-yellow"); + + verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, + "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, false); + } + + @Test + public void doNotFindMountedResourceInTheCache() + { + IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); + when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", + "yellow", null, defaultLocale, null, false)).thenReturn( + new UrlResourceStream(scope.getResource("a.css"))); + + tester.getApplication().getResourceSettings() + .setResourceStreamLocator(new CachingResourceStreamLocator(resourceStreamLocator)); + tester.getApplication() + .mountResource("/a.css", new PackageResourceReference(scope, "a.css")); + + tester.executeUrl("a.css?-yellow"); + tester.executeUrl("a.css?-yellow"); + + verify(resourceStreamLocator, times(2)).locate(scope, + "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, + false); + } + /** * @see https://issues.apache.org/jira/browse/WICKET-7024 */ diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java index 6b1f55c71ff..fd15e29cb3c 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/util/resource/locator/CachingResourceStreamLocatorTest.java @@ -168,7 +168,7 @@ void fileResource() FileResourceStream frs = new FileResourceStream(new File(".")); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true, true)).thenReturn(frs); + "extension", true)).thenReturn(frs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -178,7 +178,7 @@ void fileResource() // there is a file resource with that Key so expect just one call to the delegate verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension", true, true); + null, "extension", true); } /** @@ -192,7 +192,7 @@ void fileResourceDifferentExtensions() FileResourceStream frs = new FileResourceStream(new File(".")); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true, true)).thenReturn(frs); + "extension", true)).thenReturn(frs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -203,9 +203,9 @@ void fileResourceDifferentExtensions() // there is a file resource with that Key so expect just one call to the delegate verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension", true, true); + null, "extension", true); verify(resourceStreamLocator, times(1)).locate(String.class, "path", "style", "variation", - null, "extension2", true, true); + null, "extension2", true); } /** @@ -244,7 +244,7 @@ void lightweightResource() StringResourceStream srs = new StringResourceStream("anything"); when(resourceStreamLocator.locate(String.class, "path", "style", "variation", null, - "extension", true, true)).thenReturn(srs); + "extension", true)).thenReturn(srs); CachingResourceStreamLocator cachingLocator = new CachingResourceStreamLocator( resourceStreamLocator); @@ -255,6 +255,6 @@ void lightweightResource() // lightweight resource streams should not be cached so expect just a call to the delegate // for each call to the caching locator verify(resourceStreamLocator, times(2)).locate(String.class, "path", "style", "variation", - null, "extension", true, true); + null, "extension", true); } } 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 2c533bc59ba..320796d710b 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 @@ -27,10 +27,7 @@ import org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandler; import org.apache.wicket.request.mapper.parameter.IPageParametersEncoder; import org.apache.wicket.request.mapper.parameter.PageParameters; -import org.apache.wicket.request.resource.IResource; -import org.apache.wicket.request.resource.MetaInfStaticResourceReference; -import org.apache.wicket.request.resource.ResourceReference; -import org.apache.wicket.request.resource.ResourceReferenceRegistry; +import org.apache.wicket.request.resource.*; import org.apache.wicket.request.resource.caching.IResourceCachingStrategy; import org.apache.wicket.request.resource.caching.IStaticCacheableResource; import org.apache.wicket.request.resource.caching.ResourceUrl; @@ -133,6 +130,8 @@ public IRequestHandler mapRequest(Request request) Class scope = resolveClass(className); + // attributes = PackageResource.sanitize(attributes, scope, name.toString()); + if (scope != null && scope.getPackage() != null) { ResourceReference res = getContext().getResourceReferenceRegistry() diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java index 0521845d24e..c0a34e38b99 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/IResourceStreamLocator.java @@ -64,34 +64,10 @@ public interface IResourceStreamLocator * @param strict * whether the specified attributes must match exactly * @return The resource or null - * @deprecated */ IResourceStream locate(Class clazz, String path, String style, String variation, Locale locale, String extension, boolean strict); - /** - * Locate a resource by combining the given path, style, variation, locale and extension - * parameters. The exact search order depends on the implementation. - * - * @param clazz - * The class loader for delegating the loading of the resource - * @param path - * The path of the resource - * @param style - * Any resource style, such as a skin style (see {@link org.apache.wicket.Session}) - * @param variation - * The component's variation (of the style) - * @param locale - * The locale of the resource to load - * @param extension - * A comma separate list of extensions - * @param strict - * whether the specified attributes must match exactly - * @return The resource or null - */ - IResourceStream locate(Class clazz, String path, String style, String variation, - Locale locale, String extension, boolean strict, boolean updateCache); - /** * Markup resources and Properties files both need to iterate over different combinations of * locale, style, etc.. And though no single locate(..) method exists which is used by both, diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java index f59e6ec1386..55a15c449cb 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/ResourceStreamLocator.java @@ -140,25 +140,10 @@ public IResourceStream locate(final Class clazz, final String path) * @see org.apache.wicket.core.util.resource.locator.IResourceStreamLocator#locate(java.lang.Class, * java.lang.String, java.lang.String, java.lang.String, java.util.Locale, * java.lang.String, boolean) - * @deprecated */ @Override public IResourceStream locate(final Class clazz, String path, final String style, final String variation, Locale locale, final String extension, final boolean strict) - { - return locate(clazz, path, style, variation, locale, extension, strict, true); - } - - /** - * - * @see org.apache.wicket.core.util.resource.locator.IResourceStreamLocator#locate(java.lang.Class, - * java.lang.String, java.lang.String, java.lang.String, java.util.Locale, - * java.lang.String, boolean) - */ - @Override - public IResourceStream locate(final Class clazz, String path, final String style, - final String variation, Locale locale, final String extension, final boolean strict, - boolean updateCache) { // If path contains a locale, then it'll replace the locale provided to this method PathLocale data = ResourceUtils.getLocaleFromFilename(path); diff --git a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java index 6eed118ab93..7cc3a45684f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/core/util/resource/locator/caching/CachingResourceStreamLocator.java @@ -119,7 +119,6 @@ public IResourceStream locate(Class scope, String path, String style, String return locate(scope, path, style, variation, locale, extension, strict, true); } - @Override public IResourceStream locate(Class scope, String path, String style, String variation, Locale locale, String extension, boolean strict, boolean updateCache) { @@ -129,7 +128,7 @@ public IResourceStream locate(Class scope, String path, String style, String final IResourceStream result; if (resourceStreamReference == null) { - result = delegate.locate(scope, path, style, variation, locale, extension, strict, updateCache); + result = delegate.locate(scope, path, style, variation, locale, extension, strict); if (updateCache) { 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 63d9eda1859..06c20f588ce 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 @@ -32,6 +32,7 @@ import org.apache.wicket.WicketRuntimeException; import org.apache.wicket.core.util.lang.WicketObjects; import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator; +import org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator; import org.apache.wicket.javascript.IJavaScriptCompressor; import org.apache.wicket.markup.html.IPackageResourceGuard; import org.apache.wicket.mock.MockWebRequest; @@ -149,6 +150,13 @@ public PackageResourceBlockedException(String message) */ private boolean cachingEnabled = true; + /** + * controls whether + * {@link org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator} + * should update the cache + */ + private boolean serverResourceStreamReferenceCacheUpdate = true; + /** * text encoding (may be null) - only makes sense for character-based resources */ @@ -240,6 +248,27 @@ public void setCachingEnabled(final boolean enabled) this.cachingEnabled = enabled; } + /** + * Returns true if the cache should be updated for this resource + * + * @return if the cache update is enabled + */ + public boolean isServerResourceStreamReferenceCacheUpdate() + { + return serverResourceStreamReferenceCacheUpdate; + } + + /** + * Sets the cache update for this resource to be enabled + * + * @param enabled + * if the cache update should be enabled + */ + public void setServerResourceStreamReferenceCacheUpdate(final boolean enabled) + { + this.serverResourceStreamReferenceCacheUpdate = enabled; + } + /** * get text encoding (intended for character-based resources) * @@ -531,7 +560,7 @@ private ResourceResponse sendResourceError(ResourceResponse resourceResponse, in @Override public IResourceStream getResourceStream() { - return internalGetResourceStream(getCurrentStyle(), getCurrentLocale(), isCachingEnabled()); + return internalGetResourceStream(getCurrentStyle(), getCurrentLocale(), isServerResourceStreamReferenceCacheUpdate()); } /** @@ -557,8 +586,18 @@ private IResourceStream internalGetResourceStream(final String style, final Loca IResourceStreamLocator resourceStreamLocator = Application.get() .getResourceSettings() .getResourceStreamLocator(); - IResourceStream resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, - style, variation, locale, null, false, updateCache); + IResourceStream resourceStream = null; + + if (resourceStreamLocator instanceof CachingResourceStreamLocator cache) + { + resourceStream = cache.locate(getScope(), absolutePath, style, variation, locale, null, + false, updateCache); + } + else + { + resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, style, + variation, locale, null, false); + } String realPath = absolutePath; if (resourceStream instanceof IFixedLocationResourceStream) @@ -856,4 +895,39 @@ public PackageResource readBuffered(boolean readBuffered) return this; } + public static ResourceReference.UrlAttributes sanitize( + ResourceReference.UrlAttributes urlAttributes, Class scope, String name) + { + PackageResource urlResource = new PackageResource(scope, name, urlAttributes.getLocale(), + urlAttributes.getStyle(), urlAttributes.getVariation()); + urlResource.setServerResourceStreamReferenceCacheUpdate(false); + IResourceStream filesystemMatch = urlResource.getResourceStream(); + + ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name, + urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation()); + + ResourceReference.Key filesystemKey = new ResourceReference.Key(scope.getName(), name, + filesystemMatch.getLocale(), filesystemMatch.getStyle(), + filesystemMatch.getVariation()); + + try + { + filesystemMatch.close(); + } + catch (IOException e) + { + log.error("failed to close", e); + } + + if (!urlKey.equals(filesystemKey)) + { + return new ResourceReference.UrlAttributes(filesystemKey.getLocale(), + filesystemKey.getStyle(), filesystemKey.getVariation()); + } + else + { + return urlAttributes; + } + } + } diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java index 83014337b53..324531d9903 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java @@ -111,6 +111,8 @@ public PackageResourceReference(final String name) public PackageResource getResource() { final String extension = getExtension(); + final Class scope = getScope(); + final String name = getName(); final PackageResource resource; @@ -122,43 +124,12 @@ public PackageResource getResource() //resource attributes (locale, style, variation) might be encoded in the URL final Url url = requestCycle.getRequest().getUrl(); urlAttributes = ResourceUtil.decodeResourceReferenceAttributes(url); + urlAttributes = PackageResource.sanitize(urlAttributes, scope, name); } String currentVariation = getCurrentVariation(urlAttributes); String currentStyle = getCurrentStyle(urlAttributes); Locale currentLocale = getCurrentLocale(urlAttributes); - Class scope = getScope(); - String name = getName(); - - if (urlAttributes != null) // sanitize - { - PackageResource urlResource = new PackageResource(scope, name, currentLocale, - currentStyle, currentVariation); - urlResource.setCachingEnabled(false); - IResourceStream filesystemMatch = urlResource.getResourceStream(); - - ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name, - currentLocale, currentStyle, currentVariation); - - ResourceReference.Key filesystemKey = new ResourceReference.Key(scope.getName(), name, - filesystemMatch.getLocale(), filesystemMatch.getStyle(), - filesystemMatch.getVariation()); - - if (!urlKey.equals(filesystemKey)) - { - currentLocale = filesystemKey.getLocale(); - currentStyle = filesystemKey.getStyle(); - currentVariation = filesystemKey.getVariation(); - } - try - { - filesystemMatch.close(); - } - catch (IOException e) - { - log.error("failed to close", e); - } - } if (CSS_EXTENSION.equals(extension)) { From 117d4f59ddb713df7308cdbb01182da72d6edb9c Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Fri, 1 Nov 2024 18:00:29 -0300 Subject: [PATCH 06/16] WICKET-7024 URL sanitization for not mounted resources --- .../request/mapper/TestMapperContext.java | 2 +- .../resource/LessResourceReferenceTest.java | 11 ++- .../PackageResourceReferenceTest.java | 12 +++- .../mapper/BasicResourceReferenceMapper.java | 14 +++- .../request/resource/PackageResource.java | 43 ++++++++++-- .../resource/ResourceReferenceRegistry.java | 70 +++++++++++++++---- 6 files changed, 121 insertions(+), 31 deletions(-) diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java index 1f163a29e6d..57be3aa65cb 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java @@ -126,7 +126,7 @@ public ResourceReferenceRegistry getResourceReferenceRegistry() private final ResourceReferenceRegistry registry = new ResourceReferenceRegistry() { @Override - protected ResourceReference createDefaultResourceReference(Key key) + protected ResourceReference createDefaultResourceReference(Key key, boolean updateCache) { // Do not create package resource here because it requires "real" application return null; diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java index 56146d4a486..e136cd663ec 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java @@ -29,11 +29,7 @@ import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.mock.MockApplication; import org.apache.wicket.protocol.http.WebApplication; -import org.apache.wicket.request.resource.CssPackageResource; -import org.apache.wicket.request.resource.CssResourceReference; -import org.apache.wicket.request.resource.PackageResource; -import org.apache.wicket.request.resource.ResourceReference; -import org.apache.wicket.request.resource.ResourceReferenceRegistry; +import org.apache.wicket.request.resource.*; import org.apache.wicket.util.file.Files; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.StringResourceStream; @@ -54,7 +50,7 @@ class LessResourceReferenceTest extends WicketTestCase * An {@link org.apache.wicket.request.resource.IResourceReferenceFactory} that creates * LessResourceReference for resources with extension '.less' */ - static class LessResourceReferenceFactory extends ResourceReferenceRegistry.DefaultResourceReferenceFactory + static class LessResourceReferenceFactory implements IResourceReferenceFactory { @Override public ResourceReference create(ResourceReference.Key key) @@ -68,7 +64,8 @@ public ResourceReference create(ResourceReference.Key key) } else { - result = super.create(key); + result = new ResourceReferenceRegistry.DefaultResourceReferenceFactory().create( + key); } } return result; 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 922a192e220..b8abe874942 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 @@ -69,7 +69,7 @@ class PackageResourceReferenceTest extends WicketTestCase { private static Class scope = PackageResourceReferenceTest.class; - private static final Locale defaultLocale = Locale.CHINA; + private static final Locale defaultLocale = Locale.CHINA; private static final Locale[] locales = { null, new Locale("en"), new Locale("en", "US") }; private static final String[] styles = { null, "style" }; private static final String[] variations = { null, "var" }; @@ -444,13 +444,15 @@ public void decodeStyleFromUrl() } @Test - @Disabled public void doNotFindResourceInTheCache() { IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, false)).thenReturn( new UrlResourceStream(scope.getResource("a.css"))); + when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", + "yellow", null, null, null, false)).thenReturn( + new UrlResourceStream(scope.getResource("a.css"))); tester.getApplication().getResourceSettings() .setResourceStreamLocator(new CachingResourceStreamLocator(resourceStreamLocator)); @@ -460,8 +462,12 @@ public void doNotFindResourceInTheCache() tester.executeUrl( "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-yellow"); + // WICKET-7129: proposal to remove the duplicated resource resolution + verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, + "org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false); verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, - "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, false); + "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, + false); } @Test 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 320796d710b..500370145ac 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 @@ -130,10 +130,20 @@ public IRequestHandler mapRequest(Request request) Class scope = resolveClass(className); - // attributes = PackageResource.sanitize(attributes, scope, name.toString()); - if (scope != null && scope.getPackage() != null) { + ResourceReference auxRes = getContext().getResourceReferenceRegistry() + .getResourceReference(scope, name.toString(), attributes.getLocale(), + attributes.getStyle(), attributes.getVariation(), true, true, false); + if (auxRes != null) + { + IResource resource = auxRes.getResource(); + if (resource instanceof PackageResource packageResource) + { + attributes = PackageResource.sanitize(attributes, scope, name.toString()); + } + } + ResourceReference res = getContext().getResourceReferenceRegistry() .getResourceReference(scope, name.toString(), attributes.getLocale(), attributes.getStyle(), attributes.getVariation(), true, true); 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 06c20f588ce..8d5fe26a7a7 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 @@ -737,15 +737,50 @@ public static boolean exists(final ResourceReference.Key key) * @param variation * The component's variation (of the style) * @return {@code true} if a resource could be loaded, {@code false} otherwise + * + * @deprecated @see {@link PackageResource#exists(Class, String, Locale, String, String, boolean)} */ public static boolean exists(final Class scope, final String path, final Locale locale, final String style, final String variation) + { + return exists(scope, path, locale, style, variation, true); + } + + /** + * Checks whether a resource for a given set of criteria exists. + * + * @param scope + * This argument will be used to get the class loader for loading the package + * resource, and to determine what package it is in. Typically this is the class in + * which you call this method + * @param path + * The path to the resource + * @param locale + * The locale of the resource + * @param style + * The style of the resource (see {@link org.apache.wicket.Session}) + * @param variation + * The component's variation (of the style) + * @param updateCache + * if the server resource stream reference cache should be updated + * @return {@code true} if a resource could be loaded, {@code false} otherwise + */ + public static boolean exists(final Class scope, final String path, final Locale locale, + final String style, final String variation, final boolean updateCache) { String absolutePath = Packages.absolutePath(scope, path); - return Application.get() - .getResourceSettings() - .getResourceStreamLocator() - .locate(scope, absolutePath, style, variation, locale, null, false) != null; + IResourceStreamLocator resourceStreamLocator = Application.get().getResourceSettings() + .getResourceStreamLocator(); + if (resourceStreamLocator instanceof CachingResourceStreamLocator cache) + { + return cache.locate(scope, absolutePath, style, variation, locale, null, false, + updateCache) != null; + } + else + { + return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null, + false) != null; + } } @Override diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java index b021342f8d1..8167277064f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java @@ -74,9 +74,15 @@ public static class DefaultResourceReferenceFactory implements IResourceReferenc { @Override public ResourceReference create(Key key) + { + return create(key, true); + } + + public ResourceReference create(Key key, boolean updateCache) { ResourceReference result = null; - if (PackageResource.exists(key)) + if (PackageResource.exists(key.getScopeClass(), key.getName(), key.getLocale(), + key.getStyle(), key.getVariation(), updateCache)) { result = new PackageResourceReference(key); } @@ -180,6 +186,17 @@ public final ResourceReference unregisterResourceReference(final Key key) return removed; } + /** + * @deprecated @see {@link ResourceReferenceRegistry#getResourceReference(Class, String, Locale, String, String, boolean, boolean, boolean)} + */ + public final ResourceReference getResourceReference(final Class scope, final String name, + final Locale locale, final String style, final String variation, final boolean strict, + final boolean createIfNotFound) + { + return getResourceReference(scope, name, locale, style, variation, strict, createIfNotFound, + true); + } + /** * Get a resource reference matching the parameters from the registry or if not found and * requested, create an default resource reference and add it to the registry. @@ -187,10 +204,10 @@ public final ResourceReference unregisterResourceReference(final Key key) * Part of the search is scanning the class (scope) and it's superclass for static * ResourceReference fields. Found fields get registered automatically (but are different from * auto-generated ResourceReferences). - * + * * @see #createDefaultResourceReference(org.apache.wicket.request.resource.ResourceReference.Key) * @see ClassScanner - * + * * @param scope * The scope of resource reference (e.g. the Component's class) * @param name @@ -206,16 +223,25 @@ public final ResourceReference unregisterResourceReference(final Key key) * @param createIfNotFound * If true a default resource reference is created if no entry can be found in the * registry. The newly created resource reference will be added to the registry. + * @param updateCache + * If true, the server resource stream reference cache should be updated * @return Either the resource reference found in the registry or, if requested, a resource * reference automatically created based on the parameters provided. The automatically * created resource reference will automatically be added to the registry. */ - public final ResourceReference getResourceReference(final Class scope, final String name, - final Locale locale, final String style, final String variation, final boolean strict, - final boolean createIfNotFound) + public ResourceReference getResourceReference(Class scope, String name, Locale locale, + String style, String variation, boolean strict, boolean createIfNotFound, boolean updateCache) { return getResourceReference(new Key(scope.getName(), name, locale, style, variation), - strict, createIfNotFound); + strict, createIfNotFound, updateCache); + } + + /** + * @deprecated @see {@link ResourceReferenceRegistry#getResourceReference(Key, boolean, boolean, boolean)} + */ + public final ResourceReference getResourceReference(final Key key, final boolean strict, + final boolean createIfNotFound){ + return getResourceReference(key, strict, createIfNotFound, true); } /** @@ -236,12 +262,14 @@ public final ResourceReference getResourceReference(final Class scope, final * @param createIfNotFound * If true a default resource reference is created if no entry can be found in the * registry. The newly created resource reference will be added to the registry. + * @param strict + * If true, the server resource stream reference cache should be updated * @return Either the resource reference found in the registry or, if requested, a resource * reference automatically created based on the parameters provided. The automatically * created resource reference will automatically be added to the registry. */ public final ResourceReference getResourceReference(final Key key, final boolean strict, - final boolean createIfNotFound) + final boolean createIfNotFound, boolean updateCache) { ResourceReference resource = _getResourceReference(key.getScope(), key.getName(), key.getLocale(), key.getStyle(), key.getVariation(), strict); @@ -262,7 +290,7 @@ public final ResourceReference getResourceReference(final Key key, final boolean // Still nothing found => Shall a new reference be auto-created? if ((resource == null) && createIfNotFound) { - resource = addDefaultResourceReference(key); + resource = addDefaultResourceReference(key, updateCache); } } @@ -334,12 +362,12 @@ private ResourceReference _getResourceReference(final String scope, final String * the data making up the resource reference * @return The default resource created */ - private ResourceReference addDefaultResourceReference(final Key key) + private ResourceReference addDefaultResourceReference(final Key key, final boolean updateCache) { // Can be subclassed to create other than PackagedResourceReference - ResourceReference reference = createDefaultResourceReference(key); + ResourceReference reference = createDefaultResourceReference(key, updateCache); - if (reference != null) + if (reference != null && updateCache) { // number of RRs which can be auto-added is restricted (cache size). Remove entries, and // unregister excessive ones, if needed. @@ -383,6 +411,14 @@ private void enforceAutoAddedCacheSize(int maxSize) } } + /** + * @deprecated @see {@link ResourceReferenceRegistry#createDefaultResourceReference(Key, boolean)} + */ + protected ResourceReference createDefaultResourceReference(final Key key) + { + return createDefaultResourceReference(key, true); + } + /** * Creates a default resource reference in case no registry entry and it was requested to create * one. @@ -393,14 +429,20 @@ private void enforceAutoAddedCacheSize(int maxSize) * the data making up the resource reference * @return The {@link ResourceReference} created or {@code null} if not successful */ - protected ResourceReference createDefaultResourceReference(final Key key) + protected ResourceReference createDefaultResourceReference(final Key key, final boolean updateCache) { IResourceReferenceFactory factory = getResourceReferenceFactory(); if (factory == null) { factory = new DefaultResourceReferenceFactory(); } - return factory.create(key); + if (factory instanceof DefaultResourceReferenceFactory defaultFactory) + { + return defaultFactory.create(key, updateCache); + + } else { + return factory.create(key); + } } /** From 546b6b3b00bd676b2c29a6ab46cfb0b332369543 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Fri, 1 Nov 2024 18:53:26 -0300 Subject: [PATCH 07/16] WICKET-7024 javadoc fix --- .../request/resource/PackageResourceReferenceTest.java | 6 +++--- .../apache/wicket/request/resource/PackageResource.java | 2 +- .../request/resource/ResourceReferenceRegistry.java | 8 ++++---- 3 files changed, 8 insertions(+), 8 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 b8abe874942..11444c5a615 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 @@ -24,8 +24,10 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import java.io.IOException; import java.io.InputStream; @@ -55,12 +57,10 @@ import org.apache.wicket.request.resource.ResourceReference.UrlAttributes; import org.apache.wicket.response.ByteArrayResponse; import org.apache.wicket.util.io.IOUtils; -import org.apache.wicket.util.resource.FileResourceStream; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.StringResourceStream; import org.apache.wicket.util.tester.WicketTestCase; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** 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 8d5fe26a7a7..be4f11fae0c 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 @@ -738,7 +738,7 @@ public static boolean exists(final ResourceReference.Key key) * The component's variation (of the style) * @return {@code true} if a resource could be loaded, {@code false} otherwise * - * @deprecated @see {@link PackageResource#exists(Class, String, Locale, String, String, boolean)} + * @deprecated use {@link PackageResource#exists(Class, String, Locale, String, String, boolean)} */ public static boolean exists(final Class scope, final String path, final Locale locale, final String style, final String variation) diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java index 8167277064f..82e143e0c88 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java @@ -187,7 +187,7 @@ public final ResourceReference unregisterResourceReference(final Key key) } /** - * @deprecated @see {@link ResourceReferenceRegistry#getResourceReference(Class, String, Locale, String, String, boolean, boolean, boolean)} + * @deprecated use {@link ResourceReferenceRegistry#getResourceReference(Class, String, Locale, String, String, boolean, boolean, boolean)} */ public final ResourceReference getResourceReference(final Class scope, final String name, final Locale locale, final String style, final String variation, final boolean strict, @@ -237,7 +237,7 @@ public ResourceReference getResourceReference(Class scope, String name, Local } /** - * @deprecated @see {@link ResourceReferenceRegistry#getResourceReference(Key, boolean, boolean, boolean)} + * @deprecated use {@link ResourceReferenceRegistry#getResourceReference(Key, boolean, boolean, boolean)} */ public final ResourceReference getResourceReference(final Key key, final boolean strict, final boolean createIfNotFound){ @@ -262,7 +262,7 @@ public final ResourceReference getResourceReference(final Key key, final boolean * @param createIfNotFound * If true a default resource reference is created if no entry can be found in the * registry. The newly created resource reference will be added to the registry. - * @param strict + * @param updateCache * If true, the server resource stream reference cache should be updated * @return Either the resource reference found in the registry or, if requested, a resource * reference automatically created based on the parameters provided. The automatically @@ -412,7 +412,7 @@ private void enforceAutoAddedCacheSize(int maxSize) } /** - * @deprecated @see {@link ResourceReferenceRegistry#createDefaultResourceReference(Key, boolean)} + * @deprecated use {@link ResourceReferenceRegistry#createDefaultResourceReference(Key, boolean)} */ protected ResourceReference createDefaultResourceReference(final Key key) { From 692c257d7e6281ee7fb8ba2418943a78945ca467 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Sat, 2 Nov 2024 15:05:54 -0300 Subject: [PATCH 08/16] WICKET-7024 restoring ResourceReferenceRegistry API --- .../BasicResourceReferenceMapperTest.java | 17 ++++ .../request/mapper/TestMapperContext.java | 2 +- .../resource/LessResourceReferenceTest.java | 11 ++- .../PackageResourceReferenceTest.java | 14 +-- .../mapper/BasicResourceReferenceMapper.java | 12 +-- .../request/resource/PackageResource.java | 90 ++++--------------- .../resource/PackageResourceReference.java | 1 - .../resource/ResourceReferenceRegistry.java | 70 +++------------ 8 files changed, 59 insertions(+), 158 deletions(-) diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java index def27a40688..550424ecfcc 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java @@ -42,6 +42,9 @@ import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.StringResourceStream; +import org.apache.wicket.util.tester.WicketTester; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** @@ -62,6 +65,20 @@ protected IMapperContext getContext() } }; + WicketTester tester; + @BeforeEach + public void setup() + { + // set the application ResourceSettings, used by the BasicResourceReferenceMapper + tester = new WicketTester(); + } + + @AfterEach + public void destroy() + { + tester.destroy(); + } + /** * */ diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java index 57be3aa65cb..1f163a29e6d 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java @@ -126,7 +126,7 @@ public ResourceReferenceRegistry getResourceReferenceRegistry() private final ResourceReferenceRegistry registry = new ResourceReferenceRegistry() { @Override - protected ResourceReference createDefaultResourceReference(Key key, boolean updateCache) + protected ResourceReference createDefaultResourceReference(Key key) { // Do not create package resource here because it requires "real" application return null; diff --git a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java index e136cd663ec..56146d4a486 100644 --- a/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java +++ b/wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java @@ -29,7 +29,11 @@ import org.apache.wicket.markup.html.WebPage; import org.apache.wicket.mock.MockApplication; import org.apache.wicket.protocol.http.WebApplication; -import org.apache.wicket.request.resource.*; +import org.apache.wicket.request.resource.CssPackageResource; +import org.apache.wicket.request.resource.CssResourceReference; +import org.apache.wicket.request.resource.PackageResource; +import org.apache.wicket.request.resource.ResourceReference; +import org.apache.wicket.request.resource.ResourceReferenceRegistry; import org.apache.wicket.util.file.Files; import org.apache.wicket.util.resource.IResourceStream; import org.apache.wicket.util.resource.StringResourceStream; @@ -50,7 +54,7 @@ class LessResourceReferenceTest extends WicketTestCase * An {@link org.apache.wicket.request.resource.IResourceReferenceFactory} that creates * LessResourceReference for resources with extension '.less' */ - static class LessResourceReferenceFactory implements IResourceReferenceFactory + static class LessResourceReferenceFactory extends ResourceReferenceRegistry.DefaultResourceReferenceFactory { @Override public ResourceReference create(ResourceReference.Key key) @@ -64,8 +68,7 @@ public ResourceReference create(ResourceReference.Key key) } else { - result = new ResourceReferenceRegistry.DefaultResourceReferenceFactory().create( - key); + result = super.create(key); } } return result; 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 11444c5a615..a909ad38f45 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 @@ -447,9 +447,6 @@ public void decodeStyleFromUrl() public void doNotFindResourceInTheCache() { IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); - when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", - "yellow", null, defaultLocale, null, false)).thenReturn( - new UrlResourceStream(scope.getResource("a.css"))); when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false)).thenReturn( new UrlResourceStream(scope.getResource("a.css"))); @@ -462,12 +459,8 @@ public void doNotFindResourceInTheCache() tester.executeUrl( "wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-yellow"); - // WICKET-7129: proposal to remove the duplicated resource resolution verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, "org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false); - verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class, - "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, - false); } @Test @@ -475,7 +468,7 @@ public void doNotFindMountedResourceInTheCache() { IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class); when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css", - "yellow", null, defaultLocale, null, false)).thenReturn( + "yellow", null, null, null, false)).thenReturn( new UrlResourceStream(scope.getResource("a.css"))); tester.getApplication().getResourceSettings() @@ -487,12 +480,11 @@ public void doNotFindMountedResourceInTheCache() tester.executeUrl("a.css?-yellow"); verify(resourceStreamLocator, times(2)).locate(scope, - "org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null, - false); + "org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false); } /** - * @see https://issues.apache.org/jira/browse/WICKET-7024 + * https://issues.apache.org/jira/browse/WICKET-7024 */ @Test public void notDecodeStyleFromUrl() 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 500370145ac..23f34ab351e 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,17 +132,7 @@ public IRequestHandler mapRequest(Request request) if (scope != null && scope.getPackage() != null) { - ResourceReference auxRes = getContext().getResourceReferenceRegistry() - .getResourceReference(scope, name.toString(), attributes.getLocale(), - attributes.getStyle(), attributes.getVariation(), true, true, false); - if (auxRes != null) - { - IResource resource = auxRes.getResource(); - if (resource instanceof PackageResource packageResource) - { - attributes = PackageResource.sanitize(attributes, scope, name.toString()); - } - } + attributes = PackageResource.sanitize(attributes, scope, name.toString()); ResourceReference res = getContext().getResourceReferenceRegistry() .getResourceReference(scope, name.toString(), attributes.getLocale(), 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 be4f11fae0c..c1897f5d026 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 @@ -150,13 +150,6 @@ public PackageResourceBlockedException(String message) */ private boolean cachingEnabled = true; - /** - * controls whether - * {@link org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator} - * should update the cache - */ - private boolean serverResourceStreamReferenceCacheUpdate = true; - /** * text encoding (may be null) - only makes sense for character-based resources */ @@ -248,27 +241,6 @@ public void setCachingEnabled(final boolean enabled) this.cachingEnabled = enabled; } - /** - * Returns true if the cache should be updated for this resource - * - * @return if the cache update is enabled - */ - public boolean isServerResourceStreamReferenceCacheUpdate() - { - return serverResourceStreamReferenceCacheUpdate; - } - - /** - * Sets the cache update for this resource to be enabled - * - * @param enabled - * if the cache update should be enabled - */ - public void setServerResourceStreamReferenceCacheUpdate(final boolean enabled) - { - this.serverResourceStreamReferenceCacheUpdate = enabled; - } - /** * get text encoding (intended for character-based resources) * @@ -560,8 +532,8 @@ private ResourceResponse sendResourceError(ResourceResponse resourceResponse, in @Override public IResourceStream getResourceStream() { - return internalGetResourceStream(getCurrentStyle(), getCurrentLocale(), isServerResourceStreamReferenceCacheUpdate()); - } + return internalGetResourceStream(getCurrentStyle(), getCurrentLocale()); + } /** * @return whether {@link org.apache.wicket.resource.ITextResourceCompressor} can be used to @@ -581,23 +553,13 @@ public void setCompress(boolean compress) this.compress = compress; } - private IResourceStream internalGetResourceStream(final String style, final Locale locale, boolean updateCache) + private IResourceStream internalGetResourceStream(final String style, final Locale locale) { IResourceStreamLocator resourceStreamLocator = Application.get() .getResourceSettings() .getResourceStreamLocator(); - IResourceStream resourceStream = null; - - if (resourceStreamLocator instanceof CachingResourceStreamLocator cache) - { - resourceStream = cache.locate(getScope(), absolutePath, style, variation, locale, null, - false, updateCache); - } - else - { - resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, style, - variation, locale, null, false); - } + IResourceStream resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, + style, variation, locale, null, false); String realPath = absolutePath; if (resourceStream instanceof IFixedLocationResourceStream) @@ -737,35 +699,14 @@ public static boolean exists(final ResourceReference.Key key) * @param variation * The component's variation (of the style) * @return {@code true} if a resource could be loaded, {@code false} otherwise - * - * @deprecated use {@link PackageResource#exists(Class, String, Locale, String, String, boolean)} */ public static boolean exists(final Class scope, final String path, final Locale locale, final String style, final String variation) { - return exists(scope, path, locale, style, variation, true); + return getResourceStream(scope, path, locale, style, variation, true) != null; } - /** - * Checks whether a resource for a given set of criteria exists. - * - * @param scope - * This argument will be used to get the class loader for loading the package - * resource, and to determine what package it is in. Typically this is the class in - * which you call this method - * @param path - * The path to the resource - * @param locale - * The locale of the resource - * @param style - * The style of the resource (see {@link org.apache.wicket.Session}) - * @param variation - * The component's variation (of the style) - * @param updateCache - * if the server resource stream reference cache should be updated - * @return {@code true} if a resource could be loaded, {@code false} otherwise - */ - public static boolean exists(final Class scope, final String path, final Locale locale, + private static IResourceStream getResourceStream(final Class scope, final String path, final Locale locale, final String style, final String variation, final boolean updateCache) { String absolutePath = Packages.absolutePath(scope, path); @@ -773,13 +714,11 @@ public static boolean exists(final Class scope, final String path, final Loca .getResourceStreamLocator(); if (resourceStreamLocator instanceof CachingResourceStreamLocator cache) { - return cache.locate(scope, absolutePath, style, variation, locale, null, false, - updateCache) != null; + return cache.locate(scope, absolutePath, style, variation, locale, null, false, updateCache); } else { - return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null, - false) != null; + return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null, false); } } @@ -933,10 +872,13 @@ public PackageResource readBuffered(boolean readBuffered) public static ResourceReference.UrlAttributes sanitize( ResourceReference.UrlAttributes urlAttributes, Class scope, String name) { - PackageResource urlResource = new PackageResource(scope, name, urlAttributes.getLocale(), - urlAttributes.getStyle(), urlAttributes.getVariation()); - urlResource.setServerResourceStreamReferenceCacheUpdate(false); - IResourceStream filesystemMatch = urlResource.getResourceStream(); + IResourceStream filesystemMatch = getResourceStream(scope, name, urlAttributes.getLocale(), + urlAttributes.getStyle(), urlAttributes.getVariation(), false); + + if (filesystemMatch == null) + { + return urlAttributes; + } ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name, urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation()); diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java index 324531d9903..3a5958cfd9d 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java @@ -18,7 +18,6 @@ import static org.apache.wicket.util.resource.ResourceUtils.MIN_POSTFIX_DEFAULT_AS_EXTENSION; -import java.io.IOException; import java.util.Locale; import java.util.concurrent.ConcurrentMap; diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java index 82e143e0c88..b021342f8d1 100644 --- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java +++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReferenceRegistry.java @@ -74,15 +74,9 @@ public static class DefaultResourceReferenceFactory implements IResourceReferenc { @Override public ResourceReference create(Key key) - { - return create(key, true); - } - - public ResourceReference create(Key key, boolean updateCache) { ResourceReference result = null; - if (PackageResource.exists(key.getScopeClass(), key.getName(), key.getLocale(), - key.getStyle(), key.getVariation(), updateCache)) + if (PackageResource.exists(key)) { result = new PackageResourceReference(key); } @@ -186,17 +180,6 @@ public final ResourceReference unregisterResourceReference(final Key key) return removed; } - /** - * @deprecated use {@link ResourceReferenceRegistry#getResourceReference(Class, String, Locale, String, String, boolean, boolean, boolean)} - */ - public final ResourceReference getResourceReference(final Class scope, final String name, - final Locale locale, final String style, final String variation, final boolean strict, - final boolean createIfNotFound) - { - return getResourceReference(scope, name, locale, style, variation, strict, createIfNotFound, - true); - } - /** * Get a resource reference matching the parameters from the registry or if not found and * requested, create an default resource reference and add it to the registry. @@ -204,10 +187,10 @@ public final ResourceReference getResourceReference(final Class scope, final * Part of the search is scanning the class (scope) and it's superclass for static * ResourceReference fields. Found fields get registered automatically (but are different from * auto-generated ResourceReferences). - * + * * @see #createDefaultResourceReference(org.apache.wicket.request.resource.ResourceReference.Key) * @see ClassScanner - * + * * @param scope * The scope of resource reference (e.g. the Component's class) * @param name @@ -223,25 +206,16 @@ public final ResourceReference getResourceReference(final Class scope, final * @param createIfNotFound * If true a default resource reference is created if no entry can be found in the * registry. The newly created resource reference will be added to the registry. - * @param updateCache - * If true, the server resource stream reference cache should be updated * @return Either the resource reference found in the registry or, if requested, a resource * reference automatically created based on the parameters provided. The automatically * created resource reference will automatically be added to the registry. */ - public ResourceReference getResourceReference(Class scope, String name, Locale locale, - String style, String variation, boolean strict, boolean createIfNotFound, boolean updateCache) + public final ResourceReference getResourceReference(final Class scope, final String name, + final Locale locale, final String style, final String variation, final boolean strict, + final boolean createIfNotFound) { return getResourceReference(new Key(scope.getName(), name, locale, style, variation), - strict, createIfNotFound, updateCache); - } - - /** - * @deprecated use {@link ResourceReferenceRegistry#getResourceReference(Key, boolean, boolean, boolean)} - */ - public final ResourceReference getResourceReference(final Key key, final boolean strict, - final boolean createIfNotFound){ - return getResourceReference(key, strict, createIfNotFound, true); + strict, createIfNotFound); } /** @@ -262,14 +236,12 @@ public final ResourceReference getResourceReference(final Key key, final boolean * @param createIfNotFound * If true a default resource reference is created if no entry can be found in the * registry. The newly created resource reference will be added to the registry. - * @param updateCache - * If true, the server resource stream reference cache should be updated * @return Either the resource reference found in the registry or, if requested, a resource * reference automatically created based on the parameters provided. The automatically * created resource reference will automatically be added to the registry. */ public final ResourceReference getResourceReference(final Key key, final boolean strict, - final boolean createIfNotFound, boolean updateCache) + final boolean createIfNotFound) { ResourceReference resource = _getResourceReference(key.getScope(), key.getName(), key.getLocale(), key.getStyle(), key.getVariation(), strict); @@ -290,7 +262,7 @@ public final ResourceReference getResourceReference(final Key key, final boolean // Still nothing found => Shall a new reference be auto-created? if ((resource == null) && createIfNotFound) { - resource = addDefaultResourceReference(key, updateCache); + resource = addDefaultResourceReference(key); } } @@ -362,12 +334,12 @@ private ResourceReference _getResourceReference(final String scope, final String * the data making up the resource reference * @return The default resource created */ - private ResourceReference addDefaultResourceReference(final Key key, final boolean updateCache) + private ResourceReference addDefaultResourceReference(final Key key) { // Can be subclassed to create other than PackagedResourceReference - ResourceReference reference = createDefaultResourceReference(key, updateCache); + ResourceReference reference = createDefaultResourceReference(key); - if (reference != null && updateCache) + if (reference != null) { // number of RRs which can be auto-added is restricted (cache size). Remove entries, and // unregister excessive ones, if needed. @@ -411,14 +383,6 @@ private void enforceAutoAddedCacheSize(int maxSize) } } - /** - * @deprecated use {@link ResourceReferenceRegistry#createDefaultResourceReference(Key, boolean)} - */ - protected ResourceReference createDefaultResourceReference(final Key key) - { - return createDefaultResourceReference(key, true); - } - /** * Creates a default resource reference in case no registry entry and it was requested to create * one. @@ -429,20 +393,14 @@ protected ResourceReference createDefaultResourceReference(final Key key) * the data making up the resource reference * @return The {@link ResourceReference} created or {@code null} if not successful */ - protected ResourceReference createDefaultResourceReference(final Key key, final boolean updateCache) + protected ResourceReference createDefaultResourceReference(final Key key) { IResourceReferenceFactory factory = getResourceReferenceFactory(); if (factory == null) { factory = new DefaultResourceReferenceFactory(); } - if (factory instanceof DefaultResourceReferenceFactory defaultFactory) - { - return defaultFactory.create(key, updateCache); - - } else { - return factory.create(key); - } + return factory.create(key); } /** From 985fb6be8ab5ae41e54892492ff241deb5585446 Mon Sep 17 00:00:00 2001 From: Pedro Santos Date: Sat, 2 Nov 2024 22:01:20 -0300 Subject: [PATCH 09/16] WICKET-7024 javadoc + code cleanup --- .../request/resource/PackageResource.java | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) 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 c1897f5d026..58d5bc3534c 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 @@ -869,24 +869,19 @@ public PackageResource readBuffered(boolean readBuffered) return this; } + /** + * @return UrlAttributes with an existent locale/style/variation if a resource is bound to the + * scope+name + */ public static ResourceReference.UrlAttributes sanitize( ResourceReference.UrlAttributes urlAttributes, Class scope, String name) { IResourceStream filesystemMatch = getResourceStream(scope, name, urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation(), false); - if (filesystemMatch == null) { return urlAttributes; } - - ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name, - urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation()); - - ResourceReference.Key filesystemKey = new ResourceReference.Key(scope.getName(), name, - filesystemMatch.getLocale(), filesystemMatch.getStyle(), - filesystemMatch.getVariation()); - try { filesystemMatch.close(); @@ -895,16 +890,8 @@ public static ResourceReference.UrlAttributes sanitize( { log.error("failed to close", e); } - - if (!urlKey.equals(filesystemKey)) - { - return new ResourceReference.UrlAttributes(filesystemKey.getLocale(), - filesystemKey.getStyle(), filesystemKey.getVariation()); - } - else - { - return urlAttributes; - } + return new ResourceReference.UrlAttributes(filesystemMatch.getLocale(), + filesystemMatch.getStyle(), filesystemMatch.getVariation()); } } From 128f11d8673e08dbd7b6af6c2fbd7a4f1a2b0f46 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:13:57 +0200 Subject: [PATCH 10/16] Bump org.apache.maven.archetype:archetype-packaging from 3.3.0 to 3.3.1 (#1023) --- archetypes/quickstart/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archetypes/quickstart/pom.xml b/archetypes/quickstart/pom.xml index 07e81d8ba37..d59b6d64366 100644 --- a/archetypes/quickstart/pom.xml +++ b/archetypes/quickstart/pom.xml @@ -52,7 +52,7 @@ org.apache.maven.archetype archetype-packaging - 3.3.0 + 3.3.1 From 875ba47ae44aa9ac1df8b2afc8709b500435ea77 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:14:34 +0200 Subject: [PATCH 11/16] Bump maven-surefire-plugin.version from 3.5.1 to 3.5.2 (#1029) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c5edf88b79d..53ff5edc6db 100644 --- a/pom.xml +++ b/pom.xml @@ -201,7 +201,7 @@ 3.1.1 3.2.0 3.3.1 - 3.5.1 + 3.5.2 3.3.1 3.2.0 3.4.0 From fe914926810e4c0c2f39dd8c2e10055ada5b7f94 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:14:54 +0200 Subject: [PATCH 12/16] Bump org.apache.maven.plugins:maven-archetype-plugin from 3.3.0 to 3.3.1 (#1028) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 53ff5edc6db..93b808247aa 100644 --- a/pom.xml +++ b/pom.xml @@ -186,7 +186,7 @@ 3.6.0 0.23.0 - 3.3.0 + 3.3.1 5.1.9 3.4.0 3.13.0 From aa6b708713bc15130e1df2218d5fccd01198d457 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:15:04 +0200 Subject: [PATCH 13/16] Bump org.bouncycastle:bcprov-jdk18on from 1.78.1 to 1.79 (#1027) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 93b808247aa..610ce3763ae 100644 --- a/pom.xml +++ b/pom.xml @@ -143,7 +143,7 @@ 9.7.1 1.9.22.1 3.26.3 - 1.78.1 + 1.79 1.15.7 4.2.0 4.4 From 24b3d9886e31f5b0635e9b0162adab7f308544e9 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:15:24 +0200 Subject: [PATCH 14/16] Bump org.checkerframework:checker-qual from 3.48.1 to 3.48.2 (#1026) --- wicket-migration/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wicket-migration/pom.xml b/wicket-migration/pom.xml index d32f5a06ea1..0203692f99a 100644 --- a/wicket-migration/pom.xml +++ b/wicket-migration/pom.xml @@ -65,7 +65,7 @@ org.checkerframework checker-qual - 3.48.1 + 3.48.2 test From c7e1834148e98a3d8f5617155b9eef506a02611d Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 18:15:42 +0200 Subject: [PATCH 15/16] Bump org.apache.maven.plugins:maven-javadoc-plugin from 3.10.1 to 3.11.1 (#1024) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 610ce3763ae..f2672b8d66f 100644 --- a/pom.xml +++ b/pom.xml @@ -196,7 +196,7 @@ 3.2.7 3.1.3 3.4.2 - 3.10.1 + 3.11.1 3.8.0 3.1.1 3.2.0 From 47e1d23c2c46eecd07aa2f3ad861f2aeea3060b2 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sun, 3 Nov 2024 23:04:40 +0200 Subject: [PATCH 16/16] Bump net.bytebuddy:byte-buddy from 1.15.7 to 1.15.9 (#1025) --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f2672b8d66f..f594be5ec0c 100644 --- a/pom.xml +++ b/pom.xml @@ -144,7 +144,7 @@ 1.9.22.1 3.26.3 1.79 - 1.15.7 + 1.15.9 4.2.0 4.4 2.0.0-M2