You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by pa...@apache.org on 2022/10/06 12:32:11 UTC

[myfaces] branch main updated: MYFACES-4447: avoid UnsupportedOperationException

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

paulnicolucci 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 67a9f5280 MYFACES-4447: avoid UnsupportedOperationException
     new 1f8c026e7 Merge pull request #329 from pnicolucci/ExtensionlessMappingFaces40
67a9f5280 is described below

commit 67a9f5280585d62c2f6ca81a44e9e911caa11d0d
Author: Paul Nicolucci <pn...@gmail.com>
AuthorDate: Wed Oct 5 22:44:48 2022 -0400

    MYFACES-4447: avoid UnsupportedOperationException
---
 .../org/apache/myfaces/webapp/FacesInitializerImpl.java  | 10 ++++++----
 .../myfaces/webapp/MyFacesContainerInitializer.java      | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/webapp/FacesInitializerImpl.java b/impl/src/main/java/org/apache/myfaces/webapp/FacesInitializerImpl.java
index 9cc2f89fa..3554265e6 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/FacesInitializerImpl.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/FacesInitializerImpl.java
@@ -23,7 +23,6 @@ import org.apache.myfaces.config.FacesConfigurator;
 import org.apache.myfaces.config.RuntimeConfig;
 import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
 import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
-import org.apache.myfaces.application.FacesServletMappingUtils;
 import org.apache.myfaces.context.ExceptionHandlerImpl;
 import org.apache.myfaces.application.viewstate.StateUtils;
 import org.apache.myfaces.util.WebConfigParamUtils;
@@ -45,6 +44,7 @@ import jakarta.faces.event.PostConstructApplicationEvent;
 import jakarta.faces.event.PreDestroyApplicationEvent;
 import jakarta.faces.event.SystemEvent;
 import jakarta.servlet.ServletContext;
+import jakarta.servlet.ServletRegistration;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.List;
@@ -707,13 +707,15 @@ public class FacesInitializerImpl implements FacesInitializer
      */
     protected void initAutomaticExtensionlessMapping(FacesContext facesContext, ServletContext servletContext)
     {
-        FacesServletMappingUtils.ServletRegistrationInfo facesServletRegistration =
-                FacesServletMappingUtils.getFacesServletRegistration(facesContext, servletContext);
+        ServletRegistration facesServletRegistration =
+                (ServletRegistration) servletContext.getAttribute(
+                    MyFacesContainerInitializer.FACES_SERVLET_SERVLETREGISTRATION);
+
         if (facesServletRegistration != null)
         {
             facesContext.getApplication().getViewHandler().getViews(facesContext, "/", 
                     ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME).forEach(s -> {
-                        facesServletRegistration.getRegistration().addMapping(s);
+                        facesServletRegistration.addMapping(s);
                     });
         }
     }
diff --git a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
index 6c919d215..29a3062ea 100644
--- a/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
+++ b/impl/src/main/java/org/apache/myfaces/webapp/MyFacesContainerInitializer.java
@@ -102,6 +102,16 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
      */
     public static final String FACES_SERVLET_FOUND = "org.apache.myfaces.FACES_SERVLET_FOUND";
 
+    /**
+     * Store the FacesServlet ServletRegistration using this key in the ServletContext.
+     * The is necessary for the Faces 4.0 Extensionless Mapping feature. This is used
+     * in FacesInitializerImpl when configuring the Extensionless Mapping feature since
+     * an UnsupportedOperationException is thrown when calling the ServletContext.getServletRegistrations
+     * method if the StartupServletContextListener was added programmatically.
+     */
+    public static final String FACES_SERVLET_SERVLETREGISTRATION =
+                "org.apache.myfaces.FACES_SERVLET_SERVLETREGISTRATION";
+
     private static final String FACES_CONFIG_RESOURCE = "/WEB-INF/faces-config.xml";
     private static final Logger log = Logger.getLogger(MyFacesContainerInitializer.class.getName());
     private static final String[] FACES_SERVLET_MAPPINGS = { "/faces/*", "*.jsf", "*.faces" };
@@ -178,6 +188,9 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
                     // added the mapping dynamically.
                     servletContext.setAttribute(FACES_SERVLET_ADDED_ATTRIBUTE, Boolean.TRUE);
 
+                    // Add the FacesServlet ServletRegistration as an attribute for use during initialization.
+                    servletContext.setAttribute(FACES_SERVLET_SERVLETREGISTRATION, servlet);
+
                     // add a log message
                     log.log(Level.INFO, "Added FacesServlet with mappings=" + Arrays.toString(mappings));
                 }
@@ -321,6 +334,9 @@ public class MyFacesContainerInitializer implements ServletContainerInitializer
                 // we found a FacesServlet; set an attribute for use during initialization
                 servletContext.setAttribute(FACES_SERVLET_FOUND, Boolean.TRUE);
                 isFacesServletPresent = true;
+
+                // Add the FacesServlet ServletRegistration as an attribute for use during initialization.
+                servletContext.setAttribute(FACES_SERVLET_SERVLETREGISTRATION, servletEntry.getValue());
             }
         }
         return isFacesServletPresent;