Skip to content

Commit

Permalink
extract duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
AbnerHuang2 committed Sep 3, 2022
1 parent ad15e80 commit af2418b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
* 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;

/**
* @author Abner
* @since 8/31/22
*/
public class UrlUtils {
public final class UrlUtils {
private static final String ILLEGAL_KEY_REGEX = "[/\\\\]+|^\\.";

private UrlUtils() {}
Expand All @@ -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)));
}

}
4 changes: 4 additions & 0 deletions apollo-openapi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-core</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-common</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -170,7 +170,7 @@ public List<ItemDTO> 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);
Expand Down

0 comments on commit af2418b

Please sign in to comment.