Search in sources :

Example 1 with CompiledPattern

use of com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern in project struts by apache.

the class NamedVariablePatternMatcherTest method testMatch.

@Test
public void testMatch() {
    NamedVariablePatternMatcher matcher = new NamedVariablePatternMatcher();
    Map<String, String> vars = new HashMap<>();
    CompiledPattern pattern = new CompiledPattern(Pattern.compile("foo([^/]+)"), Arrays.asList("bar"));
    assertTrue(matcher.match(vars, "foobaz", pattern));
    assertEquals("baz", vars.get("bar"));
}
Also used : HashMap(java.util.HashMap) CompiledPattern(com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern) Test(org.junit.Test)

Example 2 with CompiledPattern

use of com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern in project struts by apache.

the class MethodFilterInterceptorUtil method applyMethod.

/**
 * Static method to decide if the specified <code>method</code> should be
 * apply (not filtered) depending on the set of <code>excludeMethods</code> and
 * <code>includeMethods</code>.
 *
 * <ul>
 * <li>
 * 	<code>includeMethods</code> takes precedence over <code>excludeMethods</code>
 * </li>
 * </ul>
 * <b>Note:</b> Supports wildcard listings in includeMethods/excludeMethods
 *
 * @param excludeMethods  list of methods to exclude.
 * @param includeMethods  list of methods to include.
 * @param method the specified method to check
 * @return <tt>true</tt> if the method should be applied.
 */
public static boolean applyMethod(Set<String> excludeMethods, Set<String> includeMethods, String method) {
    // quick check to see if any actual pattern matching is needed
    boolean needsPatternMatch = false;
    for (String includeMethod : includeMethods) {
        if (!"*".equals(includeMethod) && includeMethod.contains("*")) {
            needsPatternMatch = true;
            break;
        }
    }
    for (String excludeMethod : excludeMethods) {
        if (!"*".equals(excludeMethod) && excludeMethod.contains("*")) {
            needsPatternMatch = true;
            break;
        }
    }
    // still allowing for wildcards later
    if (!needsPatternMatch && (includeMethods.contains("*") || includeMethods.size() == 0)) {
        if (excludeMethods != null && excludeMethods.contains(method) && !includeMethods.contains(method)) {
            return false;
        }
    }
    // test the methods using pattern matching
    WildcardHelper wildcard = new WildcardHelper();
    String methodCopy;
    if (method == null) {
        // no method specified
        methodCopy = "";
    } else {
        methodCopy = new String(method);
    }
    for (String pattern : includeMethods) {
        if (pattern.contains("*")) {
            int[] compiledPattern = wildcard.compilePattern(pattern);
            HashMap<String, String> matchedPatterns = new HashMap<>();
            boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern);
            if (matches) {
                // run it, includeMethods takes precedence
                return true;
            }
        } else {
            if (pattern.equals(methodCopy)) {
                // run it, includeMethods takes precedence
                return true;
            }
        }
    }
    if (excludeMethods.contains("*")) {
        return false;
    }
    // CHECK ME: Previous implementation used include method
    for (String pattern : excludeMethods) {
        if (pattern.contains("*")) {
            int[] compiledPattern = wildcard.compilePattern(pattern);
            HashMap<String, String> matchedPatterns = new HashMap<>();
            boolean matches = wildcard.match(matchedPatterns, methodCopy, compiledPattern);
            if (matches) {
                // if found, and wasn't included earlier, don't run it
                return false;
            }
        } else {
            if (pattern.equals(methodCopy)) {
                // if found, and wasn't included earlier, don't run it
                return false;
            }
        }
    }
    // default fall-back from before changes
    return includeMethods.size() == 0 || includeMethods.contains(method) || includeMethods.contains("*");
}
Also used : WildcardHelper(com.opensymphony.xwork2.util.WildcardHelper) HashMap(java.util.HashMap)

Example 3 with CompiledPattern

use of com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern in project struts by apache.

the class NamedVariablePatternMatcherTest method testCompile.

@Test
public void testCompile() {
    NamedVariablePatternMatcher matcher = new NamedVariablePatternMatcher();
    assertNull(matcher.compilePattern(null));
    assertNull(matcher.compilePattern(""));
    CompiledPattern pattern = matcher.compilePattern("foo");
    assertEquals("foo", pattern.getPattern().pattern());
    pattern = matcher.compilePattern("foo{jim}");
    assertEquals("foo([^/]+)", pattern.getPattern().pattern());
    assertEquals("jim", pattern.getVariableNames().get(0));
    pattern = matcher.compilePattern("foo{jim}/{bob}");
    assertEquals("foo([^/]+)/([^/]+)", pattern.getPattern().pattern());
    assertEquals("jim", pattern.getVariableNames().get(0));
    assertEquals("bob", pattern.getVariableNames().get(1));
    assertTrue(pattern.getPattern().matcher("foostar/jie").matches());
    assertFalse(pattern.getPattern().matcher("foo/star/jie").matches());
}
Also used : CompiledPattern(com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern) Test(org.junit.Test)

Aggregations

CompiledPattern (com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 WildcardHelper (com.opensymphony.xwork2.util.WildcardHelper)1