0

Following BalusC's instructions on that answer:

How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet

I added the following Context element to my Tomcat server.xml to make my media files available to Tomcat's own DefaultServlet.

<Context docBase="/home/jwi/media" path="/service/media" />

This works like charm and the media is available at:

http://localhost:8080/service/media/example.mp4

The ApplicationPath from my application (build on Jersey 2.x) is set to: @ApplicationPath("service").

Within that application I have a request filter that checks every incoming request for a valid user session.

@Provider
@PreMatching
@Priority(1)
public class SessionFilter implements ContainerRequestFilter {

    @Context
    private ServletContext _context;
    @Context
    private HttpServletRequest _request;
    @Context
    private HttpServletResponse _response;

    public void filter(ContainerRequestContext requestContext) throws IOException {

        HttpSession session = _request.getSession(false);
        boolean isLoggedIn = session != null && session.getAttribute("username") != null;
        boolean isLoginRequest = _request.getRequestURI().contains("login");

        if (isLoggedIn || isLoginRequest) {
            // Since filter chain is invoked by @Priority annotation here's nothing to do.
        } else {
            URI indexPage = UriBuilder.fromUri("/index.html").build();
            requestContext.abortWith(Response.temporaryRedirect(indexPage).build());
        }
    }
}

My problem is, that filter is never called on the media elements. So when I open http://localhost:8080/service/media/example.mp4 the filter isn't called at all.

How do I add Tomcat's DefaultServlet to my request filter?

jwi
  • 1,116
  • 1
  • 18
  • 38

1 Answers1

0

Have you considered a Servlet Filter instead?

@WebFilter("/service/media/*")
public class SessionFilter implements Filter {

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

    }

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

        ...

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
} 
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
  • That's why I mentioned the @ApplicationPath Annoation in my issue above. The ApplicationPath is set to 'service' and the media path in server.xml is also set to '/service/media'. Normaly the RequestFilter should catch every request starting with 'service' as request uri. – jwi Aug 03 '17 at 13:22
  • @jwi I think I misread your question. Have you tried the Servlet `Filter`? – cassiomolin Aug 03 '17 at 13:24
  • I thought it would be just fine if the media path inside the server.xml starts with the ApplicationPath from my JAX-RS application - this doesn't work. I'll try now your advice with the Servlet Filter. ;) – jwi Aug 03 '17 at 13:26
  • Implemented the WebFilter but the requests still don't get caught by the filter. :/ – jwi Aug 04 '17 at 10:59