From ad1f1c31976e1d799cf2c4c5f86af6dd2359c064 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Fri, 1 Sep 2023 00:03:00 +0800 Subject: [PATCH] Polish --- .../common/web/AuthenticationConfiguration.java | 2 +- .../common/web/CorsFilterConfiguration.java | 2 +- .../exception/DefaultGlobalExceptionResolver.java | 13 +++---------- .../common/web/result/ResponseBodyHandler.java | 12 ++++++------ .../common/web/servlet/ContentCachingRequest.java | 11 ++++++++--- .../common/web/support/LongToStringSerializer.java | 6 +++--- .../authentication/AbstractAuthenticator.java | 8 ++++---- .../authentication/AuthenticationInterceptor.java | 9 ++++----- .../com/schbrain/common/web/utils/ExcelUtils.java | 6 +++--- .../web/utils/RequestContentCachingUtils.java | 14 +++++++++++--- 10 files changed, 44 insertions(+), 39 deletions(-) diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java b/commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java index e486e29..f9e4508 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/AuthenticationConfiguration.java @@ -21,4 +21,4 @@ public class AuthenticationConfiguration { return new AuthenticationInterceptor(authenticator); } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java b/commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java index 24ede8d..9e953e2 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/CorsFilterConfiguration.java @@ -49,4 +49,4 @@ public class CorsFilterConfiguration { return registrationBean; } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java index 6df4b81..ef6172b 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/exception/DefaultGlobalExceptionResolver.java @@ -1,7 +1,6 @@ package com.schbrain.common.web.exception; import com.schbrain.common.web.annotation.ResponseWrapOption; -import com.schbrain.common.web.utils.HandlerMethodAnnotationUtils; import lombok.EqualsAndHashCode; import lombok.extern.slf4j.Slf4j; import org.springframework.web.context.request.ServletWebRequest; @@ -24,6 +23,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static com.schbrain.common.web.utils.HandlerMethodAnnotationUtils.getAnnotation; + /** * @author liaozan * @since 2022/8/30 @@ -33,13 +34,9 @@ import java.util.concurrent.ConcurrentHashMap; public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExceptionResolver { private final GlobalExceptionHandler exceptionHandler; - private final ExceptionHandlerMethodResolver handlerMethodResolver; - private final HandlerMethodArgumentResolverComposite argumentResolverComposite; - private final HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite; - private final Map, ExceptionHandlerMethodResolver> exceptionHandlerMethodResolvers = new ConcurrentHashMap<>(64); public DefaultGlobalExceptionResolver(ExceptionHandlerExceptionResolver handlerMethodResolver, GlobalExceptionHandler exceptionHandler) { @@ -54,8 +51,7 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; - ResponseWrapOption responseWrapOption = HandlerMethodAnnotationUtils.getAnnotation(handlerMethod, ResponseWrapOption.class); - + ResponseWrapOption responseWrapOption = getAnnotation(handlerMethod, ResponseWrapOption.class); if (responseWrapOption == null) { return true; } @@ -123,9 +119,6 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti return exceptionHandlerMethodResolvers.computeIfAbsent(handlerType, key -> new ExceptionHandlerMethodResolver(handlerType)); } - /** - * copy from spring - */ private Object[] getArguments(Exception exception, HandlerMethod handlerMethod) { List exceptions = new ArrayList<>(); Throwable exToExpose = exception; diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java b/commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java index 592fb93..b021425 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/result/ResponseBodyHandler.java @@ -14,6 +14,8 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; + /** * @author liaozan * @since 2021/10/15 @@ -53,19 +55,17 @@ public class ResponseBodyHandler implements ResponseBodyAdvice { return false; } - Class declaringClass = targetMethod.getDeclaringClass(); - - ResponseWrapOption responseWrapOption = targetMethod.getAnnotation(ResponseWrapOption.class); + ResponseWrapOption responseWrapOption = getAnnotation(targetMethod, ResponseWrapOption.class); if (responseWrapOption == null) { - responseWrapOption = declaringClass.getAnnotation(ResponseWrapOption.class); + responseWrapOption = getAnnotation(targetMethod.getDeclaringClass(), ResponseWrapOption.class); } if (responseWrapOption != null) { return !responseWrapOption.ignore(); } - String packageName = declaringClass.getPackage().getName(); + String packageName = targetMethod.getDeclaringClass().getPackage().getName(); return basePackages.stream().anyMatch(packageName::startsWith); } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java b/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java index 8a1da89..375def6 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java @@ -18,7 +18,7 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { public ContentCachingRequest(HttpServletRequest request) { super(request); - this.inputStream = initWrappedInputStream(request); + this.inputStream = createWrappedInputStream(request); } @Override @@ -27,7 +27,9 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { } /** - * Return the cached request content as a String. The Charset used to decode the cached content is the same as returned by getCharacterEncoding. + * Return the cached request content as a String. + *

+ * The Charset used to decode the cached content is the same as returned by getCharacterEncoding. */ public String getContentAsString() { return getContentAsString(getCharacterEncoding()); @@ -40,7 +42,10 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { return inputStream.getContentAsString(charset); } - private WrappedByteArrayInputStream initWrappedInputStream(HttpServletRequest request) { + /** + * Wrap request inputStream to WrappedByteArrayInputStream + */ + private WrappedByteArrayInputStream createWrappedInputStream(HttpServletRequest request) { try { byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream()); return new WrappedByteArrayInputStream(bytes); diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java b/commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java index f272aba..8ff2e8c 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/support/LongToStringSerializer.java @@ -22,11 +22,11 @@ public class LongToStringSerializer extends StdSerializer { } @Override - public void serialize(Long value, JsonGenerator gen, SerializerProvider provider) throws IOException { + public void serialize(Long value, JsonGenerator generator, SerializerProvider provider) throws IOException { if (value.doubleValue() > FRONT_MAX_VALUE) { - gen.writeString(value.toString()); + generator.writeString(value.toString()); } else { - gen.writeNumber(value); + generator.writeNumber(value); } } diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java b/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java index 7155a8c..4aa0da2 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AbstractAuthenticator.java @@ -3,7 +3,6 @@ package com.schbrain.common.web.support.authentication; import cn.hutool.extra.spring.SpringUtil; import com.schbrain.common.annotation.IgnoreLogin; import com.schbrain.common.web.properties.WebProperties; -import com.schbrain.common.web.utils.HandlerMethodAnnotationUtils; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -14,6 +13,8 @@ import javax.annotation.Nullable; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import static com.schbrain.common.web.utils.HandlerMethodAnnotationUtils.hasAnnotation; + /** * @author liaozan * @since 2022/11/12 @@ -35,8 +36,7 @@ public abstract class AbstractAuthenticator implements Authenticator { @Override public boolean validate(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) { - boolean ignore = HandlerMethodAnnotationUtils.hasAnnotation(handlerMethod, IgnoreLogin.class); - if (ignore) { + if (hasAnnotation(handlerMethod, IgnoreLogin.class)) { return true; } String authentication = getAuthentication(request); @@ -60,4 +60,4 @@ public abstract class AbstractAuthenticator implements Authenticator { return authentication; } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java b/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java index 62e5ec6..757b740 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/support/authentication/AuthenticationInterceptor.java @@ -2,13 +2,13 @@ package com.schbrain.common.web.support.authentication; import cn.hutool.extra.servlet.ServletUtil; import com.schbrain.common.util.JacksonUtils; +import com.schbrain.common.util.ValidateUtils; import com.schbrain.common.web.result.ResponseDTO; import com.schbrain.common.web.support.BaseHandlerInterceptor; import lombok.Data; import lombok.EqualsAndHashCode; import org.springframework.core.Ordered; import org.springframework.http.MediaType; -import org.springframework.util.Assert; import org.springframework.web.method.HandlerMethod; import javax.servlet.http.HttpServletRequest; @@ -28,7 +28,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements private Authenticator authenticator; public AuthenticationInterceptor(Authenticator authenticator) { - Assert.notNull(authenticator, "authenticator must not be null"); + ValidateUtils.notNull(authenticator, "authenticator must not be null"); this.authenticator = authenticator; } @@ -39,8 +39,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements @Override protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) { - boolean validated = authenticator.validate(request, response, handlerMethod); - if (validated) { + if (authenticator.validate(request, response, handlerMethod)) { return true; } writeResult(response, buildAccessDeniedResponse()); @@ -61,4 +60,4 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements return ResponseDTO.error("未获取到认证信息, 请尝试重新登录", LOGIN_REQUIRED, ALERT); } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java b/commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java index acea460..a282869 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/utils/ExcelUtils.java @@ -21,14 +21,14 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils { public static void writeToResponse(List dataList, String fileName) { if (CollectionUtils.isEmpty(dataList)) { - throw new ExcelException("DataList is empty"); + throw new ExcelException("dataList is empty"); } writeToResponse(dataList, dataList.get(0).getClass(), fileName); } public static void writeToResponse(List dataList, Class head, String fileName) { if (CollectionUtils.isEmpty(dataList)) { - throw new ExcelException("DataList is empty"); + throw new ExcelException("dataList is empty"); } try { HttpServletResponse response = ServletUtils.getResponse(); @@ -48,4 +48,4 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils { } } -} \ No newline at end of file +} diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java b/commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java index b7a4832..b1f87e7 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/utils/RequestContentCachingUtils.java @@ -1,8 +1,8 @@ package com.schbrain.common.web.utils; +import com.schbrain.common.util.ValidateUtils; import com.schbrain.common.web.servlet.ContentCachingRequest; import lombok.extern.slf4j.Slf4j; -import org.springframework.util.Assert; import javax.annotation.Nullable; import javax.servlet.http.HttpServletRequest; @@ -20,7 +20,7 @@ public class RequestContentCachingUtils { * Make request content cacheable to avoid stream closed error after inputStream closed */ public static ContentCachingRequest wrapIfRequired(HttpServletRequest request) { - Assert.notNull(request, "request must not be null"); + ValidateUtils.notNull(request, "request must not be null"); if (request instanceof ContentCachingRequest) { return (ContentCachingRequest) request; } else { @@ -33,12 +33,20 @@ public class RequestContentCachingUtils { */ @Nullable public static String getRequestBody(HttpServletRequest request) { + return getRequestBody(request, request.getCharacterEncoding()); + } + + /** + * Get request body content + */ + @Nullable + public static String getRequestBody(HttpServletRequest request, String characterEncoding) { ContentCachingRequest requestToUse = getNativeRequest(request, ContentCachingRequest.class); if (requestToUse == null) { log.warn("request is not an instance of {}", ContentCachingRequest.class.getSimpleName()); return null; } - return requestToUse.getContentAsString(); + return requestToUse.getContentAsString(characterEncoding); } } -- GitLab