Commit ad1f1c31 authored by liaozan's avatar liaozan 🏀

Polish

parent 7e3fec07
package com.schbrain.common.web.exception; package com.schbrain.common.web.exception;
import com.schbrain.common.web.annotation.ResponseWrapOption; import com.schbrain.common.web.annotation.ResponseWrapOption;
import com.schbrain.common.web.utils.HandlerMethodAnnotationUtils;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.ServletWebRequest; import org.springframework.web.context.request.ServletWebRequest;
...@@ -24,6 +23,8 @@ import java.util.List; ...@@ -24,6 +23,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static com.schbrain.common.web.utils.HandlerMethodAnnotationUtils.getAnnotation;
/** /**
* @author liaozan * @author liaozan
* @since 2022/8/30 * @since 2022/8/30
...@@ -33,13 +34,9 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -33,13 +34,9 @@ import java.util.concurrent.ConcurrentHashMap;
public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExceptionResolver { public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExceptionResolver {
private final GlobalExceptionHandler exceptionHandler; private final GlobalExceptionHandler exceptionHandler;
private final ExceptionHandlerMethodResolver handlerMethodResolver; private final ExceptionHandlerMethodResolver handlerMethodResolver;
private final HandlerMethodArgumentResolverComposite argumentResolverComposite; private final HandlerMethodArgumentResolverComposite argumentResolverComposite;
private final HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite; private final HandlerMethodReturnValueHandlerComposite returnValueHandlerComposite;
private final Map<Class<?>, ExceptionHandlerMethodResolver> exceptionHandlerMethodResolvers = new ConcurrentHashMap<>(64); private final Map<Class<?>, ExceptionHandlerMethodResolver> exceptionHandlerMethodResolvers = new ConcurrentHashMap<>(64);
public DefaultGlobalExceptionResolver(ExceptionHandlerExceptionResolver handlerMethodResolver, GlobalExceptionHandler exceptionHandler) { public DefaultGlobalExceptionResolver(ExceptionHandlerExceptionResolver handlerMethodResolver, GlobalExceptionHandler exceptionHandler) {
...@@ -54,8 +51,7 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti ...@@ -54,8 +51,7 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti
protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) { protected boolean shouldApplyTo(HttpServletRequest request, @Nullable Object handler) {
if (handler instanceof HandlerMethod) { if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler; HandlerMethod handlerMethod = (HandlerMethod) handler;
ResponseWrapOption responseWrapOption = HandlerMethodAnnotationUtils.getAnnotation(handlerMethod, ResponseWrapOption.class); ResponseWrapOption responseWrapOption = getAnnotation(handlerMethod, ResponseWrapOption.class);
if (responseWrapOption == null) { if (responseWrapOption == null) {
return true; return true;
} }
...@@ -123,9 +119,6 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti ...@@ -123,9 +119,6 @@ public class DefaultGlobalExceptionResolver extends AbstractHandlerMethodExcepti
return exceptionHandlerMethodResolvers.computeIfAbsent(handlerType, key -> new ExceptionHandlerMethodResolver(handlerType)); return exceptionHandlerMethodResolvers.computeIfAbsent(handlerType, key -> new ExceptionHandlerMethodResolver(handlerType));
} }
/**
* copy from spring
*/
private Object[] getArguments(Exception exception, HandlerMethod handlerMethod) { private Object[] getArguments(Exception exception, HandlerMethod handlerMethod) {
List<Throwable> exceptions = new ArrayList<>(); List<Throwable> exceptions = new ArrayList<>();
Throwable exToExpose = exception; Throwable exToExpose = exception;
......
...@@ -14,6 +14,8 @@ import java.util.List; ...@@ -14,6 +14,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
/** /**
* @author liaozan * @author liaozan
* @since 2021/10/15 * @since 2021/10/15
...@@ -53,18 +55,16 @@ public class ResponseBodyHandler implements ResponseBodyAdvice<Object> { ...@@ -53,18 +55,16 @@ public class ResponseBodyHandler implements ResponseBodyAdvice<Object> {
return false; return false;
} }
Class<?> declaringClass = targetMethod.getDeclaringClass(); ResponseWrapOption responseWrapOption = getAnnotation(targetMethod, ResponseWrapOption.class);
ResponseWrapOption responseWrapOption = targetMethod.getAnnotation(ResponseWrapOption.class);
if (responseWrapOption == null) { if (responseWrapOption == null) {
responseWrapOption = declaringClass.getAnnotation(ResponseWrapOption.class); responseWrapOption = getAnnotation(targetMethod.getDeclaringClass(), ResponseWrapOption.class);
} }
if (responseWrapOption != null) { if (responseWrapOption != null) {
return !responseWrapOption.ignore(); return !responseWrapOption.ignore();
} }
String packageName = declaringClass.getPackage().getName(); String packageName = targetMethod.getDeclaringClass().getPackage().getName();
return basePackages.stream().anyMatch(packageName::startsWith); return basePackages.stream().anyMatch(packageName::startsWith);
} }
......
...@@ -18,7 +18,7 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { ...@@ -18,7 +18,7 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
public ContentCachingRequest(HttpServletRequest request) { public ContentCachingRequest(HttpServletRequest request) {
super(request); super(request);
this.inputStream = initWrappedInputStream(request); this.inputStream = createWrappedInputStream(request);
} }
@Override @Override
...@@ -27,7 +27,9 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { ...@@ -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.
* <p>
* The Charset used to decode the cached content is the same as returned by getCharacterEncoding.
*/ */
public String getContentAsString() { public String getContentAsString() {
return getContentAsString(getCharacterEncoding()); return getContentAsString(getCharacterEncoding());
...@@ -40,7 +42,10 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { ...@@ -40,7 +42,10 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
return inputStream.getContentAsString(charset); return inputStream.getContentAsString(charset);
} }
private WrappedByteArrayInputStream initWrappedInputStream(HttpServletRequest request) { /**
* Wrap request inputStream to WrappedByteArrayInputStream
*/
private WrappedByteArrayInputStream createWrappedInputStream(HttpServletRequest request) {
try { try {
byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream()); byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream());
return new WrappedByteArrayInputStream(bytes); return new WrappedByteArrayInputStream(bytes);
......
...@@ -22,11 +22,11 @@ public class LongToStringSerializer extends StdSerializer<Long> { ...@@ -22,11 +22,11 @@ public class LongToStringSerializer extends StdSerializer<Long> {
} }
@Override @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) { if (value.doubleValue() > FRONT_MAX_VALUE) {
gen.writeString(value.toString()); generator.writeString(value.toString());
} else { } else {
gen.writeNumber(value); generator.writeNumber(value);
} }
} }
......
...@@ -3,7 +3,6 @@ package com.schbrain.common.web.support.authentication; ...@@ -3,7 +3,6 @@ package com.schbrain.common.web.support.authentication;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import com.schbrain.common.annotation.IgnoreLogin; import com.schbrain.common.annotation.IgnoreLogin;
import com.schbrain.common.web.properties.WebProperties; import com.schbrain.common.web.properties.WebProperties;
import com.schbrain.common.web.utils.HandlerMethodAnnotationUtils;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
...@@ -14,6 +13,8 @@ import javax.annotation.Nullable; ...@@ -14,6 +13,8 @@ import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import static com.schbrain.common.web.utils.HandlerMethodAnnotationUtils.hasAnnotation;
/** /**
* @author liaozan * @author liaozan
* @since 2022/11/12 * @since 2022/11/12
...@@ -35,8 +36,7 @@ public abstract class AbstractAuthenticator implements Authenticator { ...@@ -35,8 +36,7 @@ public abstract class AbstractAuthenticator implements Authenticator {
@Override @Override
public boolean validate(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) { public boolean validate(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) {
boolean ignore = HandlerMethodAnnotationUtils.hasAnnotation(handlerMethod, IgnoreLogin.class); if (hasAnnotation(handlerMethod, IgnoreLogin.class)) {
if (ignore) {
return true; return true;
} }
String authentication = getAuthentication(request); String authentication = getAuthentication(request);
......
...@@ -2,13 +2,13 @@ package com.schbrain.common.web.support.authentication; ...@@ -2,13 +2,13 @@ package com.schbrain.common.web.support.authentication;
import cn.hutool.extra.servlet.ServletUtil; import cn.hutool.extra.servlet.ServletUtil;
import com.schbrain.common.util.JacksonUtils; import com.schbrain.common.util.JacksonUtils;
import com.schbrain.common.util.ValidateUtils;
import com.schbrain.common.web.result.ResponseDTO; import com.schbrain.common.web.result.ResponseDTO;
import com.schbrain.common.web.support.BaseHandlerInterceptor; import com.schbrain.common.web.support.BaseHandlerInterceptor;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.method.HandlerMethod; import org.springframework.web.method.HandlerMethod;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -28,7 +28,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements ...@@ -28,7 +28,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements
private Authenticator authenticator; private Authenticator authenticator;
public AuthenticationInterceptor(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; this.authenticator = authenticator;
} }
...@@ -39,8 +39,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements ...@@ -39,8 +39,7 @@ public class AuthenticationInterceptor extends BaseHandlerInterceptor implements
@Override @Override
protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) { protected boolean preHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) {
boolean validated = authenticator.validate(request, response, handlerMethod); if (authenticator.validate(request, response, handlerMethod)) {
if (validated) {
return true; return true;
} }
writeResult(response, buildAccessDeniedResponse()); writeResult(response, buildAccessDeniedResponse());
......
...@@ -21,14 +21,14 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils { ...@@ -21,14 +21,14 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils {
public static <T> void writeToResponse(List<T> dataList, String fileName) { public static <T> void writeToResponse(List<T> dataList, String fileName) {
if (CollectionUtils.isEmpty(dataList)) { if (CollectionUtils.isEmpty(dataList)) {
throw new ExcelException("DataList is empty"); throw new ExcelException("dataList is empty");
} }
writeToResponse(dataList, dataList.get(0).getClass(), fileName); writeToResponse(dataList, dataList.get(0).getClass(), fileName);
} }
public static <T> void writeToResponse(List<T> dataList, Class<?> head, String fileName) { public static <T> void writeToResponse(List<T> dataList, Class<?> head, String fileName) {
if (CollectionUtils.isEmpty(dataList)) { if (CollectionUtils.isEmpty(dataList)) {
throw new ExcelException("DataList is empty"); throw new ExcelException("dataList is empty");
} }
try { try {
HttpServletResponse response = ServletUtils.getResponse(); HttpServletResponse response = ServletUtils.getResponse();
......
package com.schbrain.common.web.utils; package com.schbrain.common.web.utils;
import com.schbrain.common.util.ValidateUtils;
import com.schbrain.common.web.servlet.ContentCachingRequest; import com.schbrain.common.web.servlet.ContentCachingRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
...@@ -20,7 +20,7 @@ public class RequestContentCachingUtils { ...@@ -20,7 +20,7 @@ public class RequestContentCachingUtils {
* Make request content cacheable to avoid stream closed error after inputStream closed * Make request content cacheable to avoid stream closed error after inputStream closed
*/ */
public static ContentCachingRequest wrapIfRequired(HttpServletRequest request) { 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) { if (request instanceof ContentCachingRequest) {
return (ContentCachingRequest) request; return (ContentCachingRequest) request;
} else { } else {
...@@ -33,12 +33,20 @@ public class RequestContentCachingUtils { ...@@ -33,12 +33,20 @@ public class RequestContentCachingUtils {
*/ */
@Nullable @Nullable
public static String getRequestBody(HttpServletRequest request) { 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); ContentCachingRequest requestToUse = getNativeRequest(request, ContentCachingRequest.class);
if (requestToUse == null) { if (requestToUse == null) {
log.warn("request is not an instance of {}", ContentCachingRequest.class.getSimpleName()); log.warn("request is not an instance of {}", ContentCachingRequest.class.getSimpleName());
return null; return null;
} }
return requestToUse.getContentAsString(); return requestToUse.getContentAsString(characterEncoding);
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment