From af2418bab93e45c4b9ed7210932e9f256dd0dbb8 Mon Sep 17 00:00:00 2001
From: Abner <1101964585@qq.com>
Date: Sat, 3 Sep 2022 17:54:27 +0800
Subject: [PATCH] extract duplication
---
.../adminservice/controller/ItemController.java | 12 ++++++------
.../apollo/common}/utils/UrlUtils.java | 15 +++++++++++++--
apollo-openapi/pom.xml | 4 ++++
.../openapi/client/url/OpenApiPathBuilder.java | 17 +++++++++--------
.../openapi/v1/controller/ItemController.java | 12 ++++--------
.../apollo/portal/api/AdminServiceAPI.java | 12 +++++++-----
.../apollo/portal/service/ItemService.java | 4 ++--
7 files changed, 45 insertions(+), 31 deletions(-)
rename {apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi => apollo-common/src/main/java/com/ctrip/framework/apollo/common}/utils/UrlUtils.java (72%)
diff --git a/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java b/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java
index 502ff9595d8..8a0f1e41b3d 100644
--- a/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java
+++ b/apollo-adminservice/src/main/java/com/ctrip/framework/apollo/adminservice/controller/ItemController.java
@@ -31,12 +31,13 @@
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
+import com.ctrip.framework.apollo.common.utils.UrlUtils;
import com.ctrip.framework.apollo.core.utils.StringUtils;
-
-import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
import java.util.stream.Collectors;
-
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
@@ -233,8 +234,7 @@ public ItemDTO get(@PathVariable("appId") String appId,
public ItemDTO getByEncodeKey(@PathVariable("appId") String appId,
@PathVariable("clusterName") String clusterName,
@PathVariable("namespaceName") String namespaceName, @PathVariable("key") String key) {
- key = new String(Base64.getUrlDecoder().decode(key.getBytes(StandardCharsets.UTF_8)));
- return this.get(appId,clusterName,namespaceName,key);
+ return this.get(appId,clusterName,namespaceName, UrlUtils.decode(key));
}
@GetMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items-with-page")
diff --git a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/utils/UrlUtils.java b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/utils/UrlUtils.java
similarity index 72%
rename from apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/utils/UrlUtils.java
rename to apollo-common/src/main/java/com/ctrip/framework/apollo/common/utils/UrlUtils.java
index f0630af649a..ff3bb1c1d9a 100644
--- a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/utils/UrlUtils.java
+++ b/apollo-common/src/main/java/com/ctrip/framework/apollo/common/utils/UrlUtils.java
@@ -14,10 +14,12 @@
* limitations under the License.
*
*/
-package com.ctrip.framework.apollo.openapi.utils;
+package com.ctrip.framework.apollo.common.utils;
import com.google.common.base.Strings;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -25,7 +27,7 @@
* @author Abner
* @since 8/31/22
*/
-public class UrlUtils {
+public final class UrlUtils {
private static final String ILLEGAL_KEY_REGEX = "[/\\\\]+|^\\.";
private UrlUtils() {}
@@ -38,4 +40,13 @@ public static boolean hasIllegalChar(String key) {
final Matcher matcher = pattern.matcher(key);
return matcher.find();
}
+
+ public static String encode(String key){
+ return new String(Base64.getEncoder().encode(key.getBytes(StandardCharsets.UTF_8)));
+ }
+
+ public static String decode(String key){
+ return new String(Base64.getDecoder().decode(key.getBytes(StandardCharsets.UTF_8)));
+ }
+
}
diff --git a/apollo-openapi/pom.xml b/apollo-openapi/pom.xml
index 3c55580c58b..0761ec36ef7 100644
--- a/apollo-openapi/pom.xml
+++ b/apollo-openapi/pom.xml
@@ -37,6 +37,10 @@
com.ctrip.framework.apollo
apollo-core
+
+ com.ctrip.framework.apollo
+ apollo-common
+
org.apache.httpcomponents
httpclient
diff --git a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/url/OpenApiPathBuilder.java b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/url/OpenApiPathBuilder.java
index 083b303c4e0..b322a7a957f 100644
--- a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/url/OpenApiPathBuilder.java
+++ b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/url/OpenApiPathBuilder.java
@@ -16,14 +16,16 @@
*/
package com.ctrip.framework.apollo.openapi.client.url;
-import com.ctrip.framework.apollo.openapi.utils.UrlUtils;
+import com.ctrip.framework.apollo.common.utils.UrlUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;
-
-import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
public class OpenApiPathBuilder {
@@ -84,10 +86,9 @@ public OpenApiPathBuilder namespacesPathVal(String namespaces) {
}
public OpenApiPathBuilder itemsPathVal(String items) {
- if(UrlUtils.hasIllegalChar(items)){
- items = new String(Base64.getUrlEncoder().encode(items.getBytes(StandardCharsets.UTF_8)));
- pathVariable.put(ENCODE_ITEMS_PATH, items);
- }else{
+ if (UrlUtils.hasIllegalChar(items)) {
+ pathVariable.put(ENCODE_ITEMS_PATH, UrlUtils.encode(items));
+ } else {
pathVariable.put(ITEMS_PATH, escapePath(items));
}
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java
index 21191d20a1e..55437942af5 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java
@@ -23,6 +23,7 @@
import com.ctrip.framework.apollo.openapi.api.ItemOpenApiService;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenPageDTO;
+import com.ctrip.framework.apollo.common.utils.UrlUtils;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.ctrip.framework.apollo.portal.service.ItemService;
import com.ctrip.framework.apollo.portal.spi.UserService;
@@ -42,8 +43,6 @@
import javax.validation.Valid;
import javax.validation.constraints.Positive;
import javax.validation.constraints.PositiveOrZero;
-import java.nio.charset.StandardCharsets;
-import java.util.Base64;
@Validated
@RestController("openapiItemController")
@@ -72,8 +71,7 @@ public OpenItemDTO getItem(@PathVariable String appId, @PathVariable String env,
@GetMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/encodeItems/{key:.+}")
public OpenItemDTO getItemByEncodeKey(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName,
@PathVariable String namespaceName, @PathVariable String key) {
- key = new String(Base64.getUrlDecoder().decode(key.getBytes(StandardCharsets.UTF_8)));
- return this.getItem(appId, env, clusterName, namespaceName, key);
+ return this.getItem(appId, env, clusterName, namespaceName, UrlUtils.decode(key));
}
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName, #env)")
@@ -133,8 +131,7 @@ public void updateItemByEncodeKey(@PathVariable String appId, @PathVariable Stri
@PathVariable String clusterName, @PathVariable String namespaceName,
@PathVariable String key, @RequestBody OpenItemDTO item,
@RequestParam(defaultValue = "false") boolean createIfNotExists, HttpServletRequest request){
- key = new String(Base64.getUrlDecoder().decode(key.getBytes(StandardCharsets.UTF_8)));
- this.updateItem(appId,env,clusterName,namespaceName,key,item,createIfNotExists,request);
+ this.updateItem(appId,env,clusterName,namespaceName,UrlUtils.decode(key),item,createIfNotExists,request);
}
@PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName, #env)")
@@ -162,8 +159,7 @@ public void deleteItemByEncodeKey(@PathVariable String appId, @PathVariable Stri
@PathVariable String clusterName, @PathVariable String namespaceName,
@PathVariable String key, @RequestParam String operator,
HttpServletRequest request) {
- key = new String(Base64.getUrlDecoder().decode(key.getBytes(StandardCharsets.UTF_8)));
- this.deleteItem(appId,env,clusterName,namespaceName,key,operator,request);
+ this.deleteItem(appId,env,clusterName,namespaceName,UrlUtils.decode(key),operator,request);
}
@GetMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java
index 3e7e3455fb4..65f2ff92794 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java
@@ -18,6 +18,7 @@
import com.ctrip.framework.apollo.common.dto.*;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
+import com.ctrip.framework.apollo.common.utils.UrlUtils;
import com.ctrip.framework.apollo.portal.environment.Env;
import com.google.common.base.Joiner;
import org.springframework.boot.actuate.health.Health;
@@ -30,9 +31,11 @@
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
-
-import java.nio.charset.StandardCharsets;
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
@Service
@@ -189,9 +192,8 @@ public ItemDTO loadItem(Env env, String appId, String clusterName, String namesp
}
public ItemDTO loadItemByEncodeKey(Env env, String appId, String clusterName, String namespaceName, String key) {
- key = new String(Base64.getEncoder().encode(key.getBytes(StandardCharsets.UTF_8)));
return restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/encodeItems/{key}",
- ItemDTO.class, appId, clusterName, namespaceName, key);
+ ItemDTO.class, appId, clusterName, namespaceName, UrlUtils.encode(key));
}
public ItemDTO loadItemById(Env env, long itemId) {
diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java
index 618bffe5b52..0f1e99affca 100644
--- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java
+++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java
@@ -23,7 +23,7 @@
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
-import com.ctrip.framework.apollo.openapi.utils.UrlUtils;
+import com.ctrip.framework.apollo.common.utils.UrlUtils;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI.ItemAPI;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI.NamespaceAPI;
import com.ctrip.framework.apollo.portal.api.AdminServiceAPI.ReleaseAPI;
@@ -170,7 +170,7 @@ public List findDeletedItems(String appId, Env env, String clusterName,
}
public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) {
- if(UrlUtils.hasIllegalChar(key)){
+ if (UrlUtils.hasIllegalChar(key)) {
return itemAPI.loadItemByEncodeKey(env,appId,clusterName,namespaceName,key);
}
return itemAPI.loadItem(env, appId, clusterName, namespaceName, key);