Skip to content

Commit

Permalink
Merge branch 'master' into WICKET-7125-setBeanManager
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-g authored Nov 4, 2024
2 parents c4baa33 + 47e1d23 commit 3bf08d1
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 45 deletions.
2 changes: 1 addition & 1 deletion archetypes/quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<extension>
<groupId>org.apache.maven.archetype</groupId>
<artifactId>archetype-packaging</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
</extension>
</extensions>
<pluginManagement>
Expand Down
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@
<asm.version>9.7.1</asm.version>
<aspectj.version>1.9.22.1</aspectj.version>
<assertj-core.version>3.26.3</assertj-core.version>
<bouncycastle.version>1.78.1</bouncycastle.version>
<byte-buddy.version>1.15.7</byte-buddy.version>
<bouncycastle.version>1.79</bouncycastle.version>
<byte-buddy.version>1.15.9</byte-buddy.version>
<cdi-unit.version>5.0.0-EA7</cdi-unit.version>
<commons-collections4.version>4.4</commons-collections4.version>
<commons-fileupload.version>2.0.0-M2</commons-fileupload.version>
Expand Down Expand Up @@ -187,7 +187,7 @@
<!-- Maven plugins -->
<build-helper-maven-plugin.version>3.6.0</build-helper-maven-plugin.version>
<japicmp-maven-plugin.version>0.23.0</japicmp-maven-plugin.version>
<maven-archetype-plugin.version>3.3.0</maven-archetype-plugin.version>
<maven-archetype-plugin.version>3.3.1</maven-archetype-plugin.version>
<maven-bundle-plugin.version>5.1.9</maven-bundle-plugin.version>
<maven-clean-plugin.version>3.4.0</maven-clean-plugin.version>
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
Expand All @@ -197,12 +197,12 @@
<maven-gpg-plugin.version>3.2.7</maven-gpg-plugin.version>
<maven-install-plugin.version>3.1.3</maven-install-plugin.version>
<maven-jar-plugin.version>3.4.2</maven-jar-plugin.version>
<maven-javadoc-plugin.version>3.10.1</maven-javadoc-plugin.version>
<maven-javadoc-plugin.version>3.11.1</maven-javadoc-plugin.version>
<maven-project-info-reports-plugin.version>3.8.0</maven-project-info-reports-plugin.version>
<maven-release-plugin.version>3.1.1</maven-release-plugin.version>
<maven-remote-resources-plugin.version>3.2.0</maven-remote-resources-plugin.version>
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
<maven-surefire-plugin.version>3.5.1</maven-surefire-plugin.version>
<maven-surefire-plugin.version>3.5.2</maven-surefire-plugin.version>
<maven-source-plugin.version>3.3.1</maven-source-plugin.version>
<maven-toolchains-plugin.version>3.2.0</maven-toolchains-plugin.version>
<maven-war-plugin.version>3.4.0</maven-war-plugin.version>
Expand Down
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 @@ -16,17 +16,31 @@
*/
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;
import static org.junit.jupiter.api.Assertions.assertTrue;
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;
import java.util.Locale;

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;
import org.apache.wicket.protocol.http.mock.MockHttpServletResponse;
import org.apache.wicket.request.Request;
Expand All @@ -43,6 +57,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;
Expand All @@ -53,6 +69,7 @@
class PackageResourceReferenceTest extends WicketTestCase
{
private static Class<PackageResourceReferenceTest> 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" };
Expand Down Expand Up @@ -396,4 +413,108 @@ void noRequestCycle()
assertEquals(variations[1], resource.getResourceStream().getVariation());
}

@Test
public void getResourceWithNoStyle()
{
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"));
assertThat(tester.getLastResponseAsString(), not(containsString("blue")));
}

@Test
public void doNotFindResourceInTheCache()
{
IResourceStreamLocator resourceStreamLocator = mock(IResourceStreamLocator.class);
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));

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, null, 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, null, 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, null, null, false);
}

/**
* 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(), not(is("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("<html><head></head><body></body></html>");
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a{
color: blue;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a{
color: orange;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,6 +132,8 @@ public IRequestHandler mapRequest(Request request)

if (scope != null && scope.getPackage() != null)
{
attributes = PackageResource.sanitize(attributes, scope, name.toString());

ResourceReference res = getContext().getResourceReferenceRegistry()
.getResourceReference(scope, name.toString(), attributes.getLocale(),
attributes.getStyle(), attributes.getVariation(), true, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,18 @@ 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);
}

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);
Expand All @@ -121,7 +130,10 @@ public IResourceStream locate(Class<?> scope, String path, String style, String
{
result = delegate.locate(scope, path, style, variation, locale, extension, strict);

updateCache(key, result);
if (updateCache)
{
updateCache(key, result);
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -701,12 +702,24 @@ public static boolean exists(final ResourceReference.Key key)
*/
public static boolean exists(final Class<?> scope, final String path, final Locale locale,
final String style, final String variation)
{
return getResourceStream(scope, path, locale, style, variation, true) != null;
}

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);
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);
}
else
{
return resourceStreamLocator.locate(scope, absolutePath, style, variation, locale, null, false);
}
}

@Override
Expand Down Expand Up @@ -855,4 +868,30 @@ public PackageResource readBuffered(boolean readBuffered)
this.readBuffered = 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;
}
try
{
filesystemMatch.close();
}
catch (IOException e)
{
log.error("failed to close", e);
}
return new ResourceReference.UrlAttributes(filesystemMatch.getLocale(),
filesystemMatch.getStyle(), filesystemMatch.getVariation());
}

}
Loading

0 comments on commit 3bf08d1

Please sign in to comment.