Created
June 16, 2011 08:33
-
-
Save wspringer/1028893 to your computer and use it in GitHub Desktop.
Setting up resourceHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Handler addResourceHandler(Handler handler, File directory, final String index) throws Exception { | |
final HandlerList list = new HandlerList(); | |
ContextHandler contextHandler = new ContextHandler("/resources"); | |
contextHandler.setHandler(createResourceHandler(directory)); | |
if (index != null) { | |
list.addHandler(new AbstractHandler() { | |
@Override | |
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { | |
if ("/".equals(target)) { | |
response.sendRedirect(index); | |
} | |
} | |
}); | |
} | |
list.addHandler(contextHandler); | |
list.addHandler(handler); | |
return list; | |
} | |
private ResourceHandler createResourceHandler(File directory) throws IOException, URISyntaxException { | |
ResourceHandler resourceHandler = new ResourceHandler(); | |
resourceHandler.setBaseResource(new FileResource(directory.toURL())); | |
MimeTypes mimeTypes = resourceHandler.getMimeTypes(); | |
mimeTypes.addMimeMapping("eot", "application/vnd.ms-fontobject"); | |
mimeTypes.addMimeMapping("otf", "application/octet-stream"); | |
mimeTypes.addMimeMapping("ttf", "application/octet-stream"); | |
return resourceHandler; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment