This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch restful-api
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/restful-api by this push:
new 36f3282 Add default 404 handler and make it can be overridden. (#1401)
36f3282 is described below
commit 36f328222442e14a40dd4fa3759e5f7fa416ef7e
Author: 吴伟杰 <[email protected]>
AuthorDate: Sun Aug 23 22:59:44 2020 +0800
Add default 404 handler and make it can be overridden. (#1401)
---
.../handler/impl/DefaultExceptionHandler.java | 38 +++++++++++++++++++++
.../DefaultHandlerNotFoundExceptionHandler.java | 39 ++++++++++++++++++++++
.../restful/pipeline/ExceptionHandling.java | 38 ++++++++++++---------
.../restful/pipeline/NettyRestfulServiceTest.java | 8 +++++
4 files changed, 107 insertions(+), 16 deletions(-)
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultExceptionHandler.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultExceptionHandler.java
new file mode 100644
index 0000000..55ca96b
--- /dev/null
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultExceptionHandler.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.elasticjob.restful.handler.impl;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+import org.apache.shardingsphere.elasticjob.restful.Http;
+import
org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandleResult;
+import org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandler;
+
+/**
+ * A default handler for handling {@link Throwable}.
+ */
+public final class DefaultExceptionHandler implements
ExceptionHandler<Throwable> {
+
+ @Override
+ public ExceptionHandleResult handleException(final Throwable ex) {
+ return ExceptionHandleResult.builder()
+ .statusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code())
+ .contentType(Http.DEFAULT_CONTENT_TYPE)
+ .result(ex.getLocalizedMessage())
+ .build();
+ }
+}
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultHandlerNotFoundExceptionHandler.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultHandlerNotFoundExceptionHandler.java
new file mode 100644
index 0000000..9b78c27
--- /dev/null
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/handler/impl/DefaultHandlerNotFoundExceptionHandler.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.elasticjob.restful.handler.impl;
+
+import io.netty.handler.codec.http.HttpResponseStatus;
+import org.apache.shardingsphere.elasticjob.restful.Http;
+import
org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandleResult;
+import org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandler;
+import
org.apache.shardingsphere.elasticjob.restful.handler.HandlerNotFoundException;
+
+/**
+ * A default handler for {@link HandlerNotFoundException}.
+ */
+public final class DefaultHandlerNotFoundExceptionHandler implements
ExceptionHandler<HandlerNotFoundException> {
+
+ @Override
+ public ExceptionHandleResult handleException(final
HandlerNotFoundException ex) {
+ return ExceptionHandleResult.builder()
+ .statusCode(HttpResponseStatus.NOT_FOUND.code())
+ .result(ex.getLocalizedMessage())
+ .contentType(Http.DEFAULT_CONTENT_TYPE)
+ .build();
+ }
+}
diff --git
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/ExceptionHandling.java
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/ExceptionHandling.java
index 1603b81..e280c1f 100644
---
a/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/ExceptionHandling.java
+++
b/elasticjob-infra/elasticjob-restful/src/main/java/org/apache/shardingsphere/elasticjob/restful/pipeline/ExceptionHandling.java
@@ -30,24 +30,43 @@ import io.netty.handler.codec.http.HttpVersion;
import lombok.extern.slf4j.Slf4j;
import
org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandleResult;
import org.apache.shardingsphere.elasticjob.restful.handler.ExceptionHandler;
-import org.apache.shardingsphere.elasticjob.restful.Http;
+import
org.apache.shardingsphere.elasticjob.restful.handler.HandlerNotFoundException;
+import
org.apache.shardingsphere.elasticjob.restful.handler.impl.DefaultExceptionHandler;
+import
org.apache.shardingsphere.elasticjob.restful.handler.impl.DefaultHandlerNotFoundExceptionHandler;
import
org.apache.shardingsphere.elasticjob.restful.serializer.ResponseBodySerializer;
import
org.apache.shardingsphere.elasticjob.restful.serializer.ResponseBodySerializerFactory;
import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Catch exceptions and look for a ExceptionHandler.
*/
+@Slf4j
@Sharable
public final class ExceptionHandling extends ChannelInboundHandlerAdapter {
private static final DefaultExceptionHandler DEFAULT_EXCEPTION_HANDLER =
new DefaultExceptionHandler();
- private final Map<Class<? extends Throwable>, ExceptionHandler<? extends
Throwable>> exceptionHandlers;
+ private final Map<Class<? extends Throwable>, ExceptionHandler<? extends
Throwable>> exceptionHandlers = new ConcurrentHashMap<>();
public ExceptionHandling(final Map<Class<? extends Throwable>,
ExceptionHandler<? extends Throwable>> exceptionHandlers) {
- this.exceptionHandlers = exceptionHandlers;
+ initDefaultExceptionHandlers();
+ addCustomExceptionHandlers(exceptionHandlers);
+ }
+
+ private void initDefaultExceptionHandlers() {
+ exceptionHandlers.put(HandlerNotFoundException.class, new
DefaultHandlerNotFoundExceptionHandler());
+ }
+
+ private void addCustomExceptionHandlers(final Map<Class<? extends
Throwable>, ExceptionHandler<? extends Throwable>> exceptionHandlers) {
+ for (Map.Entry<Class<? extends Throwable>, ExceptionHandler<? extends
Throwable>> entry : exceptionHandlers.entrySet()) {
+ Class<? extends Throwable> exceptionType = entry.getKey();
+ ExceptionHandler<? extends Throwable> oldHandler =
this.exceptionHandlers.put(exceptionType, entry.getValue());
+ if (null != oldHandler) {
+ log.info("Overriding ExceptionHandler for [{}]",
exceptionType.getName());
+ }
+ }
}
@Override
@@ -86,17 +105,4 @@ public final class ExceptionHandling extends
ChannelInboundHandlerAdapter {
}
return (ExceptionHandler<T>) exceptionHandler;
}
-
- @Slf4j
- private static class DefaultExceptionHandler implements
ExceptionHandler<Throwable> {
-
- @Override
- public ExceptionHandleResult handleException(final Throwable ex) {
- return ExceptionHandleResult.builder()
-
.statusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code())
- .contentType(Http.DEFAULT_CONTENT_TYPE)
- .result(ex)
- .build();
- }
- }
}
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 87ca121..713f178 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
@@ -113,6 +113,14 @@ public class NettyRestfulServiceTest {
}, 10000L);
}
+ @Test(timeout = 10000L)
+ public void assertHandlerNotFound() {
+ DefaultFullHttpRequest request = new
DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/not/found");
+ HttpClient.request(HOST, PORT, request, httpResponse -> {
+ assertThat(httpResponse.status().code(), is(404));
+ }, 10000L);
+ }
+
@AfterClass
public static void tearDown() {
if (null != restfulService) {