use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class HandlerMethodArgumentResolverSupport method checkAnnotatedParamNoReactiveWrapper.
/**
* Evaluate the {@code Predicate} on the method parameter type if it has the
* given annotation, nesting within {@link java.util.Optional} if necessary,
* but raise an {@code IllegalStateException} if the same matches the generic
* type within a reactive type wrapper.
*/
protected <A extends Annotation> boolean checkAnnotatedParamNoReactiveWrapper(MethodParameter param, Class<A> annotationType, BiPredicate<A, Class<?>> typePredicate) {
A annotation = param.getParameterAnnotation(annotationType);
if (annotation == null) {
return false;
}
param = param.nestedIfOptional();
Class<?> type = param.getNestedParameterType();
ReactiveAdapter adapter = getAdapterRegistry().getAdapter(type);
if (adapter != null) {
assertHasValues(adapter, param);
param = param.nested();
type = param.getNestedParameterType();
}
if (typePredicate.test(annotation, type)) {
if (adapter == null) {
return true;
}
throw getReactiveWrapperError(param);
}
return false;
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method handleResult.
@Override
@SuppressWarnings("unchecked")
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Mono<Object> valueMono;
ResolvableType valueType;
ReactiveAdapter adapter = getAdapter(result);
if (adapter != null) {
Assert.isTrue(!adapter.isMultiValue(), "Only single-value async return type supported.");
valueMono = result.getReturnValue().map(value -> Mono.from(adapter.toPublisher(value))).orElse(Mono.empty());
valueType = adapter.isNoValue() ? ResolvableType.forClass(Void.class) : result.getReturnType().getGeneric(0);
} else {
valueMono = Mono.justOrEmpty(result.getReturnValue());
valueType = result.getReturnType();
}
return valueMono.otherwiseIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO).then(returnValue -> {
Mono<List<View>> viewsMono;
Model model = result.getModel();
MethodParameter parameter = result.getReturnTypeSource();
Locale acceptLocale = exchange.getRequest().getHeaders().getAcceptLanguageAsLocale();
Locale locale = acceptLocale != null ? acceptLocale : Locale.getDefault();
Class<?> clazz = valueType.getRawClass();
if (clazz == null) {
clazz = returnValue.getClass();
}
if (returnValue == NO_VALUE || Void.class.equals(clazz) || void.class.equals(clazz)) {
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (Model.class.isAssignableFrom(clazz)) {
model.addAllAttributes(((Model) returnValue).asMap());
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (Map.class.isAssignableFrom(clazz)) {
model.addAllAttributes((Map<String, ?>) returnValue);
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (View.class.isAssignableFrom(clazz)) {
viewsMono = Mono.just(Collections.singletonList((View) returnValue));
} else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
viewsMono = resolveViews(returnValue.toString(), locale);
} else {
String name = getNameForReturnValue(clazz, parameter);
model.addAttribute(name, returnValue);
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
}
return resolveAsyncAttributes(model.asMap()).doOnSuccess(aVoid -> addBindingResult(result.getBindingContext(), exchange)).then(viewsMono).then(views -> render(views, model.asMap(), exchange));
});
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method supports.
@Override
public boolean supports(HandlerResult result) {
if (hasModelAnnotation(result.getReturnTypeSource())) {
return true;
}
Class<?> type = result.getReturnType().getRawClass();
ReactiveAdapter adapter = getAdapter(result);
if (adapter != null) {
if (adapter.isNoValue()) {
return true;
}
type = result.getReturnType().getGeneric(0).getRawClass();
}
return (CharSequence.class.isAssignableFrom(type) || View.class.isAssignableFrom(type) || Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type));
}
Aggregations