You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2023/02/10 10:34:06 UTC

[shenyu] branch master updated: Fix 4354 Optimize code for shenyu-admin (#4367)

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

xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git


The following commit(s) were added to refs/heads/master by this push:
     new 2be1cd7bc Fix 4354 Optimize code for shenyu-admin (#4367)
2be1cd7bc is described below

commit 2be1cd7bc59ad043e65a509feec614b8278bc85f
Author: balloon72 <96...@users.noreply.github.com>
AuthorDate: Fri Feb 10 18:33:58 2023 +0800

    Fix 4354 Optimize code for shenyu-admin (#4367)
    
    * Optimize code for shenyu-admin
    
    * Optimize code for shenyu-admin
    
    * Optimize code for shenyu-admin
    
    ---------
    
    Co-authored-by: hanmo1 <ISFA-9844>
    Co-authored-by: xiaoyu <xi...@apache.org>
---
 .../shenyu/admin/aspect/DataPermissionAspect.java       |  2 +-
 .../disruptor/subscriber/ApiDocExecutorSubscriber.java  | 14 ++++++--------
 .../admin/listener/AbstractListDataChangedListener.java |  2 +-
 .../apache/shenyu/admin/listener/etcd/EtcdClient.java   |  2 +-
 .../org/apache/shenyu/admin/model/bean/DocInfo.java     |  2 +-
 .../shenyu/admin/model/dto/CreateResourceDTO.java       | 17 -----------------
 .../shenyu/admin/model/entity/DashboardUserDO.java      |  3 +--
 .../org/apache/shenyu/admin/model/vo/RoleEditVO.java    |  6 ++----
 .../mybatis/oracle/OracleSQLPrepareInterceptor.java     |  2 +-
 .../shenyu/admin/service/impl/AppAuthServiceImpl.java   |  4 ++--
 .../service/manager/impl/LoadServiceDocEntryImpl.java   |  4 ++--
 .../apache/shenyu/admin/utils/CommonUpstreamUtils.java  | 16 +++++++++-------
 .../java/org/apache/shenyu/admin/utils/HttpUtils.java   |  6 +++---
 13 files changed, 30 insertions(+), 50 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/DataPermissionAspect.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/DataPermissionAspect.java
index 9d53efcee..c5ad25226 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/DataPermissionAspect.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/aspect/DataPermissionAspect.java
@@ -94,7 +94,7 @@ public class DataPermissionAspect {
             case DATA_PERMISSION_SELECTOR:
             case DATA_PERMISSION_RULE:
                 Stream.of(args)
-                        .filter(arg -> arg instanceof FilterQuery)
+                        .filter(FilterQuery.class::isInstance)
                         .forEach(q -> ((FilterQuery) q).setFilterIds(dataPermissionList));
                 break;
             default:
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java
index bdda61d4f..b14823a45 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/disruptor/subscriber/ApiDocExecutorSubscriber.java
@@ -44,13 +44,11 @@ public class ApiDocExecutorSubscriber implements ExecutorTypeSubscriber<ApiDocRe
 
     @Override
     public void executor(final Collection<ApiDocRegisterDTO> dataList) {
-        dataList.forEach(apiDoc -> {
-            Optional.ofNullable(this.shenyuClientRegisterService.get(apiDoc.getRpcType()))
-                    .ifPresent(shenyuClientRegisterService -> {
-                        synchronized (shenyuClientRegisterService) {
-                            shenyuClientRegisterService.registerApiDoc(apiDoc);
-                        }
-                    });
-        });
+        dataList.forEach(apiDoc -> Optional.ofNullable(this.shenyuClientRegisterService.get(apiDoc.getRpcType()))
+                .ifPresent(shenyuClientRegisterService -> {
+                    synchronized (shenyuClientRegisterService) {
+                        shenyuClientRegisterService.registerApiDoc(apiDoc);
+                    }
+                }));
     }
 }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
index f05441445..b6d1f77b4 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/AbstractListDataChangedListener.java
@@ -69,7 +69,7 @@ public abstract class AbstractListDataChangedListener implements DataChangedList
      *
      * @param changeData changeData
      */
-    public AbstractListDataChangedListener(final ChangeData changeData) {
+    protected AbstractListDataChangedListener(final ChangeData changeData) {
         this.changeData = changeData;
     }
 
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java
index 54c9b6594..a8c1ec3d9 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdClient.java
@@ -62,7 +62,7 @@ public class EtcdClient {
             List<KeyValue> keyValues = client.getKVClient().get(ByteSequence.from(key, StandardCharsets.UTF_8), option).get().getKvs();
             return !keyValues.isEmpty();
         } catch (Exception e) {
-            LOG.error("check node exists error.", e);
+            LOG.error("check node exists error. {}", e.getMessage());
             throw new ShenyuException(e.getMessage());
         }
     }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
index 8470b4b5b..10f249160 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/bean/DocInfo.java
@@ -26,7 +26,7 @@ public class DocInfo {
 
     private String title;
 
-    private transient String clusterName;
+    private String clusterName;
 
     private List<DocModule> docModuleList;
 
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/CreateResourceDTO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/CreateResourceDTO.java
index e1aab5254..5d1a0cb94 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/CreateResourceDTO.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/dto/CreateResourceDTO.java
@@ -96,23 +96,6 @@ public class CreateResourceDTO {
     public CreateResourceDTO() {
     }
 
-    public CreateResourceDTO(final String parentId, final String title, final String name, final String url, final String component,
-                             final Integer resourceType, final Integer sort, final String icon,
-                             final Boolean isLeaf, final Integer isRoute, final String perms, final Integer status) {
-        this.parentId = parentId;
-        this.title = title;
-        this.name = name;
-        this.url = url;
-        this.component = component;
-        this.resourceType = resourceType;
-        this.sort = sort;
-        this.icon = icon;
-        this.isLeaf = isLeaf;
-        this.isRoute = isRoute;
-        this.perms = perms;
-        this.status = status;
-    }
-
     /**
      * Gets the value of parentId.
      *
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
index 349b1069a..20bb361e2 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/DashboardUserDO.java
@@ -178,12 +178,11 @@ public final class DashboardUserDO extends BaseDO {
     public static DashboardUserDO buildDashboardUserDO(final DashboardUserModifyPasswordDTO dashboardUserModifyPasswordDTO) {
         return Optional.ofNullable(dashboardUserModifyPasswordDTO).map(item -> {
             Timestamp currentTime = new Timestamp(System.currentTimeMillis());
-            DashboardUserDO dashboardUserDO = DashboardUserDO.builder()
+            return DashboardUserDO.builder()
                     .password(item.getPassword())
                     .dateUpdated(currentTime)
                     .id(item.getId())
                     .build();
-            return dashboardUserDO;
         }).orElse(null);
     }
 
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/RoleEditVO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/RoleEditVO.java
index 697499f65..f6cd015d9 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/RoleEditVO.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/vo/RoleEditVO.java
@@ -215,8 +215,7 @@ public class RoleEditVO implements Serializable {
              * @return build object.
              */
             public PermissionInfo build() {
-                PermissionInfo permissionInfo = new PermissionInfo(treeList, permissionIds);
-                return permissionInfo;
+                return new PermissionInfo(treeList, permissionIds);
             }
         }
     }
@@ -520,8 +519,7 @@ public class RoleEditVO implements Serializable {
              * @return build object.
              */
             public ResourceInfo build() {
-                ResourceInfo resourceInfo = new ResourceInfo(id, title, name, children, isLeaf, parentId);
-                return resourceInfo;
+                return new ResourceInfo(id, title, name, children, isLeaf, parentId);
             }
         }
     }
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLPrepareInterceptor.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLPrepareInterceptor.java
index 461171aaf..a5e9a8dfb 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLPrepareInterceptor.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mybatis/oracle/OracleSQLPrepareInterceptor.java
@@ -62,7 +62,7 @@ public class OracleSQLPrepareInterceptor implements Interceptor {
         // replace desc and true
         String replaceSql = boundSql.getSql().toLowerCase()
                 .replace("`desc`", "\"desc\"")
-                .replace("true", "\'true\'");
+                .replace("true", "'true'");
         // replace`
         replaceSql = replaceSql.replace("`", "");
         // replace resource
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
index f6a83673f..9a301124a 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java
@@ -113,7 +113,7 @@ public class AppAuthServiceImpl implements AppAuthService {
                 .build();
 
         // save authPath
-        if (appAuthDO.getOpen()) {
+        if (Boolean.TRUE.equals(appAuthDO.getOpen())) {
             List<AuthPathDO> collect = authApplyDTO.getPathList()
                     .stream()
                     .map(path -> AuthPathDO.create(path, appAuthDO.getId(), authApplyDTO.getAppName()))
@@ -147,7 +147,7 @@ public class AppAuthServiceImpl implements AppAuthService {
             authParamMapper.save(AuthParamDO.create(appAuthDO.getId(), authApplyDTO.getAppName(), authApplyDTO.getAppParam()));
         }
 
-        if (appAuthDO.getOpen()) {
+        if (Boolean.TRUE.equals(appAuthDO.getOpen())) {
             List<AuthPathDO> existList = authPathMapper.findByAuthIdAndAppName(appAuthDO.getId(), authApplyDTO.getAppName());
             if (CollectionUtils.isNotEmpty(existList)) {
                 authPathMapper.deleteByAuthIdAndAppName(appAuthDO.getId(), authApplyDTO.getAppName());
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImpl.java
index ae18f471c..103ab6067 100755
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/manager/impl/LoadServiceDocEntryImpl.java
@@ -61,7 +61,7 @@ public class LoadServiceDocEntryImpl implements LoadServiceDocEntry {
     private static final Logger LOG = LoggerFactory.getLogger(LoadServiceDocEntryImpl.class);
 
     @SuppressWarnings("unchecked")
-    private static Map<String, String> supportSwaggerPluginMap = Collections.EMPTY_MAP;
+    private static Map<String, String> supportSwaggerPluginMap = Collections.emptyMap();
 
     private final SelectorService selectorService;
 
@@ -216,7 +216,7 @@ public class LoadServiceDocEntryImpl implements LoadServiceDocEntry {
                 }
             } catch (Exception e) {
                 LOG.error("Error getting cluster instance list. contextPath={} error={}", contextPath, e);
-                return null;
+                return Collections.emptyList();
             }
         }
         return allInstances;
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CommonUpstreamUtils.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CommonUpstreamUtils.java
index be6cc842b..6dd874a42 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CommonUpstreamUtils.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CommonUpstreamUtils.java
@@ -35,6 +35,8 @@ import java.util.stream.Collectors;
  * Build upstream for rpc plugin.
  */
 public class CommonUpstreamUtils {
+
+    private static final String LOCALHOST = "localhost";
     
     /**
      * Build divide upstream divide upstream.
@@ -44,7 +46,7 @@ public class CommonUpstreamUtils {
      * @return the divide upstream
      */
     public static DivideUpstream buildDefaultDivideUpstream(final String host, final Integer port) {
-        return DivideUpstream.builder().upstreamHost("localhost").protocol("http://").upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
+        return DivideUpstream.builder().upstreamHost(LOCALHOST).protocol("http://").upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
                 .status(Objects.nonNull(port) && StringUtils.isNotBlank(host)).build();
     }
 
@@ -55,7 +57,7 @@ public class CommonUpstreamUtils {
      * @return the divide upstream
      */
     public static DivideUpstream buildDefaultAliveDivideUpstream(final String upstreamUrl) {
-        return DivideUpstream.builder().upstreamHost("localhost").protocol("http://").upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
+        return DivideUpstream.builder().upstreamHost(LOCALHOST).protocol("http://").upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
     }
 
     /**
@@ -67,7 +69,7 @@ public class CommonUpstreamUtils {
      * @return the divide upstream
      */
     public static DivideUpstream buildDivideUpstream(final String protocol, final String host, final Integer port) {
-        return DivideUpstream.builder().upstreamHost("localhost").protocol(protocol).upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
+        return DivideUpstream.builder().upstreamHost(LOCALHOST).protocol(protocol).upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
                 .status(Objects.nonNull(port) && StringUtils.isNotBlank(host)).build();
     }
 
@@ -79,7 +81,7 @@ public class CommonUpstreamUtils {
      * @return the divide upstream
      */
     public static DivideUpstream buildAliveDivideUpstream(final String protocol, final String upstreamUrl) {
-        return DivideUpstream.builder().upstreamHost("localhost").protocol(protocol).upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
+        return DivideUpstream.builder().upstreamHost(LOCALHOST).protocol(protocol).upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
     }
 
     /**
@@ -91,7 +93,7 @@ public class CommonUpstreamUtils {
      * @return the websocket upstream
      */
     public static WebSocketUpstream buildWebSocketUpstream(final String protocol, final String host, final Integer port) {
-        return WebSocketUpstream.builder().host("localhost").protocol(protocol).upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
+        return WebSocketUpstream.builder().host(LOCALHOST).protocol(protocol).upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
                 .status(Objects.nonNull(port) && StringUtils.isNotBlank(host)).build();
     }
 
@@ -103,7 +105,7 @@ public class CommonUpstreamUtils {
      * @return the dubbo upstream
      */
     public static DubboUpstream buildDefaultDubboUpstream(final String host, final Integer port) {
-        return DubboUpstream.builder().upstreamHost("localhost").protocol("dubbo://").upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
+        return DubboUpstream.builder().upstreamHost(LOCALHOST).protocol("dubbo://").upstreamUrl(buildUrl(host, port)).weight(50).warmup(10).timestamp(System.currentTimeMillis())
                 .status(Objects.nonNull(port) && StringUtils.isNotBlank(host)).build();
     }
 
@@ -114,7 +116,7 @@ public class CommonUpstreamUtils {
      * @return the dubbo upstream
      */
     public static DubboUpstream buildAliveDubboUpstream(final String upstreamUrl) {
-        return DubboUpstream.builder().upstreamHost("localhost").protocol("dubbo://").upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
+        return DubboUpstream.builder().upstreamHost(LOCALHOST).protocol("dubbo://").upstreamUrl(upstreamUrl).weight(50).warmup(10).timestamp(System.currentTimeMillis()).build();
     }
 
     /**
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
index 5c574bdce..937c517a8 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/HttpUtils.java
@@ -32,12 +32,12 @@ import org.apache.commons.codec.digest.DigestUtils;
 
 import java.io.ByteArrayOutputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.Serializable;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -161,7 +161,7 @@ public class HttpUtils {
                 @Override
                 public List<Cookie> loadForRequest(final HttpUrl httpUrl) {
                     List<Cookie> cookies = cookieStore.get(httpUrl.host());
-                    return cookies != null ? cookies : new ArrayList<Cookie>();
+                    return cookies != null ? cookies : new ArrayList<>();
                 }
             }).build();
     }
@@ -640,7 +640,7 @@ public class HttpUtils {
             }
             InputStream input = null;
             try {
-                input = new FileInputStream(file);
+                input = Files.newInputStream(file.toPath());
                 return toBytes(input);
             } finally {
                 try {