According to the documentation for resources and sub-resources, it should be possible to use DynamicFeature
. As an effect, I expect the filter to be registered and called up whenever the endpoint of the subresource is called - i.e. GET /some/sub/x
. However, the following does not work for me (and 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";
}
}
}
(it is more complex and customizable 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 DynamicFeature
, that I would like to register a filter for the resource and sub-resource. In fact, I would expect it to configure()
be called also for SubResource
, therefore resourceInfo.getResourceClass() == SubResource.class
, but it will not be able 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();
if (resourceClass == SomeResource.class) {
LOG.info("register resource filter");
context.register(new SomeFilter());
}
if (resourceClass == SomeResource.SubResource.class) {
LOG.info("register resource filter");
context.register(new SomeFilter());
}
if (resourceInfo.getResourceMethod().getReturnType() == SomeResource.SubResource.class) {
LOG.info("register subresource filter");
context.register(new SomeFilter());
}
}
}
I run this on WildFly 8.2 with CXF and without Jersey or RESTEasy
EDIT
, SomeFilter
@Provider
DynamicFeature
, , - .