关于该答案的BalusC's条指示:
How to stream audio/video files such as MP3, MP4, AVI, etc using a Servlet
我将以下Context
元素添加到我的Tomcat server.xml
,以使我的媒体文件可供Tomcat自己的DefaultServlet
使用。
<Context docBase="/home/jwi/media" path="/service/media" />
这就像魅力一样,媒体可以在:
http://localhost:8080/service/media/example.mp4
我的应用程序中的ApplicationPath
(在Jersey 2.x上构建)设置为:@ApplicationPath("service")
。
在该应用程序中,我有一个请求过滤器,用于检查有效用户会话的每个传入请求。
@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());
}
}
}
我的问题是,从不在媒体元素上调用过滤器。因此,当我打开http://localhost:8080/service/media/example.mp4
时,根本不会调用过滤器。
如何将Tomcat的DefaultServlet添加到我的请求过滤器?
答案 0 :(得分:0)
您是否考虑过Servlet Filter
?
@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() {
}
}