user2684301
user2684301

Reputation: 2620

Grizzly + Static Content + Servlet Filter

I can get Grizzly to serve static content

I can create the servlet filter to filter a named servlet

But I can't get the servlet filter to filter the static content. How do I do that?

Here is the code I have so far:

WebappContext webappContext = new WebappContext("grizzly web context", "");
FilterRegistration authFilterReg = webappContext.addFilter("Authentication Filter", org.package.AuthenticationFilter.class);

// If I create a ServletContainer, I can add the filter to it like this:
// authFilterReg.addMappingForServletNames(EnumSet.allOf(DispatcherType.class), "servletName");

HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(BASE_URI);
webappContext.deploy(httpServer);

// This works, but the content does not go through the authentication filter above
httpServer.getServerConfiguration().addHttpHandler(new StaticHttpHandler(absolutePath), "/static");

Upvotes: 1

Views: 1197

Answers (1)

alexey
alexey

Reputation: 1979

The ServletFilters registered as part of WebappContext (Web application) will be executed only for requests related to this WebappContext (Web application).

So, one of the solutions I see is to register DefaultServlet [1] on the WebappContext and use it instead of StaticHttpHandler. Something like:

ArraySet<File> set = new ArraySet<File>(File.class);
set.add(new File(absolutePath));
ServletRegistration defaultServletReg = webappContext.addServlet("DefaultServlet", new DefaultServlet(set) {});
defaultServletReg.addMapping("/static");

[1] https://github.com/GrizzlyNIO/grizzly-mirror/blob/2.3.x/modules/http-servlet/src/main/java/org/glassfish/grizzly/servlet/DefaultServlet.java

Upvotes: 1

Related Questions