private Router createRootTreeAcceptor(
      RouteToPathBuilder<PathPattern> lastRoutedBuilder, boolean subResourceMode) {
    final Router routingRoot;
    if (lastRoutedBuilder != null) {
      routingRoot = lastRoutedBuilder.build();
    } else {
      /**
       * Create an empty routing root that accepts any request, does not do anything and does not
       * return any inflector. This will cause 404 being returned for every request.
       */
      routingRoot =
          Routers.acceptingTree(
                  new Function<ContainerRequest, ContainerRequest>() {

                    @Override
                    public ContainerRequest apply(ContainerRequest input) {
                      return input;
                    }
                  })
              .build();
    }

    if (subResourceMode) {
      return routingRoot;
    } else {
      return rootBuilder.root(routingRoot);
    }
  }
  @Before
  public void setupApplication() {
    Services services = HK2.get().create(null, new ServerModule());

    final Ref<ServiceProviders> providers =
        services.forContract(new TypeLiteral<Ref<ServiceProviders>>() {}).get();
    providers.set(services.forContract(ServiceProviders.Builder.class).get().build());
    final Ref<MessageBodyWorkers> workers =
        services.forContract(new TypeLiteral<Ref<MessageBodyWorkers>>() {}).get();
    workers.set(new MessageBodyFactory(providers.get()));
    final Ref<ExceptionMappers> mappers =
        services.forContract(new TypeLiteral<Ref<ExceptionMappers>>() {}).get();
    mappers.set(new ExceptionMapperFactory(providers.get()));

    Injector injector = services.forContract(Injector.class).get();
    injector.inject(this);

    final InvokerBuilder invokerBuilder = injector.inject(InvokerBuilder.class);
    this.invoker =
        invokerBuilder.build(
            routeBuilder.root(
                routeBuilder
                    .route("a(/.*)?")
                    .to(LastPathSegmentTracingFilter.class)
                    .to(
                        routeBuilder
                            .route("b(/.*)?")
                            .to(LastPathSegmentTracingFilter.class)
                            .to(
                                routeBuilder
                                    .route("c(/)?")
                                    .to(LastPathSegmentTracingFilter.class)
                                    .to(Routers.asTreeAcceptor(new AsyncInflector(injector))))
                            .to(
                                routeBuilder
                                    .route("d(/)?")
                                    .to(LastPathSegmentTracingFilter.class)
                                    .to(Routers.asTreeAcceptor(new AsyncInflector(injector)))))
                    .build()));
    this.requestScope = injector.inject(RequestScope.class);
  }
  private Router createSingleMethodAcceptor(
      final ResourceMethod resourceMethod, final boolean subResourceMode) {
    Router methodAcceptor = null;
    switch (resourceMethod.getType()) {
      case RESOURCE_METHOD:
      case SUB_RESOURCE_METHOD:
        methodAcceptor = Routers.asTreeAcceptor(createInflector(resourceMethod));
        break;
      case SUB_RESOURCE_LOCATOR:
        methodAcceptor = new SubResourceLocatorRouter(locator, this, resourceMethod);
        break;
    }

    // TODO: solve this via instance-based method handler model?
    if (subResourceMode) {
      return methodAcceptor;
    } else {
      return pushHandlerAcceptorBuilder.build(
          resourceMethod.getInvocable().getHandler(), methodAcceptor);
    }
  }