Skip to content

Commit 8aca4f1

Browse files
committed
Fixed router path bug, and support OPTIONS check.
1 parent 0ca902c commit 8aca4f1

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import akka.actor.ActorRef;
55
import tech.dbgsoftware.easyrest.actors.ActorFactory;
66
import tech.dbgsoftware.easyrest.actors.ExceptionHandleActor;
7+
import tech.dbgsoftware.easyrest.actors.response.OutputActor;
78
import tech.dbgsoftware.easyrest.aop.StaticAopStepUtil;
89
import tech.dbgsoftware.easyrest.model.HttpEntity;
910
import tech.dbgsoftware.easyrest.utils.LogUtils;
@@ -15,7 +16,7 @@ public Receive createReceive() {
1516
return receiveBuilder().match(HttpEntity.class, (httpEntity -> {
1617
HttpEntity[] httpEntityTemp = {httpEntity};
1718
StaticAopStepUtil.getAopPreCommitStepList().forEach((step) -> {
18-
if (httpEntityTemp[0].getErrorMap().size() == 0) {
19+
if (httpEntityTemp[0].getErrorMap().size() == 0 && !httpEntityTemp[0].isOptionsCheck()) {
1920
try {
2021
httpEntityTemp[0] = step.executeStep(httpEntityTemp[0]);
2122
} catch (Exception e) {
@@ -25,7 +26,11 @@ public Receive createReceive() {
2526
}
2627
});
2728
if (httpEntityTemp[0].getErrorMap().size() == 0) {
28-
ActorFactory.createActor(ControllerInvokeActor.class).tell(httpEntityTemp[0], ActorRef.noSender());
29+
if (httpEntityTemp[0].isOptionsCheck()) {
30+
ActorFactory.createActor(OutputActor.class).tell(httpEntityTemp[0], ActorRef.noSender());
31+
} else {
32+
ActorFactory.createActor(ControllerInvokeActor.class).tell(httpEntityTemp[0], ActorRef.noSender());
33+
}
2934
} else {
3035
ActorFactory.createActor(ExceptionHandleActor.class).tell(httpEntityTemp[0], ActorRef.noSender());
3136
}

EasyRest/src/main/java/tech/dbgsoftware/easyrest/actors/response/OutputActor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class OutputActor extends AbstractActor {
1010
@Override
1111
public Receive createReceive() {
1212
return receiveBuilder().match(HttpEntity.class, (httpEntity -> {
13-
if (httpEntity.getRestObject().getMethod().getReturnType().equals(ResponseEntity.class) ||
13+
if (httpEntity.isOptionsCheck() ||
14+
httpEntity.getRestObject().getMethod().getReturnType().equals(ResponseEntity.class) ||
1415
httpEntity.getRestObject().getMethod().getReturnType().getSimpleName().equalsIgnoreCase(Void.class.getSimpleName())) {
1516
httpEntity.getResponse().buildResponse(httpEntity.getResponseEntity());
1617
} else {

EasyRest/src/main/java/tech/dbgsoftware/easyrest/aop/pre/AopRequestValidateStep.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ public HttpEntity executeStep(HttpEntity entity) throws Exception {
2929
throw new PageNotFoundException(String.format(NOT_FOUND, url));
3030
} else {
3131
if (!restObject.getHttpMethodList().contains(entity.getRequest().getRequestHttpMethod().toLowerCase())){
32-
entity.getResponse().getRealResponse().setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
33-
throw new MethodNotAllowedException(String.format(NOT_ALLOWED, entity.getRequest().getRequestHttpMethod()));
32+
if (entity.getRequest().getRequestHttpMethod().toLowerCase().equalsIgnoreCase("options")) {
33+
entity.setOptionsCheck(true);
34+
} else {
35+
entity.getResponse().getRealResponse().setStatus(HttpResponseStatus.METHOD_NOT_ALLOWED);
36+
throw new MethodNotAllowedException(String.format(NOT_ALLOWED, entity.getRequest().getRequestHttpMethod()));
37+
}
3438
}
3539
}
3640
entity.setRestObject(restObject);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class HttpEntity {
1414
private Request request;
1515
private Response response;
1616
private Class controller;
17+
private Boolean isOptionsCheck = false;
1718
private ChannelHandlerContext channelHandlerContext;
1819
private ResponseEntity responseEntity;
1920
private RestObject restObject;
@@ -26,6 +27,14 @@ public HttpEntity(Request request, Response response, ChannelHandlerContext chan
2627
this.channelHandlerContext = channelHandlerContext;
2728
}
2829

30+
public Boolean isOptionsCheck() {
31+
return isOptionsCheck;
32+
}
33+
34+
public void setOptionsCheck(Boolean optionsCheck) {
35+
isOptionsCheck = optionsCheck;
36+
}
37+
2938
public void setMethod(Method method) {
3039
this.method = method;
3140
}

EasyRest/src/main/java/tech/dbgsoftware/easyrest/network/router/RouterProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void putRouter(String bindUrl, String methodUrl, HttpMethod httpM
7474
url.insert(0, "/");
7575
}
7676
if (!bindUrl.endsWith("/") && !methodUrl.startsWith("/")){
77-
url.insert(bindUrl.length(), "/");
77+
url.insert(url.length(), "/");
7878
}
7979
url.append(methodUrl);
8080
RouterProvider.registerUrl(url.toString(), httpMethod, restObject);

0 commit comments

Comments
 (0)