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();
}
}
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);
}
}
}
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()));
});
}
Aggregations