1

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");
user2684301
  • 2,550
  • 1
  • 24
  • 33

1 Answers1

1

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

alexey
  • 1,959
  • 10
  • 9
  • could you please take a look at my grizzly question: http://stackoverflow.com/questions/35123194/jersey-2-render-swagger-static-content-correctly-without-trailing-slash – macemers Feb 24 '16 at 06:47