0

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,

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
Abhinav Ramakrishnan
  • 1,070
  • 12
  • 22

1 Answers1

4

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)

Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
  • This was certainly in the right direction. I ended up just coming up with my own handler since that was easier than hunting down the documentation :P – Abhinav Ramakrishnan Sep 02 '16 at 00:40