com.github.fieldintercept.springboot.SpringWebMvcRegistrarUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of field-intercept Show documentation
Show all versions of field-intercept Show documentation
Field intercept project for Spring
The newest version!
package com.github.fieldintercept.springboot;
import com.github.fieldintercept.ReturnFieldDispatchAop;
import com.github.fieldintercept.util.BeanMap;
import com.github.fieldintercept.util.PlatformDependentUtil;
import com.github.fieldintercept.util.SpringWebUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.core.MethodParameter;
import org.springframework.core.annotation.Order;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.async.DeferredResult;
import org.springframework.web.context.request.async.WebAsyncUtils;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import java.util.function.Supplier;
public class SpringWebMvcRegistrarUtil {
public static BeanDefinition[] newBeanDefinitions(Supplier propertiesSupplier) {
return new BeanDefinition[]{
BeanDefinitionBuilder.genericBeanDefinition(NonBlockHandlerMethodReturnValueHandlerPostProcessor.class, () -> new NonBlockHandlerMethodReturnValueHandlerPostProcessor(propertiesSupplier)).getBeanDefinition()
};
}
private static class NonBlockHandlerMethodReturnValueHandlerPostProcessor implements BeanPostProcessor {
private final Supplier propertiesSupplier;
private NonBlockHandlerMethodReturnValueHandlerPostProcessor(Supplier propertiesSupplier) {
this.propertiesSupplier = propertiesSupplier;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerAdapter) {
RequestMappingHandlerAdapter requestMappingHandler = (RequestMappingHandlerAdapter) bean;
List newHandlers = new ArrayList<>();
newHandlers.add(new NonBlockHandlerMethodReturnValueHandler(propertiesSupplier));
List oldHandlers = requestMappingHandler.getReturnValueHandlers();
if (oldHandlers != null) {
newHandlers.addAll(oldHandlers);
}
requestMappingHandler.setReturnValueHandlers(newHandlers);
}
return bean;
}
}
@Order(Integer.MIN_VALUE + 10)
public static class NonBlockHandlerMethodReturnValueHandler implements AsyncHandlerMethodReturnValueHandler {
private final Supplier propertiesSupplier;
public NonBlockHandlerMethodReturnValueHandler(Supplier propertiesSupplier) {
this.propertiesSupplier = propertiesSupplier;
}
@Override
public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) {
return isAsyncReturnValue0(returnValue, returnType);
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return isAsyncReturnValue0(null, returnType);
}
private boolean isAsyncReturnValue0(Object returnValue, MethodParameter returnType) {
FieldinterceptProperties properties = propertiesSupplier.get();
if (properties.getBatchAggregation().isPendingNonBlock()) {
ReturnFieldDispatchAop.Pending