WebFlux support for LocaleContext related arguments

Issue: SPR-15998
This commit is contained in:
Rossen Stoyanchev
2017-09-27 21:45:03 -04:00
parent 0f1bd49f6d
commit 869b818e96
3 changed files with 58 additions and 12 deletions

View File

@@ -16,12 +16,19 @@
package org.springframework.web.reactive.result.method.annotation;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.TimeZoneAwareLocaleContext;
import org.springframework.core.MethodParameter;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.result.method.HandlerMethodArgumentResolverSupport;
import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver;
@@ -36,6 +43,9 @@ import org.springframework.web.util.UriComponentsBuilder;
* <li>{@link ServerHttpRequest}
* <li>{@link ServerHttpResponse}
* <li>{@link HttpMethod}
* <li>{@link Locale}
* <li>{@link TimeZone}
* <li>{@link ZoneId}
* <li>{@link UriBuilder} or {@link UriComponentsBuilder} -- for building URL's
* relative to the current request
* </ul>
@@ -63,6 +73,9 @@ public class ServerWebExchangeArgumentResolver extends HandlerMethodArgumentReso
ServerHttpRequest.class.isAssignableFrom(type) ||
ServerHttpResponse.class.isAssignableFrom(type) ||
HttpMethod.class == type ||
Locale.class == type ||
TimeZone.class == type ||
ZoneId.class == type ||
UriBuilder.class == type || UriComponentsBuilder.class == type);
}
@@ -83,6 +96,19 @@ public class ServerWebExchangeArgumentResolver extends HandlerMethodArgumentReso
else if (HttpMethod.class == paramType) {
return exchange.getRequest().getMethod();
}
else if (Locale.class == paramType) {
return exchange.getLocaleContext().getLocale();
}
else if (TimeZone.class == paramType) {
LocaleContext localeContext = exchange.getLocaleContext();
TimeZone timeZone = getTimeZone(localeContext);
return timeZone != null ? timeZone : TimeZone.getDefault();
}
else if (ZoneId.class == paramType) {
LocaleContext localeContext = exchange.getLocaleContext();
TimeZone timeZone = getTimeZone(localeContext);
return timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault();
}
else if (UriBuilder.class == paramType || UriComponentsBuilder.class == paramType) {
return UriComponentsBuilder.fromHttpRequest(exchange.getRequest());
}
@@ -93,4 +119,13 @@ public class ServerWebExchangeArgumentResolver extends HandlerMethodArgumentReso
}
}
@Nullable
private TimeZone getTimeZone(LocaleContext localeContext) {
TimeZone timeZone = null;
if (localeContext instanceof TimeZoneAwareLocaleContext) {
timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone();
}
return timeZone;
}
}