This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/main by this push:
     new 999b36fead AVRO-4229 - Update StatsServer resource handler (#3648)
999b36fead is described below

commit 999b36feadf76ff9eb7535a66a00dbffba8b0b75
Author: László Dénes Terjéki <[email protected]>
AuthorDate: Tue Feb 17 07:11:25 2026 +0100

    AVRO-4229 - Update StatsServer resource handler (#3648)
    
    * AVRO-4229 - Update StatsServer resource handler
    
    * Fix review comments
    
    - moved ASFv2 to the top
    - static content only served when it is available
---
 .../org/apache/avro/ipc/jetty/StaticServlet.java   | 45 ----------------------
 .../org/apache/avro/ipc/jetty/StatsServer.java     | 41 +++++++++++++++-----
 2 files changed, 32 insertions(+), 54 deletions(-)

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 2e28ba14eb..0000000000
--- 
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 43e066190d..ea83dd1ac6 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
@@ -1,7 +1,3 @@
-package org.apache.avro.ipc.jetty;
-
-import org.apache.avro.ipc.stats.StatsPlugin;
-import org.apache.avro.ipc.stats.StatsServlet;
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -19,9 +15,19 @@ import org.apache.avro.ipc.stats.StatsServlet;
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
+package org.apache.avro.ipc.jetty;
+
+import org.apache.avro.ipc.stats.StatsPlugin;
+import org.apache.avro.ipc.stats.StatsServlet;
+
 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 +48,28 @@ public class StatsServer {
     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("/");
+
+    ServletHolder servletHolder = new ServletHolder(new StatsServlet(plugin));
+    servletContext.addServlet(servletHolder, "/");
+
+    HandlerList handlers = new HandlerList(servletContext);
+
+    Resource classPathResource = 
Resource.newClassPathResource("/org/apache/avro/ipc/stats/static");
+    if (classPathResource.exists() && classPathResource.isDirectory()) {
+      ResourceHandler resourceHandler = new ResourceHandler();
+      resourceHandler.setBaseResource(classPathResource);
+      resourceHandler.setDirectoriesListed(false); // Optional: prevent 
directory listing
+
+      ContextHandler staticContext = new ContextHandler();
+      staticContext.setContextPath("/static");
+      staticContext.setHandler(resourceHandler);
+
+      handlers.prependHandler(staticContext);
+    }
 
-    handler.addServletWithMapping(new ServletHolder(new StatsServlet(plugin)), 
"/");
+    httpServer.setHandler(handlers);
 
     httpServer.start();
   }

Reply via email to