You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by lu...@apache.org on 2017/04/06 21:35:32 UTC

svn commit: r1790469 - in /myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces: context/servlet/StartupServletExternalContextImpl.java webapp/AbstractFacesInitializer.java

Author: lu4242
Date: Thu Apr  6 21:35:31 2017
New Revision: 1790469

URL: http://svn.apache.org/viewvc?rev=1790469&view=rev
Log:
MYFACES-4105 Implement extensionless mapping of views (add web config param to enable automatic extensionless mapping)

Modified:
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
    myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java

Modified: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java?rev=1790469&r1=1790468&r2=1790469&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/context/servlet/StartupServletExternalContextImpl.java Thu Apr  6 21:35:31 2017
@@ -45,11 +45,13 @@ public class StartupServletExternalConte
     public static final String EXCEPTION_TEXT = "This method is not supported during ";
     
     private boolean _startup;
+    private ServletContext _servletContext;
     
     public StartupServletExternalContextImpl(final ServletContext servletContext,
             boolean startup)
     {
         super(servletContext);
+        _servletContext = servletContext;
         _startup = startup;
     }
     
@@ -103,7 +105,7 @@ public class StartupServletExternalConte
     @Override
     public String getRequestContextPath()
     {
-        throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
+        return _servletContext.getContextPath();
     }
 
     @Override
@@ -163,13 +165,13 @@ public class StartupServletExternalConte
     @Override
     public String getRequestPathInfo()
     {
-        throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
+        return "";
     }
 
     @Override
     public String getRequestServletPath()
     {
-        throw new UnsupportedOperationException(EXCEPTION_TEXT + _getTime());
+        return "";
     }
 
     @Override

Modified: myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java
URL: http://svn.apache.org/viewvc/myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java?rev=1790469&r1=1790468&r2=1790469&view=diff
==============================================================================
--- myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java (original)
+++ myfaces/core/branches/2.3.x/impl/src/main/java/org/apache/myfaces/webapp/AbstractFacesInitializer.java Thu Apr  6 21:35:31 2017
@@ -27,6 +27,8 @@ import org.apache.myfaces.config.element
 import org.apache.myfaces.context.ReleaseableExternalContext;
 import org.apache.myfaces.context.servlet.StartupFacesContextImpl;
 import org.apache.myfaces.context.servlet.StartupServletExternalContextImpl;
+import org.apache.myfaces.shared.application.FacesServletMappingUtils;
+import org.apache.myfaces.shared.context.ExceptionHandlerImpl;
 import org.apache.myfaces.shared.util.StateUtils;
 import org.apache.myfaces.shared.util.WebConfigParamUtils;
 import org.apache.myfaces.cdi.dependent.BeanEntry;
@@ -59,14 +61,15 @@ import java.util.Locale;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
+import javax.faces.application.ViewVisitOption;
 import javax.faces.push.PushContext;
+import javax.servlet.ServletRegistration;
 import javax.websocket.DeploymentException;
 import javax.websocket.server.ServerContainer;
 import javax.websocket.server.ServerEndpointConfig;
 import org.apache.myfaces.push.EndpointImpl;
 import org.apache.myfaces.push.WebsocketConfigurator;
 import org.apache.myfaces.push.WebsocketFacesInit;
-import org.apache.myfaces.shared.context.ExceptionHandlerImpl;
 import org.apache.myfaces.shared.util.ClassUtils;
 import org.apache.myfaces.spi.ServiceProviderFinder;
 import org.apache.myfaces.spi.ServiceProviderFinderFactory;
@@ -112,6 +115,14 @@ public abstract class AbstractFacesIniti
     public static final String INIT_PARAM_LOG_WEB_CONTEXT_PARAMS = "org.apache.myfaces.LOG_WEB_CONTEXT_PARAMS";
     public static final String INIT_PARAM_LOG_WEB_CONTEXT_PARAMS_DEFAULT ="auto";
     
+    /**
+     * This parameter enables automatic extensionless mapping for all JSF views.
+     */
+    @JSFWebConfigParam(since="2.3", expectedValues = "true, false", defaultValue = "false")
+    public static final String INIT_PARAM_AUTOMATIC_EXTENSIONLESS_MAPPING = 
+            "org.apache.myfaces.AUTOMATIC_EXTENSIONLESS_MAPPING";
+    public static final boolean INIT_PARAM_AUTOMATIC_EXTENSIONLESS_MAPPING_DEFAULT = false;
+    
     public static final String CDI_BEAN_MANAGER_INSTANCE = "oam.cdi.BEAN_MANAGER_INSTANCE";
     
     private static final String CDI_SERVLET_CONTEXT_BEAN_MANAGER_ATTRIBUTE = 
@@ -218,6 +229,14 @@ public abstract class AbstractFacesIniti
             
             //Start ViewPoolProcessor if necessary
             ViewPoolProcessor.initialize(facesContext);
+            
+            Boolean automaticExtensionlessMapping = WebConfigParamUtils.getBooleanInitParameter(
+                    externalContext, INIT_PARAM_AUTOMATIC_EXTENSIONLESS_MAPPING, 
+                    INIT_PARAM_AUTOMATIC_EXTENSIONLESS_MAPPING_DEFAULT);
+            if (Boolean.TRUE.equals(automaticExtensionlessMapping))
+            {
+                initAutomaticExtensionlessMapping(facesContext, servletContext);
+            }
 
             // print out a very prominent log message if the project stage is != Production
             if (!facesContext.isProjectStage(ProjectStage.Production) &&
@@ -774,4 +793,40 @@ public abstract class AbstractFacesIniti
             }
         }
     }
+    
+    /**
+     * 
+     * @since 2.3
+     * @param facesContext 
+     */
+    protected void initAutomaticExtensionlessMapping(FacesContext facesContext, ServletContext servletContext)
+    {
+        final ServletRegistration facesServletRegistration = getFacesServletRegistration(facesContext, servletContext); 
+        if (facesServletRegistration != null)
+        {
+            facesContext.getApplication().getViewHandler().getViews(facesContext, "/", 
+                    ViewVisitOption.RETURN_AS_MINIMAL_IMPLICIT_OUTCOME).forEach(s -> {
+                        facesServletRegistration.addMapping(s);
+                    });
+        }
+    }
+    
+    private ServletRegistration getFacesServletRegistration(FacesContext facesContext, 
+            ServletContext servletContext)
+    {
+        ServletRegistration facesServletRegistration = null;
+        Map<String, ? extends ServletRegistration> map = servletContext.getServletRegistrations();
+        if (map != null)
+        {
+            for (Map.Entry<String, ? extends ServletRegistration> entry : map.entrySet())
+            {
+                if (FacesServletMappingUtils.isFacesServlet(facesContext, entry.getValue().getClassName()))
+                {
+                    facesServletRegistration = entry.getValue();
+                    break;
+                }
+            }
+        }
+        return facesServletRegistration;
+    }
 }