]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/filter/PizzaApplicationNoCacheFilter.java
ed48dc2fac638e83b059b5725c057db962985c17
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / filter / PizzaApplicationNoCacheFilter.java
1 /*
2  * Copyright (C) 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.pizzaapplication.filter;
18
19 import java.io.IOException;
20 import javax.faces.application.ResourceHandler;
21 import javax.servlet.Filter;
22 import javax.servlet.FilterChain;
23 import javax.servlet.FilterConfig;
24 import javax.servlet.ServletException;
25 import javax.servlet.ServletRequest;
26 import javax.servlet.ServletResponse;
27 import javax.servlet.annotation.WebFilter;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30
31 /**
32  * A filter for no-caching of JSF requests send from client. This class was
33  * taken from stackoverflow.com at
34  * https://stackoverflow.com/questions/3642919/javax-faces-application-viewexpiredexception-view-could-not-be-restored
35  * and was slightly expanded with "final" keyword and comments
36  * <p>
37  * @author Bauke "BalusC" Scholz<unknown@email.invalid>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @WebFilter (servletNames = {"Faces Servlet"})
41 public class PizzaApplicationNoCacheFilter implements Filter {
42
43         @Override
44         public void destroy () {
45                 System.out.println("destroy!"); //NOI18N
46         }
47
48         @Override
49         public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
50                 // Cast parameter
51                 final HttpServletRequest req = (HttpServletRequest) request;
52                 final HttpServletResponse res = (HttpServletResponse) response;
53
54                 // Is the request URI not starting with resource identifier?
55                 if (!req.getPathInfo().startsWith(ResourceHandler.RESOURCE_IDENTIFIER)) {
56                         // Set non-caching header
57                         res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); //NOI18N
58                         res.setHeader("Pragma", "no-cache"); // //NOI18N
59                         res.setDateHeader("Expires", 0); // //NOI18N
60
61                         // Is AJAX?
62                         if ("true".equals(req.getParameter("javax.faces.partial.ajax"))) {
63                                 // Set Accept header
64                                 res.setHeader("Accept", "text/xml"); //NOI18N
65                         }
66                 }
67
68                 // Continue filtering
69                 chain.doFilter(request, response);
70         }
71
72         @Override
73         public void init (final FilterConfig filterConfig) throws ServletException {
74                 System.out.println("init!"); //NOI18N
75         }
76
77 }