govin
govin

Reputation: 6693

Prematch filters in Java resteasy

I have a block of code like this. But it doesn't seem to find the resource method which has the lower cased url specified in @Path("resource-name"). How do I apply this filter before the method on the resource or controller is matched?

public class LowerCaseRequestFilter implements Filter {

    @Inject
    public LowerCaseRequestFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(@Nonnull ServletRequest servletRequest, @Nonnull ServletResponse servletResponse, @Nonnull FilterChain filterChain) throws IOException, ServletException {

        final HttpServletRequest req = (HttpServletRequest) servletRequest;
        final String requestUrl = req.getRequestURI();
        final String newUrl = requestUrl.toLowerCase();
        req.getRequestDispatcher(newUrl).forward(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }

Upvotes: 1

Views: 1146

Answers (1)

lefloh
lefloh

Reputation: 10961

If you are using JAX-RS 2 you can use a @PreMatching ContainerRequestFilter:

@Provider
@PreMatching
public class LowerCaseFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        String newUri = requestContext.getUriInfo().getRequestUri().toString().toLowerCase();
        requestContext.setRequestUri(URI.create(newUri));
    }

}

Upvotes: 2

Related Questions