Skip to content

Commit 270d039

Browse files
committed
refactor(config): improve JSON parsing and logging in getTabularIngestSizeLimits
Refactored `getTabularIngestSizeLimits` to utilize try-with-resources for safer JSON parsing. Enhanced logging with lambdas and improved iteration over JSON entries.
1 parent 0ca703a commit 270d039

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/main/java/edu/harvard/iq/dataverse/util/SystemConfig.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -519,46 +519,49 @@ public Map<String, Long> getTabularIngestSizeLimits() {
519519
if (limitEntry != null) {
520520
// Case A: the setting is using JSON to support multiple formats
521521
if (limitEntry.trim().startsWith("{")) {
522-
try {
523-
JsonObject limits = Json.createReader(new StringReader(limitEntry)).readObject();
522+
try (JsonReader reader = Json.createReader(new StringReader(limitEntry))) {
523+
JsonObject limits = reader.readObject();
524524

525525
Map<String, Long> limitsMap = new HashMap<>();
526526
// We add the default in case the JSON does not contain the default (which is optional).
527527
limitsMap.put(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, -1L);
528528

529-
for (String formatName : limits.keySet()) {
529+
for (Map.Entry<String, JsonValue> format : limits.entrySet()) {
530+
String formatName = format.getKey();
530531
String lowercaseFormatName = formatName.toLowerCase();
531532

532533
try {
533-
JsonValue value = limits.get(formatName);
534-
Long sizeOption;
534+
JsonValue value = format.getValue();
535+
long sizeOption;
535536

536537
// We want to be able to use either numbers or string values, so detect which one it is.
538+
// This is necessary as we need to tell the JSON parser what to do, it doesn't automatically handle this for us.
537539
if (value.getValueType() == JsonValue.ValueType.STRING) {
538-
sizeOption = Long.valueOf(limits.getString(formatName));
540+
sizeOption = Long.parseLong(limits.getString(formatName));
539541
} else if (value.getValueType() == JsonValue.ValueType.NUMBER) {
542+
// Will throw if not a whole number!
540543
sizeOption = limits.getJsonNumber(formatName).longValueExact();
541544
} else {
542-
logger.warning("Invalid value type for format " + formatName + ": expected string or number");
545+
logger.warning(() -> "Invalid value type for format " + formatName + ": expected string or number");
543546
logger.warning("Disabling all tabular ingest completely until fixed!");
544547
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, 0L);
545548
}
546549

547550
limitsMap.put(lowercaseFormatName, sizeOption);
548551
} catch (NumberFormatException nfe) {
549-
logger.warning("Could not convert " + SettingsServiceBean.Key.TabularIngestSizeLimit + " to long for format " + formatName + " (not a valid number)");
552+
logger.warning(() -> "Could not convert " + SettingsServiceBean.Key.TabularIngestSizeLimit + " to long for format " + formatName + " (not a valid number)");
550553
logger.warning("Disabling all tabular ingest completely until fixed!");
551554
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, 0L);
552555
} catch (ArithmeticException ae) {
553-
logger.warning("Number too large or has fractional part for format " + formatName);
556+
logger.warning(() -> "Number too large or has fractional part for format " + formatName);
554557
logger.warning("Disabling all tabular ingest completely until fixed!");
555558
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, 0L);
556559
}
557560
}
558561

559562
return Collections.unmodifiableMap(limitsMap);
560563
} catch (JsonParsingException e) {
561-
logger.warning("Invalid TabularIngestSizeLimit option found, cannot parse JSON: " + e.getMessage());
564+
logger.warning(() -> "Invalid TabularIngestSizeLimit option found, cannot parse JSON: " + e.getMessage());
562565
logger.warning("Disabling all tabular ingest completely until fixed!");
563566
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, 0L);
564567
}
@@ -568,7 +571,7 @@ public Map<String, Long> getTabularIngestSizeLimits() {
568571
Long limit = Long.valueOf(limitEntry);
569572
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, limit);
570573
} catch (NumberFormatException nfe) {
571-
logger.warning("Could not convert " + SettingsServiceBean.Key.TabularIngestSizeLimit + " to long: " + nfe);
574+
logger.warning(() -> "Could not convert " + SettingsServiceBean.Key.TabularIngestSizeLimit + " to long: " + nfe);
572575
logger.warning("Disabling all tabular ingest completely until fixed!");
573576
return Map.of(TABULAR_INGEST_SIZE_LIMITS_DEFAULT_KEY, 0L);
574577
}

0 commit comments

Comments
 (0)