use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ModelInitializer method handleResult.
private Mono<Void> handleResult(HandlerResult handlerResult, BindingContext bindingContext) {
Object value = handlerResult.getReturnValue();
if (value != null) {
ResolvableType type = handlerResult.getReturnType();
ReactiveAdapter adapter = this.adapterRegistry.getAdapter(type.resolve(), value);
if (isAsyncVoidType(type, adapter)) {
return Mono.from(adapter.toPublisher(value));
}
String name = getAttributeName(handlerResult.getReturnTypeSource());
bindingContext.getModel().asMap().putIfAbsent(name, value);
}
return Mono.empty();
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class RequestAttributeMethodArgumentResolver method resolveNamedValue.
@Override
protected Object resolveNamedValue(String name, MethodParameter parameter, ServerWebExchange exchange) {
Object value = exchange.getAttribute(name);
ReactiveAdapter toAdapter = getAdapterRegistry().getAdapter(parameter.getParameterType());
if (toAdapter != null) {
if (value == null) {
Assert.isTrue(toAdapter.supportsEmpty(), () -> "No request attribute '" + name + "' and target type " + parameter.getGenericParameterType() + " doesn't support empty values.");
return toAdapter.fromPublisher(Mono.empty());
}
if (parameter.getParameterType().isAssignableFrom(value.getClass())) {
return value;
}
ReactiveAdapter fromAdapter = getAdapterRegistry().getAdapter(value.getClass());
Assert.isTrue(fromAdapter != null, () -> getClass().getSimpleName() + " doesn't support " + "reactive type wrapper: " + parameter.getGenericParameterType());
return toAdapter.fromPublisher(fromAdapter.toPublisher(value));
}
return value;
}
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) {
if (adapter.isMultiValue()) {
throw new IllegalArgumentException("Multi-value reactive types not supported in view resolution: " + result.getReturnType());
}
valueMono = (result.getReturnValue() != null ? Mono.from(adapter.toPublisher(result.getReturnValue())) : Mono.empty());
valueType = (adapter.isNoValue() ? ResolvableType.forClass(Void.class) : result.getReturnType().getGeneric());
} else {
valueMono = Mono.justOrEmpty(result.getReturnValue());
valueType = result.getReturnType();
}
return valueMono.switchIfEmpty(exchange.isNotModified() ? Mono.empty() : NO_VALUE_MONO).flatMap(returnValue -> {
Mono<List<View>> viewsMono;
Model model = result.getModel();
MethodParameter parameter = result.getReturnTypeSource();
Locale locale = LocaleContextHolder.getLocale(exchange.getLocaleContext());
Class<?> clazz = valueType.toClass();
if (clazz == Object.class) {
clazz = returnValue.getClass();
}
if (returnValue == NO_VALUE || clazz == void.class || clazz == Void.class) {
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (CharSequence.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
viewsMono = resolveViews(returnValue.toString(), locale);
} else if (Rendering.class.isAssignableFrom(clazz)) {
Rendering render = (Rendering) returnValue;
HttpStatus status = render.status();
if (status != null) {
exchange.getResponse().setStatusCode(status);
}
exchange.getResponse().getHeaders().putAll(render.headers());
model.addAllAttributes(render.modelAttributes());
Object view = render.view();
if (view == null) {
view = getDefaultViewName(exchange);
}
viewsMono = (view instanceof String ? resolveViews((String) view, locale) : Mono.just(Collections.singletonList((View) view)));
} else if (Model.class.isAssignableFrom(clazz)) {
model.addAllAttributes(((Model) returnValue).asMap());
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (Map.class.isAssignableFrom(clazz) && !hasModelAnnotation(parameter)) {
model.addAllAttributes((Map<String, ?>) returnValue);
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
} else if (View.class.isAssignableFrom(clazz)) {
viewsMono = Mono.just(Collections.singletonList((View) returnValue));
} else {
String name = getNameForReturnValue(parameter);
model.addAttribute(name, returnValue);
viewsMono = resolveViews(getDefaultViewName(exchange), locale);
}
BindingContext bindingContext = result.getBindingContext();
updateBindingResult(bindingContext, exchange);
return viewsMono.flatMap(views -> render(views, model.asMap(), bindingContext, 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().toClass();
ReactiveAdapter adapter = getAdapter(result);
if (adapter != null) {
if (adapter.isNoValue()) {
return true;
}
type = result.getReturnType().getGeneric().toClass();
}
return (CharSequence.class.isAssignableFrom(type) || Rendering.class.isAssignableFrom(type) || Model.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type) || View.class.isAssignableFrom(type) || !BeanUtils.isSimpleProperty(type));
}
use of org.springframework.core.ReactiveAdapter in project spring-framework by spring-projects.
the class ResponseEntityResultHandler method supports.
@Override
public boolean supports(HandlerResult result) {
Class<?> valueType = resolveReturnValueType(result);
if (isSupportedType(valueType)) {
return true;
}
ReactiveAdapter adapter = getAdapter(result);
return adapter != null && !adapter.isNoValue() && isSupportedType(result.getReturnType().getGeneric().toClass());
}
Aggregations