Don Rhummy
Don Rhummy

Reputation: 25830

How make my ContainerRequestFilter called on @ServerEndpoint onOpen?

I have a web socket @ServerEndpoint that has an annotation which my filter listens to. But my filter is never called (my filter is called on normal JAX-RS resources)

how can i make my filter get called for a web socket endpoint?

@ServerEndpoint("/push/register")
public class WebSocketListener
{
    @OnOpen
    @MyFilter
    public void open(Session session)
    {
        ...
    }
}

The filter:

@MyFilter
@Provider
@Priority( Priorities.AUTHENTICATION )
public class AuthenticationFilter implements ContainerRequestFilter
{
    @Context
    private HttpServletRequest request;

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException
    {
        ...
    }
}

Upvotes: 0

Views: 1065

Answers (1)

cassiomolin
cassiomolin

Reputation: 130917

JAX-RS filters have nothing to do with websocket endpoints and name binding should not work either.


You could implement Filter from the Servlet API using the same URI defined in the @ServerEndpoint annotation, then you'll be able to intercept the handshake request.

However filters won't intercept WebSocket frames.

@WebFilter("/push/register")
public class AuthenticationFilter implements Filter {

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

        ...

        chain.doFilter(request, response);    
    }

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

    }

    @Override
    public void destroy() {

    }
}

Upvotes: 1

Related Questions