Polishing

This commit is contained in:
Juergen Hoeller
2017-05-05 23:19:08 +02:00
parent 4fdd85324d
commit 39f8bd663e
17 changed files with 69 additions and 105 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -35,21 +35,23 @@ public class ServerResponseResultHandler implements HandlerResultHandler {
private final HandlerStrategies strategies;
/**
* Create a {@code ResponseResultHandler} with default strategies.
* Create a {@code ServerResponseResultHandler} with default strategies.
*/
public ServerResponseResultHandler() {
this(HandlerStrategies.builder().build());
}
/**
* Create a {@code ResponseResultHandler} with the given strategies.
* Create a {@code ServerResponseResultHandler} with the given strategies.
*/
public ServerResponseResultHandler(HandlerStrategies strategies) {
Assert.notNull(strategies, "'strategies' must not be null");
Assert.notNull(strategies, "HandlerStrategies must not be null");
this.strategies = strategies;
}
@Override
public boolean supports(HandlerResult result) {
return result.getReturnValue()
@@ -59,8 +61,8 @@ public class ServerResponseResultHandler implements HandlerResultHandler {
@Override
public Mono<Void> handleResult(ServerWebExchange exchange, HandlerResult result) {
ServerResponse response = (ServerResponse) result.getReturnValue().orElseThrow(
IllegalStateException::new);
ServerResponse response = (ServerResponse) result.getReturnValue().orElseThrow(IllegalStateException::new);
return response.writeTo(exchange, this.strategies);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.result;
import java.util.ArrayList;
@@ -54,15 +55,11 @@ public abstract class HandlerResultHandlerSupport implements Ordered {
private int order = LOWEST_PRECEDENCE;
protected HandlerResultHandlerSupport(RequestedContentTypeResolver contentTypeResolver) {
this(contentTypeResolver, new ReactiveAdapterRegistry());
}
protected HandlerResultHandlerSupport(RequestedContentTypeResolver contentTypeResolver,
ReactiveAdapterRegistry adapterRegistry) {
Assert.notNull(contentTypeResolver, "'contentTypeResolver' is required.");
Assert.notNull(adapterRegistry, "'adapterRegistry' is required.");
Assert.notNull(contentTypeResolver, "RequestedContentTypeResolver is required");
Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required");
this.contentTypeResolver = contentTypeResolver;
this.adapterRegistry = adapterRegistry;
}

View File

@@ -17,7 +17,6 @@
package org.springframework.web.reactive.result.method.annotation;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -84,8 +83,8 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
ReactiveAdapterRegistry adapterRegistry) {
super(adapterRegistry);
Assert.notEmpty(messageReaders, "At least one HttpMessageReader is required.");
Assert.notNull(adapterRegistry, "'adapterRegistry' is required");
Assert.notEmpty(messageReaders, "At least one HttpMessageReader is required");
Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required");
this.messageReaders = messageReaders;
this.supportedMediaTypes = messageReaders.stream()
.flatMap(converter -> converter.getReadableMediaTypes().stream())
@@ -156,13 +155,12 @@ public abstract class AbstractMessageReaderArgumentResolver extends HandlerMetho
}
private Throwable handleReadError(MethodParameter parameter, Throwable ex) {
return ex instanceof DecodingException ?
new ServerWebInputException("Failed to read HTTP message", parameter, ex) : ex;
return (ex instanceof DecodingException ?
new ServerWebInputException("Failed to read HTTP message", parameter, ex) : ex);
}
private ServerWebInputException handleMissingBody(MethodParameter parameter) {
Method method = parameter.getMethod();
return new ServerWebInputException("Request body is missing: " + method.toGenericString());
private ServerWebInputException handleMissingBody(MethodParameter param) {
return new ServerWebInputException("Request body is missing: " + param.getMethod().toGenericString());
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -51,32 +51,27 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
/**
* Constructor with {@link HttpMessageWriter}s and a
* {@code RequestedContentTypeResolver}.
*
* @param messageWriters for serializing Objects to the response body stream
* @param contentTypeResolver for resolving the requested content type
*/
protected AbstractMessageWriterResultHandler(List<HttpMessageWriter<?>> messageWriters,
RequestedContentTypeResolver contentTypeResolver) {
super(contentTypeResolver);
Assert.notEmpty(messageWriters, "At least one message writer is required.");
this.messageWriters = messageWriters;
this(messageWriters, contentTypeResolver, new ReactiveAdapterRegistry());
}
/**
* Constructor with an additional {@link ReactiveAdapterRegistry}.
*
* @param messageWriters for serializing Objects to the response body stream
* @param contentTypeResolver for resolving the requested content type
* @param adapterRegistry for adapting other reactive types (e.g. rx.Observable,
* rx.Single, etc.) to Flux or Mono
*/
protected AbstractMessageWriterResultHandler(List<HttpMessageWriter<?>> messageWriters,
RequestedContentTypeResolver contentTypeResolver,
ReactiveAdapterRegistry adapterRegistry) {
RequestedContentTypeResolver contentTypeResolver, ReactiveAdapterRegistry adapterRegistry) {
super(contentTypeResolver, adapterRegistry);
Assert.notEmpty(messageWriters, "At least one message writer is required.");
Assert.notEmpty(messageWriters, "At least one message writer is required");
this.messageWriters = messageWriters;
}
@@ -124,8 +119,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa
}
else {
if (getProducibleMediaTypes(elementType).isEmpty()) {
return Mono.error(new IllegalStateException(
"No converter for return value type: " + elementType));
return Mono.error(new IllegalStateException("No converter for return value type: " + elementType));
}
}

View File

@@ -20,9 +20,9 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.MediaType;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerWebExchange;
@@ -31,28 +31,17 @@ import org.springframework.web.reactive.accept.FixedContentTypeResolver;
import org.springframework.web.reactive.accept.HeaderContentTypeResolver;
import org.springframework.web.reactive.accept.RequestedContentTypeResolver;
import static org.junit.Assert.assertEquals;
import static org.springframework.http.MediaType.ALL;
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
import static org.springframework.http.MediaType.IMAGE_GIF;
import static org.springframework.http.MediaType.IMAGE_JPEG;
import static org.springframework.http.MediaType.IMAGE_PNG;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.junit.Assert.*;
import static org.springframework.http.MediaType.*;
/**
* Unit tests for {@link HandlerResultHandlerSupport}.
*
* @author Rossen Stoyanchev
*/
public class HandlerResultHandlerTests {
private TestResultHandler resultHandler;
@Before
public void setup() throws Exception {
this.resultHandler = new TestResultHandler();
}
private final TestResultHandler resultHandler = new TestResultHandler();
@Test
@@ -116,7 +105,7 @@ public class HandlerResultHandlerTests {
}
public TestResultHandler(RequestedContentTypeResolver contentTypeResolver) {
super(contentTypeResolver);
super(contentTypeResolver, new ReactiveAdapterRegistry());
}
}