Thursday, December 4, 2014

Searching for an Open AM Session In The Console

This is a short post to capture something I've been wondering about but never taken the time to figure out. The Sessions tab in OpenAM's admin console shows a table of current sessions and includes a Search box. I've tried regular expressions in there but nothing seems to work except an exact match and a single character asterisk.

Well, as part of getting the RADIUS server running in OpenAM I just ran into the SessionService java class today. And I happened to see that its singleton instance had a method to list valid sessions. That method takes a pattern string. That delegated to a matchFilter method as shown below:

    public static boolean matchFilter(String string, String pattern) {
        if (pattern.equals("*") || pattern.equals(string)) {
            return true;
        }

        int length = pattern.length();
        int wildCardIndex = pattern.indexOf("*");

        if (wildCardIndex >= 0) {
            String patternSubStr = pattern.substring(0, wildCardIndex);

            if (!string.startsWith(patternSubStr, 0)) {
                return false;
            }

            int beginIndex = patternSubStr.length() + 1;
            int stringIndex = 0;

            if (wildCardIndex > 0) {
                stringIndex = beginIndex;
            }

            String sub = pattern.substring(beginIndex, length);

            while ((wildCardIndex = pattern.indexOf("*", beginIndex)) != -1) {
                patternSubStr = pattern.substring(beginIndex, wildCardIndex);

                if (string.indexOf(patternSubStr, stringIndex) == -1) {
                    return false;
                }

                beginIndex = wildCardIndex + 1;
                stringIndex = stringIndex + patternSubStr.length() + 1;
                sub = pattern.substring(beginIndex, length);
            }

            if (string.endsWith(sub)) {
                return true;
            }
        }
        return false;
    }

Simply speaking, it allows for an exact match, a single '*' to match everything, or inclusion of from one to many '*' characters. So I threw together a quick test that had the following results. The string on the left is the first String to the method and the second is the pattern.

("12345?", "*12345?") = true
("12345?", "12345?*") = true
("12345?", "*2345?") = true
("12345?", "12345*") = true
("12345?", "*?") = true
("12345?", "1*") = true
("12345?", "*23*5?") = true
("12345?", "*2*5?") = true
("12345?", "**5*") = true
("12345?", "*23*5*") = true

Once I discovered that I wondered if this was what backed the search field in the Sessions console. And sure enough the behavior there appears to match the behavior here exactly. So the Search field supports the following rules:
  • You can include from one to many '*' characters. 
  • They can also be at the beginning and the end. 
  • They match on zero to many characters. 
This may be documented somewhere but I wanted to capture it in case it wasn't well know. Enjoy.





No comments:

Post a Comment