Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

java - Jetty: How to nest HandlerWrapper, HandlerList, and ContextHandlerCollection, and ContextHandler

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,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...