11package org .casbin ;
22
33import org .apache .commons .cli .*;
4- import org .casbin .jcasbin .exception .CasbinConfigException ;
5- import org .casbin .jcasbin .main .Enforcer ;
4+ import org .casbin .jcasbin .exception .CasbinEffectorException ;
5+ import org .casbin .jcasbin .main .EnforceResult ;
6+
67
78public class Client {
89 private static void configureOptions (Options options ) {
@@ -29,46 +30,60 @@ public static Object run(String[] args) throws ParseException {
2930 CommandLineParser parser = new DefaultParser ();
3031 CommandLine cmd = parser .parse (options , args );
3132
32- String modelPath = cmd .getOptionValue ("model" );
33- String policyPath = cmd .getOptionValue ("policy" );
34- Enforcer enforcer = null ;
33+ String model = cmd .getOptionValue ("model" );
34+ String policy = cmd .getOptionValue ("policy" );
35+ NewEnforcer enforcer = null ;
3536 try {
36- enforcer = new Enforcer (modelPath , policyPath );
37- } catch (CasbinConfigException ex ) {
38- ex .printStackTrace ();
37+ enforcer = new NewEnforcer (model , policy );
38+ } catch (NullPointerException | CasbinEffectorException | UnsupportedOperationException e ) {
39+ System .out .println ("unsupported effect:" + e .getMessage ());
40+ System .exit (0 );
41+ } catch (Exception e ) {
42+ System .out .println (e .getMessage ());
43+ System .exit (0 );
3944 }
4045
41- if (cmd .hasOption ("enforce" )) {
42- String enforceArgs = cmd .getOptionValue ("enforce" ).replace (" " ,"" );
43- boolean result = enforcer .enforce (enforceArgs .split ("," ));
44- System .out .println (result ? "Allow" : "Ban" );
45- return result ;
46- } else if (cmd .hasOption ("enforceEx" )) {
47- String enforceArgs = cmd .getOptionValue ("enforceEx" ).replace (" " ,"" );
48- boolean result = enforcer .enforceEx (enforceArgs .split ("," )).isAllow ();
49- System .out .println (result ? "Allow" : "Ban" );
50- return result ;
51- }else if (cmd .hasOption ("addPolicy" )){
52- String policyArgs = cmd .getOptionValue ("addPolicy" ).replace (" " ,"" );
53- boolean result = enforcer .addPolicy (policyArgs .split ("," ));
54- System .out .println (result ? "Add Success" : "Add Failed" );
55- enforcer .savePolicy ();
56- return result ;
57- }else if (cmd .hasOption ("removePolicy" )){
58- String policyArgs = cmd .getOptionValue ("removePolicy" ).replace (" " ,"" );
59- boolean result = enforcer .removePolicy (policyArgs .split ("," ));
60- System .out .println (result ? "Remove Success" : "Remove Failed" );
61- enforcer .savePolicy ();
62- return result ;
63- }else {
64- System .out .println ("Command Error" );
65- return null ;
46+ try {
47+ if (cmd .hasOption ("enforce" )) {
48+ String enforceArgs = cmd .getOptionValue ("enforce" ).replace (" " ,"" );
49+ boolean result = enforcer .enforce (enforceArgs .split ("," ));
50+ System .out .println (result ? "Allow" : "Ban" );
51+ return result ;
52+ } else if (cmd .hasOption ("enforceEx" )) {
53+ String enforceArgs = cmd .getOptionValue ("enforceEx" ).replace (" " ,"" );
54+ EnforceResult enforceResult = enforcer .enforceEx (enforceArgs .split ("," ));
55+ boolean allow = enforceResult .isAllow ();
56+ if (allow ) {
57+ System .out .printf ("%s Reason: %s" , allow , enforceResult .getExplain ());
58+ } else {
59+ System .out .println (allow );
60+ }
61+ return allow ;
62+ }else if (cmd .hasOption ("addPolicy" )){
63+ String policyArgs = cmd .getOptionValue ("addPolicy" ).replace (" " ,"" );
64+ boolean result = enforcer .addPolicy (policyArgs .split ("," ));
65+ System .out .println (result ? "Add Success" : "Add Failed" );
66+ enforcer .savePolicy ();
67+ return result ;
68+ }else if (cmd .hasOption ("removePolicy" )){
69+ String policyArgs = cmd .getOptionValue ("removePolicy" ).replace (" " ,"" );
70+ boolean result = enforcer .removePolicy (policyArgs .split ("," ));
71+ System .out .println (result ? "Remove Success" : "Remove Failed" );
72+ enforcer .savePolicy ();
73+ return result ;
74+ }else {
75+ System .out .println ("Command Error" );
76+ return null ;
77+ }
78+ } catch (Exception e ) {
79+ System .out .println ("unsupported effect:" + e .getMessage ());
80+ System .exit (0 );
6681 }
82+ return null ;
6783 }
6884
6985 public static void main (String [] args ) throws ParseException {
7086 Client cli = new Client ();
71- Object run = cli .run (args );
72- System .out .println (run );
87+ Object run = run (args );
7388 }
7489}
0 commit comments