Skip to content

Commit 692c257

Browse files
committed
WICKET-7024 restoring ResourceReferenceRegistry API
1 parent 546b6b3 commit 692c257

File tree

8 files changed

+59
-158
lines changed

8 files changed

+59
-158
lines changed

wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapperTest.java

+17
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
import org.apache.wicket.util.lang.Args;
4343
import org.apache.wicket.util.resource.IResourceStream;
4444
import org.apache.wicket.util.resource.StringResourceStream;
45+
import org.apache.wicket.util.tester.WicketTester;
46+
import org.junit.jupiter.api.AfterEach;
47+
import org.junit.jupiter.api.BeforeEach;
4548
import org.junit.jupiter.api.Test;
4649

4750
/**
@@ -62,6 +65,20 @@ protected IMapperContext getContext()
6265
}
6366
};
6467

68+
WicketTester tester;
69+
@BeforeEach
70+
public void setup()
71+
{
72+
// set the application ResourceSettings, used by the BasicResourceReferenceMapper
73+
tester = new WicketTester();
74+
}
75+
76+
@AfterEach
77+
public void destroy()
78+
{
79+
tester.destroy();
80+
}
81+
6582
/**
6683
*
6784
*/

wicket-core-tests/src/test/java/org/apache/wicket/core/request/mapper/TestMapperContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public ResourceReferenceRegistry getResourceReferenceRegistry()
126126
private final ResourceReferenceRegistry registry = new ResourceReferenceRegistry()
127127
{
128128
@Override
129-
protected ResourceReference createDefaultResourceReference(Key key, boolean updateCache)
129+
protected ResourceReference createDefaultResourceReference(Key key)
130130
{
131131
// Do not create package resource here because it requires "real" application
132132
return null;

wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/LessResourceReferenceTest.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@
2929
import org.apache.wicket.markup.html.WebPage;
3030
import org.apache.wicket.mock.MockApplication;
3131
import org.apache.wicket.protocol.http.WebApplication;
32-
import org.apache.wicket.request.resource.*;
32+
import org.apache.wicket.request.resource.CssPackageResource;
33+
import org.apache.wicket.request.resource.CssResourceReference;
34+
import org.apache.wicket.request.resource.PackageResource;
35+
import org.apache.wicket.request.resource.ResourceReference;
36+
import org.apache.wicket.request.resource.ResourceReferenceRegistry;
3337
import org.apache.wicket.util.file.Files;
3438
import org.apache.wicket.util.resource.IResourceStream;
3539
import org.apache.wicket.util.resource.StringResourceStream;
@@ -50,7 +54,7 @@ class LessResourceReferenceTest extends WicketTestCase
5054
* An {@link org.apache.wicket.request.resource.IResourceReferenceFactory} that creates
5155
* LessResourceReference for resources with extension '.less'
5256
*/
53-
static class LessResourceReferenceFactory implements IResourceReferenceFactory
57+
static class LessResourceReferenceFactory extends ResourceReferenceRegistry.DefaultResourceReferenceFactory
5458
{
5559
@Override
5660
public ResourceReference create(ResourceReference.Key key)
@@ -64,8 +68,7 @@ public ResourceReference create(ResourceReference.Key key)
6468
}
6569
else
6670
{
67-
result = new ResourceReferenceRegistry.DefaultResourceReferenceFactory().create(
68-
key);
71+
result = super.create(key);
6972
}
7073
}
7174
return result;

wicket-core-tests/src/test/java/org/apache/wicket/core/request/resource/PackageResourceReferenceTest.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -447,9 +447,6 @@ public void decodeStyleFromUrl()
447447
public void doNotFindResourceInTheCache()
448448
{
449449
IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
450-
when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css",
451-
"yellow", null, defaultLocale, null, false)).thenReturn(
452-
new UrlResourceStream(scope.getResource("a.css")));
453450
when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css",
454451
"yellow", null, null, null, false)).thenReturn(
455452
new UrlResourceStream(scope.getResource("a.css")));
@@ -462,20 +459,16 @@ public void doNotFindResourceInTheCache()
462459
tester.executeUrl(
463460
"wicket/resource/org.apache.wicket.core.request.resource.PackageResourceReferenceTest/a.css?-yellow");
464461

465-
// WICKET-7129: proposal to remove the duplicated resource resolution
466462
verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class,
467463
"org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false);
468-
verify(resourceStreamLocator, times(2)).locate(PackageResourceReferenceTest.class,
469-
"org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null,
470-
false);
471464
}
472465

