I appreciate the suggestion, but that sounded a bit indirect …
Instead I copied File/PathResource/Manager into new classes MappedPathResource/Manager,
ripped out all the things that aren’t relevant in this case (like symlink support, case
sensitivity, and watching the file system). The result is simpler than expected: the
Resource class is basically identical to PathResource (minus pointer type back to the
manager), and the new MappedPathResourceManager is just 150 lines or so. Here’s how to use
it:
Map<String,Path> mappings = new HashMap<>();
// insert some test data
mappings.put( "/foo", Paths.get(
System.getProperty("user.home") + "/.bashrc" ));
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(
new ResourceHandler(
new MappedPathResourceManager( mappings )))
.build();
server.start();
and it seems to work and serve my purposes.
Code is here:
https://github.com/jernst/undertow-mappedpathresource
Q: Is that something that would be of use to anybody else? If so, I’d be happy to clean up
the code and submit a PR.
P.S Ideally a bit a refactoring should be done so that PathResource and MappedPathResource
share the same code (all these inner classes and threaded-related behavior) but that would
require some kind of intermediate superclass that collects functionality common to
PathResourceManager and MappedPathResourceManager.
P.S.2: I like Undertow. Give what kind of $%*&WQ@)$^& web servers there are in
Java land …
Cheers,
Johannes.
On Feb 6, 2018, at 16:01, Stuart Douglas <sdouglas(a)redhat.com>
wrote:
You could possibly use io.undertow.server.handlers.resource.ResourceSupplier, and then
use PathResourceManager under the hood to actually return a path resource. If all these
files have a common root path you could basically create a PathResourceManager that maps
to this, and then use Map<String, Resource> in the supplier.
Stuart
On Tue, Feb 6, 2018 at 9:06 PM, Johannes Ernst <jernst(a)indiecomputing.com
<mailto:jernst@indiecomputing.com>> wrote:
I’m attempting to serve a bunch of static files, which exist in the file system, but in
various directories that do not correspond to the URL namespace. Example:
http://example.com/foo/one <
http://example.com/foo/one> => /var/lib/one/bar
http://example.com/foo/two <
http://example.com/foo/two> => /var/lib/two/bar
I have the mapping from incoming request URL to File to be served in a
Map<String,File>. Now I’m attempting to hook up Undertow so it can serve those files
according to my mapping, and I’m not entirely sure where to best plug this in without
having to write loads of code.
As far as I can tell, I would need to implement my version of ResourceManager and
Resource, sort of like File/PathResource/Manager, but there’s a lot of code there, not all
of which I understand, and it would perhaps turn into a maintenance nightmare on my end.
I cannot see a straightforward way of overriding those classes either to “slide in” an
alternate mapping.
Is there a better way of doing this?
Thanks,
Johannes.
_______________________________________________
undertow-dev mailing list
undertow-dev(a)lists.jboss.org <mailto:undertow-dev@lists.jboss.org>
https://lists.jboss.org/mailman/listinfo/undertow-dev
<
https://lists.jboss.org/mailman/listinfo/undertow-dev>