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 @Component
public class DemoWebConfigurer implements TieshengWebConfigurer, TsLoginConfigurer { public class DemoWebConfigurer implements TieshengWebConfigurer, TsLoginConfigurer {
@Autowired
GlobalConfig globalConfig;
@Override @Override
public RequestUserInfo getCurrentUserName(TokenBean tokenBean) { public RequestUserInfo getCurrentUserName(TokenBean tokenBean) {
RequestUserInfo info = new RequestUserInfo(); RequestUserInfo info = new RequestUserInfo();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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