From 7d16ed5cd39183396bc3f7f030879c3e7f74225b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20D=C3=A9nes=20Terj=C3=A9ki?= Date: Thu, 12 Feb 2026 19:08:55 +0100 Subject: [PATCH] AVRO-4229 - Update StatsServer resource handler --- .../apache/avro/ipc/jetty/StaticServlet.java | 45 ------------------- .../apache/avro/ipc/jetty/StatsServer.java | 27 ++++++++--- 2 files changed, 22 insertions(+), 50 deletions(-) delete mode 100644 lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StaticServlet.java diff --git a/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StaticServlet.java b/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StaticServlet.java deleted file mode 100644 index 2e28ba14eba..00000000000 --- a/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StaticServlet.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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 - * - * https://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.avro.ipc.jetty; - -import java.net.URL; - -import org.eclipse.jetty.servlet.DefaultServlet; -import org.eclipse.jetty.util.resource.Resource; - -/** - * Very simple servlet class capable of serving static files. - */ -public class StaticServlet extends DefaultServlet { - private static final long serialVersionUID = 1L; - - @Override - public Resource getResource(String pathInContext) { - // Take only last slice of the URL as a filename, so we can adjust path. - // This also prevents mischief like '../../foo.css' - String[] parts = pathInContext.split("/"); - String filename = parts[parts.length - 1]; - - URL resource = getClass().getClassLoader().getResource("org/apache/avro/ipc/stats/static/" + filename); - if (resource == null) { - return null; - } - return Resource.newResource(resource); - } -} diff --git a/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StatsServer.java b/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StatsServer.java index 43e066190d7..0f6e07cae70 100644 --- a/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StatsServer.java +++ b/lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StatsServer.java @@ -20,8 +20,12 @@ * limitations under the License. */ import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.servlet.ServletHandler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.ResourceHandler; +import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.resource.Resource; /* This is a server that displays live information from a StatsPlugin. * @@ -42,11 +46,24 @@ public StatsServer(StatsPlugin plugin, int port) throws Exception { this.httpServer = new Server(port); this.plugin = plugin; - ServletHandler handler = new ServletHandler(); - httpServer.setHandler(handler); - handler.addServletWithMapping(new ServletHolder(new StaticServlet()), "/"); + ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS); + servletContext.setContextPath("/"); - handler.addServletWithMapping(new ServletHolder(new StatsServlet(plugin)), "/"); + ServletHolder servletHolder = new ServletHolder(new StatsServlet(plugin)); + servletContext.addServlet(servletHolder, "/"); + + ResourceHandler resourceHandler = new ResourceHandler(); + resourceHandler.setBaseResource(Resource.newClassPathResource("/org/apache/avro/ipc/stats/static")); + resourceHandler.setDirectoriesListed(false); // Optional: prevent directory listing + + ContextHandler staticContext = new ContextHandler(); + staticContext.setContextPath("/static"); + staticContext.setHandler(resourceHandler); + + HandlerList handlers = new HandlerList(); + handlers.setHandlers(new org.eclipse.jetty.server.Handler[] { staticContext, servletContext }); + + httpServer.setHandler(handlers); httpServer.start(); }