Embedded Jetty rewrites not working properly

十年热恋 提交于 2019-12-05 02:56:30
Joakim Erdfelt

Replace the 2 lines ...

_ctx.setHandler(rewriter);
_handlerCollection.setHandlers(new Handler[] {_ctx});

with

rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] {rewriter});

That will make the rewriter rules kick in before the normal context handling.

Think of the context handling as a tree. In your example code you have ....

server
+--  HandlerCollection
     [0]-- WebAppContext
           +-- Your servlets and filters in web.xml
           +-- DefaultServlet
               +-- RewriteHandler

That means if the WebAppContext can't handle the request, then the RewriteHandler is executed to see if it can handle the request. That will never happen, as WebAppContext is setup to use DefaultServlet if nothing else matches.

The simple change suggested changes the tree to look like this ...

server
+--  HandlerCollection
     [0]-- RewriteHandler
           +-- WebAppContext
               +-- Your servlets and filters in web.xml
               +-- DefaultServlet

This will allow the RewriteHandler to do its thing before the WebAppContext is even asked.

Note: you can also have your code utilize the HandlerCollection a bit more properly for this scenario as well.

// _ctx.setHandler(rewriter);
// rewriter.setHandler(_ctx);
_handlerCollection.setHandlers(new Handler[] { rewriter, _ctx });

This will result in the following tree

server
+--  HandlerCollection
     [0]-- RewriteHandler
     [1]-- WebAppContext
           +-- Your servlets and filters in web.xml
           +-- DefaultServlet
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!