You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by jw...@apache.org on 2014/01/24 15:19:25 UTC

svn commit: r1561022 - in /myfaces/trinidad/trunk/trinidad-impl/src/main: java/org/apache/myfaces/trinidadinternal/config/xmlHttp/ java/org/apache/myfaces/trinidadinternal/webapp/ xrts/org/apache/myfaces/trinidadinternal/resource/

Author: jwaldman
Date: Fri Jan 24 14:19:25 2014
New Revision: 1561022

URL: http://svn.apache.org/r1561022
Log:
TRINIDAD-2445 Prevent exceptions from propagating out of the ServletFilter
thanks to Arjun Vade for patch

Modified:
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/xmlHttp/XmlHttpConfigurator.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java
    myfaces/trinidad/trunk/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/xmlHttp/XmlHttpConfigurator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/xmlHttp/XmlHttpConfigurator.java?rev=1561022&r1=1561021&r2=1561022&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/xmlHttp/XmlHttpConfigurator.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/config/xmlHttp/XmlHttpConfigurator.java Fri Jan 24 14:19:25 2014
@@ -26,6 +26,10 @@ import java.lang.reflect.InvocationTarge
 import java.lang.reflect.Proxy;
 
 import javax.faces.FacesException;
+import javax.faces.FactoryFinder;
+import javax.faces.application.Application;
+import javax.faces.application.ApplicationFactory;
+import javax.faces.application.ProjectStage;
 import javax.faces.context.ExternalContext;
 
 import javax.servlet.ServletException;
@@ -101,8 +105,6 @@ public class XmlHttpConfigurator extends
 
   /**
    * Handle a server-side error by reporting it back to the client.
-   * TODO: add configuration to hide this in a production
-   * environment.
    */
   public static void handleError(ExternalContext ec, 
                                  Throwable t) throws IOException
@@ -119,9 +121,22 @@ public class XmlHttpConfigurator extends
     rw.startElement("error-name", null);
     rw.writeText(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
     rw.endElement("error-name");
+
+    String errorMessage = _getErrorMessage(error);
+
+    // Default exception message contains the type of the exception.
+    // Do not send this info to client in Production mode
+    ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
+    Application application = factory.getApplication();
+    if (application.getProjectStage() != ProjectStage.Production)
+    {
+      errorMessage = _getExceptionString(t) + errorMessage;
+    }
+
     rw.startElement("error-message", null);
-    rw.writeText(_getExceptionString(t) + _PLEASE_SEE_ERROR_LOG + error, null);
+    rw.writeText(errorMessage, null);
     rw.endElement("error-message");
+
     rw.endElement("error");
     rw.endElement("partial-response");
     rw.endDocument();
@@ -161,7 +176,12 @@ public class XmlHttpConfigurator extends
 
   static private String _getErrorString()
   {
-    return _PPR_ERROR_PREFIX + _getErrorCount();
+    return _LOG.getMessage("EXC_PPR_ERROR_PREFIX", _getErrorCount());
+  }
+
+  static private String _getErrorMessage(String error)
+  {
+    return _LOG.getMessage("EXC_PLEASE_SEE_ERROR_LOG", error);
   }
 
   static private synchronized int _getErrorCount()
@@ -169,12 +189,6 @@ public class XmlHttpConfigurator extends
     return (++_ERROR_COUNT);
   }
 
-  static private final String _PPR_ERROR_PREFIX = "Server Exception during PPR, #";
-  // TODO Get this from a resource bundle?
-  static private final String _PLEASE_SEE_ERROR_LOG =
-    "For more information, please see the server's error log for\n" +
-    "an entry beginning with: ";
-
   static private int _ERROR_COUNT = 0;
 
   static private final TrinidadLogger _LOG =

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java?rev=1561022&r1=1561021&r2=1561022&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/webapp/TrinidadFilterImpl.java Fri Jan 24 14:19:25 2014
@@ -169,88 +169,112 @@ public class TrinidadFilterImpl implemen
     // provide a (Pseudo-)FacesContext for configuration tasks
     PseudoFacesContext facesContext = new PseudoFacesContext(externalContext);
     facesContext.setAsCurrentInstance();
-    
-    GlobalConfiguratorImpl config = GlobalConfiguratorImpl.getInstance();
-    config.beginRequest(externalContext);
-    
-    // Allow Configurators to wrap response and request
-    request = (ServletRequest)externalContext.getRequest();
-    response = (ServletResponse)externalContext.getResponse();
-    
-    String noJavaScript = request.getParameter(XhtmlConstants.NON_JS_BROWSER);
-        
-    // Wrap the request only for Non-javaScript browsers
-    if(noJavaScript != null &&
-              XhtmlConstants.NON_JS_BROWSER_TRUE.equals(noJavaScript))
-    {
-      request = new BasicHTMLBrowserRequestWrapper((HttpServletRequest)request);
-    } 
 
