use of org.rx.util.StringBuilder in project rxlib by RockyLOMO.
the class App method readSetting.
@ErrorCode(value = "keyError", messageKeys = { "$key", "$file" })
@ErrorCode(value = "partialKeyError", messageKeys = { "$key", "$file" })
public static Object readSetting(String key, String yamlFile, boolean throwOnEmpty) {
Map<String, Object> settings = readSettings(yamlFile + ".yml");
Object val;
if ((val = settings.get(key)) != null) {
return val;
}
StringBuilder kBuf = new StringBuilder();
String d = ".";
String[] splits = split(key, d);
int c = splits.length - 1;
for (int i = 0; i <= c; i++) {
if (kBuf.getLength() > 0) {
kBuf.append(d);
}
String k = kBuf.append(splits[i]).toString();
if ((val = settings.get(k)) == null) {
continue;
}
if (i == c) {
return val;
}
if ((settings = as(val, Map.class)) == null) {
throw new SystemException(values(k, yamlFile), "partialKeyError");
}
kBuf.setLength(0);
}
if (!throwOnEmpty) {
return null;
}
throw new SystemException(values(key, yamlFile), "keyError");
}
use of org.rx.util.StringBuilder in project rxlib by RockyLOMO.
the class SystemException method setFriendlyMessage.
private void setFriendlyMessage(Map<String, Object> methodSettings, String messageName, ErrorCode errorCode, Object[] messageValues) {
if (!App.isNullOrEmpty(errorCode.value())) {
messageName += "[" + errorCode.value() + "]";
}
if (!Exception.class.equals(errorCode.cause())) {
messageName += "<" + errorCode.cause().getSimpleName() + ">";
}
String msg = as(methodSettings.get(messageName), String.class);
if (msg == null) {
Logger.debug("SystemException: Not found messageName %s", messageName);
return;
}
switch(errorCode.messageFormatter()) {
case StringFormat:
msg = String.format(msg, messageValues);
break;
case MessageFormat:
msg = MessageFormat.format(msg, messageValues);
break;
default:
String[] messageKeys = errorCode.messageKeys();
if (messageKeys.length != messageValues.length) {
Logger.debug("SystemException: MessageKeys length %s not equals messageValues length %s", messageKeys.length, messageValues.length);
return;
}
NQuery<Tuple<String, Object>> query = NQuery.of(messageKeys).select((p, i) -> Tuple.of(p, messageValues[i]));
if (data != null) {
query = query.union(NQuery.of(data.entrySet()).select(p -> Tuple.of(p.getKey(), p.getValue())).toList());
}
StringBuilder temp = new StringBuilder().append(msg);
for (Tuple<String, Object> tuple : query.orderByDescending(p -> p.left.length())) {
temp.replace(tuple.left, toJsonString(tuple.right));
}
msg = temp.toString();
break;
}
friendlyMessage = msg;
}
use of org.rx.util.StringBuilder in project rxlib by RockyLOMO.
the class FeignInterceptor method onProcess.
@Override
protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
Signature signature = joinPoint.getSignature();
if (!(signature instanceof MethodSignature)) {
return joinPoint.proceed();
}
Method method = ((MethodSignature) signature).getMethod();
RequestMapping apiMapping = method.getAnnotation(RequestMapping.class);
if (apiMapping == null) {
return joinPoint.proceed();
}
String url = "";
FeignClient feignClient = null;
for (Class<?> pi : joinPoint.getTarget().getClass().getInterfaces()) {
if ((feignClient = pi.getAnnotation(FeignClient.class)) != null) {
break;
}
}
if (feignClient != null) {
url += feignClient.url();
}
RequestMapping baseMapping = method.getDeclaringClass().getAnnotation(RequestMapping.class);
Function<RequestMapping, String> pf = p -> String.join(",", !ArrayUtils.isEmpty(p.value()) ? p.value() : p.path());
if (baseMapping != null) {
url += pf.apply(baseMapping);
}
url += pf.apply(apiMapping);
String httpMethod = ArrayUtils.isEmpty(apiMapping.method()) ? "POST" : String.join(",", Arrays.stream(apiMapping.method()).map(p -> p.name()).collect(Collectors.toList()));
msg.appendLine().appendLine("%s\t\t%s", httpMethod, resolveUrl(url, signature));
return super.onProcess(joinPoint, msg);
}
Aggregations