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 e486e29bf84503e4cd393314a38abd68880ce64a..f9e450855b0f1f81d2a70d311263bb00a66b5350 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 24ede8dbd2ec13dd2df40ef469a549491b6c035b..9e953e29788d1862de1fae5b04c995010469dc69 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 6df4b81544ff094c8463913ec7f5658b142e569e..ef6172ba0afbb6cd008b4e4eb2f071ed1f6c2afb 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 592fb93abd45d2b4303183568449ccbaf95cddef..b0214250b256c22ec83033210330a11dc567d49d 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 8a1da89dcdc88d8e9b1f954130173a30c3fe5d2a..375def65c1070b0d22a98cc52de1400a11b3c1b0 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 f272aba0edd6c8a0bcb391a8e5344808cbbf988b..8ff2e8c3149396a8e851cb9e9920469eb788c4ba 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 7155a8cca9c66e5be678c7eff29fbbb31b3ce0c2..4aa0da2c82d7e5c55810ee959f7ec19e270494dd 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 62e5ec6f9da4a0869f90402fd4b126b2827f1f3d..757b74093d561df9c5c6a90d13dd8540faa525db 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 acea460b2b00026308c283953536aea55c11a7d1..a282869da5ec64a0b5ad712e9863a43d5e4422fb 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 b7a4832ae7994ab532d4654cf9d857307ff92c7b..b1f87e71c89d21a64b5549de5068c6982e629a25 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); } }