Browse Source

Converted to use guard clause to reduce nesting.

Luke Taylor 18 năm trước cách đây
mục cha
commit
28a138f8ec

+ 92 - 90
core/src/main/java/org/springframework/security/intercept/web/FilterInvocationDefinitionSourceEditor.java

@@ -54,124 +54,126 @@ public class FilterInvocationDefinitionSourceEditor extends PropertyEditorSuppor
 
         if ((s == null) || "".equals(s)) {
             // Leave target object empty
-            source.setDecorated(new RegExpBasedFilterInvocationDefinitionMap());
-        } else {
-            // Check if we need to override the default definition map
-            if (s.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) {
-                source.setDecorated(new PathBasedFilterInvocationDefinitionMap());
+            setValue(new RegExpBasedFilterInvocationDefinitionMap());
 
-                if (logger.isDebugEnabled()) {
-                    logger.debug(("Detected " + DIRECTIVE_PATTERN_TYPE_APACHE_ANT
-                        + " directive; using Apache Ant style path expressions"));
-                }
-            } else {
-                source.setDecorated(new RegExpBasedFilterInvocationDefinitionMap());
-            }
+            return;
+        }
 
-            if (s.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1) {
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Detected " + DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
-                        + " directive; Instructing mapper to convert URLs to lowercase before comparison");
-                }
+        // Check if we need to override the default definition map
+        if (s.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) {
+            source.setDecorated(new PathBasedFilterInvocationDefinitionMap());
 
-                source.setConvertUrlToLowercaseBeforeComparison(true);
+            if (logger.isDebugEnabled()) {
+                logger.debug(("Detected " + DIRECTIVE_PATTERN_TYPE_APACHE_ANT
+                    + " directive; using Apache Ant style path expressions"));
             }
+        } else {
+            source.setDecorated(new RegExpBasedFilterInvocationDefinitionMap());
+        }
 
-            BufferedReader br = new BufferedReader(new StringReader(s));
-            int counter = 0;
-            String line;
+        if (s.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1) {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Detected " + DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
+                    + " directive; Instructing mapper to convert URLs to lowercase before comparison");
+            }
 
-            List mappings = new ArrayList();
+            source.setConvertUrlToLowercaseBeforeComparison(true);
+        }
 
