|
3 | 3 | import com.auth0.jwt.creators.Message; |
4 | 4 | import com.google.common.base.Strings; |
5 | 5 | import java.io.UnsupportedEncodingException; |
| 6 | +import java.lang.reflect.Field; |
6 | 7 | import java.net.URI; |
7 | 8 | import java.net.URISyntaxException; |
8 | 9 | import java.text.ParseException; |
|
11 | 12 | import java.util.HashMap; |
12 | 13 | import java.util.List; |
13 | 14 | import java.util.Map; |
| 15 | +import java.util.Set; |
14 | 16 | import javax.xml.ws.http.HTTPException; |
15 | 17 | import oiccli.HTTP.Response; |
16 | 18 | import oiccli.client_info.ClientInfo; |
@@ -67,33 +69,60 @@ public class Service { |
67 | 69 | private List<Object> preConstruct; |
68 | 70 | private List<String> postConstruct; |
69 | 71 | private List<String> postParseResponse; |
| 72 | + private Map<String,String> conf; |
70 | 73 |
|
71 | 74 |
|
72 | | - public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod, Map<String, String> args) { |
| 75 | + public Service(String httpLib, KeyJar keyJar, String clientAuthenticationMethod, Map<String,String> conf, Map<String, String> args) throws NoSuchFieldException, IllegalAccessException { |
73 | 76 | this.httpLib = httpLib; |
74 | 77 | this.keyJar = keyJar; |
75 | 78 | this.clientAuthenticationMethod = clientAuthenticationMethod; |
76 | 79 | this.events = new ArrayList<>(); |
77 | 80 | this.endpoint = ""; |
78 | 81 | this.defaultRequestArgs = new HashMap<>(); |
| 82 | + |
| 83 | + if(conf != null) { |
| 84 | + this.conf = conf; |
| 85 | + List<String> params = Arrays.asList("msgType", "responseCls", "errorMsg", "defaultAuthenticationMethod", |
| 86 | + "httpMethod", "bodyType", "responseBodyType"); |
| 87 | + for(String param : params) { |
| 88 | + if(conf.containsKey(param)) { |
| 89 | + this.getClass().getField(param).set(this, conf.get("param")); |
| 90 | + } |
| 91 | + } |
| 92 | + } else { |
| 93 | + this.conf = new HashMap<>(); |
| 94 | + } |
| 95 | + |
79 | 96 | this.preConstruct = new ArrayList<>(); |
80 | 97 | this.postConstruct = new ArrayList<>(); |
81 | 98 | this.postParseResponse = new ArrayList<>(); |
82 | | - this.httpMethod = "GET"; |
83 | | - this.bodyType = "urlencoded"; |
84 | | - this.responseBodyType = "json"; |
85 | | - this.defaultAuthenticationMethod = ""; |
86 | | - this.request = ""; |
87 | | - this.endpoint = ""; |
88 | | - this.endpointName = ""; |
89 | | - this.setUp(); |
90 | 99 | } |
91 | 100 |
|
92 | | - public void gatherRequestArgs(ClientInfo clientInfo, Map<String, Object> args) { |
| 101 | + public Map<String, Object> gatherRequestArgs(ClientInfo clientInfo, Map<String, Object> args) throws NoSuchFieldException { |
| 102 | + Map<String,Object> arArgs = new HashMap<>(args); |
| 103 | + Set<String> properties = this.msgType.getCParam().keySet(); |
93 | 104 |
|
| 105 | + Field field; |
| 106 | + for(String property : properties) { |
| 107 | + if(!arArgs.containsKey(property)) { |
| 108 | + field = clientInfo.getClass().getField(property); |
| 109 | + if(field != null) { |
| 110 | + arArgs.put(property, field); |
| 111 | + } else { |
| 112 | + field = this.conf.getRequestArgs().getProp(); |
| 113 | + if(field != null) { |
| 114 | + arArgs.put(property, field); |
| 115 | + } else { |
| 116 | + arArgs.put(property, this.defaultRequestArgs.get(property)); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return arArgs; |
94 | 123 | } |
95 | 124 |
|
96 | | - public static Map<String, Map<String, String>> updateHttpArgs(Map<String, String> httpArgs, Map<String, Map<String, String>> info) { |
| 125 | + public static Map<String, Object> updateHttpArgs(Map<String, String> httpArgs, Map<String, Map<String, String>> info) { |
97 | 126 | Map<String, String> hArgs = info.get("hArgs"); |
98 | 127 | if (hArgs == null) { |
99 | 128 | hArgs = new HashMap<>(); |
@@ -161,7 +190,7 @@ public Message constructMessage(ClientInfo clientInfo, Map<String, Object> reque |
161 | 190 |
|
162 | 191 | List<Map<String, Object>> returnedArgs = this.doPreConstruct(clientInfo, requestArgs, args); |
163 | 192 |
|
164 | | - if (!this.msgType.c_param.containsKey("state")) { |
| 193 | + if (!this.msgType.getCParam().containsKey("state")) { |
165 | 194 | args.remove("state"); |
166 | 195 | } |
167 | 196 |
|
@@ -295,7 +324,8 @@ public Map<String, Object> doRequestInit(ClientInfo clientInfo, String bodyType, |
295 | 324 | } |
296 | 325 |
|
297 | 326 | public String getUrlInfo(String info) throws URISyntaxException { |
298 | | - String query = null, fragment = null; |
| 327 | + String query = null; |
| 328 | + String fragment = null; |
299 | 329 | if (info.contains("?") || info.contains("#")) { |
300 | 330 | URI uri = new URI(info); |
301 | 331 | query = uri.getQuery(); |
@@ -395,7 +425,7 @@ public String getValueType(FakeResponse response, String bodyType) throws WrongC |
395 | 425 | } |
396 | 426 | } |
397 | 427 |
|
398 | | - public ErrorResponse parseRequestResponse(ErrorResponse response, ClientInfo clientInfo, String responseBodyType, |
| 428 | + public static ErrorResponse parseRequestResponse(ErrorResponse response, ClientInfo clientInfo, String responseBodyType, |
399 | 429 | String state, Map<String, Object> args) { |
400 | 430 | int statusCode = response.getStatusCode(); |
401 | 431 | if (successfulCodes.contains(statusCode)) { |
|
0 commit comments