Skip to content

Commit

Permalink
优化 FileTypeEnum 取值逻辑, 避免使用异常当作逻辑分支 (#4607)
Browse files Browse the repository at this point in the history
* 优化 FileTypeEnum 取值逻辑, 避免使用异常当作逻辑分支
异常的代价是很大的, 非必要情况下. 应该尽量避免

* comment

* comment

* license
  • Loading branch information
wjm0729 authored Dec 31, 2020
1 parent 2af6784 commit 4da3c82
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,12 @@ public String doGetConfig(HttpServletRequest request, HttpServletResponse respon
isBeta = true;
}
}

final String configType =
(null != cacheItem.getType()) ? cacheItem.getType() : FileTypeEnum.TEXT.getFileType();
response.setHeader("Config-Type", configType);

String contentTypeHeader;
try {
contentTypeHeader = FileTypeEnum.valueOf(configType.toUpperCase()).getContentType();
} catch (IllegalArgumentException ex) {
contentTypeHeader = FileTypeEnum.TEXT.getContentType();
}
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(configType);
String contentTypeHeader = fileTypeEnum.getContentType();
response.setHeader(HttpHeaderConsts.CONTENT_TYPE, contentTypeHeader);
}
File file = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.enums;

import com.alibaba.nacos.common.http.param.MediaType;
import com.alibaba.nacos.common.utils.StringUtils;

/**
* Config file type enum.
Expand Down Expand Up @@ -98,4 +99,24 @@ public String getFileType() {
public String getContentType() {
return contentType;
}

/**
* Get the corresponding FileTypeEnum by file extension or fileType. If not found FileTypeEnum.TEXT is returned
*
* @param extOrFileType file extension or fileType
* @return
*/
public static FileTypeEnum getFileTypeEnumByFileExtensionOrFileType(String extOrFileType) {
if (StringUtils.isNotBlank(extOrFileType)) {
String upperExtName = extOrFileType.trim().toUpperCase();
for (FileTypeEnum value : VALUES) {
if (value.name().equals(upperExtName)) {
return value;
}
}
}
return FileTypeEnum.TEXT;
}

private static final FileTypeEnum[] VALUES = FileTypeEnum.values();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1125,8 +1125,7 @@ public List<String> getTenantIdList(int page, int pageSize) {

Page<Map<String, Object>> pageList = helper
.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER);
return pageList.getPageItems().stream()
.map(map -> String.valueOf(map.get("TENANT_ID")))
return pageList.getPageItems().stream().map(map -> String.valueOf(map.get("TENANT_ID")))
.collect(Collectors.toList());
}

Expand All @@ -1139,8 +1138,7 @@ public List<String> getGroupIdList(int page, int pageSize) {

Page<Map<String, Object>> pageList = helper
.fetchPageLimit(sql, new Object[] {from, pageSize}, page, pageSize, MAP_ROW_MAPPER);
return pageList.getPageItems().stream()
.map(map -> String.valueOf(map.get("GROUP_ID")))
return pageList.getPageItems().stream().map(map -> String.valueOf(map.get("GROUP_ID")))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -2344,13 +2342,9 @@ public Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoLis
if (StringUtils.isBlank(type)) {
// simple judgment of file type based on suffix
if (configInfo.getDataId().contains(SPOT)) {
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1)
.toUpperCase();
try {
type = FileTypeEnum.valueOf(extName.toUpperCase()).getFileType();
} catch (Throwable ex) {
type = FileTypeEnum.TEXT.getFileType();
}
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
type = fileTypeEnum.getFileType();
}
}
if (configAdvanceInfo == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2590,13 +2590,9 @@ public Map<String, Object> batchInsertOrUpdate(List<ConfigAllInfo> configInfoLis
if (StringUtils.isBlank(type)) {
// simple judgment of file type based on suffix
if (configInfo.getDataId().contains(SPOT)) {
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1)
.toUpperCase();
try {
type = FileTypeEnum.valueOf(extName.toUpperCase()).getFileType();
} catch (Exception ex) {
type = FileTypeEnum.TEXT.getFileType();
}
String extName = configInfo.getDataId().substring(configInfo.getDataId().lastIndexOf(SPOT) + 1);
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(extName);
type = fileTypeEnum.getFileType();
}
}
if (configAdvanceInfo == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.test.common;

import com.alibaba.nacos.config.server.enums.FileTypeEnum;
import org.junit.Assert;
import org.junit.Test;

/**
* @author by jiangmin.wu on 2020/12/30
*/
public class FileTypeEnum_ITCase {

@Test
public void fileTypeEnum_test1() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.name());
Assert.assertEquals(fileTypeEnum, value);
}
}

@Test
public void fileTypeEnum_test2() {
for (FileTypeEnum value : FileTypeEnum.values()) {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(value.getFileType());
Assert.assertNotNull(fileTypeEnum);
}
}

@Test
public void fileTypeEnum_test3() {
FileTypeEnum fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("t");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);

fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);

fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(".");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);

fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType("1");
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);

fileTypeEnum = FileTypeEnum.getFileTypeEnumByFileExtensionOrFileType(null);
Assert.assertEquals(fileTypeEnum, FileTypeEnum.TEXT);
}

}

0 comments on commit 4da3c82

Please sign in to comment.