Skip to content

Commit

Permalink
WICKET-7024 restoring ResourceReferenceRegistry API
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrosans committed Nov 2, 2024
1 parent 546b6b3 commit 692c257
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
*
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -64,8 +68,7 @@ public ResourceReference create(ResourceReference.Key key)
}
else
{
result = new ResourceReferenceRegistry.DefaultResourceReferenceFactory().create(
key);
result = super.create(key);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")));
Expand All @@ -462,20 +459,16 @@ 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
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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)
*
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -737,49 +699,26 @@ 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);
IResourceStreamLocator resourceStreamLocator = Application.get().getResourceSettings()
.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);
}
}

Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 692c257

Please sign in to comment.