parent
0183576215
commit
3f32f72bfc
|
@ -21,7 +21,6 @@ import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
@ -150,12 +149,9 @@ public abstract class HandlerResultHandlerSupport implements Ordered {
|
||||||
private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
|
private List<MediaType> getProducibleTypes(ServerWebExchange exchange,
|
||||||
Supplier<List<MediaType>> producibleTypesSupplier) {
|
Supplier<List<MediaType>> producibleTypesSupplier) {
|
||||||
|
|
||||||
Optional<Object> optional = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE);
|
return exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE)
|
||||||
if (optional.isPresent()) {
|
.map(attribute -> (List<MediaType>) new ArrayList<>((Set<MediaType>) attribute))
|
||||||
Set<MediaType> set = (Set<MediaType>) optional.get();
|
.orElseGet(producibleTypesSupplier);
|
||||||
return new ArrayList<>(set);
|
|
||||||
}
|
|
||||||
return producibleTypesSupplier.get();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
|
private MediaType selectMoreSpecificMediaType(MediaType acceptable, MediaType producible) {
|
||||||
|
|
|
@ -20,7 +20,6 @@ import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@ -32,7 +31,6 @@ import org.springframework.core.codec.Encoder;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
import org.springframework.http.codec.EncoderHttpMessageWriter;
|
||||||
import org.springframework.http.codec.HttpMessageWriter;
|
import org.springframework.http.codec.HttpMessageWriter;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
@ -116,25 +114,26 @@ public class HttpMessageWriterView implements View {
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Mono<Void> render(Map<String, ?> model, MediaType contentType, ServerWebExchange exchange) {
|
public Mono<Void> render(Map<String, ?> model, MediaType contentType, ServerWebExchange exchange) {
|
||||||
return getObjectToRender(model)
|
Object value = getObjectToRender(model);
|
||||||
.map(value -> write(value, contentType, exchange))
|
return (value != null) ?
|
||||||
.orElseGet(() -> exchange.getResponse().setComplete());
|
write(value, contentType, exchange) :
|
||||||
|
exchange.getResponse().setComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Object> getObjectToRender(Map<String, ?> model) {
|
private Object getObjectToRender(Map<String, ?> model) {
|
||||||
|
|
||||||
Map<String, ?> result = model.entrySet().stream()
|
Map<String, ?> result = model.entrySet().stream()
|
||||||
.filter(this::isMatch)
|
.filter(this::isMatch)
|
||||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||||
|
|
||||||
if (result.isEmpty()) {
|
if (result.isEmpty()) {
|
||||||
return Optional.empty();
|
return null;
|
||||||
}
|
}
|
||||||
else if (result.size() == 1) {
|
else if (result.size() == 1) {
|
||||||
return Optional.of(result.values().iterator().next());
|
return result.values().iterator().next();
|
||||||
}
|
}
|
||||||
else if (this.canWriteMap) {
|
else if (this.canWriteMap) {
|
||||||
return Optional.of(result);
|
return result;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new IllegalStateException("Multiple matches found: " + result + " but " +
|
throw new IllegalStateException("Multiple matches found: " + result + " but " +
|
||||||
|
|
|
@ -389,7 +389,7 @@ public class RequestContext {
|
||||||
protected <T> T getModelObject(String modelName) {
|
protected <T> T getModelObject(String modelName) {
|
||||||
T modelObject = (T) this.model.get(modelName);
|
T modelObject = (T) this.model.get(modelName);
|
||||||
if (modelObject == null) {
|
if (modelObject == null) {
|
||||||
modelObject = (T) this.exchange.getAttribute(modelName);
|
modelObject = (T) this.exchange.getAttribute(modelName).orElse(null);
|
||||||
}
|
}
|
||||||
return modelObject;
|
return modelObject;
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,6 +42,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.context.ApplicationContextException;
|
import org.springframework.context.ApplicationContextException;
|
||||||
import org.springframework.core.io.buffer.DataBuffer;
|
import org.springframework.core.io.buffer.DataBuffer;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.util.MimeType;
|
||||||
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
|
import org.springframework.web.reactive.result.view.AbstractUrlBasedView;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
|
|
||||||
|
@ -177,7 +178,7 @@ public class FreeMarkerView extends AbstractUrlBasedView {
|
||||||
|
|
||||||
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
|
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().allocateBuffer();
|
||||||
try {
|
try {
|
||||||
Charset charset = getCharset(contentType).orElse(getDefaultCharset());
|
Charset charset = getCharset(contentType);
|
||||||
Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset);
|
Writer writer = new OutputStreamWriter(dataBuffer.asOutputStream(), charset);
|
||||||
getTemplate(locale).process(freeMarkerModel, writer);
|
getTemplate(locale).process(freeMarkerModel, writer);
|
||||||
}
|
}
|
||||||
|
@ -191,8 +192,8 @@ public class FreeMarkerView extends AbstractUrlBasedView {
|
||||||
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
|
return exchange.getResponse().writeWith(Flux.just(dataBuffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<Charset> getCharset(MediaType mediaType) {
|
private Charset getCharset(MediaType mediaType) {
|
||||||
return (mediaType != null ? Optional.ofNullable(mediaType.getCharset()) : Optional.empty());
|
return Optional.ofNullable(mediaType).map(MimeType::getCharset).orElse(getDefaultCharset());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue