This is an automated email from the ASF dual-hosted git repository.
wuweijie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 5588258 Minor updates for restful (#1752) (#1753)
5588258 is described below
commit 5588258155612ecafb6b30381bbca0d34949f243
Author: Tboy <[email protected]>
AuthorDate: Wed Dec 2 18:39:41 2020 +0800
Minor updates for restful (#1752) (#1753)
---
.../elasticjob/cloud/console/ConsoleBootstrap.java | 2 +-
.../cloud/console/AbstractCloudControllerTest.java | 4 ++--
.../handler/dingtalk/DingtalkJobErrorHandlerTest.java | 2 +-
.../error/handler/wechat/WechatJobErrorHandlerTest.java | 2 +-
.../elasticjob/http/executor/HttpJobExecutorTest.java | 2 +-
.../restful/NettyRestfulServiceConfiguration.java | 6 +++---
.../restful/handler/HandlerMappingRegistry.java | 8 +++-----
.../restful/pipeline/HttpRequestDispatcher.java | 15 ++++++++-------
.../restful/pipeline/NettyRestfulServiceTest.java | 2 +-
.../NettyRestfulServiceTrailingSlashInsensitiveTest.java | 2 +-
.../NettyRestfulServiceTrailingSlashSensitiveTest.java | 2 +-
11 files changed, 23 insertions(+), 24 deletions(-)
diff --git
a/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/console/ConsoleBootstrap.java
b/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/console/ConsoleBootstrap.java
index 663ce34..1b2c324 100644
---
a/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/console/ConsoleBootstrap.java
+++
b/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/console/ConsoleBootstrap.java
@@ -41,7 +41,7 @@ public class ConsoleBootstrap {
CloudAppController.init(regCenter, producerManager);
CloudOperationController.init(regCenter, reconcileService);
NettyRestfulServiceConfiguration restfulServiceConfiguration = new
NettyRestfulServiceConfiguration(config.getPort());
- restfulServiceConfiguration.addControllerInstance(new
CloudJobController(), new CloudAppController(), new CloudOperationController());
+ restfulServiceConfiguration.addControllerInstances(new
CloudJobController(), new CloudAppController(), new CloudOperationController());
restfulServiceConfiguration.addExceptionHandler(Exception.class, new
ConsoleExceptionHandler());
restfulService = new NettyRestfulService(restfulServiceConfiguration);
}
diff --git
a/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/console/AbstractCloudControllerTest.java
b/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/console/AbstractCloudControllerTest.java
index 882867c..6553ede 100644
---
a/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/console/AbstractCloudControllerTest.java
+++
b/elasticjob-cloud/elasticjob-cloud-scheduler/src/test/java/org/apache/shardingsphere/elasticjob/cloud/console/AbstractCloudControllerTest.java
@@ -76,11 +76,11 @@ public abstract class AbstractCloudControllerTest {
private static void initMesosServer() {
MesosStateService.register("127.0.0.1", 9050);
NettyRestfulServiceConfiguration masterServerConfiguration = new
NettyRestfulServiceConfiguration(9050);
- masterServerConfiguration.addControllerInstance(new
MesosMasterServerMock());
+ masterServerConfiguration.addControllerInstances(new
MesosMasterServerMock());
masterServer = new NettyRestfulService(masterServerConfiguration);
masterServer.startup();
NettyRestfulServiceConfiguration slaveServerConfiguration = new
NettyRestfulServiceConfiguration(9051);
- slaveServerConfiguration.addControllerInstance(new
MesosSlaveServerMock());
+ slaveServerConfiguration.addControllerInstances(new
MesosSlaveServerMock());
slaveServer = new NettyRestfulService(slaveServerConfiguration);
slaveServer.startup();
}
diff --git
a/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
b/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
index 6d4b787..68b7f00 100644
---
a/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
+++
b/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-dingtalk/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/dingtalk/DingtalkJobErrorHandlerTest.java
@@ -54,7 +54,7 @@ public final class DingtalkJobErrorHandlerTest {
public static void init() {
NettyRestfulServiceConfiguration config = new
NettyRestfulServiceConfiguration(PORT);
config.setHost(HOST);
- config.addControllerInstance(new DingtalkInternalController());
+ config.addControllerInstances(new DingtalkInternalController());
restfulService = new NettyRestfulService(config);
restfulService.startup();
}
diff --git
a/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
b/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
index db227e1..0d0790a 100644
---
a/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
+++
b/elasticjob-ecosystem/elasticjob-error-handler/elasticjob-error-handler-type/elasticjob-error-handler-wechat/src/test/java/org/apache/shardingsphere/elasticjob/error/handler/wechat/WechatJobErrorHandlerTest.java
@@ -54,7 +54,7 @@ public final class WechatJobErrorHandlerTest {
public static void init() {
NettyRestfulServiceConfiguration configuration = new
NettyRestfulServiceConfiguration(PORT);
configuration.setHost(HOST);
- configuration.addControllerInstance(new WechatInternalController());
+ configuration.addControllerInstances(new WechatInternalController());
restfulService = new NettyRestfulService(configuration);
restfulService.startup();
}
diff --git
a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
index b9cddde..c89b0fa 100644
---
a/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
+++
b/elasticjob-ecosystem/elasticjob-executor/elasticjob-executor-type/elasticjob-http-executor/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
@@ -72,7 +72,7 @@ public final class HttpJobExecutorTest {
public static void init() {
NettyRestfulServiceConfiguration configuration = new
NettyRestfulServiceConfiguration(PORT);
configuration.setHost(HOST);
- configuration.addControllerInstance(new InternalController());
+ configuration.addControllerInstances(new InternalController());
restfulService = new NettyRestfulService(configuration);
restfulService.startup();
}
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/NettyRestfulServiceConfiguration.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/NettyRestfulServiceConfiguration.java
index bc35204..ec4c68a 100644
---
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/NettyRestfulServiceConfiguration.java
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/NettyRestfulServiceConfiguration.java
@@ -23,9 +23,9 @@ import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandler;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@@ -47,7 +47,7 @@ public final class NettyRestfulServiceConfiguration {
@Setter
private boolean trailingSlashSensitive;
- private final List<RestfulController> controllerInstances = new
ArrayList<>();
+ private final List<RestfulController> controllerInstances = new
LinkedList<>();
private final Map<Class<? extends Throwable>, ExceptionHandler<? extends
Throwable>> exceptionHandlers = new HashMap<>();
@@ -56,7 +56,7 @@ public final class NettyRestfulServiceConfiguration {
*
* @param instances instances of RestfulController
*/
- public void addControllerInstance(final RestfulController... instances) {
+ public void addControllerInstances(final RestfulController... instances) {
controllerInstances.addAll(Arrays.asList(instances));
}
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/HandlerMappingRegistry.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/HandlerMappingRegistry.java
index a4e81e5..2d331f9 100644
---
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/HandlerMappingRegistry.java
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/HandlerMappingRegistry.java
@@ -41,12 +41,11 @@ public final class HandlerMappingRegistry {
* @param httpRequest HTTP request
* @return A MappingContext if matched, return null if mismatched.
*/
- public MappingContext<Handler> getMappingContext(final HttpRequest
httpRequest) {
+ public Optional<MappingContext<Handler>> getMappingContext(final
HttpRequest httpRequest) {
UrlPatternMap<Handler> urlPatternMap =
mappings.get(httpRequest.method());
String uriWithoutQuery = httpRequest.uri().split("\\?")[0];
return Optional
- .ofNullable(urlPatternMap.match(uriWithoutQuery))
- .orElse(null);
+ .ofNullable(urlPatternMap.match(uriWithoutQuery));
}
/**
@@ -57,8 +56,7 @@ public final class HandlerMappingRegistry {
* @param handler handler
*/
public void addMapping(final HttpMethod method, final String pathPattern,
final Handler handler) {
- mappings.computeIfAbsent(method, httpMethod -> new
RegexUrlPatternMap<>());
- UrlPatternMap<Handler> urlPatternMap = mappings.get(method);
+ UrlPatternMap<Handler> urlPatternMap =
mappings.computeIfAbsent(method, httpMethod -> new RegexUrlPatternMap<>());
urlPatternMap.put(pathPattern, handler);
}
}
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/HttpRequestDispatcher.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/HttpRequestDispatcher.java
index 08af9bf..b862c91 100644
---
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/HttpRequestDispatcher.java
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/HttpRequestDispatcher.java
@@ -24,14 +24,14 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.elasticjob.restful.RestfulController;
+import org.apache.shardingsphere.elasticjob.restful.annotation.ContextPath;
+import org.apache.shardingsphere.elasticjob.restful.annotation.Mapping;
import org.apache.shardingsphere.elasticjob.restful.handler.HandleContext;
import org.apache.shardingsphere.elasticjob.restful.handler.Handler;
import
org.apache.shardingsphere.elasticjob.restful.handler.HandlerMappingRegistry;
import
org.apache.shardingsphere.elasticjob.restful.handler.HandlerNotFoundException;
import org.apache.shardingsphere.elasticjob.restful.mapping.MappingContext;
-import org.apache.shardingsphere.elasticjob.restful.RestfulController;
-import org.apache.shardingsphere.elasticjob.restful.annotation.ContextPath;
-import org.apache.shardingsphere.elasticjob.restful.annotation.Mapping;
import java.lang.reflect.Method;
import java.util.List;
@@ -63,13 +63,14 @@ public final class HttpRequestDispatcher extends
ChannelInboundHandlerAdapter {
if (!trailingSlashSensitive) {
request.setUri(appendTrailingSlashIfAbsent(request.uri()));
}
- MappingContext<Handler> mappingContext =
mappingRegistry.getMappingContext(request);
- if (null == mappingContext) {
+ Optional<MappingContext<Handler>> mappingContext =
mappingRegistry.getMappingContext(request);
+ if (mappingContext.isPresent()) {
+ HandleContext<Handler> handleContext = new
HandleContext<>(request, mappingContext.get());
+ ctx.fireChannelRead(handleContext);
+ } else {
ReferenceCountUtil.release(request);
throw new HandlerNotFoundException(request.uri());
}
- HandleContext<Handler> handleContext = new HandleContext<>(request,
mappingContext);
- ctx.fireChannelRead(handleContext);
}
private void initMappingRegistry(final List<RestfulController>
restfulControllers) {
diff --git
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTest.java
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTest.java
index cf9fc3f..0018b18 100644
---
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTest.java
+++
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTest.java
@@ -59,7 +59,7 @@ public final class NettyRestfulServiceTest {
public static void init() {
NettyRestfulServiceConfiguration configuration = new
NettyRestfulServiceConfiguration(PORT);
configuration.setHost(HOST);
- configuration.addControllerInstance(new JobController(), new
IndexController());
+ configuration.addControllerInstances(new JobController(), new
IndexController());
configuration.addExceptionHandler(IllegalStateException.class, new
CustomIllegalStateExceptionHandler());
restfulService = new NettyRestfulService(configuration);
restfulService.startup();
diff --git
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashInsensitiveTest.java
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashInsensitiveTest.java
index 425b7a1..cca5caa 100644
---
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashInsensitiveTest.java
+++
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashInsensitiveTest.java
@@ -33,7 +33,7 @@ public final class
NettyRestfulServiceTrailingSlashInsensitiveTest {
public void assertPathDuplicateWhenTrailingSlashInsensitive() {
NettyRestfulServiceConfiguration configuration = new
NettyRestfulServiceConfiguration(PORT);
configuration.setHost(HOST);
- configuration.addControllerInstance(new TrailingSlashTestController());
+ configuration.addControllerInstances(new
TrailingSlashTestController());
RestfulService restfulService = new NettyRestfulService(configuration);
restfulService.startup();
}
diff --git
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashSensitiveTest.java
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashSensitiveTest.java
index 9aa18e9..9caa274 100644
---
a/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashSensitiveTest.java
+++
b/elasticjob-infra/elasticjob-restful/src/test/java/org/apache/shardingsphere/elasticjob/restful/pipeline/NettyRestfulServiceTrailingSlashSensitiveTest.java
@@ -49,7 +49,7 @@ public final class
NettyRestfulServiceTrailingSlashSensitiveTest {
NettyRestfulServiceConfiguration configuration = new
NettyRestfulServiceConfiguration(PORT);
configuration.setHost(HOST);
configuration.setTrailingSlashSensitive(true);
- configuration.addControllerInstance(new TrailingSlashTestController());
+ configuration.addControllerInstances(new
TrailingSlashTestController());
restfulService = new NettyRestfulService(configuration);
restfulService.startup();
}