package io.dropwizard.jetty;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ArrayTernaryTrie;
import org.eclipse.jetty.util.Trie;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * A Jetty router which routes requests based on context path.
 */
public class ContextRoutingHandler extends AbstractHandler {
    private final Trie<Handler> handlers;

    public ContextRoutingHandler(Map<String, ? extends Handler> handlers) {
        this.handlers = new ArrayTernaryTrie<>(false);
        for (Map.Entry<String, ? extends Handler> entry : handlers.entrySet()) {
            if (!this.handlers.put(entry.getKey(), entry.getValue())) {
                throw new IllegalStateException("Too many handlers");
            }
            addBean(entry.getValue());
        }
    }

    @Override
    public void handle(String target,
                       Request baseRequest,
                       HttpServletRequest request,
                       HttpServletResponse response) throws IOException, ServletException {
        final Handler handler = handlers.getBest(baseRequest.getRequestURI());
        if (handler != null) {
            handler.handle(target, baseRequest, request, response);
        }
    }

    @Override
    protected void doStart() throws Exception {
        super.doStart();
        for (String key : handlers.keySet()) {
            handlers.get(key).start();
        }
    }

    @Override
    protected void doStop() throws Exception {
        super.doStop();
        for (String key : handlers.keySet()) {
            handlers.get(key).stop();
        }
    }
}

@@ -156,24 +156,33 @@
 tractHandler
+Container
 ;%0Aimport org
@@ -222,44 +222,8 @@
 rie;
-%0Aimport org.eclipse.jetty.util.Trie;
 %0A%0Aim
@@ -532,16 +532,25 @@
 tHandler
+Container
  %7B%0A    p
@@ -562,16 +562,28 @@
 e final 
+ArrayTernary
 Trie%3CHan
@@ -1832,11 +1832,161 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    public Handler%5B%5D getHandlers() %7B%0A        return handlers.entrySet().stream().map(e -%3E e.getValue()).toArray(Handler%5B%5D::new);%0A    %7D
 %0A%7D%0A

Extend from AbstractHandlerContainer instead of AbstractHandler (#2460)