Browse Source

Refactor View contract

View now returns Mono<Void> rather than Flux<DataBuffer> which aligns
more closely with the reactive HttpMessageConverter vs the Encoder.

The change was prompted by the upcoming implementation of a View that
delegates to an existing HttpMessageConverter e.g. for JSON, XML.

The resulting change also brings the reactive View closer in spirit to
the View from spring-webmvc which returns void.
pull/1111/head
Rossen Stoyanchev 9 years ago
parent
commit
a37b2e3a84
  1. 8
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java
  2. 5
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/View.java
  3. 9
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java
  4. 9
      spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java
  5. 4
      spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java
  6. 6
      spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java
  7. 11
      spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java

8
spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java

@ -24,6 +24,7 @@ import java.util.Map;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
@ -111,10 +112,10 @@ public abstract class AbstractView implements View, ApplicationContextAware {
* @param contentType the content type selected to render with which should * @param contentType the content type selected to render with which should
* match one of the {@link #getSupportedMediaTypes() supported media types}. * match one of the {@link #getSupportedMediaTypes() supported media types}.
* @param exchange the current exchange * @param exchange the current exchange
* @return * @return {@code Mono} to represent when and if rendering succeeds
*/ */
@Override @Override
public Flux<DataBuffer> render(HandlerResult result, MediaType contentType, public Mono<Void> render(HandlerResult result, MediaType contentType,
ServerWebExchange exchange) { ServerWebExchange exchange) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
@ -151,8 +152,9 @@ public abstract class AbstractView implements View, ApplicationContextAware {
* @param renderAttributes combined output Map (never {@code null}), * @param renderAttributes combined output Map (never {@code null}),
* with dynamic values taking precedence over static attributes * with dynamic values taking precedence over static attributes
* @param exchange current exchange * @param exchange current exchange
* @return {@code Mono} to represent when and if rendering succeeds
*/ */
protected abstract Flux<DataBuffer> renderInternal(Map<String, Object> renderAttributes, protected abstract Mono<Void> renderInternal(Map<String, Object> renderAttributes,
ServerWebExchange exchange); ServerWebExchange exchange);

5
spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/View.java

@ -19,6 +19,7 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -53,8 +54,8 @@ public interface View {
* @param contentType the content type selected to render with which should * @param contentType the content type selected to render with which should
* match one of the {@link #getSupportedMediaTypes() supported media types}. * match one of the {@link #getSupportedMediaTypes() supported media types}.
* @param exchange the current exchange * @param exchange the current exchange
* @return the output stream * @return {@code Mono} to represent when and if rendering succeeds
*/ */
Flux<DataBuffer> render(HandlerResult result, MediaType contentType, ServerWebExchange exchange); Mono<Void> render(HandlerResult result, MediaType contentType, ServerWebExchange exchange);
} }

9
spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java

@ -34,7 +34,6 @@ import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -184,8 +183,7 @@ public class ViewResolutionResultHandler implements HandlerResultHandler, Ordere
return viewMono.then(returnValue -> { return viewMono.then(returnValue -> {
if (returnValue instanceof View) { if (returnValue instanceof View) {
Flux<DataBuffer> body = ((View) returnValue).render(result, null, exchange); return ((View) returnValue).render(result, null, exchange);
return exchange.getResponse().writeWith(body);
} }
else if (returnValue instanceof CharSequence) { else if (returnValue instanceof CharSequence) {
String viewName = returnValue.toString(); String viewName = returnValue.toString();
@ -194,10 +192,7 @@ public class ViewResolutionResultHandler implements HandlerResultHandler, Ordere
.concatMap(resolver -> resolver.resolveViewName(viewName, locale)) .concatMap(resolver -> resolver.resolveViewName(viewName, locale))
.next() .next()
.otherwiseIfEmpty(handleUnresolvedViewName(viewName)) .otherwiseIfEmpty(handleUnresolvedViewName(viewName))
.then(view -> { .then(view -> view.render(result, null, exchange));
Flux<DataBuffer> body = view.render(result, null, exchange);
return exchange.getResponse().writeWith(body);
});
} }
else { else {
// Should not happen // Should not happen

9
spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerView.java

@ -30,6 +30,7 @@ import freemarker.template.SimpleHash;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.Version; import freemarker.template.Version;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
@ -156,7 +157,7 @@ public class FreeMarkerView extends AbstractUrlBasedView {
} }
@Override @Override
protected Flux<DataBuffer> renderInternal(Map<String, Object> renderAttributes, ServerWebExchange exchange) { protected Mono<Void> renderInternal(Map<String, Object> renderAttributes, ServerWebExchange exchange) {
// Expose all standard FreeMarker hash models. // Expose all standard FreeMarker hash models.
SimpleHash freeMarkerModel = getTemplateModel(renderAttributes, exchange); SimpleHash freeMarkerModel = getTemplateModel(renderAttributes, exchange);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
@ -170,12 +171,12 @@ public class FreeMarkerView extends AbstractUrlBasedView {
} }
catch (IOException ex) { catch (IOException ex) {
String message = "Could not load FreeMarker template for URL [" + getUrl() + "]"; String message = "Could not load FreeMarker template for URL [" + getUrl() + "]";
return Flux.error(new IllegalStateException(message, ex)); return Mono.error(new IllegalStateException(message, ex));
} }
catch (Throwable ex) { catch (Throwable ex) {
return Flux.error(ex); return Mono.error(ex);
} }
return Flux.just(dataBuffer); return exchange.getResponse().writeWith(Flux.just(dataBuffer));
} }
/** /**

4
spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/UrlBasedViewResolverTests.java

@ -63,8 +63,8 @@ public class UrlBasedViewResolverTests {
} }
@Override @Override
protected Flux<DataBuffer> renderInternal(Map<String, Object> attributes, ServerWebExchange exchange) { protected Mono<Void> renderInternal(Map<String, Object> attributes, ServerWebExchange exchange) {
return Flux.empty(); return Mono.empty();
} }
} }

6
spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java

@ -48,6 +48,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.MockServerHttpRequest; import org.springframework.http.server.reactive.MockServerHttpRequest;
import org.springframework.http.server.reactive.MockServerHttpResponse; import org.springframework.http.server.reactive.MockServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
@ -324,10 +325,11 @@ public class ViewResolutionResultHandlerTests {
} }
@Override @Override
public Flux<DataBuffer> render(HandlerResult result, MediaType mediaType, ServerWebExchange exchange) { public Mono<Void> render(HandlerResult result, MediaType mediaType, ServerWebExchange exchange) {
String value = this.name + ": " + result.getModel().toString(); String value = this.name + ": " + result.getModel().toString();
assertNotNull(value); assertNotNull(value);
return Flux.just(asDataBuffer(value)); ServerHttpResponse response = exchange.getResponse();
return response.writeWith(Flux.just(asDataBuffer(value)));
} }
} }

11
spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewTests.java

@ -19,14 +19,12 @@ import java.net.URI;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Locale; import java.util.Locale;
import java.util.Optional;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import reactor.core.publisher.Flux;
import reactor.core.test.TestSubscriber; import reactor.core.test.TestSubscriber;
import org.springframework.context.ApplicationContextException; import org.springframework.context.ApplicationContextException;
@ -46,7 +44,6 @@ import org.springframework.web.server.session.WebSessionManager;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/** /**
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
@ -60,6 +57,8 @@ public class FreeMarkerViewTests {
private ServerWebExchange exchange; private ServerWebExchange exchange;
private MockServerHttpResponse response;
private GenericApplicationContext context; private GenericApplicationContext context;
private Configuration freeMarkerConfig; private Configuration freeMarkerConfig;
@ -83,7 +82,7 @@ public class FreeMarkerViewTests {
fv.setApplicationContext(this.context); fv.setApplicationContext(this.context);
MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path")); MockServerHttpRequest request = new MockServerHttpRequest(HttpMethod.GET, new URI("/path"));
MockServerHttpResponse response = new MockServerHttpResponse(); this.response = new MockServerHttpResponse();
WebSessionManager manager = new DefaultWebSessionManager(); WebSessionManager manager = new DefaultWebSessionManager();
this.exchange = new DefaultServerWebExchange(request, response, manager); this.exchange = new DefaultServerWebExchange(request, response, manager);
} }
@ -127,10 +126,10 @@ public class FreeMarkerViewTests {
ModelMap model = new ExtendedModelMap(); ModelMap model = new ExtendedModelMap();
model.addAttribute("hello", "hi FreeMarker"); model.addAttribute("hello", "hi FreeMarker");
HandlerResult result = new HandlerResult(new Object(), "", ResolvableType.NONE, model); HandlerResult result = new HandlerResult(new Object(), "", ResolvableType.NONE, model);
Flux<DataBuffer> flux = view.render(result, null, this.exchange); view.render(result, null, this.exchange);
TestSubscriber<DataBuffer> subscriber = new TestSubscriber<>(); TestSubscriber<DataBuffer> subscriber = new TestSubscriber<>();
subscriber.bindTo(flux).assertValuesWith(dataBuffer -> subscriber.bindTo(this.response.getBody()).assertValuesWith(dataBuffer ->
assertEquals("<html><body>hi FreeMarker</body></html>", asString(dataBuffer))); assertEquals("<html><body>hi FreeMarker</body></html>", asString(dataBuffer)));
} }

Loading…
Cancel
Save