I've created JAX-RS @NameBinding
annotation:
@Retention(RUNTIME)
@NameBinding
public @interface EnableMyFilter {
@Nonbinding
String value() default "";
}
and use this to activate filter, which checks value of @EnableMyFilter
annotation. This part is crucial to me, as creating separate annotations and filters would produce much of boilerplate code. Filter code:
@Provider
@EnableMyFilter
public class MyFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
final ExtendedUriInfo extendendUriInfo = (ExtendedUriInfo) requestContext.getUriInfo();
Method method = extendendUriInfo.getMatchedResourceMethod().getInvocable().getHandlingMethod();
RestrictedEnvironment annotation = method.getAnnotation(EnableMyFilter.class); // get method-level annotation
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(EnableMyFilter.class); // get class-level annotation
if (annotation == null) {
// here application-level annotation should be get
}
}
// do something with annotation value...
}
}
As you may see, I'm using Jersey specific ExtendedUriInfo to reflect method and get annotation (as suggested here), and if it fails, get annotation from class. But @NameBinding
can be done also on application-level, e.g.:
@ApplicationPath("/")
@EnableMyFilter("some-val")
public class RestApplication extends Application {
}
and with this application class provider will also be triggered - which is 100% good. But how can I get this class annotations in filter()?
I've tried to inject @Context
@Context
private Application application;
but application.getClass().toString()
returns org.glassfish.jersey.server.ResourceConfig$WrappingResourceConfig
(what I understand) with no annotations.
I'm working on Glassfish 4.1 build 13, Java 8.