From 3ac0886607db0c8fc85126e8fde8a226d2c9ee4f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 25 Sep 2022 22:43:26 +0200 Subject: [PATCH] Please cherry-pick: - no need for local variable "isAjax" MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../filter/AddressbookNoCacheFilter.java | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/java/org/mxchange/addressbook/filter/AddressbookNoCacheFilter.java diff --git a/src/java/org/mxchange/addressbook/filter/AddressbookNoCacheFilter.java b/src/java/org/mxchange/addressbook/filter/AddressbookNoCacheFilter.java new file mode 100644 index 00000000..5d99053e --- /dev/null +++ b/src/java/org/mxchange/addressbook/filter/AddressbookNoCacheFilter.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2022 Free Software Foundation + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.addressbook.filter; + +import java.io.IOException; +import javax.faces.application.ResourceHandler; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * A filter for no-caching of JSF requests send from client. This class was + * taken from stackoverflow.com at + * https://stackoverflow.com/questions/3642919/javax-faces-application-viewexpiredexception-view-could-not-be-restored + * and was slightly expanded with "final" keyword and comments + *

+ * @author Bauke "BalusC" Scholz + * @author Roland Häder + */ +@WebFilter (servletNames = {"Faces Servlet"}) +public class AddressbookNoCacheFilter implements Filter { + + @Override + public void destroy () { + System.out.println("destroy!"); //NOI18N + } + + @Override + public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException { + // Cast parameter + final HttpServletRequest req = (HttpServletRequest) request; + final HttpServletResponse res = (HttpServletResponse) response; + + // Is the request URI not starting with resource identifier? + if (!req.getPathInfo().startsWith(ResourceHandler.RESOURCE_IDENTIFIER)) { + // Set non-caching header + res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //NOI18N + res.setHeader("Pragma", "no-cache"); // //NOI18N + res.setDateHeader("Expires", 0); // //NOI18N + + // Is AJAX? + if ("true".equals(req.getParameter("javax.faces.partial.ajax"))) { + // Set Accept header + res.setHeader("Accept", "text/xml"); //NOI18N + } + } + + // Continue filtering + chain.doFilter(request, response); + } + + @Override + public void init (final FilterConfig filterConfig) throws ServletException { + System.out.println("init!"); //NOI18N + } + +} -- 2.39.5