22
33
44import org .apache .commons .cli .*;
5- import org .casbin .command .*;
65import org .casbin .generate .DynamicClassGenerator ;
76import org .casbin .jcasbin .util .function .CustomFunction ;
87import org .casbin .util .Util ;
98
10-
11- import java .util .Arrays ;
12- import java .util .HashMap ;
13- import java .util .Map ;
9+ import java .util .*;
1410
1511
1612public class Client {
1713
18- private static final String RBAC_COMMAND = "rbac" ;
19- private static final String RBAC_WITH_CONDITION_COMMAND = "rbac_with_condition" ;
20- private static final String RBAC_WITH_DOMAINS_COMMAND = "rbac_with_domains" ;
21- private static final String ROLEMANAGER_COMMAND = "role_manager" ;
22- private static final String MANAGEMENT_COMMAND = "management" ;
23- private static final String ENFORCE_COMMAND = "enforce" ;
24-
25- private static final Map <String , AbstractCommand > COMMANDS = new HashMap <>();
26-
27- static {
28- COMMANDS .put (RBAC_COMMAND , new RBACCommand ());
29- COMMANDS .put (RBAC_WITH_CONDITION_COMMAND , new RBACWithConditionsCommand ());
30- COMMANDS .put (RBAC_WITH_DOMAINS_COMMAND , new RBACWithDomainsCommand ());
31- COMMANDS .put (ROLEMANAGER_COMMAND , new RoleManagerCommand ());
32- COMMANDS .put (MANAGEMENT_COMMAND , new ManagementCommand ());
33- COMMANDS .put (ENFORCE_COMMAND , new EnforceCommand ());
34- }
35-
3614 public static String run (String ... args ) {
3715 String result = "" ;
3816
@@ -41,64 +19,25 @@ public static String run(String... args) {
4119 printUsageMessageAndExit ("" );
4220 }
4321
44- Options options = new Options ();
45- Option option = new Option ("m" , "model" , true , "the path of the model file or model text" );
46- options .addOption (option );
47- option = new Option ("p" , "policy" , true , "the path of the policy file or policy text" );
48- options .addOption (option );
49- option = new Option ("af" , "addFunction" , true , "add custom function" );
50- option .setRequired (false );
51- options .addOption (option );
52-
53- boolean hasAddFuntion = false ;
54- for (String arg : args ) {
55- if (arg .equals ("-af" ) || arg .equals ("-addFunction" )) {
56- hasAddFuntion = true ;
57- break ;
58- }
59- }
22+ String commandName = args [0 ];
6023
61- CommandLineParser parser = new DefaultParser ();
24+ CommandLine cmd = getCmd (Arrays .copyOfRange (args , 1 , args .length ));
25+ String model = cmd .getOptionValue ("model" );
26+ String policy = cmd .getOptionValue ("policy" );
27+ NewEnforcer enforcer = new NewEnforcer (model , policy );
6228
63- CommandLine cmd = null ;
64- if (hasAddFuntion ) {
65- cmd = parser .parse (options , Arrays .stream (args ).limit (7 ).toArray (String []::new ));
66- } else {
67- cmd = parser .parse (options , Arrays .stream (args ).limit (5 ).toArray (String []::new ));
68- }
6929
70- if (cmd .hasOption ("model" ) && cmd .hasOption ("policy" )) {
71- String model = cmd .getOptionValue ("model" );
72- String policy = cmd .getOptionValue ("policy" );
73- NewEnforcer enforcer = new NewEnforcer (model , policy );
74-
75- if (hasAddFuntion ) {
76- String codes = cmd .getOptionValue ("addFunction" );
77- String methodName = Util .getMethodName (codes );
78- CustomFunction customFunction = DynamicClassGenerator .generateClass (methodName , codes );
79- enforcer .addFunction (methodName , customFunction );
80- }
81-
82- String commandName = args [0 ];
83- AbstractCommand command = COMMANDS .get (commandName );
84-
85-
86-
87- if (command != null ) {
88- if (hasAddFuntion ) {
89- result = command .run (enforcer , Arrays .copyOfRange (args , 7 , args .length ));
90- } else {
91- result = command .run (enforcer , Arrays .copyOfRange (args , 5 , args .length ));
92- }
93- // System.exit(0);
94- } else {
95- printUsageMessageAndExit (commandName );
96- }
97-
98- } else {
99- new HelpCommand ().run ();
100- System .exit (1 );
30+ if (cmd .hasOption ("AF" )) {
31+ String codes = cmd .getOptionValue ("AF" );
32+ String methodName = Util .getMethodName (codes );
33+ CustomFunction customFunction = DynamicClassGenerator .generateClass (methodName , codes );
34+ enforcer .addFunction (methodName , customFunction );
10135 }
36+ CommandExecutor commandExecutor = new CommandExecutor (enforcer , commandName , cmd .getArgs ());
37+ Object o = commandExecutor .outputResult ();
38+ System .out .println (o );
39+ return o .toString ();
40+
10241 } catch (Exception e ) {
10342 e .printStackTrace ();
10443 System .exit (1 );
@@ -111,12 +50,31 @@ private static void printUsageMessageAndExit(String commandName) throws Exceptio
11150 if (commandName .isEmpty ()) {
11251 System .out .println ("Error: " + commandName + " not recognised" );
11352 }
114-
115- new HelpCommand ().run ();
53+ // new HelpCommand().run();
11654 System .exit (1 );
11755 }
11856
11957 public static void main (String [] args ) throws ParseException {
12058 run (args );
12159 }
60+
61+ private static CommandLine getCmd (String [] args ) throws ParseException {
62+ Options options = new Options ();
63+
64+ Option option = new Option ("AF" , "AddFunction" , true , "add function" );
65+ option .setArgs (1 );
66+ option .setRequired (false );
67+ options .addOption (option );
68+
69+ option = new Option ("m" , "model" , true , "the path of the model file or model text" );
70+ option .hasArg ();
71+ options .addOption (option );
72+
73+ option = new Option ("p" , "policy" , true , "the path of the policy file or policy text" );
74+ option .hasArg ();
75+ options .addOption (option );
76+
77+ CommandLineParser parser = new DefaultParser ();
78+ return parser .parse (options , args );
79+ }
12280}
0 commit comments