Skip to content

Commit b8ea7eb

Browse files
sgramponeBeta Bot
authored andcommitted
Cherry pick branch 'genexuslabs:gamutils_eo' into beta
1 parent e7615f2 commit b8ea7eb

3 files changed

Lines changed: 105 additions & 1 deletion

File tree

gamutils/pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@
5252
<version>1.15</version>
5353
<scope>test</scope>
5454
</dependency>
55+
<dependency>
56+
<groupId>com.google.code.gson</groupId>
57+
<artifactId>gson</artifactId>
58+
<version>2.12.1</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>com.genexus</groupId>
62+
<artifactId>gxclassR</artifactId>
63+
<version>${project.version}</version>
64+
<scope>compile</scope>
65+
</dependency>
5566
</dependencies>
5667

5768
<build>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.genexus.gam.utils;
2+
3+
import com.genexus.GxUserType;
4+
import com.google.gson.Gson;
5+
import com.google.gson.GsonBuilder;
6+
import com.google.gson.JsonParser;
7+
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonPrimitive;
9+
import com.google.gson.JsonSyntaxException;
10+
import com.google.gson.reflect.TypeToken;
11+
12+
import java.lang.reflect.Type;
13+
import java.util.LinkedHashMap;
14+
import java.util.List;
15+
import java.util.Map;
16+
17+
public class Dictionary {
18+
19+
private final Map<String, Object> userMap;
20+
private final Gson gson;
21+
22+
/********EXTERNAL OBJECT PUBLIC METHODS - BEGIN ********/
23+
24+
public Dictionary() {
25+
this.userMap = new LinkedHashMap<>();
26+
this.gson = new GsonBuilder().serializeNulls().create();
27+
}
28+
29+
public Object get(String key) {
30+
return this.userMap.get(key);
31+
}
32+
33+
public void set(String key, Object value) {
34+
objectToMap(key, value);
35+
}
36+
37+
public void remove(String key) {
38+
this.userMap.remove(key);
39+
}
40+
41+
public void clear() {
42+
this.userMap.clear();
43+
}
44+
45+
public String toJsonString() {
46+
return this.gson.toJson(this.userMap);
47+
}
48+
49+
/********EXTERNAL OBJECT PUBLIC METHODS - END ********/
50+
51+
// Convert a JSON String to Map<String, Object>
52+
private Map<String, Object> jsonStringToMap(String jsonString) {
53+
Type type = new TypeToken<Map<String, Object>>() {}.getType();
54+
return this.gson.fromJson(jsonString, type);
55+
}
56+
57+
private void objectToMap(String key, Object value) {
58+
if (value == null) {
59+
this.userMap.put(key, null);
60+
} else if (value instanceof Number || value instanceof Boolean || value instanceof Map || value instanceof List) {
61+
this.userMap.put(key, value);
62+
} else if (value instanceof GxUserType) {
63+
this.userMap.put(key, jsonStringToMap(((GxUserType) value).toJSonString()));
64+
} else if (value instanceof String) {
65+
String str = (String) value;
66+
67+
// Try to parse as JSON
68+
try {
69+
JsonElement parsed = JsonParser.parseString(str);
70+
if (parsed.isJsonObject()) {
71+
this.userMap.put(key, this.gson.fromJson(parsed, Map.class));
72+
} else if (parsed.isJsonArray()) {
73+
this.userMap.put(key, this.gson.fromJson(parsed, List.class));
74+
} else if (parsed.isJsonPrimitive()) {
75+
JsonPrimitive primitive = parsed.getAsJsonPrimitive();
76+
if (primitive.isBoolean()) {
77+
this.userMap.put(key, primitive.getAsBoolean());
78+
} else if (primitive.isNumber()) {
79+
this.userMap.put(key, primitive.getAsNumber());
80+
} else if (primitive.isString()) {
81+
this.userMap.put(key, primitive.getAsString());
82+
}
83+
}
84+
} catch (JsonSyntaxException e) {
85+
// Invalid JSON: it is left as string
86+
this.userMap.put(key, str);
87+
}
88+
} else {
89+
// Any other object: it is converted to string
90+
this.userMap.put(key, value.toString());
91+
}
92+
}
93+
}

gamutils/src/main/java/com/genexus/gam/utils/keys/PublicKeyUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private static String fixType(String input) {
108108
logger.debug("fixType");
109109
try {
110110
String extension = FilenameUtils.getExtension(input);
111-
if (extension.isEmpty()) {
111+
if (extension.isEmpty() || extension.contains("}")) {
112112
try {
113113
Base64.decode(input);
114114
logger.debug("b64");

0 commit comments

Comments
 (0)