Search in sources :

Example 1 with StringBuilder

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");
}
Also used : StringBuilder(org.rx.util.StringBuilder)

Example 2 with StringBuilder

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;
}
Also used : NestedRuntimeException(org.springframework.core.NestedRuntimeException) AccessibleObject(java.lang.reflect.AccessibleObject) Contract.toJsonString(org.rx.Contract.toJsonString) BiTuple(org.rx.bean.BiTuple) HashMap(java.util.HashMap) Field(java.lang.reflect.Field) Tuple(org.rx.bean.Tuple) MessageFormat(java.text.MessageFormat) WeakCache(org.rx.cache.WeakCache) StringBuilder(org.rx.util.StringBuilder) Const(org.rx.bean.Const) Map(java.util.Map) Contract(org.rx.Contract) Method(java.lang.reflect.Method) StringBuilder(org.rx.util.StringBuilder) AccessibleObject(java.lang.reflect.AccessibleObject) Contract.toJsonString(org.rx.Contract.toJsonString) NestedRuntimeException(org.springframework.core.NestedRuntimeException) BiTuple(org.rx.bean.BiTuple) Tuple(org.rx.bean.Tuple)

Example 3 with StringBuilder

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);
}
Also used : Arrays(java.util.Arrays) Signature(org.aspectj.lang.Signature) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Tuple(org.rx.bean.Tuple) StringBuilder(org.rx.util.StringBuilder) FeignClient(org.springframework.cloud.netflix.feign.FeignClient) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) LogInterceptor(org.rx.util.LogInterceptor) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) FeignClient(org.springframework.cloud.netflix.feign.FeignClient) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

StringBuilder (org.rx.util.StringBuilder)3 Method (java.lang.reflect.Method)2 Tuple (org.rx.bean.Tuple)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 Field (java.lang.reflect.Field)1 MessageFormat (java.text.MessageFormat)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ArrayUtils (org.apache.commons.lang3.ArrayUtils)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Signature (org.aspectj.lang.Signature)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 Contract (org.rx.Contract)1 Contract.toJsonString (org.rx.Contract.toJsonString)1 BiTuple (org.rx.bean.BiTuple)1 Const (org.rx.bean.Const)1 WeakCache (org.rx.cache.WeakCache)1