You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pe...@apache.org on 2025/01/27 15:36:12 UTC

(wicket) 02/03: WICKET-7137 move sanitizer code to PackageResourceUrlSanitizer

This is an automated email from the ASF dual-hosted git repository.

pedro pushed a commit to branch wicket-8.x
in repository https://gitbox.apache.org/repos/asf/wicket.git

commit babf7333feeae87f2979d0cdafea1fde43a22834
Author: Pedro Santos <pe...@apache.org>
AuthorDate: Sat Dec 21 16:23:27 2024 -0300

    WICKET-7137 move sanitizer code to PackageResourceUrlSanitizer
---
 .../mapper/BasicResourceReferenceMapper.java       |  2 +-
 .../request/resource/IResourceUrlSanitizer.java    | 42 +++++++++++++++
 .../wicket/request/resource/PackageResource.java   | 27 +---------
 .../request/resource/PackageResourceReference.java |  2 +-
 .../resource/PackageResourceUrlSanitizer.java      | 62 ++++++++++++++++++++++
 .../wicket/request/resource/ResourceReference.java | 12 +++++
 .../apache/wicket/settings/ResourceSettings.java   | 23 ++++++++
 7 files changed, 142 insertions(+), 28 deletions(-)

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 73edb9c610..d353cd2274 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,7 +132,7 @@ public class BasicResourceReferenceMapper extends AbstractResourceReferenceMappe
 
 			if (scope != null && scope.getPackage() != null)
 			{
-				ResourceReference.UrlAttributes sanitized = PackageResource.sanitize(attributes, scope, name.toString());
+				ResourceReference.UrlAttributes sanitized = attributes.sanitize(scope, name.toString());
 				boolean createIfNotFound = false;
 				if (sanitized != null)
 				{
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/IResourceUrlSanitizer.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/IResourceUrlSanitizer.java
new file mode 100644
index 0000000000..ecb1ee9c35
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/IResourceUrlSanitizer.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.request.resource;
+
+import org.apache.wicket.request.resource.ResourceReference.UrlAttributes;
+
+/**
+ * Sanitizes the resource URL parameters. Implementations should be concerned to return a set of
+ * style/locale/variation that is safe to end up as keys in the server resource cache, without
+ * causing unnecessary values to be kept in the primary memory.
+ * 
+ * @author Pedro Santos
+ */
+public interface IResourceUrlSanitizer
+{
+
+	/**
+	 * Sanitizes the {@link UrlAttributes} and returns a new instance with style/locale/variation
+	 * values that are safe to be kept in-memory.
+	 * 
+	 * @param urlAttributes
+	 * @param scope
+	 * @param name
+	 * @return null if there are no resource matching the scope/name
+	 */
+	public UrlAttributes sanitize(UrlAttributes urlAttributes, Class<?> scope, String name);
+
+}
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 b766dd0097..e1ace3a127 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
@@ -708,7 +708,7 @@ public class PackageResource extends AbstractResource implements IStaticCacheabl
 		return getResourceStream(scope, path, locale, style, variation, true) != null;
 	}
 
-	private static IResourceStream getResourceStream(final Class<?> scope, final String path, final Locale locale,
+	public 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);
@@ -873,29 +873,4 @@ public class PackageResource extends AbstractResource implements IStaticCacheabl
 		return this;
 	}
 
