Skip to content

Enhancement proposal: use Undertow's built-in handlers to serve static content #1346

@vic0824

Description

@vic0824

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions