Rename AbstractUrlResourceFactory to UrlResourceFactory

+ Is package private
+ Add ResourceFactory.registerResourceFactory(String)
This commit is contained in:
Joakim Erdfelt 2022-11-21 11:42:40 -06:00
parent c981b3b5f5
commit c00def58cb
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
4 changed files with 32 additions and 5 deletions

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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()
{