View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.cookiepolicy;
23  
24  import javax.faces.component.UIComponentBase;
25  import javax.faces.context.ExternalContext;
26  import javax.faces.context.FacesContext;
27  import javax.servlet.http.Cookie;
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.primefaces.util.LangUtils;
31  
32  /**
33   * <code>CookiePolicy</code> component.
34   *
35   * @author Melloware mellowaredev@gmail.com / Frank Cornelis
36   * @since 11.0.3
37   */
38  public class CookiePolicy extends UIComponentBase {
39  
40      public static final String COOKIE_POLICY_COOKIE_NAME = "CookiePolicy";
41  
42      public static final String COOKIE_POLICY_REQUEST_ATTRIBUTE = CookiePolicy.class.getName() + ".CookiePolicy";
43  
44      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.CookiePolicy";
45      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
46  
47      @Override
48      public String getFamily() {
49          return COMPONENT_FAMILY;
50      }
51  
52      public boolean hasCookiePolicyCookie(final FacesContext context) {
53          final ExternalContext externalContext = context.getExternalContext();
54          final HttpServletRequest httpServletRequest = (HttpServletRequest) externalContext.getRequest();
55          if (null != httpServletRequest.getAttribute(COOKIE_POLICY_REQUEST_ATTRIBUTE)) {
56              return true;
57          }
58          return externalContext.getRequestCookieMap().containsKey(COOKIE_POLICY_COOKIE_NAME);
59      }
60  
61      /**
62       * Checks whether the given cookie policy has been accepted.
63       *
64       * @param cookiePolicy the cookie policy to be verified.
65       * @return <code>true</code> if the cookie policy has been accepted, otherwise <code>false</code>.
66       */
67      public static boolean hasCookiePolicy(final String cookiePolicy) {
68          final FacesContext facesContext = FacesContext.getCurrentInstance();
69          final ExternalContext externalContext = facesContext.getExternalContext();
70  
71          final HttpServletRequest httpServletRequest = (HttpServletRequest) externalContext.getRequest();
72          final String requestCookiePolicy = (String) httpServletRequest.getAttribute(COOKIE_POLICY_REQUEST_ATTRIBUTE);
73          if (null != requestCookiePolicy) {
74              return requestCookiePolicy.contains(cookiePolicy);
75          }
76  
77          final Cookie cookie = (Cookie) externalContext.getRequestCookieMap().get(COOKIE_POLICY_COOKIE_NAME);
78          if (null == cookie) {
79              return false;
80          }
81          final String policy = cookie.getValue();
82          if (LangUtils.isBlank(policy)) {
83              return false;
84          }
85          return policy.contains(cookiePolicy);
86      }
87  
88  }