Author: cziegeler
Date: Mon Jul 20 15:00:54 2015
New Revision: 1691957

URL: http://svn.apache.org/r1691957
Log:
Correct error code handler overlaying

Modified:
    
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistry.java
    
felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java

Modified: 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistry.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistry.java?rev=1691957&r1=1691956&r2=1691957&view=diff
==============================================================================
--- 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistry.java
 (original)
+++ 
felix/trunk/http/base/src/main/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistry.java
 Mon Jul 20 15:00:54 2015
@@ -75,9 +75,14 @@ public final class ErrorPageRegistry
         private final ServletHandler handler;
         public final Map<Integer, ErrorRegistration> reasonMapping = new 
HashMap<Integer, ErrorRegistration>();
 
+        public final boolean usesClientErrorCodes;
+        public final boolean usesServerErrorCodes;
+        
         public ErrorRegistrationStatus(final ServletHandler handler)
         {
             this.handler = handler;
+            this.usesClientErrorCodes = hasErrorCode(handler, CLIENT_ERROR);
+            this.usesServerErrorCodes = hasErrorCode(handler, SERVER_ERROR);
         }
 
         public ServletHandler getHandler()
@@ -92,6 +97,18 @@ public final class ErrorPageRegistry
         }
     }
 
+    private static boolean hasErrorCode(final ServletHandler handler, final 
String key) 
+    {
+       for(final String val : handler.getServletInfo().getErrorPage())
+       {
+               if ( key.equals(val) ) 
+               {
+                       return true;
+               }
+       }
+       return false;
+    }
+    
     private static List<Long> hundredOf(final int start)
     {
         List<Long> result = new ArrayList<Long>();
@@ -301,7 +318,22 @@ public final class ErrorPageRegistry
                     if ( oldStatus != null )
                     {
                         removeReason(oldStatus, code, exception, -1);
-                        addReason(oldStatus, code, exception, 
DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+                       boolean addReason = true;
+                       if ( exception == null )
+                       {
+                               if ( code >= 400 && code < 500 && 
oldStatus.usesClientErrorCodes && !status.usesClientErrorCodes )
+                               {
+                                       addReason = false;
+                               } 
+                               else if ( code >= 500 && code < 600 && 
oldStatus.usesServerErrorCodes && !status.usesServerErrorCodes )
+                               {
+                                       addReason = false;
+                               }
+                       }
+                       if ( addReason )
+                       {
+                               addReason(oldStatus, code, exception, 
DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+                       }
                     }
                 }
                 else
@@ -313,12 +345,37 @@ public final class ErrorPageRegistry
         else
         {
             // failure
-            addReason(status, code, exception, 
DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+               boolean addReason = true;
+               if ( exception == null )
+               {
+                       if ( code >= 400 && code < 500 && 
status.usesClientErrorCodes && !hasErrorCode(newList.get(0), CLIENT_ERROR) )
+                       {
+                               addReason = false;
+                       } 
+                       else if ( code >= 500 && code < 600 && 
status.usesServerErrorCodes && !hasErrorCode(newList.get(0), SERVER_ERROR) )
+                       {
+                               addReason = false;
+                       }
+               }
+               if ( addReason )
+               {
+                       addReason(status, code, exception, 
DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE);
+               }
             errorMapping.put(key, newList);
         }
     }
 
-    private void addReason(final ErrorRegistrationStatus status, final long 
code, final String exception, final int reason)
+    /**
+     * Make an entry in the status object (which are used to create the DTOs)
+     * @param status The status object
+     * @param code Either the code
+     * @param exception or the exception
+     * @param reason The code for the failure reason or {@code -1} for 
success.        
+     */
+    private void addReason(final ErrorRegistrationStatus status, 
+               final long code, 
+               final String exception, 
+               final int reason)
     {
         ErrorRegistration reg = status.reasonMapping.get(reason);
         if ( reg == null )

Modified: 
felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java?rev=1691957&r1=1691956&r2=1691957&view=diff
==============================================================================
--- 
felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
 (original)
+++ 
felix/trunk/http/base/src/test/java/org/apache/felix/http/base/internal/registry/ErrorPageRegistryTest.java
 Mon Jul 20 15:00:54 2015
@@ -246,9 +246,9 @@ public class ErrorPageRegistryTest
         // check DTO
         reg.getRuntimeInfo(dto, holder.failedErrorPageDTOs);
 
-        assertEquals(1, holder.failedErrorPageDTOs.size());
-        assertEquals(2, dto.errorPageDTOs.length);
-        assertEquals(98, dto.errorPageDTOs[1].errorCodes.length);
+        // a 4xx is only registered as failure DTO if overlayed by a 4xx!
+        // -> no failure in this case
+        assertEquals(0, holder.failedErrorPageDTOs.size());
         final Set<Long> codes4 = new HashSet<Long>();
         for(final long c : dto.errorPageDTOs[1].errorCodes)
         {
@@ -264,16 +264,6 @@ public class ErrorPageRegistryTest
         {
             assertTrue(c >= 403 && c < 405);
             codes.add(c);
-        }
-        assertEquals(2, codes.size());
-
-        final FailedErrorPageDTO fep = 
holder.failedErrorPageDTOs.iterator().next();
-        assertEquals(2, fep.errorCodes.length);
-        codes.clear();
-        for(final long c : fep.errorCodes)
-        {
-            assertTrue(c >= 403 && c < 405);
-            codes.add(c);
         }
         assertEquals(2, codes.size());
     }


Reply via email to