FELIX-4548 : Implement the missing errors registration. Apply patch from Thomas Baier

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1674865 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ErrorsMapping.java b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ErrorsMapping.java
index a41ce46..de8fef3 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ErrorsMapping.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/handler/ErrorsMapping.java
@@ -17,12 +17,13 @@
 package org.apache.felix.http.base.internal.handler;
 
 import static org.apache.felix.http.base.internal.util.CollectionUtils.sortedUnion;
+import static org.apache.felix.http.base.internal.util.ErrorPageUtil.parseErrorCodes;
 
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
-import org.apache.felix.http.base.internal.util.ErrorPageUtil;
 import org.apache.felix.http.base.internal.whiteboard.RegistrationFailureException;
 import org.osgi.service.http.runtime.dto.DTOConstants;
 
@@ -50,10 +51,10 @@
         for (Map.Entry<String, ServletHandler> errorPage : remove.entrySet())
         {
             String errorString = errorPage.getKey();
-            if (ErrorPageUtil.isErrorCode(errorString))
+            List<Integer> parsedErrorCodes = parseErrorCodes(errorString);
+            if (parsedErrorCodes != null)
             {
-                Integer errorCode = Integer.valueOf(errorString);
-                newErrorCodesMap.remove(errorCode);
+                removeAllMappings(parsedErrorCodes, newErrorCodesMap);
             }
             else
             {
@@ -64,10 +65,10 @@
         for (Map.Entry<String, ServletHandler> errorPage : add.entrySet())
         {
             String errorString = errorPage.getKey();
-            if (ErrorPageUtil.isErrorCode(errorString))
+            List<Integer> parsedErrorCodes = parseErrorCodes(errorString);
+            if (parsedErrorCodes != null)
             {
-                Integer errorCode = Integer.valueOf(errorString);
-                addErrorServlet(errorCode, errorPage.getValue(), newErrorCodesMap);
+                addErrorServlets(parsedErrorCodes, errorPage.getValue(), newErrorCodesMap);
             }
             else
             {
@@ -78,6 +79,22 @@
         return new ErrorsMapping(newErrorCodesMap, newExceptionsMap);
     }
 
+    private void removeAllMappings(List<Integer> parsedErrorCodes, Map<Integer, ServletHandler> newErrorCodesMap)
+    {
+        for (Integer errorCode : parsedErrorCodes)
+        {
+            newErrorCodesMap.remove(errorCode);
+        }
+    }
+
+    private <E> void addErrorServlets(List<E> errors, ServletHandler handler, Map<E, ServletHandler> index) throws RegistrationFailureException
+    {
+        for (E error : errors)
+        {
+            addErrorServlet(error, handler, index);
+        }
+    }
+
     private <E> void addErrorServlet(E error, ServletHandler handler, Map<E, ServletHandler> index) throws RegistrationFailureException
     {
         if (index.containsKey(error))
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/ErrorPageRuntime.java b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/ErrorPageRuntime.java
index d32af7a..10d353b 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/ErrorPageRuntime.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/runtime/dto/ErrorPageRuntime.java
@@ -49,9 +49,10 @@
 
         for (String string : servletRuntime.getServletInfo().getErrorPage())
         {
-            if (ErrorPageUtil.isErrorCode(string))
+            List<Integer> parsedErrorCodes = ErrorPageUtil.parseErrorCodes(string);
+            if (parsedErrorCodes != null)
             {
-                errorCodes.add(Integer.valueOf(string));
+                errorCodes.addAll(parsedErrorCodes);
             }
             else
             {
diff --git a/http/base/src/main/java/org/apache/felix/http/base/internal/util/ErrorPageUtil.java b/http/base/src/main/java/org/apache/felix/http/base/internal/util/ErrorPageUtil.java
index 57022af..35da8e8 100644
--- a/http/base/src/main/java/org/apache/felix/http/base/internal/util/ErrorPageUtil.java
+++ b/http/base/src/main/java/org/apache/felix/http/base/internal/util/ErrorPageUtil.java
@@ -16,15 +16,46 @@
  */
 package org.apache.felix.http.base.internal.util;
 
+import static java.util.Arrays.asList;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 import java.util.regex.Pattern;
 
 public final class ErrorPageUtil
 {
-    // TODO Handle special values 4xx and 5xx
-    public static final Pattern ERROR_CODE_PATTERN = Pattern.compile("\\d{3}");
+    private static final String CLIENT_ERROR = "4xx";
+    private static final String SERVER_ERROR = "5xx";
+    private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("\\d{3}");
 
-    public static boolean isErrorCode(String string)
+    private static final List<Integer> CLIENT_ERROR_CODES = hundredOf(400);
+    private static final List<Integer> SERVER_ERROR_CODES = hundredOf(500);
+
+    private static List<Integer> hundredOf(int start)
     {
-        return ERROR_CODE_PATTERN.matcher(string).matches();
+        List<Integer> result = new ArrayList<Integer>();
+        for (int i = start; i < start + 100; i++)
+        {
+            result.add(i);
+        }
+        return Collections.unmodifiableList(result);
+    }
+
+    public static List<Integer> parseErrorCodes(String string)
+    {
+        if (CLIENT_ERROR.equalsIgnoreCase(string))
+        {
+            return CLIENT_ERROR_CODES;
+        }
+        else if (SERVER_ERROR.equalsIgnoreCase(string))
+        {
+            return SERVER_ERROR_CODES;
+        }
+        else if (ERROR_CODE_PATTERN.matcher(string).matches())
+        {
+            return asList(Integer.parseInt(string));
+        }
+        return null;
     }
 }