Search in sources :

Example 1 with ProceedEventArgs

use of org.rx.bean.ProceedEventArgs in project rxlib by RockyLOMO.

the class BaseInterceptor method doAround.

public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
    if (BooleanUtils.isTrue(idempotent.get())) {
        return joinPoint.proceed();
    }
    idempotent.set(Boolean.TRUE);
    try {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = as(signature, MethodSignature.class);
        boolean isVoid = methodSignature == null || methodSignature.getReturnType().equals(void.class);
        ProceedEventArgs eventArgs = new ProceedEventArgs(signature.getDeclaringType(), joinPoint.getArgs(), isVoid);
        raiseEvent(onProcessing, eventArgs);
        if (eventArgs.isCancel()) {
            return joinPoint.proceed();
        }
        RxConfig rxConfig = RxConfig.INSTANCE;
        eventArgs.setLogStrategy(rxConfig.getLogStrategy());
        eventArgs.setLogTypeWhitelist(rxConfig.getLogTypeWhitelist());
        try {
            eventArgs.proceed(() -> joinPoint.proceed(eventArgs.getParameters()));
        } catch (Throwable e) {
            eventArgs.setError(e);
            raiseEvent(onError, eventArgs);
            if (eventArgs.getError() != null) {
                throw eventArgs.getError();
            }
            eventArgs.setError(e);
        } finally {
            raiseEvent(onProceed, eventArgs);
            App.log(eventArgs, msg -> {
                msg.appendLine("Call:\t%s", signature.getName());
                msg.appendLine("Parameters:\t%s", jsonString(signature, eventArgs.getParameters()));
                if (eventArgs.getError() != null) {
                    msg.appendLine("Error:\t%s", eventArgs.getError().getMessage());
                } else {
                    msg.appendLine("ReturnValue:\t%s\tElapsed=%sms", jsonString(signature, eventArgs.getReturnValue()), eventArgs.getElapsedMillis());
                }
            });
        }
        return eventArgs.getReturnValue();
    } finally {
        idempotent.remove();
    }
}
Also used : ProceedEventArgs(org.rx.bean.ProceedEventArgs) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature)

Example 2 with ProceedEventArgs

use of org.rx.bean.ProceedEventArgs in project rxlib by RockyLOMO.

the class HttpClient method invoke.

@SneakyThrows
private ResponseContent invoke(String url, HttpMethod method, RequestContent content) {
    ProceedEventArgs args = new ProceedEventArgs(this.getClass(), new Object[] { method, content.toString() }, false);
    try {
        Request.Builder request = createRequest(url);
        RequestBody requestBody = content.toBody();
        switch(method) {
            case POST:
                request.post(requestBody);
                break;
            case HEAD:
                request.head();
                break;
            case PUT:
                request.put(requestBody);
                break;
            case PATCH:
                request.patch(requestBody);
                break;
            case DELETE:
                request.delete(requestBody);
                break;
            default:
                request.get();
                break;
        }
        if (responseContent != null) {
            responseContent.response.close();
        }
        return responseContent = args.proceed(() -> new ResponseContent(client.newCall(request.build()).execute()));
    } catch (Throwable e) {
        args.setError(e);
        throw e;
    } finally {
        if (enableLog) {
            if (responseContent != null) {
                App.logExtra("body", responseContent.toString());
            }
            logHttp(args, url);
        }
    }
}
Also used : ProceedEventArgs(org.rx.bean.ProceedEventArgs) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Example 3 with ProceedEventArgs

use of org.rx.bean.ProceedEventArgs in project rxlib by RockyLOMO.

the class RestClient method facade.

public static <T> T facade(Class<T> contract, String serverPrefixUrl, BiFunc<String, Boolean> checkResponse) {
    RequestMapping baseMapping = contract.getAnnotation(RequestMapping.class);
    String prefix = serverPrefixUrl + getFirstPath(baseMapping);
    boolean defMethod = isPostMethod(baseMapping);
    return proxy(contract, (m, p) -> {
        RequestMapping pathMapping = m.getAnnotation(RequestMapping.class);
        String path = getFirstPath(pathMapping);
        if (Strings.isEmpty(path)) {
            path = m.getName();
        }
        String reqUrl = prefix + path;
        boolean doPost = Arrays.isEmpty(pathMapping.method()) ? defMethod : isPostMethod(pathMapping);
        Parameter[] parameters = m.getParameters();
        Func<Map<String, Object>> getFormData = () -> {
            Map<String, Object> data = new HashMap<>();
            for (int i = 0; i < parameters.length; i++) {
                Parameter parameter = parameters[i];
                RequestParam param = parameter.getAnnotation(RequestParam.class);
                String name = param != null ? !Strings.isEmpty(param.value()) ? param.value() : param.name() : parameter.getName();
                Object val = p.arguments[i];
                if (val == null && param != null) {
                    val = Reflects.changeType(param.defaultValue(), parameter.getType());
                }
                data.put(name, val);
            }
            return data;
        };
        String responseText;
        ProceedEventArgs args = new ProceedEventArgs(contract, new Object[1], m.getReturnType().equals(void.class));
        HttpClient client = new HttpClient();
        try {
            if (doPost) {
                if (parameters.length == 1 && parameters[0].isAnnotationPresent(RequestBody.class)) {
                    args.getParameters()[0] = p.arguments[0];
                    responseText = args.proceed(() -> client.postJson(reqUrl, args.getParameters()[0]).toString());
                } else {
                    Map<String, Object> data = getFormData.invoke();
                    args.getParameters()[0] = data;
                    responseText = args.proceed(() -> client.post(reqUrl, data).toString());
                }
            } else {
                Map<String, Object> data = getFormData.invoke();
                args.getParameters()[0] = data;
                responseText = args.proceed(() -> client.get(HttpClient.buildUrl(reqUrl, data)).toString());
            }
            if (checkResponse != null && !checkResponse.invoke(responseText)) {
                throw new InvalidException("Response status error");
            }
        } catch (Exception e) {
            args.setError(e);
            throw e;
        } finally {
            App.log(args, msg -> {
                if (doPost) {
                    msg.appendLine("POST: %s %s", args.getTraceId(), reqUrl);
                } else {
                    msg.appendLine("GET: %s %s", args.getTraceId(), reqUrl);
                }
                msg.appendLine("Request:\t%s", toJsonString(args.getParameters()));
                msg.append("Response:\t%s", args.getReturnValue());
            });
        }
        if (m.getReturnType().equals(Void.class)) {
            return null;
        }
        return fromJson(responseText, ifNull(RESULT_TYPE.get(), m.getReturnType()));
    });
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) ProceedEventArgs(org.rx.bean.ProceedEventArgs) InvalidException(org.rx.exception.InvalidException) InvalidException(org.rx.exception.InvalidException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Parameter(java.lang.reflect.Parameter) HashMap(java.util.HashMap) Map(java.util.Map) RequestBody(org.springframework.web.bind.annotation.RequestBody)

Aggregations

ProceedEventArgs (org.rx.bean.ProceedEventArgs)3 Parameter (java.lang.reflect.Parameter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Signature (org.aspectj.lang.Signature)1 MethodSignature (org.aspectj.lang.reflect.MethodSignature)1 InvalidException (org.rx.exception.InvalidException)1 RequestBody (org.springframework.web.bind.annotation.RequestBody)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 RequestParam (org.springframework.web.bind.annotation.RequestParam)1