-            while (true) {
-                counter++;
+        BufferedReader br = new BufferedReader(new StringReader(s));
+        int counter = 0;
+        String line;
 
-                try {
-                    line = br.readLine();
-                } catch (IOException ioe) {
-                    throw new IllegalArgumentException(ioe.getMessage());
-                }
+        List mappings = new ArrayList();
 
-                if (line == null) {
-                    break;
-                }
+        while (true) {
+            counter++;
 
-                line = line.trim();
+            try {
+                line = br.readLine();
+            } catch (IOException ioe) {
+                throw new IllegalArgumentException(ioe.getMessage());
+            }
 
-                if (logger.isDebugEnabled()) {
-                    logger.debug("Line " + counter + ": " + line);
-                }
+            if (line == null) {
+                break;
+            }
 
-                if (line.startsWith("//")) {
-                    continue;
-                }
+            line = line.trim();
 
-                // Attempt to detect malformed lines (as per SEC-204)
-                if (line.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1) {
-                    // Directive found; check for second directive or name=value
-                    if ((line.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) || (line.lastIndexOf("=") != -1)) {
-                        throw new IllegalArgumentException("Line appears to be malformed: " + line);
-                    }
-                }
+            if (logger.isDebugEnabled()) {
+                logger.debug("Line " + counter + ": " + line);
+            }
 
-                // Attempt to detect malformed lines (as per SEC-204)
-                if (line.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) {
-                    // Directive found; check for second directive or name=value
-                    if ((line.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1)
-                        || (line.lastIndexOf("=") != -1)) {
-                        throw new IllegalArgumentException("Line appears to be malformed: " + line);
-                    }
-                }
+            if (line.startsWith("//")) {
+                continue;
+            }
 
-                // Skip lines that are not directives
-                if (line.lastIndexOf('=') == -1) {
-                    continue;
+            // Attempt to detect malformed lines (as per SEC-204)
+            if (line.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1) {
+                // Directive found; check for second directive or name=value
+                if ((line.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) || (line.lastIndexOf("=") != -1)) {
+                    throw new IllegalArgumentException("Line appears to be malformed: " + line);
                 }
+            }
 
-                if (line.lastIndexOf("==") != -1) {
-                    throw new IllegalArgumentException("Only single equals should be used in line " + line);
+            // Attempt to detect malformed lines (as per SEC-204)
+            if (line.lastIndexOf(DIRECTIVE_PATTERN_TYPE_APACHE_ANT) != -1) {
+                // Directive found; check for second directive or name=value
+                if ((line.lastIndexOf(DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON) != -1)
+                    || (line.lastIndexOf("=") != -1)) {
+                    throw new IllegalArgumentException("Line appears to be malformed: " + line);
                 }
+            }
 
-                // Tokenize the line into its name/value tokens
-                // As per SEC-219, use the LAST equals as the delimiter between LHS and RHS
-                String name = StringSplitUtils.substringBeforeLast(line, "=");
-                String value = StringSplitUtils.substringAfterLast(line, "=");
+            // Skip lines that are not directives
+            if (line.lastIndexOf('=') == -1) {
+                continue;
+            }
 
-                if (!StringUtils.hasText(name) || !StringUtils.hasText(value)) {
-                    throw new IllegalArgumentException("Failed to parse a valid name/value pair from " + line);
-                }
+            if (line.lastIndexOf("==") != -1) {
+                throw new IllegalArgumentException("Only single equals should be used in line " + line);
+            }
+
+            // Tokenize the line into its name/value tokens
+            // As per SEC-219, use the LAST equals as the delimiter between LHS and RHS
+            String name = StringSplitUtils.substringBeforeLast(line, "=");
+            String value = StringSplitUtils.substringAfterLast(line, "=");
 
-                // Attempt to detect malformed lines (as per SEC-204)
-                if (source.isConvertUrlToLowercaseBeforeComparison()
-                    && source.getDecorated() instanceof PathBasedFilterInvocationDefinitionMap) {
-                    // Should all be lowercase; check each character
-                    // We only do this for Ant (regexp have control chars)
-                    for (int i = 0; i < name.length(); i++) {
-                        String character = name.substring(i, i + 1);
-
-                        if (!character.toLowerCase().equals(character)) {
-                            throw new IllegalArgumentException("You are using the "
-                                + DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
-                                + " with Ant Paths, yet you have specified an uppercase character in line: " + line
-                                + " (character '" + character + "')");
-                        }
+            if (!StringUtils.hasText(name) || !StringUtils.hasText(value)) {
+                throw new IllegalArgumentException("Failed to parse a valid name/value pair from " + line);
+            }
+
+            // Attempt to detect malformed lines (as per SEC-204)
+            if (source.isConvertUrlToLowercaseBeforeComparison()
+                && source.getDecorated() instanceof PathBasedFilterInvocationDefinitionMap) {
+                // Should all be lowercase; check each character
+                // We only do this for Ant (regexp have control chars)
+                for (int i = 0; i < name.length(); i++) {
+                    String character = name.substring(i, i + 1);
+
+                    if (!character.toLowerCase().equals(character)) {
+                        throw new IllegalArgumentException("You are using the "
+                            + DIRECTIVE_CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
+                            + " with Ant Paths, yet you have specified an uppercase character in line: " + line
+                            + " (character '" + character + "')");
                     }
                 }
+            }
 
-                FilterInvocationDefinitionSourceMapping mapping = new FilterInvocationDefinitionSourceMapping();
-                mapping.setUrl(name);
-
-                String[] tokens = org.springframework.util.StringUtils
-                        .commaDelimitedListToStringArray(value);
+            FilterInvocationDefinitionSourceMapping mapping = new FilterInvocationDefinitionSourceMapping();
+            mapping.setUrl(name);
 
-                for (int i = 0; i < tokens.length; i++) {
-                    mapping.addConfigAttribute(tokens[i].trim());
-                }
+            String[] tokens = StringUtils.commaDelimitedListToStringArray(value);
 
-                mappings.add(mapping);
+            for (int i = 0; i < tokens.length; i++) {
+                mapping.addConfigAttribute(tokens[i].trim());
             }
-            source.setMappings(mappings);
+
+            mappings.add(mapping);
         }
+        source.setMappings(mappings);
+
 
         setValue(source.getDecorated());
     }