You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/06/29 12:10:55 UTC

[shardingsphere-elasticjob-lite] branch master updated: fix method if return void, web page is not refresh. (#868)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob-lite.git


The following commit(s) were added to refs/heads/master by this push:
     new b2df050  fix method if return void, web page is not refresh. (#868)
b2df050 is described below

commit b2df0507a216ee92fd2cd472d4966c1f127f7952
Author: keker <as...@163.com>
AuthorDate: Mon Jun 29 20:10:44 2020 +0800

    fix method if return void, web page is not refresh. (#868)
---
 .../config/advice/ConsoleRestControllerAdvice.java | 67 ++++++++++++++++++
 .../controller/RegistryCenterController.java       | 27 ++++---
 .../lite/lifecycle/restful/GsonJsonProvider.java   | 82 ----------------------
 .../lifecycle/restful/RESTfulAPIException.java     | 30 --------
 .../restful/RESTfulAPIExceptionMapper.java         | 37 ----------
 .../lifecycle/restful/RestfulExceptionTest.java    | 31 --------
 .../lite/lifecycle/restful/fixture/Caller.java     | 35 ---------
 .../lifecycle/restful/fixture/TestRESTfulApi.java  | 51 --------------
 8 files changed, 80 insertions(+), 280 deletions(-)

diff --git a/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/config/advice/ConsoleRestControllerAdvice.java b/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/config/advice/ConsoleRestControllerAdvice.java
new file mode 100644
index 0000000..dccb44c
--- /dev/null
+++ b/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/config/advice/ConsoleRestControllerAdvice.java
@@ -0,0 +1,67 @@
+/*
+ * 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.lite.console.config.advice;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.elasticjob.lite.exception.ExceptionUtils;
+import org.springframework.core.MethodParameter;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.server.ServerHttpRequest;
+import org.springframework.http.server.ServerHttpResponse;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
+
+/**
+ * Console rest controller advice.
+ **/
+@RestControllerAdvice
+@Slf4j
+public final class ConsoleRestControllerAdvice implements ResponseBodyAdvice<Object> {
+
+    @Override
+    public boolean supports(final MethodParameter returnType, final Class<? extends HttpMessageConverter<?>> converterType) {
+        //only advice return void method.
+        if (null == returnType.getMethod()) {
+            return false;
+        }
+        return void.class.isAssignableFrom(returnType.getMethod().getReturnType());
+    }
+
+    @Override
+    public Object beforeBodyWrite(final Object body, final MethodParameter returnType, final MediaType selectedContentType,
+                                  final Class<? extends HttpMessageConverter<?>> selectedConverterType, final ServerHttpRequest request, final ServerHttpResponse response) {
+        //if the method return void, then the value is true and returns.
+        return null == body ? true : body;
+    }
+
+    /**
+     * Handle exception.
+     *
+     * @param ex exception
+     * @return response result
+     */
+    @ExceptionHandler(Exception.class)
+    public ResponseEntity<String> toResponse(final Exception ex) {
+        log.error("CONSOLE ERROR", ex);
+        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ExceptionUtils.transform(ex));
+    }
+}
diff --git a/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/controller/RegistryCenterController.java b/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/controller/RegistryCenterController.java
index 78f633c..a455f14 100644
--- a/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/controller/RegistryCenterController.java
+++ b/elastic-job-lite-console/src/main/java/org/apache/shardingsphere/elasticjob/lite/console/controller/RegistryCenterController.java
@@ -17,6 +17,11 @@
 
 package org.apache.shardingsphere.elasticjob.lite.console.controller;
 
+import java.util.Collection;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
 import org.apache.shardingsphere.elasticjob.lite.console.domain.RegistryCenterConfiguration;
 import org.apache.shardingsphere.elasticjob.lite.console.service.RegistryCenterConfigurationService;
 import org.apache.shardingsphere.elasticjob.lite.console.util.SessionRegistryCenterConfiguration;
@@ -30,28 +35,22 @@ import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpSession;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.MediaType;
-import java.util.Collection;
-
 /**
  * Registry center RESTful API.
  */
 @RestController
 @RequestMapping("/registry-center")
 public final class RegistryCenterController {
-    
+
     public static final String REG_CENTER_CONFIG_KEY = "reg_center_config_key";
-    
+
     private RegistryCenterConfigurationService regCenterService;
-    
+
     @Autowired
     public RegistryCenterController(final RegistryCenterConfigurationService regCenterService) {
         this.regCenterService = regCenterService;
     }
-    
+
     /**
      * Judge whether registry center is activated.
      *
@@ -61,7 +60,7 @@ public final class RegistryCenterController {
     public boolean activated() {
         return regCenterService.loadActivated().isPresent();
     }
-    
+
     /**
      * Load configuration from registry center.
      *
@@ -73,7 +72,7 @@ public final class RegistryCenterController {
         regCenterService.loadActivated().ifPresent(regCenterConfig -> setRegistryCenterNameToSession(regCenterConfig, request.getSession()));
         return regCenterService.loadAll().getRegistryCenterConfiguration();
     }
-    
+
     /**
      * Add registry center.
      *
@@ -94,7 +93,7 @@ public final class RegistryCenterController {
     public void delete(@RequestBody final RegistryCenterConfiguration config) {
         regCenterService.delete(config.getName());
     }
-    
+
     /**
      * Connect to registry center.
      *
@@ -110,7 +109,7 @@ public final class RegistryCenterController {
         }
         return isConnected;
     }
-    
+
     private boolean setRegistryCenterNameToSession(final RegistryCenterConfiguration regCenterConfig, final HttpSession session) {
         session.setAttribute(REG_CENTER_CONFIG_KEY, regCenterConfig);
         try {
diff --git a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/GsonJsonProvider.java b/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/GsonJsonProvider.java
deleted file mode 100644
index a065b29..0000000
--- a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/GsonJsonProvider.java
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * 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.lite.lifecycle.restful;
-
-import org.apache.shardingsphere.elasticjob.lite.util.json.GsonFactory;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.Produces;
-import javax.ws.rs.WebApplicationException;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.ext.MessageBodyReader;
-import javax.ws.rs.ext.MessageBodyWriter;
-import javax.ws.rs.ext.Provider;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.lang.annotation.Annotation;
-import java.lang.reflect.Type;
-
-/**
- * Gson json provider.
- */
-@Provider
-@Produces(MediaType.APPLICATION_JSON)
-@Consumes(MediaType.APPLICATION_JSON)
-public final class GsonJsonProvider implements MessageBodyWriter<Object>, MessageBodyReader<Object> {
-    
-    private static final String UTF_8 = "UTF-8";
-    
-    @Override
-    public Object readFrom(final Class<Object> type, final Type genericType, final Annotation[] annotations,
-                           final MediaType mediaType, final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) {
-        try (InputStreamReader streamReader = new InputStreamReader(entityStream, UTF_8)) {
-            return GsonFactory.getGson().fromJson(streamReader, type.equals(genericType) ? type : genericType);
-        } catch (final IOException ex) {
-            throw new RESTfulAPIException(ex);
-        }
-    }
-    
-    @Override
-    public void writeTo(final Object object, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType,
-                        final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws WebApplicationException {
-        try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8)) {
-            GsonFactory.getGson().toJson(object, type.equals(genericType) ? type : genericType, writer);
-        } catch (final IOException ex) {
-            throw new RESTfulAPIException(ex);
-        }
-    }
-    
-    @Override
-    public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
-        return true;
-    }
-    
-    @Override
-    public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
-        return true;
-    }
-    
-    @Override
-    public long getSize(final Object object, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType) {
-        return -1;
-    }
-}
diff --git a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIException.java b/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIException.java
deleted file mode 100644
index e3d8c2f..0000000
--- a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIException.java
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * 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.lite.lifecycle.restful;
-
-/**
- * RESTful API exception.
- */
-public final class RESTfulAPIException extends RuntimeException {
-    
-    private static final long serialVersionUID = -7594937349408972960L;
-    
-    public RESTfulAPIException(final Throwable cause) {
-        super(cause);
-    }
-}
diff --git a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIExceptionMapper.java b/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIExceptionMapper.java
deleted file mode 100644
index 5c96478..0000000
--- a/elastic-job-lite-lifecycle/src/main/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RESTfulAPIExceptionMapper.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * 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.lite.lifecycle.restful;
-
-import org.apache.shardingsphere.elasticjob.lite.exception.ExceptionUtils;
-
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.ext.ExceptionMapper;
-import javax.ws.rs.ext.Provider;
-
-/**
- * RESTFul API exception mapper.
- */
-@Provider
-public final class RESTfulAPIExceptionMapper implements ExceptionMapper<Throwable> {
-    
-    @Override
-    public Response toResponse(final Throwable cause) {
-        return Response.ok(ExceptionUtils.transform(cause), MediaType.TEXT_PLAIN).status(Response.Status.INTERNAL_SERVER_ERROR).build();
-    }
-}
diff --git a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RestfulExceptionTest.java b/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RestfulExceptionTest.java
deleted file mode 100644
index 42b2a5b..0000000
--- a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/RestfulExceptionTest.java
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * 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.lite.lifecycle.restful;
-
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.assertThat;
-
-public final class RestfulExceptionTest {
-    
-    @Test
-    public void assertRestfulException() {
-        assertThat(new RESTfulAPIException(new RuntimeException()).getCause(), instanceOf(RuntimeException.class));
-    }
-}
diff --git a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/Caller.java b/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/Caller.java
deleted file mode 100644
index e1a1914..0000000
--- a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/Caller.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * 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.lite.lifecycle.restful.fixture;
-
-public interface Caller {
-    
-    /**
-     * Execute call.
-     *
-     * @param value value
-     */
-    void call(String value);
-    
-    /**
-     * Execute call.
-     *
-     * @param value value
-     */
-    void call(int value);
-}
diff --git a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/TestRESTfulApi.java b/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/TestRESTfulApi.java
deleted file mode 100644
index acca85b..0000000
--- a/elastic-job-lite-lifecycle/src/test/java/org/apache/shardingsphere/elasticjob/lite/lifecycle/restful/fixture/TestRESTfulApi.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * 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.lite.lifecycle.restful.fixture;
-
-import lombok.Setter;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.core.MediaType;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.stream.Collectors;
-
-@Path("/test")
-public final class TestRESTfulApi {
-    
-    @Setter
-    private static Caller caller;
-    
-    /**
-     * For test.
-     * 
-     * @param map request parameters
-     * @return response
-     */
-    @POST
-    @Path("/call")
-    @Consumes(MediaType.APPLICATION_JSON)
-    public Map<String, String> call(final Map<String, String> map) {
-        caller.call(map.get("string"));
-        caller.call(Integer.valueOf(map.get("integer")));
-        return map.entrySet().stream().collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue() + "_processed", (a, b) -> b, () -> new LinkedHashMap<>(map.size())));
-    }
-}