-    //To maintain backward compatibilty, wrap the request at the filter level
-    Map<String, String[]> addedParams = FileUploadConfiguratorImpl.getAddedParameters(externalContext);
-    
-    if(addedParams != null)
+    // for error handling
+    boolean isPartialRequest=false;
+
+    GlobalConfiguratorImpl config;
+    try
     {
-      FileUploadConfiguratorImpl.apply(externalContext);
-      request = new UploadRequestWrapper((HttpServletRequest)request, addedParams);
+      isPartialRequest = facesContext.getPartialViewContext().isAjaxRequest();
+      config = GlobalConfiguratorImpl.getInstance();
+      config.beginRequest(externalContext);
+    }
+    catch(Throwable t)
+    {
+      facesContext.release();
+      _handleException(externalContext, isPartialRequest, t);
+      return;
     }
-    
-    boolean responseComplete = facesContext.getResponseComplete();
-
-    // release the PseudoFacesContext, since _doFilterImpl() has its own FacesContext
-    facesContext.release();
 
     try
     {
+      boolean responseComplete=false;
+      try
+      {
+        // Allow Configurators to wrap response and request
+        request = (ServletRequest)externalContext.getRequest();
+        response = (ServletResponse)externalContext.getResponse();
+
+        String noJavaScript = request.getParameter(XhtmlConstants.NON_JS_BROWSER);
+
+        // Wrap the request only for Non-javaScript browsers
+        if(noJavaScript != null &&
+                  XhtmlConstants.NON_JS_BROWSER_TRUE.equals(noJavaScript))
+        {
+          request = new BasicHTMLBrowserRequestWrapper((HttpServletRequest)request);
+        }
+
+        //To maintain backward compatibilty, wrap the request at the filter level
+         Map<String, String[]> addedParams = FileUploadConfiguratorImpl.getAddedParameters(externalContext);
+
+        if(addedParams != null)
+        {
+          FileUploadConfiguratorImpl.apply(externalContext);
+          request = new UploadRequestWrapper((HttpServletRequest)request, addedParams);
+          isPartialRequest = CoreRenderKit.isPartialRequest(addedParams);
+        }
+
+        responseComplete = facesContext.getResponseComplete();
+      }
+      finally
+      {
+        // release the PseudoFacesContext, since _doFilterImpl() has its own FacesContext
+        facesContext.release();
+      }
+
       // Abort processing if the response has been completed by Configurator
       if (!responseComplete)
       {
         _doFilterImpl(request, response, chain);
       }
     }
-    // For PPR errors, handle the request specially
     catch (Throwable t)
     {
-      boolean isPartialRequest;
-      if (addedParams != null)
-      {
-        isPartialRequest = CoreRenderKit.isPartialRequest(addedParams);
-      }
-      else
+      _handleException(externalContext, isPartialRequest, t);
+    }
+    finally
+    {
+      try
       {
-        isPartialRequest = CoreRenderKit.isPartialRequest(externalContext);
+        config.endRequest(externalContext);
       }
-
-      if (isPartialRequest)
+      catch(Throwable t)
       {
-        XmlHttpConfigurator.handleError(externalContext, t);
-      }
-      else
-      {
-        // For non-partial requests, just re-throw.  It is not
-        // our responsibility to catch these
-        if (t instanceof RuntimeException)
-          throw ((RuntimeException) t);
-        if (t instanceof Error)
-          throw ((Error) t);
-        if (t instanceof IOException)
-          throw ((IOException) t);
-        if (t instanceof ServletException)
-          throw ((ServletException) t);
-
-        // Should always be one of those four types to have
-        // gotten here.
-        _LOG.severe(t);
+        _handleException(externalContext, isPartialRequest, t);
       }
+    }
+  }
 
+  /**
+   * For PPR errors, handle the request specially
+   */
+  private static void _handleException(ExternalContext externalContext, boolean isPartialRequest, Throwable t)
+    throws IOException, ServletException
+  {
+    if (isPartialRequest)
+    {
+      XmlHttpConfigurator.handleError(externalContext, t);
     }
-    finally
+    else
     {
-      config.endRequest(externalContext);
+      // For non-partial requests, just re-throw.  It is not
+      // our responsibility to catch these
+      if (t instanceof RuntimeException)
+        throw ((RuntimeException) t);
+      if (t instanceof Error)
+        throw ((Error) t);
+      if (t instanceof IOException)
+        throw ((IOException) t);
+      if (t instanceof ServletException)
+        throw ((ServletException) t);
+
+      // Should always be one of those four types to have
+      // gotten here.
+      _LOG.severe(t);
     }
   }
 
-
   @SuppressWarnings("unchecked")
   private void _doFilterImpl(
     ServletRequest  request,

Modified: myfaces/trinidad/trunk/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts?rev=1561022&r1=1561021&r2=1561022&view=diff
==============================================================================
--- myfaces/trinidad/trunk/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts (original)
+++ myfaces/trinidad/trunk/trinidad-impl/src/main/xrts/org/apache/myfaces/trinidadinternal/resource/LoggerBundle.xrts Fri Jan 24 14:19:25 2014
@@ -1234,4 +1234,7 @@ The skin {0} specified on the requestMap
 
 <resource key="SP_LOADING_UNKNOWN_SKIN">Cannot load skin with id {0}. This skin is unknown to the current TrinidadSkinProvider.</resource>
 
+<resource key="EXC_PLEASE_SEE_ERROR_LOG">An error occurred while processing the request. For more information, please see the server's error log for an entry beginning with: {0}</resource>
+
+<resource key="EXC_PPR_ERROR_PREFIX">Server Exception during PPR, #{0}</resource>
 </resources>