2

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.

Community
  • 1
  • 1
Radzikowski
  • 2,377
  • 4
  • 27
  • 39
  • If you need some arbitrary application level configuration value, maybe just set a configuration property, and inject [`Configuration`](http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/Configuration.html) to get the value. You can override `public Map getProperties()` in the `Application` class. The values in that map will be available in the `Configuration` – Paul Samsotha Mar 27 '15 at 02:44
  • @peeskillet As [doc](http://docs.oracle.com/javaee/7/api/javax/ws/rs/NameBinding.html) says NameBinding annotation can be attached to Application subclass. I want to make lib for JAX-RS extension, to manage app security, environment settings and more. So your suggestions do not apply here. – Radzikowski Mar 27 '15 at 07:32
  • I see. Thanks for the link. I'm not sure I see the point of it, as not annotating the resources will cause the filter to be used anyway. In any case, are you saying the using the explicit `RestApplication.class` will not work? Can you give an example of why it would not. I'm trying to get a better understanding of what it is you're trying to do exactly – Paul Samsotha Mar 27 '15 at 07:40
  • Nevermind. I think I see what you're saying. This library will not include the `RestApplication` class, am I right? It's just an example of a class some user of your library will create. Am I correct? – Paul Samsotha Mar 27 '15 at 07:46
  • I don't know. I've been playing around with this idea, and a couple ways I can see it working if either the user explicitly defines the class as a configuration property or if you scan the classpath (on load) and look for the Application subclass, and add it yourself as a configuration property. You might offer an optimization to explicitly define the Application subclass as a configuration property – Paul Samsotha Mar 27 '15 at 08:57
  • As an aside, you can inject the JAX-RS standard `ResourceInfo`, if you don't want to tie this lib to Jersey. See [here](http://stackoverflow.com/a/29201811/2587435) – Paul Samsotha Mar 27 '15 at 09:26
  • So it looks like there is no easy way to achieve this. It's sad. But thank you for the ideas and for link about using ResourceInfo. – Radzikowski Mar 27 '15 at 12:49

0 Answers0