public static List < String > filterCommands ( Collection < String > commands , String whitelistRegex , String blacklistRegex , Logger log ) { List < String > filteredCommands = new LinkedList < String > ( ) ; Pattern whitelistPattern = Pattern . compile ( whitelistRegex ) ; Pattern blacklistPattern = Pattern . compile ( blacklistRegex ) ; for ( String command : commands ) { if ( whitelistPattern . matcher ( command ) . matches ( ) && ! blacklistPattern . matcher ( command ) . matches ( ) ) { filteredCommands . add ( command ) ; } else { log . warn ( String . format ( "Removing restricted command: %s" , command ) ) ; } } return filteredCommands ; }
Filter a collection of String commands to match a whitelist regex and not match a blacklist regex .