diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/StaticHttpContentFactory.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/VirtualHttpContentFactory.java similarity index 51% rename from jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/StaticHttpContentFactory.java rename to jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/VirtualHttpContentFactory.java index 6fcfabab2f2..5ac2dda1b0a 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/StaticHttpContentFactory.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/content/VirtualHttpContentFactory.java @@ -17,23 +17,32 @@ import java.io.IOException; import org.eclipse.jetty.util.resource.Resource; -public class StaticHttpContentFactory implements HttpContent.Factory +/** + * An {@link HttpContent.Factory} implementation which takes a Resource and fakes this resource as + * an entry in every directory. If any request is made for this resources file name, and it is not + * already present in that directory then the resource contained in this factory will be served instead. + */ +public class VirtualHttpContentFactory implements HttpContent.Factory { private final HttpContent.Factory _factory; - private final Resource _styleSheet; + private final Resource _resource; + private final String _contentType; + private final String _matchSuffix; - public StaticHttpContentFactory(HttpContent.Factory factory, Resource styleSheet) + public VirtualHttpContentFactory(HttpContent.Factory factory, Resource resource, String contentType) { _factory = factory; - _styleSheet = styleSheet; + _resource = resource; + _matchSuffix = "/" + _resource.getFileName(); + _contentType = contentType; } /** * @return Returns the stylesheet as a Resource. */ - public Resource getStyleSheet() + public Resource getResource() { - return _styleSheet; + return _resource; } @Override @@ -42,10 +51,13 @@ public class StaticHttpContentFactory implements HttpContent.Factory HttpContent content = _factory.getContent(path); if (content != null) return content; - - if ((_styleSheet != null) && (path != null) && path.endsWith("/jetty-dir.css")) - return new ResourceHttpContent(_styleSheet, "text/css"); - + if (matchResource(path)) + return new ResourceHttpContent(_resource, _contentType); return null; } -} \ No newline at end of file + + protected boolean matchResource(String path) + { + return (_resource != null) && (path != null) && path.endsWith(_matchSuffix); + } +} diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java index 1008773e5ff..fb81f71909a 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ResourceHandler.java @@ -23,8 +23,8 @@ import org.eclipse.jetty.http.content.FileMappingHttpContentFactory; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContentFactory; import org.eclipse.jetty.http.content.ResourceHttpContentFactory; -import org.eclipse.jetty.http.content.StaticHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; +import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.server.Context; @@ -106,7 +106,7 @@ public class ResourceHandler extends Handler.Wrapper { HttpContent.Factory contentFactory = new ResourceHttpContentFactory(ResourceFactory.of(getBaseResource()), getMimeTypes()); contentFactory = new FileMappingHttpContentFactory(contentFactory); - contentFactory = new StaticHttpContentFactory(contentFactory, getStyleSheet()); + contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, getPrecompressedFormats()); contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), getByteBufferPool()); return contentFactory; diff --git a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java index 41c5e720ea0..13807825810 100644 --- a/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java +++ b/jetty-core/jetty-server/src/test/java/org/eclipse/jetty/server/handler/ResourceHandlerTest.java @@ -47,8 +47,8 @@ import org.eclipse.jetty.http.content.FileMappingHttpContentFactory; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContentFactory; import org.eclipse.jetty.http.content.ResourceHttpContentFactory; -import org.eclipse.jetty.http.content.StaticHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; +import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.HttpConfiguration; @@ -668,7 +668,7 @@ public class ResourceHandlerTest { HttpContent.Factory contentFactory = new ResourceHttpContentFactory(ResourceFactory.of(getBaseResource()), getMimeTypes()); contentFactory = new FileMappingHttpContentFactory(contentFactory); - contentFactory = new StaticHttpContentFactory(contentFactory, getStyleSheet()); + contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, getPrecompressedFormats()); contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, 0, getByteBufferPool()); return contentFactory; diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java index f0ab2a2e588..a7671c6eefe 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/DefaultServlet.java @@ -57,8 +57,8 @@ import org.eclipse.jetty.http.content.FileMappingHttpContentFactory; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContentFactory; import org.eclipse.jetty.http.content.ResourceHttpContentFactory; -import org.eclipse.jetty.http.content.StaticHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; +import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.io.ByteBufferInputStream; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.NoopByteBufferPool; @@ -158,7 +158,7 @@ public class DefaultServlet extends HttpServlet if (getInitBoolean("useFileMappedBuffer", false)) contentFactory = new FileMappingHttpContentFactory(contentFactory); - contentFactory = new StaticHttpContentFactory(contentFactory, styleSheet); + contentFactory = new VirtualHttpContentFactory(contentFactory, styleSheet, "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, precompressedFormats); int maxCacheSize = getInitInt("maxCacheSize", -2); diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java index fbd3db81347..9383ee2adb6 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ResourceHandler.java @@ -33,8 +33,8 @@ import org.eclipse.jetty.http.content.FileMappingHttpContentFactory; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContentFactory; import org.eclipse.jetty.http.content.ResourceHttpContentFactory; -import org.eclipse.jetty.http.content.StaticHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; +import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.server.Server; @@ -134,7 +134,7 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory, { HttpContent.Factory contentFactory = new ResourceHttpContentFactory(this, _mimeTypes); contentFactory = new FileMappingHttpContentFactory(contentFactory); - contentFactory = new StaticHttpContentFactory(contentFactory, getStyleSheet()); + contentFactory = new VirtualHttpContentFactory(contentFactory, getStyleSheet(), "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, _resourceService.getPrecompressedFormats()); contentFactory = new ValidatingCachingHttpContentFactory(contentFactory, Duration.ofSeconds(1).toMillis(), _byteBufferPool); return contentFactory; diff --git a/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java b/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java index 6bec020b258..e88832fde11 100644 --- a/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java +++ b/jetty-ee9/jetty-ee9-servlet/src/main/java/org/eclipse/jetty/ee9/servlet/DefaultServlet.java @@ -39,8 +39,8 @@ import org.eclipse.jetty.http.content.FileMappingHttpContentFactory; import org.eclipse.jetty.http.content.HttpContent; import org.eclipse.jetty.http.content.PreCompressedHttpContentFactory; import org.eclipse.jetty.http.content.ResourceHttpContentFactory; -import org.eclipse.jetty.http.content.StaticHttpContentFactory; import org.eclipse.jetty.http.content.ValidatingCachingHttpContentFactory; +import org.eclipse.jetty.http.content.VirtualHttpContentFactory; import org.eclipse.jetty.io.ByteBufferPool; import org.eclipse.jetty.io.NoopByteBufferPool; import org.eclipse.jetty.server.Server; @@ -253,7 +253,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory, Welc contentFactory = new ResourceHttpContentFactory(this, _mimeTypes); if (_useFileMappedBuffer) contentFactory = new FileMappingHttpContentFactory(contentFactory); - contentFactory = new StaticHttpContentFactory(contentFactory, _styleSheet); + contentFactory = new VirtualHttpContentFactory(contentFactory, _styleSheet, "text/css"); contentFactory = new PreCompressedHttpContentFactory(contentFactory, _resourceService.getPrecompressedFormats()); int maxCacheSize = getInitInt("maxCacheSize", -2);