Jetty: How to nest HandlerWrapper, HandlerList, an

2019-02-21 00:47发布

I'm trying to build an api server on Jetty.

I want to have multiple apis on routes that look like /apis/api1/endpoint, /apis/api2/endpoint, /apis/api3/endpoint, etc

Essentially I have a HandlerWrapper, that contains a HandlerList of ContextHandlerCollections that in essence just does:

public void handle(...) {
    if (uri.startsWith("/apis/")) {
        log.info("This is an api request");
        this.getHandlerList.handle(...)
    } else {
        super.handle()
    }
}

private HandlerList getHandlerList() {
    HandlerList handlerList = new HandlerList();
    ContextHandlerCollection contextHandlerCollection = new ContextHandlerCollection();
    ContextHandler api1 = new ContextHandler("/apis/api1/endpoint");
    api1.setHandler(new Api1Handler());
    contextHandlerCollection.addHandler(api1);
    handlerList.addHandler(contextHandlerCollection);
    return handlerList
}

Now when I try to do:

curl localhost:port/apis/api1/endpoint

I get a 404 not found but I see in the logs the statement "This is an api request".

Any hints?

I basically want one ContextHandlerCollection for each api1, api2 etc. And the ContextHandlerCollection should be composed of a set of endpoint-specific handlers to choose from.

What am I missing?

Cheers,

1条回答
等我变得足够好
2楼-- · 2019-02-21 01:27

Handler - the base form of handling a request, its not a terminal point for the request processing unless you call request.setHandled(true)

HandlerWrapper - a handler that can perform some processing and decide if it should hand off the request to a nested (wrapped) handler.

HandlerCollection - a collection of handlers, following the standard java collection rules regarding execution order. Each handler in the collection is executed until one of them calls request.setHandled(true)

HandlerList - a specialized HandlerCollection that follows java.util.List ordering of execution of child Handlers

ContextHandler - a specialized HandlerWrapper that only executes its wrapped Handler if the request context-path and virtual hosts matches.

ContextHandlerCollection - a HashMap of ContextHandler that will only execute those child handlers (in the collection) that has a match to the request context-path (and virtual hosts)

查看更多
登录 后发表回答