Commit ad1f1c31 authored by liaozan's avatar liaozan 🏀

Polish

parent 7e3fec07
......@@ -21,4 +21,4 @@ public class AuthenticationConfiguration {
return new AuthenticationInterceptor(authenticator);
}
}
\ No newline at end of file
}
......@@ -49,4 +49,4 @@ public class CorsFilterConfiguration {
return registrationBean;
}
}
\ No newline at end of file
}
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<Class<?>, 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<Throwable> exceptions = new ArrayList<>();
Throwable exToExpose = exception;
......
......@@ -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<Object> {
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
}
......@@ -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.
* <p>
* 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);
......
......@@ -22,11 +22,11 @@ public class LongToStringSerializer extends StdSerializer<Long> {
}
@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);
}
}
......
......@@ -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
}
......@@ -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
}
......@@ -21,14 +21,14 @@ public class ExcelUtils extends com.schbrain.common.util.ExcelUtils {
public static <T> void writeToResponse(List<T> 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 <T> void writeToResponse(List<T> 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
}
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);
}
}
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