Browse Source

Apply "instanceof pattern matching" in spring-webflux

This commit also applies additional clean-up tasks such as the following.

- final fields
- diamond operator (<>) for anonymous inner classes
- multi-catch

This has only been applied to `src/main/java`.
pull/27567/head
Sam Brannen 4 years ago
parent
commit
8135ae6d38
  1. 8
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java
  2. 8
      spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java
  3. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java
  4. 2
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java
  5. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java
  6. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java
  7. 6
      spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java
  8. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java
  9. 21
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java
  10. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java
  11. 13
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java
  12. 8
      spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java
  13. 13
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java
  14. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java
  15. 3
      spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java
  16. 16
      spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java
  17. 4
      spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java
  18. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java
  19. 5
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java
  20. 17
      spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java

8
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -66,7 +66,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -66,7 +66,7 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
};
private ExchangeStrategies strategies;
private final ExchangeStrategies strategies;
private int statusCode = 200;
@ -104,8 +104,8 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder { @@ -104,8 +104,8 @@ final class DefaultClientResponseBuilder implements ClientResponse.Builder {
this.headers.addAll(other.headers().asHttpHeaders());
}
this.originalResponse = other;
this.request = (other instanceof DefaultClientResponse ?
((DefaultClientResponse) other).request() : EMPTY_REQUEST);
this.request = (other instanceof DefaultClientResponse defaultClientResponse ?
defaultClientResponse.request() : EMPTY_REQUEST);
}

