mirror of https://github.com/OpenVidu/openvidu.git
openvidu-server: better check of string array in OpenviduConfig
parent
622d549903
commit
aa7a2c7adf
|
@ -455,7 +455,7 @@ public class OpenviduConfig {
|
||||||
String kmsUris;
|
String kmsUris;
|
||||||
try {
|
try {
|
||||||
// First check if castable to a List
|
// First check if castable to a List
|
||||||
List<?> list = checkArray(parameters, parameter, admitStringified);
|
List<String> list = checkStringArray(parameters, parameter, admitStringified);
|
||||||
String elementString;
|
String elementString;
|
||||||
for (Object element : list) {
|
for (Object element : list) {
|
||||||
try {
|
try {
|
||||||
|
@ -484,7 +484,7 @@ public class OpenviduConfig {
|
||||||
String webhookHeaders;
|
String webhookHeaders;
|
||||||
try {
|
try {
|
||||||
// First check if castable to a List
|
// First check if castable to a List
|
||||||
List<?> list = checkArray(parameters, parameter, admitStringified);
|
List<String> list = checkStringArray(parameters, parameter, admitStringified);
|
||||||
String elementString;
|
String elementString;
|
||||||
for (Object element : list) {
|
for (Object element : list) {
|
||||||
try {
|
try {
|
||||||
|
@ -496,7 +496,7 @@ public class OpenviduConfig {
|
||||||
+ ") that is not a string: " + e.getMessage());
|
+ ") that is not a string: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
webhookHeaders = list.toString();
|
webhookHeaders = listToQuotedStringifiedArray(list);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// If it is not a list, try casting to String
|
// If it is not a list, try casting to String
|
||||||
webhookHeaders = checkString(parameters, parameter);
|
webhookHeaders = checkString(parameters, parameter);
|
||||||
|
@ -512,7 +512,7 @@ public class OpenviduConfig {
|
||||||
String webhookEvents;
|
String webhookEvents;
|
||||||
try {
|
try {
|
||||||
// First check if castable to a List
|
// First check if castable to a List
|
||||||
List<?> list = checkArray(parameters, parameter, admitStringified);
|
List<String> list = checkStringArray(parameters, parameter, admitStringified);
|
||||||
String elementString;
|
String elementString;
|
||||||
for (Object element : list) {
|
for (Object element : list) {
|
||||||
try {
|
try {
|
||||||
|
@ -523,7 +523,7 @@ public class OpenviduConfig {
|
||||||
+ element + ") that is not a string: " + e.getMessage());
|
+ element + ") that is not a string: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
webhookEvents = list.toString();
|
webhookEvents = listToQuotedStringifiedArray(list);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// If it is not a list, try casting to String
|
// If it is not a list, try casting to String
|
||||||
webhookEvents = checkString(parameters, parameter);
|
webhookEvents = checkString(parameters, parameter);
|
||||||
|
@ -606,15 +606,16 @@ public class OpenviduConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<?> checkArray(Map<String, ?> parameters, String key, boolean admitStringified) throws Exception {
|
public List<String> checkStringArray(Map<String, ?> parameters, String key, boolean admitStringified)
|
||||||
List<?> list;
|
throws Exception {
|
||||||
|
List<String> list;
|
||||||
try {
|
try {
|
||||||
if (parameters.get(key) instanceof Collection<?>) {
|
if (parameters.get(key) instanceof Collection<?>) {
|
||||||
list = (List<String>) parameters.get(key);
|
list = (List<String>) parameters.get(key);
|
||||||
} else if (admitStringified) {
|
} else if (admitStringified) {
|
||||||
list = this.stringifiedArrayOfStringToListOfStrings((String) parameters.get(key));
|
list = this.stringifiedArrayOfStringToListOfStrings((String) parameters.get(key));
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Property '" + key + "' must be an integer");
|
throw new Exception("Property '" + key + "' must be an array");
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
} catch (ClassCastException e) {
|
} catch (ClassCastException e) {
|
||||||
|
@ -655,12 +656,6 @@ public class OpenviduConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> stringifiedArrayOfStringToListOfStrings(String json) {
|
public List<String> stringifiedArrayOfStringToListOfStrings(String json) {
|
||||||
json = json.replaceAll("\\s", ""); // Remove all white spaces
|
|
||||||
json = json.replaceAll("\\\\", ""); // Remove previous escapes
|
|
||||||
json = json.replaceAll("\"", ""); // Remove previous double quotes
|
|
||||||
json = json.replaceFirst("^\\[", "[\\\""); // Escape first char
|
|
||||||
json = json.replaceFirst("\\]$", "\\\"]"); // Escape last char
|
|
||||||
json = json.replaceAll(",", "\\\",\\\""); // Escape middle strings
|
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
JsonArray jsonArray = gson.fromJson(json, JsonArray.class);
|
JsonArray jsonArray = gson.fromJson(json, JsonArray.class);
|
||||||
List<String> list = JsonUtils.toStringList(jsonArray);
|
List<String> list = JsonUtils.toStringList(jsonArray);
|
||||||
|
@ -831,4 +826,8 @@ public class OpenviduConfig {
|
||||||
OpenViduServer.publicurlType = type;
|
OpenViduServer.publicurlType = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String listToQuotedStringifiedArray(List<String> list) {
|
||||||
|
return "[" + list.stream().map(s -> "\"" + s + "\"").collect(Collectors.joining(", ")) + "]";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue