Skip to content

Commit 999b36f

Browse files
authored
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
1 parent 8c2c0da commit 999b36f

File tree

2 files changed

+32
-54
lines changed

2 files changed

+32
-54
lines changed

lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StaticServlet.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

lang/java/ipc-jetty/src/main/java/org/apache/avro/ipc/jetty/StatsServer.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
package org.apache.avro.ipc.jetty;
2-
3-
import org.apache.avro.ipc.stats.StatsPlugin;
4-
import org.apache.avro.ipc.stats.StatsServlet;
51
/*
62
* Licensed to the Apache Software Foundation (ASF) under one
73
* or more contributor license agreements. See the NOTICE file
@@ -19,9 +15,19 @@
1915
* See the License for the specific language governing permissions and
2016
* limitations under the License.
2117
*/
18+
19+
package org.apache.avro.ipc.jetty;
20+
21+
import org.apache.avro.ipc.stats.StatsPlugin;
22+
import org.apache.avro.ipc.stats.StatsServlet;
23+
2224
import org.eclipse.jetty.server.Server;
23-
import org.eclipse.jetty.servlet.ServletHandler;
25+
import org.eclipse.jetty.server.handler.ContextHandler;
26+
import org.eclipse.jetty.server.handler.HandlerList;
27+
import org.eclipse.jetty.server.handler.ResourceHandler;
28+
import org.eclipse.jetty.servlet.ServletContextHandler;
2429
import org.eclipse.jetty.servlet.ServletHolder;
30+
import org.eclipse.jetty.util.resource.Resource;
2531

2632
/* This is a server that displays live information from a StatsPlugin.
2733
*
@@ -42,11 +48,28 @@ public StatsServer(StatsPlugin plugin, int port) throws Exception {
4248
this.httpServer = new Server(port);
4349
this.plugin = plugin;
4450

45-
ServletHandler handler = new ServletHandler();
46-
httpServer.setHandler(handler);
47-
handler.addServletWithMapping(new ServletHolder(new StaticServlet()), "/");
51+
ServletContextHandler servletContext = new ServletContextHandler(ServletContextHandler.SESSIONS);
52+
servletContext.setContextPath("/");
53+
54+
ServletHolder servletHolder = new ServletHolder(new StatsServlet(plugin));
55+
servletContext.addServlet(servletHolder, "/");
56+
57+
HandlerList handlers = new HandlerList(servletContext);
58+
59+
Resource classPathResource = Resource.newClassPathResource("/org/apache/avro/ipc/stats/static");
60+
if (classPathResource.exists() && classPathResource.isDirectory()) {
61+
ResourceHandler resourceHandler = new ResourceHandler();
62+
resourceHandler.setBaseResource(classPathResource);
63+
resourceHandler.setDirectoriesListed(false); // Optional: prevent directory listing
64+
65+
ContextHandler staticContext = new ContextHandler();
66+
staticContext.setContextPath("/static");
67+
staticContext.setHandler(resourceHandler);
68+
69+
handlers.prependHandler(staticContext);
70+
}
4871

49-
handler.addServletWithMapping(new ServletHolder(new StatsServlet(plugin)), "/");
72+
httpServer.setHandler(handlers);
5073

5174
httpServer.start();
5275
}

0 commit comments

Comments
 (0)