473466
@Test
474467
public void doNotFindMountedResourceInTheCache()
475468
{
476469
IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
477470
when(resourceStreamLocator.locate(scope, "org/apache/wicket/core/request/resource/a.css",
478-
"yellow", null, defaultLocale, null, false)).thenReturn(
471+
"yellow", null, null, null, false)).thenReturn(
479472
new UrlResourceStream(scope.getResource("a.css")));
480473

481474
tester.getApplication().getResourceSettings()
@@ -487,12 +480,11 @@ public void doNotFindMountedResourceInTheCache()
487480
tester.executeUrl("a.css?-yellow");
488481

489482
verify(resourceStreamLocator, times(2)).locate(scope,
490-
"org/apache/wicket/core/request/resource/a.css", "yellow", null, defaultLocale, null,
491-
false);
483+
"org/apache/wicket/core/request/resource/a.css", "yellow", null, null, null, false);
492484
}
493485

494486
/**
495-
* @see https://issues.apache.org/jira/browse/WICKET-7024
487+
* https://issues.apache.org/jira/browse/WICKET-7024
496488
*/
497489
@Test
498490
public void notDecodeStyleFromUrl()

wicket-core/src/main/java/org/apache/wicket/core/request/mapper/BasicResourceReferenceMapper.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,7 @@ public IRequestHandler mapRequest(Request request)
132132

133133
if (scope != null && scope.getPackage() != null)
134134
{
135-
ResourceReference auxRes = getContext().getResourceReferenceRegistry()
136-
.getResourceReference(scope, name.toString(), attributes.getLocale(),
137-
attributes.getStyle(), attributes.getVariation(), true, true, false);
138-
if (auxRes != null)
139-
{
140-
IResource resource = auxRes.getResource();
141-
if (resource instanceof PackageResource packageResource)
142-
{
143-
attributes = PackageResource.sanitize(attributes, scope, name.toString());
144-
}
145-
}
135+
attributes = PackageResource.sanitize(attributes, scope, name.toString());
146136

147137
ResourceReference res = getContext().getResourceReferenceRegistry()
148138
.getResourceReference(scope, name.toString(), attributes.getLocale(),

wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResource.java

+16-74
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,6 @@ public PackageResourceBlockedException(String message)
150150
*/
151151
private boolean cachingEnabled = true;
152152

153-
/**
154-
* controls whether
155-
* {@link org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator}
156-
* should update the cache
157-
*/
158-
private boolean serverResourceStreamReferenceCacheUpdate = true;
159-
160153
/**
161154
* text encoding (may be null) - only makes sense for character-based resources
162155
*/
@@ -248,27 +241,6 @@ public void setCachingEnabled(final boolean enabled)
248241
this.cachingEnabled = enabled;
249242
}
250243

251-
/**
252-
* Returns true if the cache should be updated for this resource
253-
*
254-
* @return if the cache update is enabled
255-
*/
256-
public boolean isServerResourceStreamReferenceCacheUpdate()
257-
{
258-
return serverResourceStreamReferenceCacheUpdate;
259-
}
260-
261-
/**
262-
* Sets the cache update for this resource to be enabled
263-
*
264-
* @param enabled
265-
* if the cache update should be enabled
266-
*/
267-
public void setServerResourceStreamReferenceCacheUpdate(final boolean enabled)
268-
{
269-
this.serverResourceStreamReferenceCacheUpdate = enabled;
270-
}
271-
272244
/**
273245
* get text encoding (intended for character-based resources)
274246
*
@@ -560,8 +532,8 @@ private ResourceResponse sendResourceError(ResourceResponse resourceResponse, in
560532
@Override
561533
public IResourceStream getResourceStream()
562534
{
563-
return internalGetResourceStream(getCurrentStyle(), getCurrentLocale(), isServerResourceStreamReferenceCacheUpdate());
564-
}
535+
return internalGetResourceStream(getCurrentStyle(), getCurrentLocale());
536+
}
565537

566538
/**
567539
* @return whether {@link org.apache.wicket.resource.ITextResourceCompressor} can be used to
@@ -581,23 +553,13 @@ public void setCompress(boolean compress)
581553
this.compress = compress;
582554
}
583555

584-
private IResourceStream internalGetResourceStream(final String style, final Locale locale, boolean updateCache)
556+
private IResourceStream internalGetResourceStream(final String style, final Locale locale)
585557
{
586558
IResourceStreamLocator resourceStreamLocator = Application.get()
587559
.getResourceSettings()
588560
.getResourceStreamLocator();
589-
IResourceStream resourceStream = null;
590-
591-
if (resourceStreamLocator instanceof CachingResourceStreamLocator cache)
592-
{
593-
resourceStream = cache.locate(getScope(), absolutePath, style, variation, locale, null,
594-
false, updateCache);
595-
}
596-
else
597-
{
598-
resourceStream = resourceStreamLocator.locate(getScope(), absolutePath, style,
599-
variation, locale, null, false);
600-
}
561+
IResourceStream resourceStream = resourceStreamLocator.locate(getScope(), absolutePath,
562+
style, variation, locale, null, false);
601563

602564
String realPath = absolutePath;
603565
if (resourceStream instanceof IFixedLocationResourceStream)
@@ -737,49 +699,26 @@ public static boolean exists(final ResourceReference.Key key)
737699
* @param variation
738700
* The component's variation (of the style)
739701
* @return {@code true} if a resource could be loaded, {@code false} otherwise
740-
*
741-
* @deprecated use {@link PackageResource#exists(Class, String, Locale, String, String, boolean)}
742702
*/
743703
public static boolean exists(final Class<?> scope, final String path, final Locale locale,
744704
final String style, final String variation)
745705
{
746-
return exists(scope, path, locale, style, variation, true);
706+
return getResourceStream(scope, path, locale, style, variation, true) != null;
747707
}
748708

749-
/**
750-
* Checks whether a resource for a given set of criteria exists.
751-
*
752-
* @param scope
753-
* This argument will be used to get the class loader for loading the package
754-
* resource, and to determine what package it is in. Typically this is the class in
755-
* which you call this method
756-
* @param path
757-
* The path to the resource
758-
* @param locale
759-
* The locale of the resource
760-
* @param style
761-
* The style of the resource (see {@link org.apache.wicket.Session})
762-
* @param variation
763-
* The component's variation (of the style)
764-
* @param updateCache
765-
* if the server resource stream reference cache should be updated
766-
* @return {@code true} if a resource could be loaded, {@code false} otherwise
767-
*/
768-
public static boolean exists(final Class<?> scope, final String path, final Locale locale,
709+
private static IResourceStream getResourceStream(final Class<?> scope, final String path, final Locale locale,
769710
final String style, final String variation, final boolean updateCache)
770711
{
771712
String absolutePath = Packages.absolutePath(scope, path);
772713
IResourceStreamLocator resourceStreamLocator = Application.get().getResourceSettings()
773714
.getResourceStreamLocator();
774715
if (resourceStreamLocator instanceof CachingResourceStreamLocator cache)
775716
{
776-
return cache.locate(scope, absolutePath, style, variation, locale, null, false,
777-
updateCache) != null;
717+
return cache.locate(scope, absolutePath, style, variation, locale, null, false, updateCache);
778718
}
779719
else
780720
{
781-
return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null,
782-
false) != null;
721+
return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null, false);
783722
}
784723
}
785724

