thymeleafexamples.gtvg.web.filter.GTVGFilter 源代码:
package thymeleafexamples.gtvg.web.filter; import java.io.IOException; import java.io.Writer; import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; import jakarta.servlet.FilterConfig; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.thymeleaf.ITemplateEngine; import org.thymeleaf.TemplateEngine; import org.thymeleaf.templatemode.TemplateMode; import org.thymeleaf.templateresolver.WebApplicationTemplateResolver; import org.thymeleaf.web.IWebApplication; import org.thymeleaf.web.IWebExchange; import org.thymeleaf.web.IWebRequest; import org.thymeleaf.web.servlet.JakartaServletWebApplication; import thymeleafexamples.gtvg.business.entities.User; import thymeleafexamples.gtvg.web.controller.IGTVGController; import thymeleafexamples.gtvg.web.mapping.ControllerMappings; /** * 过滤器 * @author Administrator */ public class GTVGFilter implements Filter { private ITemplateEngine templateEngine; private JakartaServletWebApplication application; public GTVGFilter() { super(); } /** * 添加用户信息到 session * @param request */ private static void addUserToSession(final HttpServletRequest request) { // Simulate a real user session by adding a user object request.getSession(true).setAttribute("user", new User("John", "Apricot", "Antarctica", null)); } /** * 初始化 * @param filterConfig * @throws ServletException */ @Override public void init(final FilterConfig filterConfig) throws ServletException { this.application = JakartaServletWebApplication.buildApplication(filterConfig.getServletContext()); this.templateEngine = buildTemplateEngine(this.application); } /** * 过滤器 * @param request * @param response * @param chain * @throws IOException * @throws ServletException */ @Override public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { addUserToSession((HttpServletRequest)request); if (!process((HttpServletRequest)request, (HttpServletResponse)response)) { chain.doFilter(request, response); } } @Override public void destroy() { // nothing to do } /** * 处理器,根据URL选择合适的处理器 * @param request * @param response * @return * @throws ServletException */ private boolean process(final HttpServletRequest request, final HttpServletResponse response) throws ServletException { try { final IWebExchange webExchange = this.application.buildExchange(request, response); final IWebRequest webRequest = webExchange.getRequest(); // 忽略 css、图片等资源 if (webRequest.getPathWithinApplication().startsWith("/css") || webRequest.getPathWithinApplication().startsWith("/images") || webRequest.getPathWithinApplication().startsWith("/favicon")) { return false; } // 根据请求URL地址匹配合适的处理器 final IGTVGController controller = ControllerMappings.resolveControllerForRequest(webRequest); if (controller == null) { return false; } // 设置响应头 response.setContentType("text/html;charset=UTF-8"); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // 调用处理器 final Writer writer = response.getWriter(); controller.process(webExchange, this.templateEngine, writer); return true; } catch (final Exception e) { try { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } catch (final IOException ignored) { // Just ignore this } throw new ServletException(e); } } /** * 构建模板引擎 * @param application * @return */ private static ITemplateEngine buildTemplateEngine(final IWebApplication application) { final WebApplicationTemplateResolver templateResolver = new WebApplicationTemplateResolver(application); // HTML是默认模式,但为了更好地理解代码,我们将对其进行设置 templateResolver.setTemplateMode(TemplateMode.HTML); // 这会将 “home” 转换为 “/WEB-INF/Templates/home.html” templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html"); // 将模板缓存TTL设置为1小时。如果未设置,条目将驻留在缓存中,直到被LRU驱逐 templateResolver.setCacheTTLMs(Long.valueOf(3600000L)); // 默认情况下,缓存设置为 true。如果希望模板在修改时自动更新,请设置为 false。 templateResolver.setCacheable(false); final TemplateEngine templateEngine = new TemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; } }