8
spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ExchangeFilterFunctions.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -116,8 +116,7 @@ public abstract class ExchangeFilterFunctions { @@ -116,8 +116,7 @@ public abstract class ExchangeFilterFunctions {
public static ExchangeFilterFunction basicAuthentication() {
return (request, next) -> {
Object attr = request.attributes().get(BASIC_AUTHENTICATION_CREDENTIALS_ATTRIBUTE);
if (attr instanceof Credentials) {
Credentials cred = (Credentials) attr;
if (attr instanceof Credentials cred) {
return next.exchange(ClientRequest.from(request)
.headers(headers -> headers.setBasicAuth(cred.username, cred.password))
.build());
@ -174,10 +173,9 @@ public abstract class ExchangeFilterFunctions { @@ -174,10 +173,9 @@ public abstract class ExchangeFilterFunctions {
if (this == other) {
return true;
}
if (!(other instanceof Credentials)) {
if (!(other instanceof Credentials otherCred)) {
return false;
}
Credentials otherCred = (Credentials) other;
return (this.username.equals(otherCred.username) && this.password.equals(otherCred.password));
}

5
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/ChangePathPatternParserVisitor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -70,8 +70,7 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor { @@ -70,8 +70,7 @@ class ChangePathPatternParserVisitor implements RouterFunctions.Visitor {
}
private void changeParser(RequestPredicate predicate) {
if (predicate instanceof Target) {
Target target = (Target) predicate;
if (predicate instanceof Target target) {
target.changeParser(this.parser);
}
}

2
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

@ -195,7 +195,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> { @@ -195,7 +195,7 @@ class DefaultEntityResponseBuilder<T> implements EntityResponse.Builder<T> {
@Override
public Mono<EntityResponse<T>> build() {
return Mono.just(new DefaultEntityResponse<T>(
return Mono.just(new DefaultEntityResponse<>(
this.status, this.headers, this.cookies, this.entity, this.inserter, this.hints));
}

4
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequestBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -69,7 +69,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder { @@ -69,7 +69,7 @@ class DefaultServerRequestBuilder implements ServerRequest.Builder {
private final List<HttpMessageReader<?>> messageReaders;
private ServerWebExchange exchange;
private final ServerWebExchange exchange;
private String methodName;

5
spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerResponseBuilder.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -78,8 +78,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder { @@ -78,8 +78,7 @@ class DefaultServerResponseBuilder implements ServerResponse.BodyBuilder {
Assert.notNull(other, "ServerResponse must not be null");
this.headers.addAll(other.headers());
this.cookies.addAll(other.cookies());
if (other instanceof AbstractServerResponse) {
AbstractServerResponse abstractOther = (AbstractServerResponse) other;
if (other instanceof AbstractServerResponse abstractOther) {
this.statusCode = abstractOther.statusCode;
this.hints.putAll(abstractOther.hints);
}

6
spring-webflux/src/main/java/org/springframework/web/reactive/handler/AbstractUrlHandlerMapping.java

@ -153,8 +153,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping { @@ -153,8 +153,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
Object handler = this.handlerMap.get(pattern);
// Bean name or resolved handler?
if (handler instanceof String) {
String handlerName = (String) handler;
if (handler instanceof String handlerName) {
handler = obtainApplicationContext().getBean(handlerName);
}
@ -223,8 +222,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping { @@ -223,8 +222,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping {
}
// Eagerly resolve handler if referencing singleton via name.
if (!this.lazyInitHandlers && handler instanceof String) {
String handlerName = (String) handler;
if (!this.lazyInitHandlers && handler instanceof String handlerName) {
if (obtainApplicationContext().isSingleton(handlerName)) {
resolvedHandler = obtainApplicationContext().getBean(handlerName);
}

5
spring-webflux/src/main/java/org/springframework/web/reactive/resource/CssLinkResourceTransformer.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -288,10 +288,9 @@ public class CssLinkResourceTransformer extends ResourceTransformerSupport { @@ -288,10 +288,9 @@ public class CssLinkResourceTransformer extends ResourceTransformerSupport {
if (this == other) {
return true;
}
if (!(other instanceof ContentChunkInfo)) {
if (!(other instanceof ContentChunkInfo otherCci)) {
return false;
}
ContentChunkInfo otherCci = (ContentChunkInfo) other;
return (this.start == otherCci.start && this.end == otherCci.end);
}

21
spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java

@ -42,6 +42,7 @@ import org.springframework.web.util.UriUtils; @@ -42,6 +42,7 @@ import org.springframework.web.util.UriUtils;
* expected to be configured at the end in a chain of resolvers.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 5.0
*/
public class PathResourceResolver extends AbstractResourceResolver {
@ -119,11 +120,12 @@ public class PathResourceResolver extends AbstractResourceResolver { @@ -119,11 +120,12 @@ public class PathResourceResolver extends AbstractResourceResolver {
return Mono.just(resource);
}
else if (logger.isWarnEnabled()) {
Resource[] allowedLocations = getAllowedLocations();
logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
"but resource \"" + resource.getURL() + "\" is neither under the " +
"current location \"" + location.getURL() + "\" nor under any of the " +
"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
Object allowedLocationsText = (getAllowedLocations() != null ? Arrays.asList(getAllowedLocations()) : "[]");
logger.warn("""
Resource path "%s" was successfully resolved, but resource \
"%s" is neither under the current location "%s" nor under any \
of the allowed locations %s"\
""".formatted(resourcePath, resource.getURL(),location.getURL(), allowedLocationsText));
}
}
return Mono.empty();
@ -177,8 +179,8 @@ public class PathResourceResolver extends AbstractResourceResolver { @@ -177,8 +179,8 @@ public class PathResourceResolver extends AbstractResourceResolver {
resourcePath = resource.getURL().toExternalForm();
locationPath = StringUtils.cleanPath(location.getURL().toString());
}
else if (resource instanceof ClassPathResource) {
resourcePath = ((ClassPathResource) resource).getPath();
else if (resource instanceof ClassPathResource classPathResource) {
resourcePath = classPathResource.getPath();
locationPath = StringUtils.cleanPath(((ClassPathResource) location).getPath());
}
else {
@ -203,10 +205,7 @@ public class PathResourceResolver extends AbstractResourceResolver { @@ -203,10 +205,7 @@ public class PathResourceResolver extends AbstractResourceResolver {
return true;
}
}
catch (IllegalArgumentException ex) {
// May not be possible to decode...
}
catch (UnsupportedEncodingException ex) {
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
// Should never happen...
}
}

3
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceUrlProvider.java

@ -106,8 +106,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed @@ -106,8 +106,7 @@ public class ResourceUrlProvider implements ApplicationListener<ContextRefreshed
mappings.forEach(mapping ->
mapping.getHandlerMap().forEach((pattern, handler) -> {
if (handler instanceof ResourceWebHandler) {
ResourceWebHandler resourceHandler = (ResourceWebHandler) handler;
if (handler instanceof ResourceWebHandler resourceHandler) {
this.handlerMap.put(pattern, resourceHandler);
}
}));

13
spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

@ -354,8 +354,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { @@ -354,8 +354,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
return;
}
for (int i = getResourceResolvers().size() - 1; i >= 0; i--) {
if (getResourceResolvers().get(i) instanceof PathResourceResolver) {
PathResourceResolver resolver = (PathResourceResolver) getResourceResolvers().get(i);
if (getResourceResolvers().get(i) instanceof PathResourceResolver resolver) {
if (ObjectUtils.isEmpty(resolver.getAllowedLocations())) {
resolver.setAllowedLocations(getLocations().toArray(new Resource[0]));
}
@ -522,10 +521,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { @@ -522,10 +521,7 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
return true;
}
}
catch (IllegalArgumentException ex) {
// May not be possible to decode...
}
catch (UnsupportedEncodingException ex) {
catch (IllegalArgumentException | UnsupportedEncodingException ex) {
// Should never happen...
}
}
@ -609,9 +605,8 @@ public class ResourceWebHandler implements WebHandler, InitializingBean { @@ -609,9 +605,8 @@ public class ResourceWebHandler implements WebHandler, InitializingBean {
headers.setContentType(mediaType);
}
if (resource instanceof HttpResource) {
HttpHeaders resourceHeaders = ((HttpResource) resource).getResponseHeaders();
exchange.getResponse().getHeaders().putAll(resourceHeaders);
if (resource instanceof HttpResource httpResource) {
exchange.getResponse().getHeaders().putAll(httpResource.getResponseHeaders());
}
}

8
spring-webflux/src/main/java/org/springframework/web/reactive/resource/VersionResourceResolver.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -65,7 +65,7 @@ import org.springframework.web.server.ServerWebExchange; @@ -65,7 +65,7 @@ import org.springframework.web.server.ServerWebExchange;
*/
public class VersionResourceResolver extends AbstractResourceResolver {
private AntPathMatcher pathMatcher = new AntPathMatcher();
private final AntPathMatcher pathMatcher = new AntPathMatcher();
/** Map from path pattern -> VersionStrategy. */
private final Map<String, VersionStrategy> versionStrategyMap = new LinkedHashMap<>();
@ -315,8 +315,8 @@ public class VersionResourceResolver extends AbstractResourceResolver { @@ -315,8 +315,8 @@ public class VersionResourceResolver extends AbstractResourceResolver {
@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers = (this.original instanceof HttpResource ?
((HttpResource) this.original).getResponseHeaders() : new HttpHeaders());
HttpHeaders headers = (this.original instanceof HttpResource httpResource ?
httpResource.getResponseHeaders() : new HttpHeaders());
headers.setETag("W/\"" + this.version + "\"");
return headers;
}

13
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

@ -200,8 +200,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap @@ -200,8 +200,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
* @param handler the bean name of a handler or a handler instance
*/
protected void detectHandlerMethods(final Object handler) {
Class<?> handlerType = (handler instanceof String ?
obtainApplicationContext().getType((String) handler) : handler.getClass());
Class<?> handlerType = (handler instanceof String beanName ?
obtainApplicationContext().getType(beanName) : handler.getClass());
if (handlerType != null) {
final Class<?> userType = ClassUtils.getUserClass(handlerType);
@ -258,8 +258,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap @@ -258,8 +258,8 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
* @return the created HandlerMethod
*/
protected HandlerMethod createHandlerMethod(Object handler, Method method) {
if (handler instanceof String) {
return new HandlerMethod((String) handler,
if (handler instanceof String beanName) {
return new HandlerMethod(beanName,
obtainApplicationContext().getAutowireCapableBeanFactory(),
obtainApplicationContext(),
method);
@ -397,14 +397,13 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap @@ -397,14 +397,13 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
@Override
protected boolean hasCorsConfigurationSource(Object handler) {
return super.hasCorsConfigurationSource(handler) ||
(handler instanceof HandlerMethod && this.mappingRegistry.getCorsConfiguration((HandlerMethod) handler) != null);
(handler instanceof HandlerMethod handlerMethod && this.mappingRegistry.getCorsConfiguration(handlerMethod) != null);
}
@Override
protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) {
CorsConfiguration corsConfig = super.getCorsConfiguration(handler, exchange);
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
if (handler instanceof HandlerMethod handlerMethod) {
if (handlerMethod.equals(PREFLIGHT_AMBIGUOUS_MATCH)) {
return ALLOW_CORS_CONFIG;
}

3
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/InvocableHandlerMethod.java

@ -224,8 +224,7 @@ public class InvocableHandlerMethod extends HandlerMethod { @@ -224,8 +224,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
return true;
}
Type parameterType = returnType.getGenericParameterType();
if (parameterType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) parameterType;
if (parameterType instanceof ParameterizedType type) {
if (type.getActualTypeArguments().length == 1) {
return Void.class.equals(type.getActualTypeArguments()[0]);
}

3
spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java

@ -343,10 +343,9 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping @@ -343,10 +343,9 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
if (this == other) {
return true;
}
if (!(other instanceof RequestMappingInfo)) {
if (!(other instanceof RequestMappingInfo otherInfo)) {
return false;
}
RequestMappingInfo otherInfo = (RequestMappingInfo) other;
return (this.patternsCondition.equals(otherInfo.patternsCondition) &&
this.methodsCondition.equals(otherInfo.methodsCondition) &&
this.paramsCondition.equals(otherInfo.paramsCondition) &&

16
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/RequestContext.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -72,7 +72,7 @@ public class RequestContext { @@ -72,7 +72,7 @@ public class RequestContext {
private Map<String, Errors> errorsMap;
@Nullable
private RequestDataValueProcessor dataValueProcessor;
private final RequestDataValueProcessor dataValueProcessor;
public RequestContext(ServerWebExchange exchange, Map<String, Object> model, MessageSource messageSource) {
@ -92,8 +92,8 @@ public class RequestContext { @@ -92,8 +92,8 @@ public class RequestContext {
LocaleContext localeContext = exchange.getLocaleContext();
Locale locale = localeContext.getLocale();
this.locale = (locale != null ? locale : Locale.getDefault());
TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext ?
((TimeZoneAwareLocaleContext) localeContext).getTimeZone() : null);
TimeZone timeZone = (localeContext instanceof TimeZoneAwareLocaleContext tzaLocaleContext ?
tzaLocaleContext.getTimeZone() : null);
this.timeZone = (timeZone != null ? timeZone : TimeZone.getDefault());
this.defaultHtmlEscape = null; // TODO
@ -385,15 +385,15 @@ public class RequestContext { @@ -385,15 +385,15 @@ public class RequestContext {
}
}
if (errors instanceof BindException) {
errors = ((BindException) errors).getBindingResult();
if (errors instanceof BindException bindException) {
errors = bindException.getBindingResult();
}
if (htmlEscape && !(errors instanceof EscapedErrors)) {
errors = new EscapedErrors(errors);
}
else if (!htmlEscape && errors instanceof EscapedErrors) {
errors = ((EscapedErrors) errors).getSource();
else if (!htmlEscape && errors instanceof EscapedErrors escapedErrors) {
errors = escapedErrors.getSource();
}
this.errorsMap.put(name, errors);

4
spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolverSupport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 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 abstract class ViewResolverSupport implements Ordered { @@ -40,7 +40,7 @@ public abstract class ViewResolverSupport implements Ordered {
public static final MediaType DEFAULT_CONTENT_TYPE = MediaType.parseMediaType("text/html;charset=UTF-8");
private List<MediaType> mediaTypes = new ArrayList<>(4);
private final List<MediaType> mediaTypes = new ArrayList<>(4);
private Charset defaultCharset = StandardCharsets.UTF_8;

5
spring-webflux/src/main/java/org/springframework/web/reactive/socket/CloseStatus.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -239,10 +239,9 @@ public final class CloseStatus { @@ -239,10 +239,9 @@ public final class CloseStatus {
if (this == other) {
return true;
}
if (!(other instanceof CloseStatus)) {
if (!(other instanceof CloseStatus otherStatus)) {
return false;
}
CloseStatus otherStatus = (CloseStatus) other;
return (this.code == otherStatus.code &&
ObjectUtils.nullSafeEquals(this.reason, otherStatus.reason));
}

5
spring-webflux/src/main/java/org/springframework/web/reactive/socket/WebSocketMessage.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -151,10 +151,9 @@ public class WebSocketMessage { @@ -151,10 +151,9 @@ public class WebSocketMessage {
if (this == other) {
return true;
}
if (!(other instanceof WebSocketMessage)) {
if (!(other instanceof WebSocketMessage otherMessage)) {
return false;
}
WebSocketMessage otherMessage = (WebSocketMessage) other;
return (this.type.equals(otherMessage.type) &&
ObjectUtils.nullSafeEquals(this.payload, otherMessage.payload));
}

17
spring-webflux/src/main/java/org/springframework/web/reactive/socket/adapter/StandardWebSocketHandlerAdapter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 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.
@ -41,13 +41,14 @@ import org.springframework.web.reactive.socket.WebSocketSession; @@ -41,13 +41,14 @@ import org.springframework.web.reactive.socket.WebSocketSession;
*
* @author Violeta Georgieva
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 5.0
*/
public class StandardWebSocketHandlerAdapter extends Endpoint {
private final WebSocketHandler delegateHandler;
private Function<Session, StandardWebSocketSession> sessionFactory;
private final Function<Session, StandardWebSocketSession> sessionFactory;
@Nullable
private StandardWebSocketSession delegateSession;
@ -89,16 +90,16 @@ public class StandardWebSocketHandlerAdapter extends Endpoint { @@ -89,16 +90,16 @@ public class StandardWebSocketHandlerAdapter extends Endpoint {
private <T> WebSocketMessage toMessage(T message) {
WebSocketSession session = this.delegateSession;
Assert.state(session != null, "Cannot create message without a session");
if (message instanceof String) {
byte[] bytes = ((String) message).getBytes(StandardCharsets.UTF_8);
if (message instanceof String text) {
byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
return new WebSocketMessage(Type.TEXT, session.bufferFactory().wrap(bytes));
}
else if (message instanceof ByteBuffer) {
DataBuffer buffer = session.bufferFactory().wrap((ByteBuffer) message);
else if (message instanceof ByteBuffer byteBuffer) {
DataBuffer buffer = session.bufferFactory().wrap(byteBuffer);
return new WebSocketMessage(Type.BINARY, buffer);
}
else if (message instanceof PongMessage) {
DataBuffer buffer = session.bufferFactory().wrap(((PongMessage) message).getApplicationData());
else if (message instanceof PongMessage pongMessage) {
DataBuffer buffer = session.bufferFactory().wrap(pongMessage.getApplicationData());
return new WebSocketMessage(Type.PONG, buffer);
}
else {

Loading…
Cancel
Save