From 0f0da73043b11aa9cc81d68657e1ce3df293a441 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 26 Apr 2019 20:36:21 +0200 Subject: [PATCH] Polishing --- .../jndi/support/SimpleJndiBeanFactory.java | 23 +++-- .../core/ReactiveAdapterRegistry.java | 18 ++-- .../org/springframework/util/Base64Utils.java | 4 +- .../AsyncHandlerMethodReturnValueHandler.java | 12 +-- .../support/AbstractMessageChannel.java | 6 +- .../oxm/jaxb/Jaxb2Marshaller.java | 10 +-- .../oxm/support/AbstractMarshaller.java | 13 ++- .../DefaultRequestExpectationTests.java | 17 ++-- .../client/MockRestServiceServerTests.java | 4 +- .../transaction/annotation/Propagation.java | 6 +- .../http/codec/HttpMessageWriter.java | 15 ++-- .../Jaxb2RootElementHttpMessageConverter.java | 12 +-- .../xml/SourceHttpMessageConverter.java | 13 +-- .../AsyncHandlerMethodReturnValueHandler.java | 6 +- .../server/NotAcceptableStatusException.java | 6 +- .../result/HandlerResultHandlerSupport.java | 18 ++-- .../AbstractMessageWriterResultHandler.java | 26 +++--- .../annotation/ControllerMethodResolver.java | 88 +++++++++---------- .../ModelAttributeMethodArgumentResolver.java | 13 ++- .../reactive/result/view/AbstractView.java | 8 +- .../annotation/ReactiveTypeHandler.java | 20 +++-- ...ResponseBodyEmitterReturnValueHandler.java | 8 +- .../servlet/support/RequestContextUtils.java | 3 +- .../web/servlet/view/RedirectView.java | 16 ++-- .../servlet/SimpleWebApplicationContext.java | 9 +- 25 files changed, 180 insertions(+), 194 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java b/spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java index a6ed8173762..8bce8004532 100644 --- a/spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java +++ b/spring-context/src/main/java/org/springframework/jndi/support/SimpleJndiBeanFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -209,13 +209,12 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac @SuppressWarnings("unchecked") private T doGetSingleton(String name, @Nullable Class requiredType) throws NamingException { synchronized (this.singletonObjects) { - if (this.singletonObjects.containsKey(name)) { - Object jndiObject = this.singletonObjects.get(name); - if (requiredType != null && !requiredType.isInstance(jndiObject)) { - throw new TypeMismatchNamingException( - convertJndiName(name), requiredType, (jndiObject != null ? jndiObject.getClass() : null)); + Object singleton = this.singletonObjects.get(name); + if (singleton != null) { + if (requiredType != null && !requiredType.isInstance(singleton)) { + throw new TypeMismatchNamingException(convertJndiName(name), requiredType, singleton.getClass()); } - return (T) jndiObject; + return (T) singleton; } T jndiObject = lookup(name, requiredType); this.singletonObjects.put(name, jndiObject); @@ -229,14 +228,12 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac } else { synchronized (this.resourceTypes) { - if (this.resourceTypes.containsKey(name)) { - return this.resourceTypes.get(name); - } - else { - Class type = lookup(name, null).getClass(); + Class type = this.resourceTypes.get(name); + if (type == null) { + type = lookup(name, null).getClass(); this.resourceTypes.put(name, type); - return type; } + return type; } } } diff --git a/spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java b/spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java index f08725f0dcd..a5393654c73 100644 --- a/spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java +++ b/spring-core/src/main/java/org/springframework/core/ReactiveAdapterRegistry.java @@ -41,7 +41,7 @@ import org.springframework.util.ReflectionUtils; * *

By default, depending on classpath availability, adapters are registered * for Reactor, RxJava 1, RxJava 2 types, {@link CompletableFuture}, and Java 9+ - * Flow.Publisher. + * {@code Flow.Publisher}. * * @author Rossen Stoyanchev * @author Sebastien Deleuze @@ -54,7 +54,7 @@ public class ReactiveAdapterRegistry { private final boolean reactorPresent; - private final List adapters = new ArrayList<>(32); + private final List adapters = new ArrayList<>(); /** @@ -112,13 +112,13 @@ public class ReactiveAdapterRegistry { /** * Register a reactive type along with functions to adapt to and from a - * Reactive Streams {@link Publisher}. The functions can assume their - * input is never be {@code null} nor {@link Optional}. + * Reactive Streams {@link Publisher}. The function arguments assume that + * their input is neither {@code null} nor {@link Optional}. */ public void registerReactiveType(ReactiveTypeDescriptor descriptor, Function> toAdapter, Function, Object> fromAdapter) { - if (reactorPresent) { + if (this.reactorPresent) { this.adapters.add(new ReactorAdapter(descriptor, toAdapter, fromAdapter)); } else { @@ -128,6 +128,7 @@ public class ReactiveAdapterRegistry { /** * Get the adapter for the given reactive type. + * @return the corresponding adapter, or {@code null} if none available */ @Nullable public ReactiveAdapter getAdapter(Class reactiveType) { @@ -141,6 +142,7 @@ public class ReactiveAdapterRegistry { * (may be {@code null} if a concrete source object is given) * @param source an instance of the reactive type * (i.e. to adapt from; may be {@code null} if the reactive type is specified) + * @return the corresponding adapter, or {@code null} if none available */ @Nullable public ReactiveAdapter getAdapter(@Nullable Class reactiveType, @Nullable Object source) { @@ -162,13 +164,13 @@ public class ReactiveAdapterRegistry { /** - * Return a shared default {@code ReactiveAdapterRegistry} instance, lazily - * building it once needed. + * Return a shared default {@code ReactiveAdapterRegistry} instance, + * lazily building it once needed. *

NOTE: We highly recommend passing a long-lived, pre-configured * {@code ReactiveAdapterRegistry} instance for customization purposes. * This accessor is only meant as a fallback for code paths that want to * fall back on a default instance if one isn't provided. - * @return the shared {@code ReactiveAdapterRegistry} instance (never {@code null}) + * @return the shared {@code ReactiveAdapterRegistry} instance * @since 5.0.2 */ public static ReactiveAdapterRegistry getSharedInstance() { diff --git a/spring-core/src/main/java/org/springframework/util/Base64Utils.java b/spring-core/src/main/java/org/springframework/util/Base64Utils.java index d80b51e84f8..64121e2b36f 100644 --- a/spring-core/src/main/java/org/springframework/util/Base64Utils.java +++ b/spring-core/src/main/java/org/springframework/util/Base64Utils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -89,7 +89,7 @@ public abstract class Base64Utils { /** * Base64-encode the given byte array to a String. - * @param src the original byte array (may be {@code null}) + * @param src the original byte array * @return the encoded byte array as a UTF-8 String */ public static String encodeToString(byte[] src) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AsyncHandlerMethodReturnValueHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AsyncHandlerMethodReturnValueHandler.java index 506ec711ccc..f7627b25a0f 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AsyncHandlerMethodReturnValueHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AsyncHandlerMethodReturnValueHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2019 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. @@ -43,8 +43,8 @@ public interface AsyncHandlerMethodReturnValueHandler extends HandlerMethodRetur * {@link #supportsReturnType(org.springframework.core.MethodParameter)} * is called and it returns {@code true}. * @param returnValue the value returned from the handler method - * @param returnType the type of the return value. - * @return true if the return value type represents an async value. + * @param returnType the type of the return value + * @return {@code true} if the return value type represents an async value */ boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType); @@ -58,9 +58,9 @@ public interface AsyncHandlerMethodReturnValueHandler extends HandlerMethodRetur * {@link #supportsReturnType(org.springframework.core.MethodParameter)} * is called and it returns {@code true}. * @param returnValue the value returned from the handler method - * @param returnType the type of the return value. - * @return the resulting ListenableFuture or {@code null} in which case no - * further handling will be performed. + * @param returnType the type of the return value + * @return the resulting ListenableFuture, or {@code null} in which case + * no further handling will be performed */ @Nullable ListenableFuture toListenableFuture(Object returnValue, MethodParameter returnType); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java b/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java index ae74d2532f8..66333fc2b5b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -42,10 +42,10 @@ public abstract class AbstractMessageChannel implements MessageChannel, Intercep protected final Log logger = LogFactory.getLog(getClass()); - private final List interceptors = new ArrayList<>(5); - private String beanName; + private final List interceptors = new ArrayList<>(5); + public AbstractMessageChannel() { this.beanName = getClass().getSimpleName() + "@" + ObjectUtils.getIdentityHexString(this); diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index 53d26232b32..cb6805cd2de 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -415,7 +415,7 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi } /** - * Indicates whether DTD parsing should be supported. + * Indicate whether DTD parsing should be supported. *

Default is {@code false} meaning that DTD is disabled. */ public void setSupportDtd(boolean supportDtd) { @@ -423,14 +423,14 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi } /** - * Whether DTD parsing is supported. + * Return whether DTD parsing is supported. */ public boolean isSupportDtd() { return this.supportDtd; } /** - * Indicates whether external XML entities are processed when unmarshalling. + * Indicate whether external XML entities are processed when unmarshalling. *

Default is {@code false}, meaning that external entities are not resolved. * Note that processing of external entities will only be enabled/disabled when the * {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or @@ -442,12 +442,12 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { - setSupportDtd(true); + this.supportDtd = true; } } /** - * Returns the configured value for whether XML external entities are allowed. + * Return whether XML external entities are allowed. */ public boolean isProcessExternalEntities() { return this.processExternalEntities; diff --git a/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java index e8eac96d822..3fae42f0441 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/support/AbstractMarshaller.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -22,7 +22,6 @@ import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; import java.io.Writer; - import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -87,7 +86,7 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { /** - * Indicates whether DTD parsing should be supported. + * Indicate whether DTD parsing should be supported. *

Default is {@code false} meaning that DTD is disabled. */ public void setSupportDtd(boolean supportDtd) { @@ -95,14 +94,14 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { } /** - * Whether DTD parsing is supported. + * Return whether DTD parsing is supported. */ public boolean isSupportDtd() { return this.supportDtd; } /** - * Indicates whether external XML entities are processed when unmarshalling. + * Indicate whether external XML entities are processed when unmarshalling. *

Default is {@code false}, meaning that external entities are not resolved. * Note that processing of external entities will only be enabled/disabled when the * {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or @@ -114,12 +113,12 @@ public abstract class AbstractMarshaller implements Marshaller, Unmarshaller { public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { - setSupportDtd(true); + this.supportDtd = true; } } /** - * Returns the configured value for whether XML external entities are allowed. + * Return whether XML external entities are allowed. * @see #createXmlReader() */ public boolean isProcessExternalEntities() { diff --git a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java index 6e9f729c418..002f31d4f5b 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/DefaultRequestExpectationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -26,15 +26,11 @@ import org.junit.rules.ExpectedException; import org.springframework.http.HttpMethod; import org.springframework.http.client.ClientHttpRequest; -import static junit.framework.TestCase.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.springframework.http.HttpMethod.GET; -import static org.springframework.http.HttpMethod.POST; -import static org.springframework.test.web.client.ExpectedCount.once; -import static org.springframework.test.web.client.ExpectedCount.twice; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; -import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; +import static org.junit.Assert.*; +import static org.springframework.http.HttpMethod.*; +import static org.springframework.test.web.client.ExpectedCount.*; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; +import static org.springframework.test.web.client.response.MockRestResponseCreators.*; /** * Unit tests for {@link DefaultRequestExpectation}. @@ -86,7 +82,6 @@ public class DefaultRequestExpectationTests { } - @SuppressWarnings("deprecation") private ClientHttpRequest createRequest(HttpMethod method, String url) { try { diff --git a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java index 45462658d5d..cae4a088bd4 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/MockRestServiceServerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -23,6 +23,7 @@ import org.junit.Test; import org.springframework.test.web.client.MockRestServiceServer.MockRestServiceServerBuilder; import org.springframework.web.client.RestTemplate; +import static org.junit.Assert.*; import static org.springframework.http.HttpMethod.*; import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; import static org.springframework.test.web.client.response.MockRestResponseCreators.*; @@ -124,6 +125,7 @@ public class MockRestServiceServerTests { try { this.restTemplate.getForEntity("/some-service/some-endpoint", String.class); + fail("Expected exception"); } catch (Exception ex) { this.restTemplate.postForEntity("/reporting-service/report-error", ex.toString(), String.class); diff --git a/spring-tx/src/main/java/org/springframework/transaction/annotation/Propagation.java b/spring-tx/src/main/java/org/springframework/transaction/annotation/Propagation.java index fc6b8f020c3..95cc22fc9e2 100644 --- a/spring-tx/src/main/java/org/springframework/transaction/annotation/Propagation.java +++ b/spring-tx/src/main/java/org/springframework/transaction/annotation/Propagation.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -40,7 +40,7 @@ public enum Propagation { * Support a current transaction, execute non-transactionally if none exists. * Analogous to EJB transaction attribute of the same name. *

Note: For transaction managers with transaction synchronization, - * PROPAGATION_SUPPORTS is slightly different from no transaction at all, + * {@code SUPPORTS} is slightly different from no transaction at all, * as it defines a transaction scope that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on @@ -87,7 +87,7 @@ public enum Propagation { /** * Execute within a nested transaction if a current transaction exists, - * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB. + * behave like {@code REQUIRED} else. There is no analogous feature in EJB. *

Note: Actual creation of a nested transaction will only work on specific * transaction managers. Out of the box, this only applies to the JDBC * DataSourceTransactionManager when working on a JDBC 3.0 driver. diff --git a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java index c0cd0aed091..0b1b94b714d 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/HttpMessageWriter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -34,12 +34,11 @@ import org.springframework.lang.Nullable; * Strategy for encoding a stream of objects of type {@code } and writing * the encoded stream of bytes to an {@link ReactiveHttpOutputMessage}. * - * @param the type of objects in the input stream - * * @author Rossen Stoyanchev * @author Arjen Poutsma * @author Sebastien Deleuze * @since 5.0 + * @param the type of objects in the input stream */ public interface HttpMessageWriter { @@ -51,7 +50,7 @@ public interface HttpMessageWriter { /** * Whether the given object type is supported by this writer. * @param elementType the type of object to check - * @param mediaType the media type for the write, possibly {@code null} + * @param mediaType the media type for the write (possibly {@code null}) * @return {@code true} if writable, {@code false} otherwise */ boolean canWrite(ResolvableType elementType, @Nullable MediaType mediaType); @@ -61,8 +60,8 @@ public interface HttpMessageWriter { * @param inputStream the objects to write * @param elementType the type of objects in the stream which must have been * previously checked via {@link #canWrite(ResolvableType, MediaType)} - * @param mediaType the content type for the write, possibly {@code null} to - * indicate that the default content type of the writer must be used. + * @param mediaType the content type for the write (possibly {@code null} to + * indicate that the default content type of the writer must be used) * @param message the message to write to * @param hints additional information about how to encode and write * @return indicates completion or error @@ -78,8 +77,8 @@ public interface HttpMessageWriter { * value; for annotated controllers, the {@link MethodParameter} can be * accessed via {@link ResolvableType#getSource()}. * @param elementType the type of Objects in the input stream - * @param mediaType the content type to use, possibly {@code null} indicating - * the default content type of the writer should be used. + * @param mediaType the content type to use (possibly {@code null} indicating + * the default content type of the writer should be used) * @param request the current request * @param response the current response * @return a {@link Mono} that indicates completion of writing or error diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java index a45dbfe50cf..dff859c2a51 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -71,7 +71,7 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa /** - * Indicates whether DTD parsing should be supported. + * Indicate whether DTD parsing should be supported. *

Default is {@code false} meaning that DTD is disabled. */ public void setSupportDtd(boolean supportDtd) { @@ -79,14 +79,14 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa } /** - * Whether DTD parsing is supported. + * Return whether DTD parsing is supported. */ public boolean isSupportDtd() { return this.supportDtd; } /** - * Indicates whether external XML entities are processed when converting to a Source. + * Indicate whether external XML entities are processed when converting to a Source. *

Default is {@code false}, meaning that external entities are not resolved. *

Note: setting this option to {@code true} also * automatically sets {@link #setSupportDtd} to {@code true}. @@ -94,12 +94,12 @@ public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessa public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { - setSupportDtd(true); + this.supportDtd = true; } } /** - * Returns the configured value for whether XML external entities are allowed. + * Return whether XML external entities are allowed. */ public boolean isProcessExternalEntities() { return this.processExternalEntities; diff --git a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java index 6eae9c457b8..957e8d03b43 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -62,6 +62,7 @@ import org.springframework.util.StreamUtils; * @author Arjen Poutsma * @author Rossen Stoyanchev * @since 3.0 + * @param the converted object type */ public class SourceHttpMessageConverter extends AbstractHttpMessageConverter { @@ -99,7 +100,7 @@ public class SourceHttpMessageConverter extends AbstractHttpMe /** - * Indicates whether DTD parsing should be supported. + * Indicate whether DTD parsing should be supported. *

Default is {@code false} meaning that DTD is disabled. */ public void setSupportDtd(boolean supportDtd) { @@ -107,14 +108,14 @@ public class SourceHttpMessageConverter extends AbstractHttpMe } /** - * Whether DTD parsing is supported. + * Return whether DTD parsing is supported. */ public boolean isSupportDtd() { return this.supportDtd; } /** - * Indicates whether external XML entities are processed when converting to a Source. + * Indicate whether external XML entities are processed when converting to a Source. *

Default is {@code false}, meaning that external entities are not resolved. *

Note: setting this option to {@code true} also * automatically sets {@link #setSupportDtd} to {@code true}. @@ -122,12 +123,12 @@ public class SourceHttpMessageConverter extends AbstractHttpMe public void setProcessExternalEntities(boolean processExternalEntities) { this.processExternalEntities = processExternalEntities; if (processExternalEntities) { - setSupportDtd(true); + this.supportDtd = true; } } /** - * Returns the configured value for whether XML external entities are allowed. + * Return whether XML external entities are allowed. */ public boolean isProcessExternalEntities() { return this.processExternalEntities; diff --git a/spring-web/src/main/java/org/springframework/web/method/support/AsyncHandlerMethodReturnValueHandler.java b/spring-web/src/main/java/org/springframework/web/method/support/AsyncHandlerMethodReturnValueHandler.java index 9e6fb610407..c43de97316c 100644 --- a/spring-web/src/main/java/org/springframework/web/method/support/AsyncHandlerMethodReturnValueHandler.java +++ b/spring-web/src/main/java/org/springframework/web/method/support/AsyncHandlerMethodReturnValueHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -38,9 +38,9 @@ public interface AsyncHandlerMethodReturnValueHandler extends HandlerMethodRetur /** * Whether the given return value represents asynchronous computation. - * @param returnValue the return value + * @param returnValue the value returned from the handler method * @param returnType the return type - * @return {@code true} if the return value is asynchronous + * @return {@code true} if the return value type represents an async value */ boolean isAsyncReturnValue(@Nullable Object returnValue, MethodParameter returnType); diff --git a/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java b/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java index c9bbb1f841a..a93651f9722 100644 --- a/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java +++ b/spring-web/src/main/java/org/springframework/web/server/NotAcceptableStatusException.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -43,10 +43,10 @@ public class NotAcceptableStatusException extends ResponseStatusException { } /** - * Constructor for when requested Content-Type is not supported. + * Constructor for when the requested Content-Type is not supported. */ public NotAcceptableStatusException(List supportedMediaTypes) { - super(HttpStatus.NOT_ACCEPTABLE, "Could not find acceptable representation", null); + super(HttpStatus.NOT_ACCEPTABLE, "Could not find acceptable representation"); this.supportedMediaTypes = Collections.unmodifiableList(supportedMediaTypes); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java index d5c54e979a8..ed101ababe3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/HandlerResultHandlerSupport.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -96,7 +96,7 @@ public abstract class HandlerResultHandlerSupport implements Ordered { /** * Get a {@code ReactiveAdapter} for the top-level return value type. - * @return the matching adapter or {@code null} + * @return the matching adapter, or {@code null} if none */ @Nullable protected ReactiveAdapter getAdapter(HandlerResult result) { @@ -105,15 +105,14 @@ public abstract class HandlerResultHandlerSupport implements Ordered { } /** - * Select the best media type for the current request through a content - * negotiation algorithm. + * Select the best media type for the current request through a content negotiation algorithm. * @param exchange the current request * @param producibleTypesSupplier the media types that can be produced for the current request - * @return the selected media type or {@code null} + * @return the selected media type, or {@code null} if none */ @Nullable - protected MediaType selectMediaType(ServerWebExchange exchange, - Supplier> producibleTypesSupplier) { + protected MediaType selectMediaType( + ServerWebExchange exchange, Supplier> producibleTypesSupplier) { MediaType contentType = exchange.getResponse().getHeaders().getContentType(); if (contentType != null && contentType.isConcrete()) { @@ -151,9 +150,8 @@ public abstract class HandlerResultHandlerSupport implements Ordered { return getContentTypeResolver().resolveMediaTypes(exchange); } - @SuppressWarnings("unchecked") - private List getProducibleTypes(ServerWebExchange exchange, - Supplier> producibleTypesSupplier) { + private List getProducibleTypes( + ServerWebExchange exchange, Supplier> producibleTypesSupplier) { Set mediaTypes = exchange.getAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); return (mediaTypes != null ? new ArrayList<>(mediaTypes) : producibleTypesSupplier.get()); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java index c51bcf39289..9bdd3172b8d 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/AbstractMessageWriterResultHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -29,8 +29,6 @@ import org.springframework.core.ReactiveAdapterRegistry; import org.springframework.core.ResolvableType; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageWriter; -import org.springframework.http.server.reactive.ServerHttpRequest; -import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.web.reactive.accept.RequestedContentTypeResolver; @@ -110,7 +108,7 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa * @return indicates completion or error * @since 5.0.2 */ - @SuppressWarnings({ "unchecked", "rawtypes" }) + @SuppressWarnings({"unchecked", "rawtypes"}) protected Mono writeBody(@Nullable Object body, MethodParameter bodyParameter, @Nullable MethodParameter actualParam, ServerWebExchange exchange) { @@ -128,32 +126,30 @@ public abstract class AbstractMessageWriterResultHandler extends HandlerResultHa } else { publisher = Mono.justOrEmpty(body); - elementType = ((bodyClass == null || bodyClass.equals(Object.class)) && body != null ? + elementType = ((bodyClass == null || bodyClass == Object.class) && body != null ? ResolvableType.forInstance(body) : bodyType); } - if (void.class == elementType.getRawClass() || Void.class == elementType.getRawClass()) { + if (elementType.getRawClass() == void.class || elementType.getRawClass() == Void.class) { return Mono.from((Publisher) publisher); } - ServerHttpRequest request = exchange.getRequest(); - ServerHttpResponse response = exchange.getResponse(); MediaType bestMediaType = selectMediaType(exchange, () -> getMediaTypesFor(elementType)); if (bestMediaType != null) { for (HttpMessageWriter writer : getMessageWriters()) { if (writer.canWrite(elementType, bestMediaType)) { return writer.write((Publisher) publisher, actualType, elementType, - bestMediaType, request, response, Collections.emptyMap()); + bestMediaType, exchange.getRequest(), exchange.getResponse(), + Collections.emptyMap()); } } } - else { - if (getMediaTypesFor(elementType).isEmpty()) { - return Mono.error(new IllegalStateException("No writer for : " + elementType)); - } - } - return Mono.error(new NotAcceptableStatusException(getMediaTypesFor(elementType))); + List mediaTypes = getMediaTypesFor(elementType); + if (bestMediaType == null && mediaTypes.isEmpty()) { + return Mono.error(new IllegalStateException("No HttpMessageWriter for " + elementType)); + } + return Mono.error(new NotAcceptableStatusException(mediaTypes)); } private ResolvableType getElementType(ReactiveAdapter adapter, ResolvableType genericType) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java index 1662e940158..5a69a89cc17 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ControllerMethodResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -54,8 +54,9 @@ import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod /** * Package-private class to assist {@link RequestMappingHandlerAdapter} with * resolving, initializing, and caching annotated methods declared in - * {@code @Controller} and {@code @ControllerAdvice} components. Assists with - * the following annotations: + * {@code @Controller} and {@code @ControllerAdvice} components. + * + *

Assists with the following annotations: *

    *
  • {@code @InitBinder} *
  • {@code @ModelAttribute} @@ -112,56 +113,56 @@ class ControllerMethodResolver { private final Map, SessionAttributesHandler> sessionAttributesHandlerCache = new ConcurrentHashMap<>(64); - ControllerMethodResolver(ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry, + ControllerMethodResolver(ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context, List> readers) { Assert.notNull(customResolvers, "ArgumentResolverConfigurer is required"); - Assert.notNull(readers, "'messageReaders' is required"); - Assert.notNull(reactiveRegistry, "ReactiveAdapterRegistry is required"); + Assert.notNull(adapterRegistry, "ReactiveAdapterRegistry is required"); Assert.notNull(context, "ApplicationContext is required"); + Assert.notNull(readers, "HttpMessageReader List is required"); - this.initBinderResolvers = initBinderResolvers(customResolvers, reactiveRegistry, context); - this.modelAttributeResolvers = modelMethodResolvers(customResolvers, reactiveRegistry, context); - this.requestMappingResolvers = requestMappingResolvers(customResolvers, reactiveRegistry, context, readers); - this.exceptionHandlerResolvers = exceptionHandlerResolvers(customResolvers, reactiveRegistry, context); - this.reactiveAdapterRegistry = reactiveRegistry; + this.initBinderResolvers = initBinderResolvers(customResolvers, adapterRegistry, context); + this.modelAttributeResolvers = modelMethodResolvers(customResolvers, adapterRegistry, context); + this.requestMappingResolvers = requestMappingResolvers(customResolvers, adapterRegistry, context, readers); + this.exceptionHandlerResolvers = exceptionHandlerResolvers(customResolvers, adapterRegistry, context); + this.reactiveAdapterRegistry = adapterRegistry; initControllerAdviceCaches(context); } private List initBinderResolvers( - ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry, + ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context) { - return initResolvers(customResolvers, reactiveRegistry, context, false, Collections.emptyList()).stream() + return initResolvers(customResolvers, adapterRegistry, context, false, Collections.emptyList()).stream() .filter(resolver -> resolver instanceof SyncHandlerMethodArgumentResolver) .map(resolver -> (SyncHandlerMethodArgumentResolver) resolver) .collect(Collectors.toList()); } private static List modelMethodResolvers( - ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry, + ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context) { - return initResolvers(customResolvers, reactiveRegistry, context, true, Collections.emptyList()); + return initResolvers(customResolvers, adapterRegistry, context, true, Collections.emptyList()); } private static List requestMappingResolvers( - ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry, + ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context, List> readers) { - return initResolvers(customResolvers, reactiveRegistry, context, true, readers); + return initResolvers(customResolvers, adapterRegistry, context, true, readers); } private static List exceptionHandlerResolvers( - ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry reactiveRegistry, + ArgumentResolverConfigurer customResolvers, ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context) { - return initResolvers(customResolvers, reactiveRegistry, context, false, Collections.emptyList()); + return initResolvers(customResolvers, adapterRegistry, context, false, Collections.emptyList()); } private static List initResolvers(ArgumentResolverConfigurer customResolvers, - ReactiveAdapterRegistry reactiveRegistry, ConfigurableApplicationContext context, + ReactiveAdapterRegistry adapterRegistry, ConfigurableApplicationContext context, boolean supportDataBinding, List> readers) { ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); @@ -169,48 +170,48 @@ class ControllerMethodResolver { // Annotation-based... List result = new ArrayList<>(); - result.add(new RequestParamMethodArgumentResolver(beanFactory, reactiveRegistry, false)); - result.add(new RequestParamMapMethodArgumentResolver(reactiveRegistry)); - result.add(new PathVariableMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new PathVariableMapMethodArgumentResolver(reactiveRegistry)); - result.add(new MatrixVariableMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new MatrixVariableMapMethodArgumentResolver(reactiveRegistry)); + result.add(new RequestParamMethodArgumentResolver(beanFactory, adapterRegistry, false)); + result.add(new RequestParamMapMethodArgumentResolver(adapterRegistry)); + result.add(new PathVariableMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new PathVariableMapMethodArgumentResolver(adapterRegistry)); + result.add(new MatrixVariableMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new MatrixVariableMapMethodArgumentResolver(adapterRegistry)); if (!readers.isEmpty()) { - result.add(new RequestBodyArgumentResolver(readers, reactiveRegistry)); - result.add(new RequestPartMethodArgumentResolver(readers, reactiveRegistry)); + result.add(new RequestBodyArgumentResolver(readers, adapterRegistry)); + result.add(new RequestPartMethodArgumentResolver(readers, adapterRegistry)); } if (supportDataBinding) { - result.add(new ModelAttributeMethodArgumentResolver(reactiveRegistry, false)); + result.add(new ModelAttributeMethodArgumentResolver(adapterRegistry, false)); } - result.add(new RequestHeaderMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new RequestHeaderMapMethodArgumentResolver(reactiveRegistry)); - result.add(new CookieValueMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new ExpressionValueMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new SessionAttributeMethodArgumentResolver(beanFactory, reactiveRegistry)); - result.add(new RequestAttributeMethodArgumentResolver(beanFactory, reactiveRegistry)); + result.add(new RequestHeaderMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new RequestHeaderMapMethodArgumentResolver(adapterRegistry)); + result.add(new CookieValueMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new ExpressionValueMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new SessionAttributeMethodArgumentResolver(beanFactory, adapterRegistry)); + result.add(new RequestAttributeMethodArgumentResolver(beanFactory, adapterRegistry)); // Type-based... if (!readers.isEmpty()) { - result.add(new HttpEntityArgumentResolver(readers, reactiveRegistry)); + result.add(new HttpEntityArgumentResolver(readers, adapterRegistry)); } - result.add(new ModelArgumentResolver(reactiveRegistry)); + result.add(new ModelArgumentResolver(adapterRegistry)); if (supportDataBinding) { - result.add(new ErrorsMethodArgumentResolver(reactiveRegistry)); + result.add(new ErrorsMethodArgumentResolver(adapterRegistry)); } - result.add(new ServerWebExchangeArgumentResolver(reactiveRegistry)); - result.add(new PrincipalArgumentResolver(reactiveRegistry)); + result.add(new ServerWebExchangeArgumentResolver(adapterRegistry)); + result.add(new PrincipalArgumentResolver(adapterRegistry)); if (requestMappingMethod) { result.add(new SessionStatusMethodArgumentResolver()); } - result.add(new WebSessionArgumentResolver(reactiveRegistry)); + result.add(new WebSessionArgumentResolver(adapterRegistry)); // Custom... result.addAll(customResolvers.getCustomResolvers()); // Catch-all... - result.add(new RequestParamMethodArgumentResolver(beanFactory, reactiveRegistry, true)); + result.add(new RequestParamMethodArgumentResolver(beanFactory, adapterRegistry, true)); if (supportDataBinding) { - result.add(new ModelAttributeMethodArgumentResolver(reactiveRegistry, true)); + result.add(new ModelAttributeMethodArgumentResolver(adapterRegistry, true)); } return result; @@ -336,7 +337,6 @@ class ControllerMethodResolver { */ @Nullable public InvocableHandlerMethod getExceptionHandlerMethod(Throwable ex, HandlerMethod handlerMethod) { - Class handlerType = handlerMethod.getBeanType(); // Controller-local first... diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java index 04ed388dd75..6315de7a6a6 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ModelAttributeMethodArgumentResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -135,8 +135,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR BindingResult errors = binder.getBindingResult(); if (adapter != null) { return adapter.fromPublisher(errors.hasErrors() ? - Mono.error(new WebExchangeBindException(parameter, errors)) : - valueMono); + Mono.error(new WebExchangeBindException(parameter, errors)) : valueMono); } else { if (errors.hasErrors() && !hasErrorsArgument(parameter)) { @@ -163,10 +162,10 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR return createAttribute(attributeName, attributeClass, context, exchange); } - ReactiveAdapter adapterFrom = getAdapterRegistry().getAdapter(null, attribute); - if (adapterFrom != null) { - Assert.isTrue(!adapterFrom.isMultiValue(), "Data binding only supports single-value async types"); - return Mono.from(adapterFrom.toPublisher(attribute)); + ReactiveAdapter adapter = getAdapterRegistry().getAdapter(null, attribute); + if (adapter != null) { + Assert.isTrue(!adapter.isMultiValue(), "Data binding only supports single-value async types"); + return Mono.from(adapter.toPublisher(attribute)); } else { return Mono.justOrEmpty(attribute); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java index 6abf174373e..20a54b685ea 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/AbstractView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -56,7 +56,7 @@ public abstract class AbstractView implements View, ApplicationContextAware { private static final Object NO_VALUE = new Object(); - private final ReactiveAdapterRegistry reactiveAdapterRegistry; + private final ReactiveAdapterRegistry adapterRegistry; private final List mediaTypes = new ArrayList<>(4); @@ -74,7 +74,7 @@ public abstract class AbstractView implements View, ApplicationContextAware { } public AbstractView(ReactiveAdapterRegistry reactiveAdapterRegistry) { - this.reactiveAdapterRegistry = reactiveAdapterRegistry; + this.adapterRegistry = reactiveAdapterRegistry; this.mediaTypes.add(ViewResolverSupport.DEFAULT_CONTENT_TYPE); } @@ -217,7 +217,7 @@ public abstract class AbstractView implements View, ApplicationContextAware { if (value == null) { continue; } - ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(null, value); + ReactiveAdapter adapter = this.adapterRegistry.getAdapter(null, value); if (adapter != null) { names.add(entry.getKey()); if (adapter.isMultiValue()) { diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java index 0947d964add..5bd7ab14af6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -76,14 +76,14 @@ class ReactiveTypeHandler { private static Log logger = LogFactory.getLog(ReactiveTypeHandler.class); - private final ReactiveAdapterRegistry reactiveRegistry; + private final ReactiveAdapterRegistry adapterRegistry; private final TaskExecutor taskExecutor; - private Boolean taskExecutorWarning; - private final ContentNegotiationManager contentNegotiationManager; + private boolean taskExecutorWarning; + public ReactiveTypeHandler() { this(ReactiveAdapterRegistry.getSharedInstance(), new SyncTaskExecutor(), new ContentNegotiationManager()); @@ -93,10 +93,12 @@ class ReactiveTypeHandler { Assert.notNull(registry, "ReactiveAdapterRegistry is required"); Assert.notNull(executor, "TaskExecutor is required"); Assert.notNull(manager, "ContentNegotiationManager is required"); - this.reactiveRegistry = registry; + this.adapterRegistry = registry; this.taskExecutor = executor; - this.taskExecutorWarning = executor instanceof SimpleAsyncTaskExecutor || executor instanceof SyncTaskExecutor; this.contentNegotiationManager = manager; + + this.taskExecutorWarning = + (executor instanceof SimpleAsyncTaskExecutor || executor instanceof SyncTaskExecutor); } @@ -104,7 +106,7 @@ class ReactiveTypeHandler { * Whether the type can be adapted to a Reactive Streams {@link Publisher}. */ public boolean isReactiveType(Class type) { - return (this.reactiveRegistry.hasAdapters() && this.reactiveRegistry.getAdapter(type) != null); + return (this.adapterRegistry.hasAdapters() && this.adapterRegistry.getAdapter(type) != null); } @@ -119,7 +121,7 @@ class ReactiveTypeHandler { ModelAndViewContainer mav, NativeWebRequest request) throws Exception { Assert.notNull(returnValue, "Expected return value"); - ReactiveAdapter adapter = this.reactiveRegistry.getAdapter(returnValue.getClass()); + ReactiveAdapter adapter = this.adapterRegistry.getAdapter(returnValue.getClass()); Assert.state(adapter != null, () -> "Unexpected return value: " + returnValue); ResolvableType elementType = ResolvableType.forMethodParameter(returnType).getGeneric(); @@ -317,7 +319,7 @@ class ReactiveTypeHandler { return; } } - + if (isTerminated) { this.done = true; Throwable ex = this.error; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java index 9aca44a3073..df642bcf666 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyEmitterReturnValueHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -83,17 +83,17 @@ public class ResponseBodyEmitterReturnValueHandler implements HandlerMethodRetur /** * Complete constructor with pluggable "reactive" type support. * @param messageConverters converters to write emitted objects with - * @param reactiveRegistry for reactive return value type support + * @param registry for reactive return value type support * @param executor for blocking I/O writes of items emitted from reactive types * @param manager for detecting streaming media types * @since 5.0 */ public ResponseBodyEmitterReturnValueHandler(List> messageConverters, - ReactiveAdapterRegistry reactiveRegistry, TaskExecutor executor, ContentNegotiationManager manager) { + ReactiveAdapterRegistry registry, TaskExecutor executor, ContentNegotiationManager manager) { Assert.notEmpty(messageConverters, "HttpMessageConverter List must not be empty"); this.messageConverters = messageConverters; - this.reactiveHandler = new ReactiveTypeHandler(reactiveRegistry, executor, manager); + this.reactiveHandler = new ReactiveTypeHandler(registry, executor, manager); } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java index 10d009fdab3..727f1c9a21a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/RequestContextUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -19,7 +19,6 @@ package org.springframework.web.servlet.support; import java.util.Locale; import java.util.Map; import java.util.TimeZone; - import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java index 4488822d50a..c6ee5ec0162 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/RedirectView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2019 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. @@ -346,7 +346,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { targetUrl = replaceUriTemplateVariables(targetUrl.toString(), model, variables, enc); } if (isPropagateQueryProperties()) { - appendCurrentQueryParams(targetUrl, request); + appendCurrentQueryParams(targetUrl, request); } if (this.exposeModelAttributes) { appendQueryProperties(targetUrl, model, enc); @@ -368,7 +368,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL - * @param model Map that contains model attributes + * @param model a Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed @@ -435,7 +435,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { * Append query properties to the redirect URL. * Stringifies, URL-encodes and formats model attributes as query properties. * @param targetUrl the StringBuilder to append the properties to - * @param model Map that contains model attributes + * @param model a Map that contains model attributes * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed * @see #queryProperties @@ -554,8 +554,9 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { /** * Determine whether the given model element value is eligible for exposure. - *

    The default implementation considers primitives, Strings, Numbers, Dates, - * URIs, URLs and Locale objects as eligible. This can be overridden in subclasses. + *

    The default implementation considers primitives, strings, numbers, dates, + * URIs, URLs etc as eligible, according to {@link BeanUtils#isSimpleValueType}. + * This can be overridden in subclasses. * @param value the model element value * @return whether the element value is eligible * @see BeanUtils#isSimpleValueType @@ -572,7 +573,6 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { * @return the encoded output String * @throws UnsupportedEncodingException if thrown by the JDK URLEncoder * @see java.net.URLEncoder#encode(String, String) - * @see java.net.URLEncoder#encode(String) */ protected String urlEncode(String input, String encodingScheme) throws UnsupportedEncodingException { return URLEncoder.encode(input, encodingScheme); @@ -602,7 +602,7 @@ public class RedirectView extends AbstractUrlBasedView implements SmartView { } /** - * Send a redirect back to the HTTP client + * Send a redirect back to the HTTP client. * @param request current HTTP request (allows for reacting to request method) * @param response current HTTP response (for sending response headers) * @param targetUrl the target URL to redirect to diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java index f5d7f3f7515..20344b47c03 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/SimpleWebApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2019 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. @@ -47,10 +47,6 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext { @Override public void refresh() throws BeansException { - MutablePropertyValues pvs = new MutablePropertyValues(); - pvs.add("commandClass", "org.springframework.tests.sample.beans.TestBean"); - pvs.add("formView", "form"); - registerSingleton("/locale.do", LocaleChecker.class); addMessage("test", Locale.ENGLISH, "test message"); @@ -63,7 +59,7 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext { registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class); registerSingleton("viewResolver", InternalResourceViewResolver.class); - pvs = new MutablePropertyValues(); + MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("location", "org/springframework/web/context/WEB-INF/sessionContext.xml"); registerSingleton("viewResolver2", XmlViewResolver.class, pvs); @@ -76,6 +72,7 @@ public class SimpleWebApplicationContext extends StaticWebApplicationContext { @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if (!(RequestContextUtils.findWebApplicationContext(request) instanceof SimpleWebApplicationContext)) { throw new ServletException("Incorrect WebApplicationContext"); }