You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ta...@apache.org on 2022/11/28 09:23:31 UTC

[myfaces] branch main updated: MYFACES-4510 Pre Servlet-6 compatibility

This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/main by this push:
     new 7b0d4d601 MYFACES-4510 Pre Servlet-6 compatibility
7b0d4d601 is described below

commit 7b0d4d601ab2ac8942e178412901c1a35821ea8a
Author: Thomas Andraschko <ta...@apache.org>
AuthorDate: Mon Nov 28 10:23:23 2022 +0100

    MYFACES-4510 Pre Servlet-6 compatibility
---
 .../org/apache/myfaces/context/flash/FlashImpl.java | 10 ++++++----
 .../lifecycle/clientwindow/ClientConfig.java        | 10 ++++++----
 .../apache/myfaces/util/ExternalSpecifications.java | 21 +++++++++++++++++++++
 3 files changed, 33 insertions(+), 8 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/context/flash/FlashImpl.java b/impl/src/main/java/org/apache/myfaces/context/flash/FlashImpl.java
index 203d2ce08..408fc9454 100644
--- a/impl/src/main/java/org/apache/myfaces/context/flash/FlashImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/context/flash/FlashImpl.java
@@ -45,6 +45,7 @@ import jakarta.faces.event.PostPutFlashValueEvent;
 import jakarta.faces.event.PreClearFlashEvent;
 import jakarta.faces.event.PreRemoveFlashValueEvent;
 import jakarta.faces.lifecycle.ClientWindow;
+import org.apache.myfaces.util.ExternalSpecifications;
 
 import org.apache.myfaces.util.lang.StringUtils;
 import org.apache.myfaces.util.token.TokenGenerator;
@@ -1188,13 +1189,14 @@ public class FlashImpl extends Flash implements ReleasableFlash
         cookie.setSecure(externalContext.isSecure());
         cookie.setHttpOnly(true);
         Object context = externalContext.getContext();
-        String sameSite = null;
-        if (context instanceof ServletContext)
+
+        if (context instanceof ServletContext && ExternalSpecifications.isServlet6Available())
         {
             ServletContext servletContext = (ServletContext)context;
-            sameSite = servletContext.getSessionCookieConfig().getAttribute("SameSite");
+            String sameSite = servletContext.getSessionCookieConfig().getAttribute("SameSite");
+            cookie.setAttribute("SameSite", Objects.toString(sameSite, "Strict"));
         }
-        cookie.setAttribute("SameSite", Objects.toString(sameSite, "Strict"));
+
         return cookie;
     }
 
diff --git a/impl/src/main/java/org/apache/myfaces/lifecycle/clientwindow/ClientConfig.java b/impl/src/main/java/org/apache/myfaces/lifecycle/clientwindow/ClientConfig.java
index 35f31430c..231c7346d 100644
--- a/impl/src/main/java/org/apache/myfaces/lifecycle/clientwindow/ClientConfig.java
+++ b/impl/src/main/java/org/apache/myfaces/lifecycle/clientwindow/ClientConfig.java
@@ -27,6 +27,7 @@ import jakarta.servlet.http.Cookie;
 import jakarta.servlet.http.HttpServletResponse;
 import java.util.Map;
 import java.util.Objects;
+import org.apache.myfaces.util.ExternalSpecifications;
 
 /**
  * Contains information about whether the user has
@@ -106,13 +107,14 @@ public class ClientConfig implements Serializable
                 Cookie cookie = new Cookie(COOKIE_NAME_NOSCRIPT_ENABLED, "" + javaScriptEnabled);
                 cookie.setPath("/"); // for all the server
                 Object context = facesContext.getExternalContext().getContext();
-                String sameSite = null;
-                if (context instanceof ServletContext)
+
+                if (context instanceof ServletContext && ExternalSpecifications.isServlet6Available())
                 {
                     ServletContext servletContext = (ServletContext)context;
-                    sameSite = servletContext.getSessionCookieConfig().getAttribute("SameSite");
+                    String sameSite = servletContext.getSessionCookieConfig().getAttribute("SameSite");
+                    cookie.setAttribute("SameSite", Objects.toString(sameSite, "Strict"));
                 }
-                cookie.setAttribute("SameSite", Objects.toString(sameSite, "Strict"));
+
                 HttpServletResponse response = (HttpServletResponse) r;
                 response.addCookie(cookie);
             }
diff --git a/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java b/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
index 69f46d4d3..d9310b2ed 100644
--- a/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
+++ b/impl/src/main/java/org/apache/myfaces/util/ExternalSpecifications.java
@@ -135,6 +135,22 @@ public final class ExternalSpecifications
         return available;
     });
 
+    private static Lazy<Boolean> sevlet6Available = new Lazy<>(() ->
+    {
+        boolean available;
+        try
+        {
+            available = jakarta.servlet.SessionCookieConfig.class.getMethod("getAttribute", String.class) != null;
+        }
+        catch (Throwable t)
+        {
+            available = false;
+        }
+        log.info("MyFaces Core Servlet 6.0 support " + (available ? "enabled" : "disabled"));
+
+        return available;
+    });
+    
     /**
      * This method determines if Bean Validation is present.
      *
@@ -163,6 +179,11 @@ public final class ExternalSpecifications
         return sevlet4Available.get();
     }
 
+    public static boolean isServlet6Available()
+    {
+        return sevlet6Available.get();
+    }
+
     /**
      * this class should not be instantiated.
      */