use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.
the class ResponseEntityResultHandler method handleResult.
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
Mono<?> returnValueMono;
MethodParameter bodyParameter;
ReactiveAdapter adapter = getAdapter(result);
if (adapter != null) {
Assert.isTrue(!adapter.isMultiValue(), "Only a single ResponseEntity supported");
returnValueMono = Mono.from(adapter.toPublisher(result.getReturnValue()));
bodyParameter = result.getReturnTypeSource().nested().nested();
} else {
returnValueMono = Mono.justOrEmpty(result.getReturnValue());
bodyParameter = result.getReturnTypeSource().nested();
}
return returnValueMono.then(returnValue -> {
Assert.isInstanceOf(HttpEntity.class, returnValue, "HttpEntity expected");
HttpEntity<?> httpEntity = (HttpEntity<?>) returnValue;
if (httpEntity instanceof ResponseEntity) {
ResponseEntity<?> responseEntity = (ResponseEntity<?>) httpEntity;
exchange.getResponse().setStatusCode(responseEntity.getStatusCode());
}
HttpHeaders entityHeaders = httpEntity.getHeaders();
HttpHeaders responseHeaders = exchange.getResponse().getHeaders();
if (!entityHeaders.isEmpty()) {
entityHeaders.entrySet().stream().filter(entry -> !responseHeaders.containsKey(entry.getKey())).forEach(entry -> responseHeaders.put(entry.getKey(), entry.getValue()));
}
if (httpEntity.getBody() == null) {
return exchange.getResponse().setComplete();
}
String etag = entityHeaders.getETag();
Instant lastModified = Instant.ofEpochMilli(entityHeaders.getLastModified());
HttpMethod httpMethod = exchange.getRequest().getMethod();
if (SAFE_METHODS.contains(httpMethod) && exchange.checkNotModified(etag, lastModified)) {
return exchange.getResponse().setComplete();
}
return writeBody(httpEntity.getBody(), bodyParameter, exchange);
});
}
use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.
the class ViewResolutionResultHandler method addBindingResult.
private void addBindingResult(BindingContext context, ServerWebExchange exchange) {
Map<String, Object> model = context.getModel().asMap();
model.keySet().stream().filter(name -> isBindingCandidate(name, model.get(name))).filter(name -> !model.containsKey(BindingResult.MODEL_KEY_PREFIX + name)).forEach(name -> {
WebExchangeDataBinder binder = context.createDataBinder(exchange, model.get(name), name);
model.put(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
});
}
use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.
the class DispatcherHandlerErrorTests method unknownReturnType.
@Test
public void unknownReturnType() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/unknown-return-type").toExchange();
Mono<Void> publisher = this.dispatcherHandler.handle(exchange);
StepVerifier.create(publisher).consumeErrorWith(error -> {
assertThat(error, instanceOf(IllegalStateException.class));
assertThat(error.getMessage(), startsWith("No HandlerResultHandler"));
}).verify();
}
use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.
the class PathExtensionContentTypeResolverTests method resolveMediaTypesFromMapping.
@Test
public void resolveMediaTypesFromMapping() throws Exception {
ServerWebExchange exchange = MockServerHttpRequest.get("/test.html").toExchange();
PathExtensionContentTypeResolver resolver = new PathExtensionContentTypeResolver();
List<MediaType> mediaTypes = resolver.resolveMediaTypes(exchange);
assertEquals(Collections.singletonList(new MediaType("text", "html")), mediaTypes);
Map<String, MediaType> mapping = Collections.singletonMap("HTML", MediaType.APPLICATION_XHTML_XML);
resolver = new PathExtensionContentTypeResolver(mapping);
mediaTypes = resolver.resolveMediaTypes(exchange);
assertEquals(Collections.singletonList(new MediaType("application", "xhtml+xml")), mediaTypes);
}
use of org.springframework.web.server.ServerWebExchange in project spring-framework by spring-projects.
the class WebFluxConfigurationSupportTests method requestMappingHandlerMapping.
@Test
public void requestMappingHandlerMapping() throws Exception {
ApplicationContext context = loadConfig(WebFluxConfig.class);
String name = "requestMappingHandlerMapping";
RequestMappingHandlerMapping mapping = context.getBean(name, RequestMappingHandlerMapping.class);
assertNotNull(mapping);
assertEquals(0, mapping.getOrder());
assertTrue(mapping.useSuffixPatternMatch());
assertTrue(mapping.useTrailingSlashMatch());
assertTrue(mapping.useRegisteredSuffixPatternMatch());
name = "webFluxContentTypeResolver";
RequestedContentTypeResolver resolver = context.getBean(name, RequestedContentTypeResolver.class);
assertSame(resolver, mapping.getContentTypeResolver());
ServerWebExchange exchange = MockServerHttpRequest.get("/path.json").toExchange();
List<MediaType> list = Collections.singletonList(MediaType.APPLICATION_JSON);
assertEquals(list, resolver.resolveMediaTypes(exchange));
exchange = MockServerHttpRequest.get("/path.xml").toExchange();
assertEquals(Collections.emptyList(), resolver.resolveMediaTypes(exchange));
}
Aggregations