From c00def58cb0e6e3d97b5f7c54ff88e81b2e8534a Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 21 Nov 2022 11:42:40 -0600 Subject: [PATCH] Rename AbstractUrlResourceFactory to UrlResourceFactory + Is package private + Add ResourceFactory.registerResourceFactory(String) --- .../jetty/util/resource/ResourceFactory.java | 5 +++++ ...ceFactory.java => UrlResourceFactory.java} | 8 +++---- .../util/resource/ResourceFactoryTest.java | 22 +++++++++++++++++++ .../util/resource/UrlResourceFactoryTest.java | 2 +- 4 files changed, 32 insertions(+), 5 deletions(-) rename jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/{AbstractUrlResourceFactory.java => UrlResourceFactory.java} (96%) diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceFactory.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceFactory.java index dfea8b0b45b..309e09331f8 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceFactory.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/ResourceFactory.java @@ -241,6 +241,11 @@ public interface ResourceFactory ResourceFactoryInternals.RESOURCE_FACTORIES.put(scheme, resource); } + static void registerUrlResourceFactory(String scheme) + { + ResourceFactoryInternals.RESOURCE_FACTORIES.put(scheme, new UrlResourceFactory(scheme)); + } + static ResourceFactory unregisterResourceFactory(String scheme) { return ResourceFactoryInternals.RESOURCE_FACTORIES.remove(scheme); diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/AbstractUrlResourceFactory.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/UrlResourceFactory.java similarity index 96% rename from jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/AbstractUrlResourceFactory.java rename to jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/UrlResourceFactory.java index c8d1d9de7e2..3f42711613e 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/AbstractUrlResourceFactory.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/resource/UrlResourceFactory.java @@ -26,15 +26,15 @@ import java.time.Instant; import org.eclipse.jetty.util.FileID; /** - * Abstract ResourceFactory for {@link java.net.URL} based resources. + * {@link ResourceFactory} for {@link java.net.URL} based resources. */ -public abstract class AbstractUrlResourceFactory implements ResourceFactory +class UrlResourceFactory implements ResourceFactory { private final String supportedProtocol; private int connectTimeout; private boolean useCaches; - protected AbstractUrlResourceFactory(String protocol) + protected UrlResourceFactory(String protocol) { this.supportedProtocol = protocol; this.connectTimeout = Integer.parseInt(System.getProperty(this.getClass().getName() + ".connectTimeout", "1000")); @@ -134,7 +134,7 @@ public abstract class AbstractUrlResourceFactory implements ResourceFactory @Override public String getName() { - return uri.getPath(); + return uri.toASCIIString(); } @Override diff --git a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceFactoryTest.java b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceFactoryTest.java index 6ed15c29991..385bfd7c46c 100644 --- a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceFactoryTest.java +++ b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceFactoryTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -60,6 +61,27 @@ public class ResourceFactoryTest assertThat(resource.getName(), is("custom-impl")); } + @Test + public void testRegisterHttpsUrlFactory() + { + ResourceFactory.registerUrlResourceFactory("https"); + // Try as a normal String input + Resource resource = ResourceFactory.root().newResource("https://webtide.com/"); + assertThat(resource.getURI(), is(URI.create("https://webtide.com/"))); + assertThat(resource.getName(), is("https://webtide.com/")); + + // Try as a formal URI object as input + URI uri = URI.create("https://webtide.com/"); + resource = ResourceFactory.root().newResource(uri); + assertThat(resource.getURI(), is(URI.create("https://webtide.com/"))); + assertThat(resource.getName(), is("https://webtide.com/")); + + // Try a sub-resource + Resource subResource = resource.resolve("favicon.ico"); + assertThat(subResource.getFileName(), is("favicon.ico")); + assertThat(subResource.length(), greaterThan(0L)); + } + public static class CustomResourceFactory implements ResourceFactory { @Override diff --git a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/UrlResourceFactoryTest.java b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/UrlResourceFactoryTest.java index e2e65a782ca..ee458f0e222 100644 --- a/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/UrlResourceFactoryTest.java +++ b/jetty-core/jetty-util/src/test/java/org/eclipse/jetty/util/resource/UrlResourceFactoryTest.java @@ -70,7 +70,7 @@ public class UrlResourceFactoryTest assertThat(favicon.getFileName(), is("favicon.ico")); } - public static class HttpsResourceFactory extends AbstractUrlResourceFactory + public static class HttpsResourceFactory extends UrlResourceFactory { public HttpsResourceFactory() {