-	/**
-	 * @return UrlAttributes with an existent locale/style/variation if a resource is bound to the
-	 * 	scope+name, otherwise returns null
-	 */
-	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 null;
-		}
-		try
-		{
-			filesystemMatch.close();
-		}
-		catch (IOException e)
-		{
-			log.error("failed to close", e);
-		}
-		return new ResourceReference.UrlAttributes(filesystemMatch.getLocale(),
-			filesystemMatch.getStyle(), filesystemMatch.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 3a5958cfd9..5129ac27a9 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
@@ -123,7 +123,7 @@ public class PackageResourceReference extends ResourceReference
 			//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);
+			urlAttributes = urlAttributes.sanitize(scope, name);
 		}
 
 		String currentVariation = getCurrentVariation(urlAttributes);
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceUrlSanitizer.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceUrlSanitizer.java
new file mode 100644
index 0000000000..ad264d6d2a
--- /dev/null
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/PackageResourceUrlSanitizer.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.request.resource;
+
+import static org.apache.wicket.request.resource.PackageResource.getResourceStream;
+
+import java.io.IOException;
+
+import org.apache.wicket.request.resource.ResourceReference.UrlAttributes;
+import org.apache.wicket.util.resource.IResourceStream;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Sanitizes the URL based on existing package resource style/locale/variations
+ * 
+ * @author Pedro Santos
+ */
+public class PackageResourceUrlSanitizer implements IResourceUrlSanitizer
+{
+	private static final Logger log = LoggerFactory.getLogger(PackageResourceUrlSanitizer.class);
+
+	/**
+	 * @return UrlAttributes with an existent locale/style/variation if a resource is bound to the
+	 *         scope+name, otherwise returns null
+	 */
+	@Override
+	public UrlAttributes sanitize(UrlAttributes urlAttributes, Class<?> scope, String name)
+	{
+		IResourceStream filesystemMatch = getResourceStream(scope, name, urlAttributes.getLocale(),
+			urlAttributes.getStyle(), urlAttributes.getVariation(), false);
+		if (filesystemMatch == null)
+		{
+			return null;
+		}
+		try
+		{
+			filesystemMatch.close();
+		}
+		catch (IOException e)
+		{
+			log.error("failed to close", e);
+		}
+		return new ResourceReference.UrlAttributes(filesystemMatch.getLocale(),
+			filesystemMatch.getStyle(), filesystemMatch.getVariation());
+	}
+
+}
diff --git a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReference.java b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReference.java
index 4812f1985a..94a55e71d7 100644
--- a/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReference.java
+++ b/wicket-core/src/main/java/org/apache/wicket/request/resource/ResourceReference.java
@@ -350,6 +350,18 @@ public abstract class ResourceReference implements IClusterable
 			return variation;
 		}
 
+		/**
+		 * @param scope
+		 * @param name
+		 * @return sanitized URL attributes if a sanitizer is set for the app
+		 */
+		public UrlAttributes sanitize(Class<?> scope, String name)
+		{
+			IResourceUrlSanitizer sanitizer = Application.get().getResourceSettings()
+				.getUrlSanitizer();
+			return sanitizer == null ? this : sanitizer.sanitize(this, scope, name);
+		}
+
 		/**
 		 * @see java.lang.Object#equals(java.lang.Object)
 		 */
diff --git a/wicket-core/src/main/java/org/apache/wicket/settings/ResourceSettings.java b/wicket-core/src/main/java/org/apache/wicket/settings/ResourceSettings.java
index ef5474851c..249811cb14 100644
--- a/wicket-core/src/main/java/org/apache/wicket/settings/ResourceSettings.java
+++ b/wicket-core/src/main/java/org/apache/wicket/settings/ResourceSettings.java
@@ -35,6 +35,8 @@ import org.apache.wicket.markup.head.ResourceAggregator.RecordedHeaderItem;
 import org.apache.wicket.markup.html.IPackageResourceGuard;
 import org.apache.wicket.markup.html.SecurePackageResourceGuard;
 import org.apache.wicket.request.http.WebResponse;
+import org.apache.wicket.request.resource.IResourceUrlSanitizer;
+import org.apache.wicket.request.resource.PackageResourceUrlSanitizer;
 import org.apache.wicket.request.resource.caching.FilenameWithVersionResourceCachingStrategy;
 import org.apache.wicket.request.resource.caching.IResourceCachingStrategy;
 import org.apache.wicket.request.resource.caching.NoOpResourceCachingStrategy;
@@ -163,6 +165,9 @@ public class ResourceSettings implements IPropertiesFactoryContext
 	// resource caching strategy
 	private IResourceCachingStrategy resourceCachingStrategy;
 
+	// resource URL sanitizer
+	private IResourceUrlSanitizer resourceUrlSanitizer = new PackageResourceUrlSanitizer();
+
 	// application these settings are bound to
 	private final Application application;
 
@@ -689,6 +694,24 @@ public class ResourceSettings implements IPropertiesFactoryContext
 		return this;
 	}
 
+	/**
+	 * @return the resource URL sanitizer, null if none
+	 */
+	public IResourceUrlSanitizer getUrlSanitizer()
+	{
+		return resourceUrlSanitizer;
+	}
+
+	/**
+	 * Sets the resource URL sanitizer
+	 * 
+	 * @param resourceUrlSanitizer
+	 */
+	public void setUrlSanitizer(IResourceUrlSanitizer resourceUrlSanitizer)
+	{
+		this.resourceUrlSanitizer = resourceUrlSanitizer;
+	}
+
 	/**
 	 * Sets whether to use pre-minified resources when available. Minified resources are detected by
 	 * name. The minified version of {@code x.js} is expected to be called {@code x.min.js}. For css