Better handling of jetty-dir.css and favicon.ico

This commit is contained in:
Joakim Erdfelt 2022-08-04 08:37:56 -05:00
parent 74f54da67e
commit 1f1b9dc2dc
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
8 changed files with 27 additions and 30 deletions

View File

@ -288,7 +288,7 @@ public class HTTP3ServerDocs
{
// tag::push[]
// The favicon bytes.
ByteBuffer faviconBuffer = BufferUtil.toBuffer(Resource.newResource("/path/to/favicon.ico"), true);
ByteBuffer faviconBuffer = BufferUtil.toBuffer(server.getResource("favicon.ico"), true);
ServerSessionListener sessionListener = new ServerSessionListener()
{

View File

@ -17,6 +17,7 @@ import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@ -48,6 +49,7 @@ import org.eclipse.jetty.util.component.Graceful;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.resource.FileSystemPool;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.thread.AutoLock;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.ShutdownThread;
@ -641,6 +643,20 @@ public class Server extends Handler.Wrapper implements Attributes
return this;
}
/**
* Return the Resource representing managed by the Server.
*
* @param name the name of the resource (relative to `/org/eclipse/jetty/server/`)
* @return the Resource found, or null if not found.
*/
public Resource getResource(String name)
{
URL url = getClass().getResource(name);
if (url == null)
return null;
return ResourceFactory.of(this).newResource(URI.create(url.toExternalForm()));
}
@Override
public String toString()
{

View File

@ -16,10 +16,7 @@ package org.eclipse.jetty.server.handler;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;
@ -41,7 +38,6 @@ import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.annotation.ManagedAttribute;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -67,27 +63,21 @@ public class DefaultHandler extends Handler.Processor
public DefaultHandler()
{
String faviconRef = "/org/eclipse/jetty/favicon.ico";
byte[] favbytes = null;
try
{
URL fav = getClass().getResource(faviconRef);
if (fav != null)
Resource faviconRes = getServer().getResource("favicon.ico");
if (faviconRes != null)
{
URI uri = fav.toURI();
try (ResourceFactory.Closeable resourceFactory = ResourceFactory.closeable())
try (InputStream is = faviconRes.newInputStream())
{
Resource resource = resourceFactory.newResource(uri);
try (InputStream is = Files.newInputStream(resource.getPath()))
{
favbytes = IO.readBytes(is);
}
favbytes = IO.readBytes(is);
}
}
}
catch (Exception e)
{
LOG.warn("Unable to find default favicon: {}", faviconRef, e);
LOG.warn("Unable to find default favicon", e);
}
finally
{

View File

@ -13,7 +13,6 @@
package org.eclipse.jetty.server.handler;
import java.net.URI;
import java.util.List;
import org.eclipse.jetty.http.CachingContentFactory;
@ -155,16 +154,12 @@ public class ResourceHandler extends Handler.Wrapper
*/
public Resource getStylesheet()
{
// TODO
return getDefaultStyleSheet();
}
public static Resource getDefaultStyleSheet()
private Resource getDefaultStyleSheet()
{
// TODO do this some other way. It is expensive to mount a whole jar when we could
// just read the resource from the URL. We also leak the Mount.
URI css = URIUtil.toURI(ResourceHandler.class.getResource("/jetty-dir.css").toString());
return ResourceFactory.root().newResource(css);
return getServer().getResource("jetty-dir.css");
}
public List<String> getWelcomeFiles()

View File

@ -189,12 +189,9 @@ public class ResourceHandler extends HandlerWrapper implements ResourceFactory,
}
}
public static Resource getDefaultStylesheet()
private Resource getDefaultStylesheet()
{
// TODO do this some other way. It is expensive to mount a whole jar when we could
// just read the resource from the URL. We also leak the Mount.
URI css = URIUtil.toURI(ResourceHandler.class.getResource("/jetty-dir.css").toString());
return ResourceFactory.root().newResource(css);
return getServer().getResource("jetty-dir.css");
}
public String[] getWelcomeFiles()

View File

@ -29,7 +29,6 @@ import jakarta.servlet.http.HttpServletResponse;
import org.eclipse.jetty.ee9.nested.CachedContentFactory;
import org.eclipse.jetty.ee9.nested.ContextHandler;
import org.eclipse.jetty.ee9.nested.ResourceContentFactory;
import org.eclipse.jetty.ee9.nested.ResourceHandler;
import org.eclipse.jetty.ee9.nested.ResourceService;
import org.eclipse.jetty.ee9.nested.ResourceService.WelcomeFactory;
import org.eclipse.jetty.http.CompressedContentFormat;
@ -222,7 +221,7 @@ public class DefaultServlet extends HttpServlet implements ResourceFactory, Welc
}
if (_stylesheet == null)
{
_stylesheet = ResourceHandler.getDefaultStylesheet();
_stylesheet = _contextHandler.getServer().getResource("jetty-dir.css");
}
}
catch (Exception e)