Skip to content

Commit e1ec855

Browse files
committed
Updating customer injection for per check.
1 parent 02495e4 commit e1ec855

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/request/ControllerInvokeActor.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import tech.dbgsoftware.easyrest.actors.Signal;
88
import tech.dbgsoftware.easyrest.actors.remote.model.RemoteInvokeObject;
99
import tech.dbgsoftware.easyrest.actors.response.ResponseProcessActor;
10+
import tech.dbgsoftware.easyrest.aop.customer.CustomInjection;
1011
import tech.dbgsoftware.easyrest.ioc.utils.BeanOperationUtils;
1112
import tech.dbgsoftware.easyrest.model.HttpEntity;
1213
import tech.dbgsoftware.easyrest.model.ResponseEntity;
@@ -23,22 +24,22 @@ public Receive createReceive() {
2324
Method method = httpEntity.getMethod();
2425
Class<?> controller = httpEntity.getController();
2526
if (method.getReturnType().getName().equalsIgnoreCase(Void.class.getSimpleName())) {
26-
invokeMethod(method, controller, httpEntity.getArgs(), null);
27+
invokeMethod(method, controller, httpEntity.getArgs(), null, httpEntity);
2728
httpEntity.setResponseEntity(ResponseEntity.buildOkResponse());
2829
} else if (method.getReturnType().equals(ResponseEntity.class)) {
29-
httpEntity.setResponseEntity((ResponseEntity) invokeMethod(method, controller, httpEntity.getArgs(), null));
30+
httpEntity.setResponseEntity((ResponseEntity) invokeMethod(method, controller, httpEntity.getArgs(), null, httpEntity));
3031
} else {
31-
httpEntity.setResponseEntity(ResponseEntity.buildBaseResponse(invokeMethod(method, controller, httpEntity.getArgs(), null)));
32+
httpEntity.setResponseEntity(ResponseEntity.buildBaseResponse(invokeMethod(method, controller, httpEntity.getArgs(), null, httpEntity)));
3233
}
3334
ActorFactory.createActor(ResponseProcessActor.class).tell(httpEntity, ActorRef.noSender());
3435
} catch (Exception e) {
3536
LogUtils.error(e.getMessage(), e);
36-
httpEntity.addError(e);
37+
httpEntity.addError(e.getCause());
3738
ActorFactory.createActor(ExceptionHandleActor.class).tell(httpEntity, ActorRef.noSender());
3839
}
3940
})).match(RemoteInvokeObject.class, (remoteInvokeObject -> {
4041
try {
41-
Object result = invokeMethod(remoteInvokeObject.getMethod(), remoteInvokeObject.getImplClass(), remoteInvokeObject.getArgs(), remoteInvokeObject.getInvokeBeanName());
42+
Object result = invokeMethod(remoteInvokeObject.getMethod(), remoteInvokeObject.getImplClass(), remoteInvokeObject.getArgs(), remoteInvokeObject.getInvokeBeanName(), null);
4243
if (remoteInvokeObject.getMethod().getReturnType().getName().equalsIgnoreCase(Void.class.getSimpleName())) {
4344
remoteInvokeObject.setResult(ResponseEntity.buildOkResponse());
4445
} else {
@@ -52,20 +53,30 @@ public Receive createReceive() {
5253
})).build();
5354
}
5455

55-
private Object invokeMethod(Method method, Class<?> controller, Object[] args, String invokeBeanName) throws Exception {
56+
private Object invokeMethod(Method method, Class<?> controller, Object[] args, String invokeBeanName, HttpEntity httpEntity) throws Exception {
5657
if (args.length > 0) {
5758
if (invokeBeanName == null || invokeBeanName.equalsIgnoreCase("null")) {
59+
invokePerCheck(BeanOperationUtils.getBean(controller), httpEntity);
5860
return method.invoke(BeanOperationUtils.getBean(controller), args);
5961
} else {
62+
invokePerCheck(BeanOperationUtils.getBean(invokeBeanName, controller), httpEntity);
6063
return method.invoke(BeanOperationUtils.getBean(invokeBeanName, controller), args);
6164
}
6265
} else {
6366
if (invokeBeanName == null || invokeBeanName.equalsIgnoreCase("null")) {
67+
invokePerCheck(BeanOperationUtils.getBean(controller), httpEntity);
6468
return method.invoke(BeanOperationUtils.getBean(controller));
6569
} else {
70+
invokePerCheck(BeanOperationUtils.getBean(invokeBeanName, controller), httpEntity);
6671
return method.invoke(BeanOperationUtils.getBean(invokeBeanName, controller));
6772
}
6873
}
6974
}
7075

76+
private void invokePerCheck(Object classz, HttpEntity httpEntity) throws Exception {
77+
if (httpEntity != null && classz instanceof CustomInjection) {
78+
CustomInjection.class.getMethods()[0].invoke(classz, httpEntity);
79+
}
80+
}
81+
7182
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tech.dbgsoftware.easyrest.aop.customer;
2+
3+
import tech.dbgsoftware.easyrest.model.HttpEntity;
4+
5+
public interface CustomInjection {
6+
7+
HttpEntity preCheck(HttpEntity httpEntity);
8+
9+
}

EasyRest/src/main/java/tech/dbgsoftware/easyrest/model/HttpEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public void addError(Exception e){
7474
errorMap.put(e.getMessage(), e.getClass().getSimpleName());
7575
}
7676

77+
public void addError(Throwable e){
78+
errorMap.put(e.getMessage(), e.getClass().getSimpleName());
79+
}
80+
7781
public RestObject getRestObject() {
7882
return restObject;
7983
}

Example/Example-Quick-Start/src/main/java/com/example/rest/TestRestService.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import tech.dbgsoftware.easyrest.annotations.method.Get;
55
import tech.dbgsoftware.easyrest.annotations.method.Post;
66
import tech.dbgsoftware.easyrest.annotations.parameter.AllDefined;
7+
import tech.dbgsoftware.easyrest.aop.customer.CustomInjection;
8+
import tech.dbgsoftware.easyrest.exception.PermissionException;
9+
import tech.dbgsoftware.easyrest.model.HttpEntity;
710

811
@BindURL("/test")
9-
public interface TestRestService {
12+
public interface TestRestService extends CustomInjection {
1013

1114
@Get
1215
String ping();
@@ -15,4 +18,8 @@ public interface TestRestService {
1518
@AllDefined
1619
String test(String a, String b);
1720

21+
@Override
22+
default HttpEntity preCheck(HttpEntity httpEntity) {
23+
throw new PermissionException("No permission for this rest api.");
24+
}
1825
}

0 commit comments

Comments
 (0)