org.springframework.messaging.handler.annotation.reactive.PayloadMethodArgumentResolver Maven / Gradle / Ivy
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.messaging.handler.annotation.reactive;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.Conventions;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.codec.Decoder;
import org.springframework.core.codec.DecodingException;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException;
import org.springframework.messaging.handler.invocation.MethodArgumentResolutionException;
import org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.SmartValidator;
import org.springframework.validation.Validator;
import org.springframework.validation.annotation.Validated;
/**
* A resolver to extract and decode the payload of a message using a
* {@link Decoder}, where the payload is expected to be a {@link Publisher} of
* {@link DataBuffer DataBuffer}.
*
* Validation is applied if the method argument is annotated with
* {@code @javax.validation.Valid} or
* {@link org.springframework.validation.annotation.Validated}. Validation
* failure results in an {@link MethodArgumentNotValidException}.
*
*
This resolver should be ordered last if {@link #useDefaultResolution} is
* set to {@code true} since in that case it supports all types and does not
* require the presence of {@link Payload}.
*
* @author Rossen Stoyanchev
* @since 5.2
*/
public class PayloadMethodArgumentResolver implements HandlerMethodArgumentResolver {
protected final Log logger = LogFactory.getLog(getClass());
private final List> decoders;
@Nullable
private final Validator validator;
private final ReactiveAdapterRegistry adapterRegistry;
private final boolean useDefaultResolution;
public PayloadMethodArgumentResolver(List> decoders, @Nullable Validator validator,
@Nullable ReactiveAdapterRegistry registry, boolean useDefaultResolution) {
Assert.isTrue(!CollectionUtils.isEmpty(decoders), "At least one Decoder is required");
this.decoders = Collections.unmodifiableList(new ArrayList<>(decoders));
this.validator = validator;
this.adapterRegistry = registry != null ? registry : ReactiveAdapterRegistry.getSharedInstance();
this.useDefaultResolution = useDefaultResolution;
}
/**
* Return a read-only list of the configured decoders.
*/
public List> getDecoders() {
return this.decoders;
}
/**
* Return the configured validator, if any.
*/
@Nullable
public Validator getValidator() {
return this.validator;
}
/**
* Return the configured {@link ReactiveAdapterRegistry}.
*/
public ReactiveAdapterRegistry getAdapterRegistry() {
return this.adapterRegistry;
}
/**
* Whether this resolver is configured to use default resolution, i.e.
* works for any argument type regardless of whether {@code @Payload} is
* present or not.
*/
public boolean isUseDefaultResolution() {
return this.useDefaultResolution;
}
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(Payload.class) || this.useDefaultResolution;
}
/**
* Decode the content of the given message payload through a compatible
* {@link Decoder}.
*
* Validation is applied if the method argument is annotated with
* {@code @javax.validation.Valid} or
* {@link org.springframework.validation.annotation.Validated}. Validation
* failure results in an {@link MethodArgumentNotValidException}.
* @param parameter the target method argument that we are decoding to
* @param message the message from which the content was extracted
* @return a Mono with the result of argument resolution
* @see #extractContent(MethodParameter, Message)
* @see #getMimeType(Message)
*/
@Override
public final Mono