-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommonUtils.java
More file actions
74 lines (57 loc) · 2.31 KB
/
Copy pathCommonUtils.java
File metadata and controls
74 lines (57 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.bitmart.api.common;
import com.bitmart.api.annotations.ParamKey;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
public final class CommonUtils {
private static final Logger log = LoggerFactory.getLogger(CommonUtils.class);
public CommonUtils() {
}
public static Map<String, Object> genRequestMap(Object cloudRequest) throws CloudException {
if(cloudRequest == null) {
return new TreeMap<>();
}
Map<String, Object> paraMap = new TreeMap<>();
try {
Field[] declaredFields = cloudRequest.getClass().getDeclaredFields();
if (declaredFields.length == 0) {
return new TreeMap<>();
}
List<Field> collect = Arrays.stream(declaredFields).collect(Collectors.toList());
// FIXED: remove the following code
// Field[] declaredFields1 = cloudRequest.getClass().getSuperclass().getDeclaredFields();
// collect.addAll(Arrays.stream(declaredFields1).collect(Collectors.toList()));
for (Field declaredField : collect) {
declaredField.setAccessible(true);
final ParamKey paramKey = declaredField.getAnnotation(ParamKey.class);
if (paramKey != null) {
String key = paramKey.value();
Object value = declaredField.get(cloudRequest);
if(paramKey.required() && value == null) {
throw new CloudException("The request parameter [" + declaredField.getName() + "] is required");
}
if(value != null) {
paraMap.put(key, value);
}
}
}
} catch (Exception ex) {
throw new CloudException(ex.getMessage());
}
return paraMap;
}
public static int getRateLimitValue(String val) {
try {
return Integer.parseInt(StringUtils.defaultIfBlank(val, "0"));
} catch (Exception e) {
log.warn("getRateLimitValue value={}", val);
}
return 0;
}
}