From acb9a7408a88fce5db3a4bd8ba29e1ace275db03 Mon Sep 17 00:00:00 2001 From: liqiang-fit2cloud Date: Mon, 17 Oct 2022 15:48:22 +0800 Subject: [PATCH] refactor: "UTF-8" -> StandardCharsets.UTF_8.name() --- .../dto/definition/request/ElementUtil.java | 5 +++-- .../request/sampler/MsHTTPSamplerProxy.java | 19 ++++++++++--------- .../api/parse/MsAbstractParser.java | 3 ++- .../metersphere/api/parse/api/ESBParser.java | 5 +++-- .../metersphere/api/parse/api/HarParser.java | 3 ++- .../api/parse/scenario/EsbDataParser.java | 3 ++- .../scenario/TcpTreeTableDataParser.java | 3 ++- .../commons/utils/DataFormattingUtil.java | 3 ++- .../io/metersphere/commons/utils/XMLUtil.java | 3 ++- .../commons/utils/mock/MockApiUtils.java | 9 +++++---- .../service/MockConfigService.java | 3 ++- .../metersphere/commons/utils/CodingUtil.java | 3 ++- .../io/metersphere/commons/utils/RsaUtil.java | 3 ++- .../utils/TcpTreeTableDataParser.java | 3 ++- .../environment/utils/XMLUtils.java | 3 ++- .../excel/utils/EasyExcelExporter.java | 5 +++-- .../notice/sender/impl/MailNoticeSender.java | 3 ++- .../notice/util/WxChatbotClient.java | 3 ++- .../metersphere/service/BaseUserService.java | 3 ++- .../metersphere/service/ResourceService.java | 5 +++-- .../excel/utils/EasyExcelExporter.java | 5 +++-- .../io/metersphere/service/UserService.java | 3 ++- .../metersphere/service/TestCaseService.java | 5 +++-- .../issue/platform/AbstractIssuePlatform.java | 3 ++- .../service/issue/platform/JiraPlatform.java | 2 +- .../metersphere/xmind/parser/XmindLegacy.java | 7 ++++--- .../xmind/utils/XmindExportUtil.java | 5 +++-- 27 files changed, 73 insertions(+), 47 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index b4cc54fc85..412af5dd91 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -63,6 +63,7 @@ import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.File; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -121,7 +122,7 @@ public class ElementUtil { csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName()); csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName()); - csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? "UTF-8" : item.getEncoding()); + csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding()); if (CollectionUtils.isEmpty(item.getFiles())) { MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + ":[ " + Translator.get("csv_no_exist") + " ]"); } else { @@ -170,7 +171,7 @@ public class ElementUtil { csvDataSet.setProperty(TestElement.TEST_CLASS, CSVDataSet.class.getName()); csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName()); - csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? "UTF-8" : item.getEncoding()); + csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? StandardCharsets.UTF_8.name() : item.getEncoding()); if (CollectionUtils.isEmpty(item.getFiles())) { MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + ":[ " + Translator.get("csv_no_exist") + " ]"); } else { diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 6d61e483e2..ebd0d15746 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -54,6 +54,7 @@ import org.json.JSONObject; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -123,7 +124,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("HttpTestSampleGui")); ElementUtil.setBaseParams(sampler, this.getParent(), config, this.getId(), this.getIndex()); sampler.setMethod(this.getMethod()); - sampler.setContentEncoding("UTF-8"); + sampler.setContentEncoding(StandardCharsets.UTF_8.name()); sampler.setFollowRedirects(this.isFollowRedirects()); sampler.setUseKeepAlive(true); sampler.setDoMultipart(this.isDoMultipartPost()); @@ -371,14 +372,14 @@ public class MsHTTPSamplerProxy extends MsTestElement { } try { URL urlObject = new URL(url); - sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); + sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name())); if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { sampler.setProperty("HTTPSampler.port", this.getPort()); } else if (urlObject.getPort() != -1) { sampler.setPort(urlObject.getPort()); } sampler.setProtocol(urlObject.getProtocol()); - sampler.setProperty("HTTPSampler.path", URLDecoder.decode(URLEncoder.encode(urlObject.getFile(), "UTF-8"), "UTF-8")); + sampler.setProperty("HTTPSampler.path", URLDecoder.decode(URLEncoder.encode(urlObject.getFile(), StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name())); } catch (Exception e) { LogUtil.error(e.getMessage(), e); } @@ -396,7 +397,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { } sampler.setPort(httpConfig.getPort()); if (StringUtils.isNotEmpty(httpConfig.getDomain())) { - sampler.setDomain(URLDecoder.decode(httpConfig.getDomain(), "UTF-8")); + sampler.setDomain(URLDecoder.decode(httpConfig.getDomain(), StandardCharsets.UTF_8.name())); sampler.setProtocol(httpConfig.getProtocol()); } else { sampler.setDomain(""); @@ -406,13 +407,13 @@ public class MsHTTPSamplerProxy extends MsTestElement { } else { URL urlObject = new URL(this.path); envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getFile(); - sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); + sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name())); sampler.setProtocol(urlObject.getProtocol()); } if (StringUtils.isNotEmpty(envPath) && !envPath.startsWith("/")) { envPath = "/" + envPath; } - sampler.setProperty("HTTPSampler.path", URLDecoder.decode(URLEncoder.encode(envPath, "UTF-8"), "UTF-8")); + sampler.setProperty("HTTPSampler.path", URLDecoder.decode(URLEncoder.encode(envPath, StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name())); } } String envPath = sampler.getPath(); @@ -441,7 +442,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { MSException.throwException("请重新选择环境"); } URL urlObject = new URL(url); - sampler.setDomain(URLDecoder.decode(urlObject.getHost(), "UTF-8")); + sampler.setDomain(URLDecoder.decode(urlObject.getHost(), StandardCharsets.UTF_8.name())); if (urlObject.getPort() > 0 && urlObject.getPort() == 10990 && StringUtils.isNotEmpty(this.getPort()) && this.getPort().startsWith("${")) { sampler.setProperty("HTTPSampler.port", this.getPort()); } else if (urlObject.getPort() != -1) { @@ -451,11 +452,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getFile(); sampler.setProperty("HTTPSampler.path", envPath); if (CollectionUtils.isNotEmpty(this.getRest()) && this.isRest()) { - envPath = getRestParameters(URLDecoder.decode(URLEncoder.encode(envPath, "UTF-8"), "UTF-8")); + envPath = getRestParameters(URLDecoder.decode(URLEncoder.encode(envPath, StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name())); sampler.setProperty("HTTPSampler.path", envPath); } if (CollectionUtils.isNotEmpty(this.getArguments())) { - sampler.setProperty("HTTPSampler.path", postQueryParameters(URLDecoder.decode(URLEncoder.encode(envPath, "UTF-8"), "UTF-8"))); + sampler.setProperty("HTTPSampler.path", postQueryParameters(URLDecoder.decode(URLEncoder.encode(envPath, StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name()))); } } } catch (Exception e) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java index 50e5358ec3..7a4d4a3649 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/MsAbstractParser.java @@ -12,6 +12,7 @@ import org.json.JSONObject; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.*; public abstract class MsAbstractParser extends ApiImportAbstractParser { @@ -52,7 +53,7 @@ public abstract class MsAbstractParser extends ApiImportAbstractParser { if (split.length > 1) { try { String queryParams = split[1]; - queryParams = URLDecoder.decode(queryParams, "UTF-8"); + queryParams = URLDecoder.decode(queryParams, StandardCharsets.UTF_8.name()); String[] params = queryParams.split("&"); for (String param : params) { String[] kv = param.split("="); diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/ESBParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/ESBParser.java index a760cc1a35..b1af72e73e 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/ESBParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/ESBParser.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.InputStream; import java.math.BigInteger; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.text.DateFormat; import java.util.*; import java.util.regex.Pattern; @@ -58,8 +59,8 @@ public class ESBParser extends EsbAbstractParser { out = response.getOutputStream(); response.reset(); response.setContentType("application/vnd.ms-excel"); - response.setCharacterEncoding("utf-8"); - response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()) + ".xlsx"); wb.write(out); wb.close(); } catch (IOException e) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/api/HarParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/api/HarParser.java index 3f57eae825..80501c4883 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/api/HarParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/api/HarParser.java @@ -24,6 +24,7 @@ import org.json.JSONObject; import java.io.InputStream; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -85,7 +86,7 @@ public class HarParser extends HarAbstractParser { } try { - url = URLDecoder.decode(url, "UTF-8"); + url = URLDecoder.decode(url, StandardCharsets.UTF_8.name()); if (url.contains("?")) { url = url.split("\\?")[0]; } diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/EsbDataParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/EsbDataParser.java index 9c90339011..7bad7fbdfa 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/EsbDataParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/EsbDataParser.java @@ -12,6 +12,7 @@ import org.dom4j.io.XMLWriter; import javax.xml.parsers.DocumentBuilderFactory; import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; import java.util.List; /** @@ -38,7 +39,7 @@ public class EsbDataParser { // 设置XML文档格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1 - outputFormat.setEncoding("UTF-8"); + outputFormat.setEncoding(StandardCharsets.UTF_8.name()); //outputFormat.setSuppressDeclaration(true); //是否生产xml头 outputFormat.setIndent(true); //设置是否缩进 outputFormat.setNewlines(true); //设置是否换行 diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java index b400006daa..1405feb982 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java @@ -21,6 +21,7 @@ import org.dom4j.io.XMLWriter; import javax.xml.parsers.DocumentBuilderFactory; import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -60,7 +61,7 @@ public class TcpTreeTableDataParser { // 设置XML文档格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1 - outputFormat.setEncoding("UTF-8"); + outputFormat.setEncoding(StandardCharsets.UTF_8.name()); //outputFormat.setSuppressDeclaration(true); //是否生产xml头 outputFormat.setIndent(true); //设置是否缩进 outputFormat.setNewlines(true); //设置是否换行 diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/DataFormattingUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/DataFormattingUtil.java index 157c4d5a39..f97c127533 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/DataFormattingUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/DataFormattingUtil.java @@ -20,6 +20,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.ByteArrayInputStream; import java.io.File; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -46,7 +47,7 @@ public class DataFormattingUtil { //获取要转化的文件 List attachmentFilePathList = new ArrayList<>(); try { - Document doc = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(jmx.getBytes("utf-8"))); + Document doc = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(jmx.getBytes(StandardCharsets.UTF_8.name()))); Element root = doc.getRootElement(); Element rootHashTreeElement = root.element(ElementConstants.HASH_TREE); List innerHashTreeElementList = rootHashTreeElement.elements(ElementConstants.HASH_TREE); diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java index 476bafc4ea..babd98419c 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/XMLUtil.java @@ -9,6 +9,7 @@ import org.json.JSONObject; import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -160,7 +161,7 @@ public class XMLUtil { public static Document stringToDocument(String xml) { try { - return EngineSourceParserFactory.getDocument(new ByteArrayInputStream(xml.getBytes("utf-8"))); + return EngineSourceParserFactory.getDocument(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))); } catch (Exception e) { LogUtil.error(e); return null; diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java index 6fc26c1a0d..86c62f0899 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/mock/MockApiUtils.java @@ -19,6 +19,7 @@ import org.json.JSONObject; import javax.script.ScriptEngine; import javax.servlet.http.HttpServletRequest; import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -483,7 +484,7 @@ public class MockApiUtils { } outSteam.close(); inputStream.close(); - result = new String(outSteam.toByteArray(), "UTF-8"); + result = new String(outSteam.toByteArray(), StandardCharsets.UTF_8.name()); } catch (Exception e) { LogUtil.error(e); } @@ -504,10 +505,10 @@ public class MockApiUtils { byte buffer[] = getRequestPostBytes(request); String charEncoding = request.getCharacterEncoding(); if (charEncoding == null) { - charEncoding = "UTF-8"; + charEncoding = StandardCharsets.UTF_8.name(); } if (buffer == null) { - return ""; + return StringUtils.EMPTY; } else { return new String(buffer, charEncoding); } @@ -520,7 +521,7 @@ public class MockApiUtils { BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( - request.getInputStream(), "UTF-8")); + request.getInputStream(), StandardCharsets.UTF_8.name())); while ((inputLine = in.readLine()) != null) { recieveData.append(inputLine); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java index e3ef47df57..2c119128a6 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/MockConfigService.java @@ -64,6 +64,7 @@ import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -1105,7 +1106,7 @@ public class MockConfigService { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); XMLUtil.setExpandEntityReferencesFalse(documentBuilderFactory); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); - builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes("utf-8")))); + builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8.name())))); isXml = true; } catch (Exception e) { e.printStackTrace(); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/CodingUtil.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/CodingUtil.java index 7fef33916d..8a263f9f75 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/CodingUtil.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/CodingUtil.java @@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils; import javax.crypto.*; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; /** @@ -15,7 +16,7 @@ import java.security.MessageDigest; */ public class CodingUtil { - private static final String UTF_8 = "UTF-8"; + private static final String UTF_8 = StandardCharsets.UTF_8.name(); private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/RsaUtil.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/RsaUtil.java index 539b359acb..8038ce60a5 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/RsaUtil.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/commons/utils/RsaUtil.java @@ -4,6 +4,7 @@ import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; import java.security.*; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; @@ -13,7 +14,7 @@ import java.security.spec.X509EncodedKeySpec; public class RsaUtil { - public static final String CHARSET = "UTF-8"; + public static final String CHARSET = StandardCharsets.UTF_8.name(); public static final String RSA_ALGORITHM = "RSA"; private static RsaKey rsaKey; diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/TcpTreeTableDataParser.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/TcpTreeTableDataParser.java index 0740fd4ec3..9f2b05d199 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/TcpTreeTableDataParser.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/TcpTreeTableDataParser.java @@ -20,6 +20,7 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.IOException; import java.io.StringWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; @@ -62,7 +63,7 @@ public class TcpTreeTableDataParser { // 设置XML文档格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1 - outputFormat.setEncoding("UTF-8"); + outputFormat.setEncoding(StandardCharsets.UTF_8.name()); //outputFormat.setSuppressDeclaration(true); //是否生产xml头 outputFormat.setIndent(true); //设置是否缩进 outputFormat.setNewlines(true); //设置是否换行 diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/XMLUtils.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/XMLUtils.java index 53661bdac1..c42dcabc15 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/XMLUtils.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/environment/utils/XMLUtils.java @@ -14,6 +14,7 @@ import org.xml.sax.helpers.XMLFilterImpl; import javax.xml.parsers.DocumentBuilderFactory; import java.io.ByteArrayInputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -179,7 +180,7 @@ public class XMLUtils { public static Document stringToDocument(String xml) { try { - return getDocument(new ByteArrayInputStream(xml.getBytes("utf-8"))); + return getDocument(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8.name()))); } catch (Exception e) { LogUtil.error(e); return null; diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java index a3c23c3fbc..6a52c6ef59 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java @@ -13,6 +13,7 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -59,8 +60,8 @@ public class EasyExcelExporter { public void buildExportResponse(HttpServletResponse response, String fileName) { try { response.setContentType("application/vnd.ms-excel"); - response.setCharacterEncoding("utf-8"); - response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()) + ".xlsx"); } catch (IOException e) { LogUtil.error(e); MSException.throwException(e.getMessage()); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java index de6778c7cd..af021432b9 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/sender/impl/MailNoticeSender.java @@ -21,6 +21,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -161,7 +162,7 @@ public class MailNoticeSender extends AbstractNoticeSender { public JavaMailSenderImpl getMailSender(Map paramMap) { Properties props = new Properties(); JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl(); - javaMailSender.setDefaultEncoding("UTF-8"); + javaMailSender.setDefaultEncoding(StandardCharsets.UTF_8.name()); javaMailSender.setProtocol("smtp"); javaMailSender.setHost(paramMap.get(ParamConstants.MAIL.SERVER.getValue())); javaMailSender.setPort(Integer.parseInt(paramMap.get(ParamConstants.MAIL.PORT.getValue()))); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/util/WxChatbotClient.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/util/WxChatbotClient.java index d0de1ca77f..789913b859 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/util/WxChatbotClient.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/notice/util/WxChatbotClient.java @@ -13,6 +13,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Map; /** @@ -29,7 +30,7 @@ public class WxChatbotClient { } HttpPost httppost = new HttpPost(webhook); httppost.addHeader("Content-Type", "application/json; charset=utf-8"); - StringEntity se = new StringEntity(message.toJsonString(), "utf-8"); + StringEntity se = new StringEntity(message.toJsonString(), StandardCharsets.UTF_8.name()); httppost.setEntity(se); SendResult sendResult = new SendResult(); diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseUserService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseUserService.java index d1b80e3997..0dcf75110e 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseUserService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/BaseUserService.java @@ -38,6 +38,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -302,7 +303,7 @@ public class BaseUserService { /*修改当前用户用户密码*/ private User updateCurrentUserPwd(EditPassWordRequest request) { - String oldPassword = CodingUtil.md5(request.getPassword(), "utf-8"); + String oldPassword = CodingUtil.md5(request.getPassword(), StandardCharsets.UTF_8.name()); String newPassword = request.getNewpassword(); String newPasswordMd5 = CodingUtil.md5(newPassword); if (StringUtils.equals(oldPassword, newPasswordMd5)) { diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/ResourceService.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/ResourceService.java index 0c61d0fd23..8278b1bba9 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/ResourceService.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/service/ResourceService.java @@ -19,6 +19,7 @@ import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.Date; @Service @@ -67,7 +68,7 @@ public class ResourceService { public String encodeFileName(String fileName) { try { - return URLEncoder.encode(fileName, "UTF-8"); + return URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { LogUtil.error(e); return fileName; @@ -76,7 +77,7 @@ public class ResourceService { public String decodeFileName(String fileName) { try { - return URLDecoder.decode(fileName, "UTF-8"); + return URLDecoder.decode(fileName, StandardCharsets.UTF_8.name()); } catch (UnsupportedEncodingException e) { LogUtil.error(e); return fileName; diff --git a/system-setting/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java b/system-setting/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java index 4c35374ac3..823557ce87 100644 --- a/system-setting/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java +++ b/system-setting/backend/src/main/java/io/metersphere/excel/utils/EasyExcelExporter.java @@ -10,6 +10,7 @@ import io.metersphere.commons.utils.LogUtil; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -55,8 +56,8 @@ public class EasyExcelExporter { public void buildExportResponse(HttpServletResponse response, String fileName) { try { response.setContentType("application/vnd.ms-excel"); - response.setCharacterEncoding("utf-8"); - response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx"); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8.name()) + ".xlsx"); } catch (IOException e) { LogUtil.error(e); MSException.throwException(e.getMessage()); diff --git a/system-setting/backend/src/main/java/io/metersphere/service/UserService.java b/system-setting/backend/src/main/java/io/metersphere/service/UserService.java index f8a248cb7b..afb6595549 100644 --- a/system-setting/backend/src/main/java/io/metersphere/service/UserService.java +++ b/system-setting/backend/src/main/java/io/metersphere/service/UserService.java @@ -51,6 +51,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.stream.Collectors; @@ -504,7 +505,7 @@ public class UserService { /*修改当前用户用户密码*/ private User updateCurrentUserPwd(EditPassWordRequest request) { - String oldPassword = CodingUtil.md5(request.getPassword(), "utf-8"); + String oldPassword = CodingUtil.md5(request.getPassword(), StandardCharsets.UTF_8.name()); String newPassword = request.getNewpassword(); String newPasswordMd5 = CodingUtil.md5(newPassword); if (StringUtils.equals(oldPassword, newPasswordMd5)) { diff --git a/test-track/backend/src/main/java/io/metersphere/service/TestCaseService.java b/test-track/backend/src/main/java/io/metersphere/service/TestCaseService.java index f6e21e6775..2f2d27488e 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/TestCaseService.java +++ b/test-track/backend/src/main/java/io/metersphere/service/TestCaseService.java @@ -73,6 +73,7 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -1377,9 +1378,9 @@ public class TestCaseService { public void testCaseXmindTemplateExport(String projectId, String importType, HttpServletResponse response) { try { response.setContentType("application/octet-stream"); - response.setCharacterEncoding("utf-8"); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); boolean isUseCustomId = trackProjectService.useCustomNum(projectId); - response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("思维导图用例模版", "UTF-8") + ".xmind"); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("思维导图用例模版", StandardCharsets.UTF_8.name()) + ".xmind"); String fileName = null; if (StringUtils.equals(importType, ExcelImportType.Update.name())) { fileName = "xmind_update.xml"; diff --git a/test-track/backend/src/main/java/io/metersphere/service/issue/platform/AbstractIssuePlatform.java b/test-track/backend/src/main/java/io/metersphere/service/issue/platform/AbstractIssuePlatform.java index 40d0678d5d..b4fa0a15fe 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/issue/platform/AbstractIssuePlatform.java +++ b/test-track/backend/src/main/java/io/metersphere/service/issue/platform/AbstractIssuePlatform.java @@ -34,6 +34,7 @@ import org.springframework.util.MultiValueMap; import java.io.File; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.*; import java.util.function.Function; import java.util.regex.Matcher; @@ -363,7 +364,7 @@ public abstract class AbstractIssuePlatform implements IssuesPlatform { files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + name)); } else if (path.contains("/resource/md/get")) { // 新数据走这里 String name = path.substring(path.indexOf("/resource/md/get") + 26); - files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + URLDecoder.decode(name, "UTF-8"))); + files.add(new File(FileUtils.MD_IMAGE_DIR + "/" + URLDecoder.decode(name, StandardCharsets.UTF_8.name()))); } } } catch (Exception e) { diff --git a/test-track/backend/src/main/java/io/metersphere/service/issue/platform/JiraPlatform.java b/test-track/backend/src/main/java/io/metersphere/service/issue/platform/JiraPlatform.java index b34e6b88bc..5f0473af61 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/issue/platform/JiraPlatform.java +++ b/test-track/backend/src/main/java/io/metersphere/service/issue/platform/JiraPlatform.java @@ -417,7 +417,7 @@ public class JiraPlatform extends AbstractIssuePlatform { try { while (matcher.find()) { String url = matcher.group(1); - result = URLDecoder.decode(url, "UTF-8"); + result = URLDecoder.decode(url, StandardCharsets.UTF_8.name()); } } catch (Exception exception) { return targetStr; diff --git a/test-track/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java b/test-track/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java index a5f8707b6a..c2e8f6d2cb 100644 --- a/test-track/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java +++ b/test-track/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java @@ -14,6 +14,7 @@ import org.json.XML; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -40,7 +41,7 @@ public class XmindLegacy { } // 去除title中svg:width属性 xmlContent = xmlContent.replaceAll("", "<title>"); - Document document = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes("utf-8")));// 读取XML文件,获得document对象 + Document document = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8.name())));// 读取XML文件,获得document对象 Element root = document.getRootElement(); List<Node> topics = root.selectNodes("//topic"); @@ -49,7 +50,7 @@ public class XmindLegacy { xmlComments = xmlComments.replace("xmlns=\"urn:xmind:xmap:xmlns:comments:2.0\"", ""); // 添加评论到content中 - Document commentDocument = XmlUtils.getDocument(new ByteArrayInputStream(xmlComments.getBytes("utf-8"))); + Document commentDocument = XmlUtils.getDocument(new ByteArrayInputStream(xmlComments.getBytes(StandardCharsets.UTF_8.name()))); List<Node> commentsList = commentDocument.selectNodes("//comment"); for (Node topic : topics) { @@ -102,7 +103,7 @@ public class XmindLegacy { * @throws Exception */ private static String removeTopicsFromString(String xmlContent) throws Exception { - Document doc = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes("utf-8"))); + Document doc = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8.name()))); if (doc != null) { Element root = doc.getRootElement(); List<Element> childrenElement = root.elements(); diff --git a/test-track/backend/src/main/java/io/metersphere/xmind/utils/XmindExportUtil.java b/test-track/backend/src/main/java/io/metersphere/xmind/utils/XmindExportUtil.java index 7e7d9de086..7119c834cd 100644 --- a/test-track/backend/src/main/java/io/metersphere/xmind/utils/XmindExportUtil.java +++ b/test-track/backend/src/main/java/io/metersphere/xmind/utils/XmindExportUtil.java @@ -17,6 +17,7 @@ import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,9 +38,9 @@ public class XmindExportUtil { IWorkbook workBook = this.createXmindByTestCase(rootXmind); response.setContentType("application/octet-stream"); - response.setCharacterEncoding("utf-8"); + response.setCharacterEncoding(StandardCharsets.UTF_8.name()); try { - response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("TestCaseExport", "UTF-8") + ".xmind"); + response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("TestCaseExport", StandardCharsets.UTF_8.name()) + ".xmind"); workBook.save(response.getOutputStream()); // EasyExcel.write(response.getOutputStream(), this.clazz).registerWriteHandler(horizontalCellStyleStrategy).sheet(sheetName).doWrite(data); } catch (UnsupportedEncodingException e) {