Search in sources :

Example 1 with WildcardHelper

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

the class PackageBasedActionConfigBuilder method checkExcludePackages.

/**
 * Checks if provided class package is on the exclude list
 *
 * @param classPackageName name of class package
 * @return false if class package is on the {@link #excludePackages} list
 */
protected boolean checkExcludePackages(String classPackageName) {
    if (excludePackages != null && excludePackages.length > 0) {
        WildcardHelper wildcardHelper = new WildcardHelper();
        // we really don't care about the results, just the boolean
        Map<String, String> matchMap = new HashMap<>();
        for (String packageExclude : excludePackages) {
            int[] packagePattern = wildcardHelper.compilePattern(packageExclude);
            if (wildcardHelper.match(matchMap, classPackageName, packagePattern)) {
                return false;
            }
        }
    }
    return true;
}
Also used : WildcardHelper(com.opensymphony.xwork2.util.WildcardHelper)

Example 2 with WildcardHelper

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

the class ActionConfigMatcherTest method testLooseMatch.

public void testLooseMatch() {
    configMap.put("*!*", configMap.get("bar/*/**"));
    ActionConfigMatcher matcher = new ActionConfigMatcher(new WildcardHelper(), configMap, true);
    // exact match
    ActionConfig m = matcher.match("foo/class/method");
    assertNotNull("ActionConfig should be matched", m);
    assertTrue("Class hasn't been replaced " + m.getClassName(), "foo.bar.classAction".equals(m.getClassName()));
    assertTrue("Method hasn't been replaced", "domethod".equals(m.getMethodName()));
    // Missing last wildcard
    m = matcher.match("foo/class");
    assertNotNull("ActionConfig should be matched", m);
    assertTrue("Class hasn't been replaced", "foo.bar.classAction".equals(m.getClassName()));
    assertTrue("Method hasn't been replaced, " + m.getMethodName(), "do".equals(m.getMethodName()));
    // Simple mapping
    m = matcher.match("class!method");
    assertNotNull("ActionConfig should be matched", m);
    assertTrue("Class hasn't been replaced, " + m.getPackageName(), "package-class".equals(m.getPackageName()));
    assertTrue("Method hasn't been replaced", "method".equals(m.getParams().get("first")));
    // Simple mapping
    m = matcher.match("class");
    assertNotNull("ActionConfig should be matched", m);
    assertTrue("Class hasn't been replaced", "package-class".equals(m.getPackageName()));
    assertTrue("Method hasn't been replaced", "".equals(m.getParams().get("first")));
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) WildcardHelper(com.opensymphony.xwork2.util.WildcardHelper)

Example 3 with WildcardHelper

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

the class NamespaceMatcherTest method testMatch.

public void testMatch() {
    Set<String> names = new HashSet<>();
    names.add("/bar");
    names.add("/foo/*/bar");
    names.add("/foo/*");
    names.add("/foo/*/jim/*");
    NamespaceMatcher matcher = new NamespaceMatcher(new WildcardHelper(), names);
    assertEquals(3, matcher.compiledPatterns.size());
    assertNull(matcher.match("/asd"));
    assertEquals("/foo/*", matcher.match("/foo/23").getPattern());
    assertEquals("/foo/*/bar", matcher.match("/foo/23/bar").getPattern());
    assertEquals("/foo/*/jim/*", matcher.match("/foo/23/jim/42").getPattern());
    assertNull(matcher.match("/foo/23/asd"));
}
Also used : WildcardHelper(com.opensymphony.xwork2.util.WildcardHelper) HashSet(java.util.HashSet)

Example 4 with WildcardHelper

use of com.opensymphony.xwork2.util.WildcardHelper 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)

Aggregations

WildcardHelper (com.opensymphony.xwork2.util.WildcardHelper)4 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1