All Downloads are FREE. Search and download functionalities are using the official Maven repository.

artoria.context.spring.ContextSupportRequestBodyAdvice Maven / Gradle / Ivy

The newest version!
package artoria.context.spring;

import artoria.bean.BeanHolder;
import artoria.context.support.ServletContext;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;

import java.lang.reflect.Type;
import java.util.Map;

/**
 * The request body advice for the servlet context.
 * @author Kahle
 * @see org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice
 */
@ControllerAdvice
public class ContextSupportRequestBodyAdvice extends RequestBodyAdviceAdapter {

    @Override
    public boolean supports(@NonNull MethodParameter methodParameter,
                            @NonNull Type targetType,
                            @NonNull Class> converterType) {
        return true;
    }

    @NonNull
    @Override
    public Object afterBodyRead(@NonNull Object body,
                                @NonNull HttpInputMessage inputMessage,
                                @NonNull MethodParameter parameter,
                                @NonNull Type targetType,
                                @NonNull Class> converterType) {
        Map beans = BeanHolder.getBeans(ServletContext.class);
        if (beans == null || beans.isEmpty()) { return body; }
        for (Map.Entry entry : beans.entrySet()) {
            ServletContext context = entry.getValue();
            context.setRequestBody(body);
        }
        return body;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy