winterhazel commented on code in PR #9107:
URL: https://github.com/apache/cloudstack/pull/9107#discussion_r1727676781
##########
server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java:
##########
@@ -1211,132 +1215,142 @@ protected String validateConfigurationValue(final
String name, String value, fin
} else {
type = configuration.getType();
}
- //no need to validate further if a
- //config can have null value.
- String errMsg = null;
- try {
- if (type.equals(Integer.class)) {
- errMsg = "There was error in trying to parse value: " + value
+ ". Please enter a valid integer value for parameter " + name;
- Integer.parseInt(value);
- } else if (type.equals(Float.class)) {
- errMsg = "There was error in trying to parse value: " + value
+ ". Please enter a valid float value for parameter " + name;
- Float.parseFloat(value);
- } else if (type.equals(Long.class)) {
- errMsg = "There was error in trying to parse value: " + value
+ ". Please enter a valid long value for parameter " + name;
- Long.parseLong(value);
- }
- } catch (final Exception e) {
- // catching generic exception as some throws NullPointerException
and some throws NumberFormatExcpeion
- logger.error(errMsg);
- return errMsg;
+
+ boolean isTypeValid = validateValueType(value, type);
+ if (!isTypeValid) {
+ return String.format("Value [%s] is not a valid [%s].", value,
type);
}
- if (value == null) {
- if (type.equals(Boolean.class)) {
- return "Please enter either 'true' or 'false'.";
- }
- if (overprovisioningFactorsForValidation.contains(name)) {
- final String msg = "value cannot be null for the parameter " +
name;
- logger.error(msg);
- return msg;
- }
- return null;
+ return validateValueRange(name, value, type, configuration);
+ }
+
+ /**
+ * Returns whether a value is valid for a configuration of the provided
type.
+ * Valid configuration values are:
+ *
+ * <ul>
+ * <li>String: any value, including null;</li>
+ * <li>Character: any value, including null;</li>
+ * <li>Boolean: strings that contain "true" or "false"
(case-sensitive);</li>
+ * <li>Integer: strings that contain exactly an integer. Values that
contain a decimal aren't valid;</li>
+ * <li>Short, Long, Float and Double: strings that contain an integer
or a decimal.</li>
+ * </ul>
+ *
+ * If a type isn't listed here, then the value will be considered invalid.
+ * @param value value to validate.
+ * @param type type of the configuration.
+ * @return boolean indicating whether the value is valid.
+ */
+ protected boolean validateValueType(String value, Class<?> type) {
+ if (type == String.class || type == Character.class) {
+ return true;
}
- value = value.trim();
try {
- if (overprovisioningFactorsForValidation.contains(name) &&
Float.parseFloat(value) <= 0f) {
- final String msg = name + " should be greater than 0";
- logger.error(msg);
- throw new InvalidParameterValueException(msg);
+ if (type == Boolean.class) {
+ return value.equals("true") || value.equals("false");
+ } else if (type == Integer.class || type == Long.class || type ==
Short.class) {
+ Integer.parseInt(value);
Review Comment:
You're right, it won't if the value exceeds +/- 2147483647. I'll adjust it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]