perf:代码调整

This commit is contained in:
曾文豪
2024-09-18 17:13:40 +08:00
parent 2621c6dedc
commit d38077b0f8
11 changed files with 76 additions and 40 deletions

31
qodana.yaml Normal file
View File

@@ -0,0 +1,31 @@
#-------------------------------------------------------------------------------#
# Qodana analysis is configured by qodana.yaml file #
# https://www.jetbrains.com/help/qodana/qodana-yaml.html #
#-------------------------------------------------------------------------------#
version: "1.0"
#Specify inspection profile for code analysis
profile:
name: qodana.starter
#Enable inspections
#include:
# - name: <SomeEnabledInspectionId>
#Disable inspections
#exclude:
# - name: <SomeDisabledInspectionId>
# paths:
# - <path/where/not/run/inspection>
projectJDK: 17 #(Applied in CI/CD pipeline)
#Execute shell command before Qodana execution (Applied in CI/CD pipeline)
#bootstrap: sh ./prepare-qodana.sh
#Install IDE plugins before Qodana execution (Applied in CI/CD pipeline)
#plugins:
# - id: <plugin.id> #(plugin id can be found at https://plugins.jetbrains.com)
#Specify Qodana linter for analysis (Applied in CI/CD pipeline)
linter: jetbrains/qodana-jvm:latest

View File

@@ -16,10 +16,6 @@ import java.util.Objects;
@Component
public class DemoWebConfigurer implements TieshengWebConfigurer, TsLoginConfigurer {
@Autowired
GlobalConfig globalConfig;
@Override
public RequestUserInfo getCurrentUserName(TokenBean tokenBean) {
RequestUserInfo info = new RequestUserInfo();

View File

@@ -1,8 +1,6 @@
package com.tiesheng.demo.config;
import com.tiesheng.web.service.CoreConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@@ -13,9 +11,6 @@ import javax.annotation.PostConstruct;
@Component
public class TestJobConfig {
@Autowired
CoreConfigService coreConfigService;
@PostConstruct
public void init() {
}

View File

@@ -219,8 +219,10 @@ public class TestController {
"83_7xqG36kdgwuf8zzWLY3jtz7bg4ucziN-0oxbE0X9zBzwbjZ4S4Ss2RM9uHeSIcRp2K-wEp6MLzWhqo2AXj0Jpzd6IiJdUsRxqdHPvEWqAdOgt83vzZwdDf7tZBkGNGeAFASZS",
paramJson);
Response execute1 = OkHttpUtil.ofHttpClient().build().newCall(request).execute();
if (execute1.body() != null) {
FileUtil.writeFromStream(execute1.body().byteStream(), file.getAbsolutePath());
execute1.close();
}
} catch (Exception e) {
throw new ApiException("每分钟最多生成5000个二维码请稍后再试");
}

View File

@@ -99,7 +99,7 @@ public class AliyunSmsSender implements TsMessageSender {
String signature = specialUrlEncode(digest);
queryMap.put("Signature", signature);
String respBody = "";
String respBody;
try {
respBody = OkHttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp);
} catch (Exception e) {

View File

@@ -76,16 +76,17 @@ public class PlatformDingConfig {
request = OkHttpUtil.ofPost(url, body);
}
request = request.newBuilder().header("x-acs-dingtalk-access-token", accessToken).build();
try {
Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute();
try (Response response = OkHttpUtil.ofHttpClient().build().newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String rawBody = response.body().string();
DingResponse<T> bean = JSON.parseObject(rawBody, typeReference);
bean.setRawBody(rawBody);
return bean;
} else {
if (response.body() != null) {
LogFactory.get().info(response.body().string());
}
}
} catch (Exception e) {
LogFactory.get().error(e);
}

View File

@@ -17,8 +17,11 @@ public class ServletKit extends ServletUtil {
public static HttpServletRequest getRequest() {
ServletRequestAttributes attributes = (ServletRequestAttributes)
RequestContextHolder.getRequestAttributes();
if (attributes != null) {
return attributes.getRequest();
}
return null;
}
/**
* 获取客户端IP
@@ -27,6 +30,9 @@ public class ServletKit extends ServletUtil {
*/
public static String getClientIP() {
HttpServletRequest request = getRequest();
if (request == null) {
return "";
}
return getClientIP(request);
}

View File

@@ -384,7 +384,7 @@ public class JWT implements RegisteredPayload<JWT> {
* @since 5.7.4
*/
public boolean validate(long leeway) {
if (false == verify()) {
if (!verify()) {
return false;
}

View File

@@ -160,13 +160,13 @@ public class JWTValidator {
}
final String algorithmIdInSigner = signer.getAlgorithmId();
if (false == StrUtil.equals(algorithmId, algorithmIdInSigner)) {
if (!StrUtil.equals(algorithmId, algorithmIdInSigner)) {
throw new ValidateException("Algorithm [{}] defined in header doesn't match to [{}]!"
, algorithmId, algorithmIdInSigner);
}
// 通过算法验证签名是否正确
if (false == jwt.verify(signer)) {
if (!jwt.verify(signer)) {
throw new ValidateException("Signature verification failed!");
}
}

View File

@@ -83,9 +83,11 @@ public class OkHttpUtil {
}
public static String get(String urlString, int timeout) {
try {
return ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
.build().newCall(ofGet(urlString)).execute().body().string();
try (Response execute = ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
.build().newCall(ofGet(urlString)).execute()) {
if (execute.body() != null) {
return execute.body().string();
}
} catch (IOException ignored) {
}
return null;
@@ -117,9 +119,11 @@ public class OkHttpUtil {
}
public static String post(String urlString, RequestBody body, int timeout) {
try {
return ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
.build().newCall(ofPost(urlString, body)).execute().body().string();
try (Response response = ofHttpClient().connectTimeout(timeout, TimeUnit.MILLISECONDS)
.build().newCall(ofPost(urlString, body)).execute()) {
if (response.body() != null) {
return response.body().string();
}
} catch (IOException ignored) {
}
return null;
@@ -135,11 +139,10 @@ public class OkHttpUtil {
}
public static File downloadFile(String url, File destFile) {
try {
Response response = ofHttpClient()
try (Response response = ofHttpClient()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.build().newCall(ofGet(url)).execute();
.build().newCall(ofGet(url)).execute()) {
if (response.isSuccessful() && response.body() != null) {
FileUtil.writeFromStream(response.body().byteStream(), destFile);
}
@@ -150,11 +153,10 @@ public class OkHttpUtil {
}
public static byte[] downloadBytes(String url) {
try {
Response response = ofHttpClient()
try (Response response = ofHttpClient()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.build().newCall(ofGet(url)).execute();
.build().newCall(ofGet(url)).execute()) {
if (response.isSuccessful() && response.body() != null) {
return response.body().bytes();
}

View File

@@ -36,12 +36,15 @@ public class OkHttpLogInterceptor implements Interceptor {
logApi.setResult(response.code());
// 如果是json,xml,text则保存记录
if (response.body() != null && StrUtil.containsAll(response.body().contentType().toString(),
"json", "xml", "text")) {
if (response.body() != null) {
MediaType mediaType = response.body().contentType();
String contentType = mediaType == null ? "" : mediaType.toString();
if (StrUtil.containsAny(contentType, "json", "xml", "text")) {
ResponseBody peekBody = response.peekBody(Long.MAX_VALUE);
logApi.setRespBody(peekBody.string());
peekBody.close();
}
}
} catch (Exception e) {
JSONObject object = new JSONObject();