11package org .casbin ;
22
33import com .fasterxml .jackson .core .JsonProcessingException ;
4+ import com .fasterxml .jackson .core .type .TypeReference ;
45import com .fasterxml .jackson .databind .ObjectMapper ;
56import org .casbin .jcasbin .main .EnforceResult ;
67import org .casbin .jcasbin .main .Enforcer ;
@@ -25,6 +26,32 @@ public CommandExecutor(NewEnforcer enforcer, String inputMethodName, String[] in
2526 this .inputVal = inputVal ;
2627 }
2728
29+ /***
30+ * Converts a string input into a JSON formatted string.
31+ *
32+ * @param input The input string to be converted to JSON format. It should be enclosed in curly braces {}.
33+ * @return A JSON formatted string representing the key-value pairs from the input string.
34+ */
35+ public static String convertToJson (String input ) {
36+ input = input .trim ().substring (1 , input .length () - 1 ).trim ();
37+ StringBuilder jsonBuilder = new StringBuilder ("{" );
38+ String [] pairs = input .split ("," );
39+ for (String pair : pairs ) {
40+ pair = pair .trim ();
41+ String [] keyValue = pair .split (":" );
42+ if (keyValue .length == 2 ) {
43+ String key = keyValue [0 ].trim ();
44+ String value = keyValue [1 ].trim ();
45+ jsonBuilder .append ("\" " ).append (key ).append ("\" :" ).append (value ).append ("," );
46+ }
47+ }
48+ if (jsonBuilder .length () > 1 ) {
49+ jsonBuilder .deleteCharAt (jsonBuilder .length () - 1 );
50+ }
51+ jsonBuilder .append ("}" );
52+ return jsonBuilder .toString ();
53+ }
54+
2855 public String outputResult () throws InvocationTargetException , IllegalAccessException , JsonProcessingException {
2956 Class <? extends Enforcer > clazz = enforcer .getClass ();
3057 Method [] methods = clazz .getMethods ();
@@ -75,7 +102,30 @@ public String outputResult() throws InvocationTargetException, IllegalAccessExce
75102 }
76103 }
77104
78- Object invoke = method .invoke (enforcer , convertedParams );
105+ Object [] extraConvertedParams = new Object [inputVal .length ];
106+ boolean hasJson = false ;
107+ try {
108+ ObjectMapper objectMapper = new ObjectMapper ();
109+ if (inputVal .length > 0 && inputVal [0 ].trim ().startsWith ("{" )) {
110+ Map <String , Object > objectMap = objectMapper .readValue (convertToJson (inputVal [0 ]), new TypeReference <Map <String , Object >>() {
111+ });
112+ extraConvertedParams [0 ] = objectMap ;
113+ if (inputVal .length >= 1 ) {
114+ System .arraycopy (inputVal , 1 , extraConvertedParams , 1 , inputVal .length - 1 );
115+ }
116+ hasJson = true ;
117+ }
118+ } catch (Exception e ) {
119+ e .printStackTrace ();
120+ hasJson = false ;
121+ }
122+ Object invoke ;
123+ if (hasJson ){
124+ invoke = method .invoke (enforcer , (Object ) extraConvertedParams );
125+ } else {
126+ invoke = method .invoke (enforcer , convertedParams );
127+ }
128+
79129 if (returnType == boolean .class ) {
80130 responseBody .setAllow ((Boolean ) invoke );
81131 } else if (returnType == List .class ) {
0 commit comments