@@ -933,10 +872,13 @@ public PackageResource readBuffered(boolean readBuffered)
933872
public static ResourceReference.UrlAttributes sanitize(
934873
ResourceReference.UrlAttributes urlAttributes, Class<?> scope, String name)
935874
{
936-
PackageResource urlResource = new PackageResource(scope, name, urlAttributes.getLocale(),
937-
urlAttributes.getStyle(), urlAttributes.getVariation());
938-
urlResource.setServerResourceStreamReferenceCacheUpdate(false);
939-
IResourceStream filesystemMatch = urlResource.getResourceStream();
875+
IResourceStream filesystemMatch = getResourceStream(scope, name, urlAttributes.getLocale(),
876+
urlAttributes.getStyle(), urlAttributes.getVariation(), false);
877+
878+
if (filesystemMatch == null)
879+
{
880+
return urlAttributes;
881+
}
940882

941883
ResourceReference.Key urlKey = new ResourceReference.Key(scope.getName(), name,
942884
urlAttributes.getLocale(), urlAttributes.getStyle(), urlAttributes.getVariation());

wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceReference.java

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static org.apache.wicket.util.resource.ResourceUtils.MIN_POSTFIX_DEFAULT_AS_EXTENSION;
2020

21-
import java.io.IOException;
2221
import java.util.Locale;
2322
import java.util.concurrent.ConcurrentMap;
2423

0 commit comments

Comments
 (0)