package com.vincent.rsf.httpaudit.props;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import java.util.Collections;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
/**
|
* 审计配置缓存
|
*/
|
public final class HttpAuditDbConfigHolder {
|
|
public static final String KEY_WHITELIST_ONLY = "whitelist-only";
|
public static final String KEY_EXCLUDE_AUDIT_SELF_PATHS = "exclude-audit-self-paths";
|
public static final String KEY_RULE_CACHE_REFRESH_MS = "rule-cache-refresh-ms";
|
public static final String KEY_QUERY_RESPONSE_MAX_CHARS = "query-response-max-chars";
|
public static final String KEY_MAX_RESPONSE_STORE_CHARS = "max-response-store-chars";
|
public static final String KEY_DEFAULT_REQUEST_STORE_CHARS = "default-request-store-chars";
|
public static final String KEY_PATH_DESCRIPTIONS = "path-descriptions";
|
public static final String KEY_CLEANUP_ENABLED = "cleanup-enabled";
|
public static final String KEY_CLEANUP_RETENTION_DAYS = "cleanup-retention-days";
|
|
private static final ConcurrentHashMap<String, String> CONFIG = new ConcurrentHashMap<>();
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
|
private HttpAuditDbConfigHolder() {
|
}
|
|
public static void replace(Map<String, String> map) {
|
CONFIG.clear();
|
if (map != null && !map.isEmpty()) {
|
CONFIG.putAll(map);
|
}
|
}
|
|
public static boolean getBoolean(String key, boolean defaultValue) {
|
String raw = CONFIG.get(key);
|
if (raw == null) {
|
return defaultValue;
|
}
|
return "1".equals(raw) || "true".equalsIgnoreCase(raw.trim());
|
}
|
|
public static int getInt(String key, int defaultValue) {
|
String raw = CONFIG.get(key);
|
if (raw == null) {
|
return defaultValue;
|
}
|
try {
|
return Integer.parseInt(raw.trim());
|
} catch (Exception ignore) {
|
return defaultValue;
|
}
|
}
|
|
public static long getLong(String key, long defaultValue) {
|
String raw = CONFIG.get(key);
|
if (raw == null) {
|
return defaultValue;
|
}
|
try {
|
return Long.parseLong(raw.trim());
|
} catch (Exception ignore) {
|
return defaultValue;
|
}
|
}
|
|
public static Map<String, String> getPathDescriptions(Map<String, String> defaultValue) {
|
String raw = CONFIG.get(KEY_PATH_DESCRIPTIONS);
|
if (raw == null || raw.trim().isEmpty()) {
|
return defaultValue;
|
}
|
try {
|
Map<String, String> parsed = MAPPER.readValue(raw, new TypeReference<Map<String, String>>() {
|
});
|
return parsed == null ? defaultValue : parsed;
|
} catch (Exception ignore) {
|
return defaultValue;
|
}
|
}
|
|
public static Map<String, String> snapshot() {
|
return Collections.unmodifiableMap(CONFIG);
|
}
|
}
|