vendredi 5 janvier 2018

Spring - get Method for a given request in Interceptor/Filter

Various @RequestMapping methods inside my @Controller classes have custom annotations that I want to analyze at runtime.

E.g:

@Controller
@RequestMapping("/bla")
@RequireCommunityLevel("...")
@AnotherCustomAnnotation(value = "...")
public class FakeController {

    // ...
    @RequireScore(level = Score.Platinum, score = 8500)
    @RequestMapping("/blabla")
    @AnotherCustomAnnotation(value = "...")
    public DTOResponse<MySpecialModel> getMySpecialModels() {
        // ...
    }

}

When I receive a request in my custom Interceptor and Filter classes, I want to analyze which Method the request is going to call.

I have worked in the past with custom frameworks based on Javax Servlet and Jetty and often implemented some kind of method-registry one could interrogate.

For example:

public class CustomFilter extends Filter {

    @Inject
    private MyMethodRegistry registry;

    @Override
    public void doFilter(
      ServletRequest request, 
      ServletResponse response,
      FilterChain chain) throws IOException, ServletException {

        // get the request path
        final String path = getPath(request); 

        final Method m = this.registry.getMethodForPath(path);

        // that's what I mean to do
        if (m.getAnnotation(RequireScore.class) != null) { ... }

        if (m.getDeclaringClass().getAnnotation(AnotherCustomAnnotation.class != null) { ... }

        chain.doFilter(request, response);
    }
}

I'm looking for a similar way in Spring, possibly a built-in utility to analyze at runtime the Annotation's on my @RequestMapping methods. All of this should happen within my Interceptor/Filter.





Aucun commentaire:

Enregistrer un commentaire