If someone is interested, I found a way to make MVC work with sub-resources locators with the help of a CDI InterceptionFactory.
Here's a simplified example:
public class AroundControllerLiteral extends AnnotationLiteral<AroundController> implements AroundController {}
public class ValidationInterceptorBindingLiteral extends AnnotationLiteral<ValidationInterceptorBinding> implements ValidationInterceptorBinding {}
@Path("foo")
public class MyResource {
@Inject
private BeanManager beanManager;
@Path("{bar}")
public Object bar(@PathParam("bar") String bar) {
return wrap(MySubResource.class, new MySubResource(...));
}
private <T> T wrap(Class<T> resourceClass, T resourceInstance) {
InterceptionFactory<T> interceptionFactory = beanManager.createInterceptionFactory(null, resourceClass);
interceptionFactory.configure()
.filterMethods(m -> m.isAnnotationPresent(Controller.class))
.forEach(m -> m.add(new AroundControllerLiteral()).add(new ValidationInterceptorBindingLiteral()));
return interceptionFactory.createInterceptedInstance(resourceInstance);
}
}
public class MySubResource {
MySubResource() {
/* for proxies */
}
public MySubResource(...) {
// initialize dependencies
}
@Controller
@GET
@View(...)
public void get() { ... }
}
The only downside is that I now have a direct dependency on krazo (AroundController and ValidationInterceptorBinding).
The MVC spec could maybe provide a programmatic way to wrap the subresource or obtain a specialized/pre-configured InterceptionFactory.
Another way could be to have an interceptor applied on the main resource that automatically decorate the returned subresource.
WDYT?
Xavier