I’m looking to expose a clientacesspolicy.xml file from an embedded jetty server.
My current attempt looks like this:
ContextHandler capHandler = new ContextHandler();
capHandler.setContextPath("/clientaccesspolicy.xml");
capHandler.setBaseResource(Resource.newClassPathResource("clientaccesspolicy.xml"));
HandlerList handlers = new HandlerList();
handlers.addHandler(capHandler);
...
httpServer.setHandler(handlers);
But I get a 404 accessing http://localhost:9000/clientaccesspolicy.xml
How can I expose a classpath resource to a given URL programmatically in Jetty?
Thanks,
Andy
Your code doesn’t work because a
ContextHandlerdoesn’t actually server up content.A small adjustment will make it sort of work, but to do what you really want you’ll need to write your own handler.
The “sort of works” version:
But, that version treats
/clientaccesspolicy.xmlas a directory, so it redirects to/clientaccesspolicy.xml/and then displays the contents of the XML file.What it looks like you want is a version of the
ResourceHandlerthat has a lookup of url => resource.Jetty doesn’t ship with a handler that does that, but you should be able to create a subclass of
ResourceHandlerand then overridegetResource. In that case you won’t need [or want] the ContextHandler, just check for calls to “/clientaccesspolicy.xml” and map it to the correct ClassPath Resource.