According to the documentation it should be possible to use DynamicFeature
for resources and sub-resources. As an effect I expect to have filter registered and called whenever sub-resource endpoint is being called - ie GET /some/sub/x
. However following is not working for me (nor other similar approaches):
annotation
@NameBinding @Target({METHOD, TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface SomeFilterAnn {}
resource
@SomeFilterAnn @Path("/some") public class SomeResource { @GET public String getSome() { return "some"; } @Path("/sub") public SubResource getSub() { return new SubResource(); } @SomeFilterAnn @Path("/x") public static class SubResource { @GET public String getSome() { return "sub"; } } }
filter (it is actually more complex and configured by
DynamicFilter
)@SomeFilterAnn public class SomeFilter implements ContainerRequestFilter { private static final Logger LOG = LoggerFactory.getLogger(SomeFilter.class); @Override public void filter(ContainerRequestContext requestContext) throws IOException { LOG.info("filter called"); } }
here is
DynamicFeature
that I'd like to register filter for resource and sub-resource. Actually I would expect thatconfigure()
would be called also forSubResource
soresourceInfo.getResourceClass() == SubResource.class
but cannot manage to achieve this.@Provider public class SomeDynamicFeature implements DynamicFeature { private static final Logger LOG = LoggerFactory.getLogger(SomeDynamicFeature.class); @Override public void configure(ResourceInfo resourceInfo, FeatureContext context) { Class<?> resourceClass = resourceInfo.getResourceClass(); // this works if (resourceClass == SomeResource.class) { LOG.info("register resource filter"); context.register(new SomeFilter(/*some config*/)); } // this does not work (not called for Subresource) if (resourceClass == SomeResource.SubResource.class) { LOG.info("register resource filter"); context.register(new SomeFilter(/*some config*/)); } // this also does not work (filter not called for Subresource) if (resourceInfo.getResourceMethod().getReturnType() == SomeResource.SubResource.class) { LOG.info("register subresource filter"); context.register(new SomeFilter(/*some config*/)); } } }
I'm running this on WildFly 8.2 with CXF and without Jersey or RESTEasy
EDIT
I have found that annotating SomeFilter
with @Provider
allows filter to works without need of DynamicFeature
however I don't know then how to configure my filter - here is separate question about it.