-
-
Notifications
You must be signed in to change notification settings - Fork 87
Description
Currently, a dedicated handler exists (GetDynamicContentHandler) for serving static content.
In case it is considered useful, another approach would be to assess the feasibility of using the Undertow's built-in handlers to serve static content, in particular the ResourceHandler.
Advantages: slight reduction of code-base size, possibility of caching on server side, slightly better performance (the Undertow implementation uses a statically initialized map of mime types, thus avoiding a long chain of if-then-else).
Disadvantages: not so easy to serve the html files in response to an http request that does not end with .html (at least so far I wasn't able to do it). I also didn't have time to find out if the Undertow handlers can handle also the templating part of GetDynamicContentHandler
The ResourceHandler can be added to the routes like this:
final int cacheTime = 86400; // seconds
ResourceManager classPathManager = new ClassPathResourceManager(HttpServer.class.getClassLoader(), "static");
ResourceManager resourceManager = new CachingResourceManager(100, 65536,
new DirectBufferCache(1024, 10, 10480), classPathManager, cacheTime);
ResourceHandler resourceHandler = new ResourceHandler(resourceManager);
resourceHandler.setCachable(Predicates.truePredicate());
resourceHandler.setCacheTime(cacheTime);
final PathHandler routes = new PathHandler();
routes.addPrefixPath("/", resourceHandler);
and the html file must be included in the http request: http://[hostname]:[port_number]/index.html