Browse Source

Apply "instanceof pattern matching" in remainder of spring-websocket module

See gh-30067
pull/30078/head
Sam Brannen 2 years ago
parent
commit
a31cfba992
  1. 18
      spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java
  2. 16
      spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java
  3. 19
      spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java
  4. 6
      spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java
  5. 6
      spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java
  6. 14
      spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java
  7. 6
      spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java
  8. 10
      spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java
  9. 10
      spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java
  10. 20
      spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java
  11. 18
      spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java
  12. 6
      spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java
  13. 14
      spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java
  14. 6
      spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java
  15. 17
      spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java
  16. 14
      spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java
  17. 4
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java
  18. 6
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java
  19. 14
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java
  20. 18
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java
  21. 12
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java
  22. 7
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java
  23. 6
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java
  24. 14
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java
  25. 6
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java
  26. 10
      spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java

18
spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -104,17 +104,17 @@ public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSess @@ -104,17 +104,17 @@ public abstract class AbstractWebSocketSession<T> implements NativeWebSocketSess
logger.trace("Sending " + message + ", " + this);
}
if (message instanceof TextMessage) {
sendTextMessage((TextMessage) message);
if (message instanceof TextMessage textMessage) {
sendTextMessage(textMessage);
}
else if (message instanceof BinaryMessage) {
sendBinaryMessage((BinaryMessage) message);
else if (message instanceof BinaryMessage binaryMessage) {
sendBinaryMessage(binaryMessage);
}
else if (message instanceof PingMessage) {
sendPingMessage((PingMessage) message);
else if (message instanceof PingMessage pingMessage) {
sendPingMessage(pingMessage);
}
else if (message instanceof PongMessage) {
sendPongMessage((PongMessage) message);
else if (message instanceof PongMessage pongMessage) {
sendPongMessage(pongMessage);
}
else {
throw new IllegalStateException("Unexpected WebSocketMessage type: " + message);

16
spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -84,10 +84,8 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> { @@ -84,10 +84,8 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> {
* @see jakarta.websocket.Decoder#init(EndpointConfig)
*/
public void init(EndpointConfig config) {
ApplicationContext applicationContext = getApplicationContext();
if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableListableBeanFactory beanFactory =
((ConfigurableApplicationContext) applicationContext).getBeanFactory();
if (getApplicationContext() instanceof ConfigurableApplicationContext cac) {
ConfigurableListableBeanFactory beanFactory = cac.getBeanFactory();
beanFactory.autowireBean(this);
}
}
@ -194,12 +192,12 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> { @@ -194,12 +192,12 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> {
return (T) getConversionService().convert(message, getMessageType(), getType());
}
catch (ConversionException ex) {
if (message instanceof String) {
throw new DecodeException((String) message,
if (message instanceof String string) {
throw new DecodeException(string,
"Unable to decode websocket message using ConversionService", ex);
}
if (message instanceof ByteBuffer) {
throw new DecodeException((ByteBuffer) message,
if (message instanceof ByteBuffer byteBuffer) {
throw new DecodeException(byteBuffer,
"Unable to decode websocket message using ConversionService", ex);
}
throw ex;

19
spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -93,13 +93,13 @@ public class WebSocketMessageBrokerStats { @@ -93,13 +93,13 @@ public class WebSocketMessageBrokerStats {
return null;
}
for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) {
if (handler instanceof StompSubProtocolHandler) {
return (StompSubProtocolHandler) handler;
if (handler instanceof StompSubProtocolHandler stompHandler) {
return stompHandler;
}
}
SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler();
if (defaultHandler instanceof StompSubProtocolHandler) {
return (StompSubProtocolHandler) defaultHandler;
if (defaultHandler instanceof StompSubProtocolHandler stompHandler) {
return stompHandler;
}
return null;
}
@ -193,9 +193,8 @@ public class WebSocketMessageBrokerStats { @@ -193,9 +193,8 @@ public class WebSocketMessageBrokerStats {
if (this.sockJsTaskScheduler == null) {
return "null";
}
if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler) {
return getExecutorStatsInfo(((ThreadPoolTaskScheduler) this.sockJsTaskScheduler)
.getScheduledThreadPoolExecutor());
if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler threadPoolTaskScheduler) {
return getExecutorStatsInfo(threadPoolTaskScheduler.getScheduledThreadPoolExecutor());
}
return "unknown";
}
@ -205,8 +204,8 @@ public class WebSocketMessageBrokerStats { @@ -205,8 +204,8 @@ public class WebSocketMessageBrokerStats {
return "null";
}
if (executor instanceof ThreadPoolTaskExecutor) {
executor = ((ThreadPoolTaskExecutor) executor).getThreadPoolExecutor();
if (executor instanceof ThreadPoolTaskExecutor threadPoolTaskScheduler) {
executor = threadPoolTaskScheduler.getThreadPoolExecutor();
}
if (executor instanceof ThreadPoolExecutor) {

6
spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -91,10 +91,10 @@ public class WebMvcStompEndpointRegistry implements StompEndpointRegistry { @@ -91,10 +91,10 @@ public class WebMvcStompEndpointRegistry implements StompEndpointRegistry {
private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler handler) {
WebSocketHandler actual = WebSocketHandlerDecorator.unwrap(handler);
if (!(actual instanceof SubProtocolWebSocketHandler)) {
if (!(actual instanceof SubProtocolWebSocketHandler subProtocolWebSocketHandler)) {
throw new IllegalArgumentException("No SubProtocolWebSocketHandler in " + handler);
}
return (SubProtocolWebSocketHandler) actual;
return subProtocolWebSocketHandler;
}

6
spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -137,8 +137,8 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac @@ -137,8 +137,8 @@ public abstract class WebSocketMessageBrokerConfigurationSupport extends Abstrac
WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler);
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler) {
stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) stompBrokerRelayMessageHandler);
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler sbrmh) {
stats.setStompBrokerRelay(sbrmh);
}
stats.setInboundChannelExecutor(inboundExecutor);
stats.setOutboundChannelExecutor(outboundExecutor);

14
spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2023 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.
@ -39,14 +39,14 @@ public abstract class AbstractWebSocketHandler implements WebSocketHandler { @@ -39,14 +39,14 @@ public abstract class AbstractWebSocketHandler implements WebSocketHandler {
@Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (message instanceof TextMessage) {
handleTextMessage(session, (TextMessage) message);
if (message instanceof TextMessage textMessage) {
handleTextMessage(session, textMessage);
}
else if (message instanceof BinaryMessage) {
handleBinaryMessage(session, (BinaryMessage) message);
else if (message instanceof BinaryMessage binaryMessage) {
handleBinaryMessage(session, binaryMessage);
}
else if (message instanceof PongMessage) {
handlePongMessage(session, (PongMessage) message);
else if (message instanceof PongMessage pongMessage) {
handlePongMessage(session, pongMessage);
}
else {
throw new IllegalStateException("Unexpected WebSocket message type: " + message);

6
spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -48,8 +48,8 @@ public class BeanCreatingHandlerProvider<T> implements BeanFactoryAware { @@ -48,8 +48,8 @@ public class BeanCreatingHandlerProvider<T> implements BeanFactoryAware {
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof AutowireCapableBeanFactory) {
this.beanFactory = (AutowireCapableBeanFactory) beanFactory;
if (beanFactory instanceof AutowireCapableBeanFactory autowireCapableBeanFactory) {
this.beanFactory = autowireCapableBeanFactory;
}
}

10
spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2023 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.
@ -50,15 +50,15 @@ public class WebSocketHandlerDecorator implements WebSocketHandler { @@ -50,15 +50,15 @@ public class WebSocketHandlerDecorator implements WebSocketHandler {
public WebSocketHandler getLastHandler() {
WebSocketHandler result = this.delegate;
while (result instanceof WebSocketHandlerDecorator) {
result = ((WebSocketHandlerDecorator) result).getDelegate();
while (result instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
result = webSocketHandlerDecorator.getDelegate();
}
return result;
}
public static WebSocketHandler unwrap(WebSocketHandler handler) {
if (handler instanceof WebSocketHandlerDecorator) {
return ((WebSocketHandlerDecorator) handler).getLastHandler();
if (handler instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
return webSocketHandlerDecorator.getLastHandler();
}
else {
return handler;

10
spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2023 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.
@ -59,15 +59,15 @@ public class WebSocketSessionDecorator implements WebSocketSession { @@ -59,15 +59,15 @@ public class WebSocketSessionDecorator implements WebSocketSession {
public WebSocketSession getLastSession() {
WebSocketSession result = this.delegate;
while (result instanceof WebSocketSessionDecorator) {
result = ((WebSocketSessionDecorator) result).getDelegate();
while (result instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
result = webSocketSessionDecorator.getDelegate();
}
return result;
}
public static WebSocketSession unwrap(WebSocketSession session) {
if (session instanceof WebSocketSessionDecorator) {
return ((WebSocketSessionDecorator) session).getLastSession();
if (session instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
return webSocketSessionDecorator.getLastSession();
}
else {
return session;

20
spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java

@ -230,11 +230,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE @@ -230,11 +230,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
List<Message<byte[]>> messages;
try {
ByteBuffer byteBuffer;
if (webSocketMessage instanceof TextMessage) {
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes());
if (webSocketMessage instanceof TextMessage textMessage) {
byteBuffer = ByteBuffer.wrap(textMessage.asBytes());
}
else if (webSocketMessage instanceof BinaryMessage) {
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload();
else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
byteBuffer = binaryMessage.getPayload();
}
else {
return;
@ -398,8 +398,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE @@ -398,8 +398,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
return this.immutableMessageInterceptorPresent;
}
if (channel instanceof AbstractMessageChannel) {
for (ChannelInterceptor interceptor : ((AbstractMessageChannel) channel).getInterceptors()) {
if (channel instanceof AbstractMessageChannel abstractMessageChannel) {
for (ChannelInterceptor interceptor : abstractMessageChannel.getInterceptors()) {
if (interceptor instanceof ImmutableMessageChannelInterceptor) {
this.immutableMessageInterceptorPresent = true;
return true;
@ -520,8 +520,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE @@ -520,8 +520,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
private StompHeaderAccessor getStompHeaderAccessor(Message<?> message) {
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
if (accessor instanceof StompHeaderAccessor) {
return (StompHeaderAccessor) accessor;
if (accessor instanceof StompHeaderAccessor stompHeaderAccessor) {
return stompHeaderAccessor;
}
else {
StompHeaderAccessor stompAccessor = StompHeaderAccessor.wrap(message);
@ -614,8 +614,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE @@ -614,8 +614,8 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE
long[] heartbeat = accessor.getHeartbeat();
if (heartbeat[1] > 0) {
session = WebSocketSessionDecorator.unwrap(session);
if (session instanceof SockJsSession) {
((SockJsSession) session).disableHeartbeat();
if (session instanceof SockJsSession sockJsSession) {
sockJsSession.disableHeartbeat();
}
}

18
spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -178,8 +178,8 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif @@ -178,8 +178,8 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
public void start() {
if (!isRunning()) {
this.running = true;
if (getWebSocketClient() instanceof Lifecycle) {
((Lifecycle) getWebSocketClient()).start();
if (getWebSocketClient() instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
@ -189,8 +189,8 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif @@ -189,8 +189,8 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
public void stop() {
if (isRunning()) {
this.running = false;
if (getWebSocketClient() instanceof Lifecycle) {
((Lifecycle) getWebSocketClient()).stop();
if (getWebSocketClient() instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}
@ -554,11 +554,11 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif @@ -554,11 +554,11 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
public List<Message<byte[]>> decode(WebSocketMessage<?> webSocketMessage) {
List<Message<byte[]>> result = Collections.emptyList();
ByteBuffer byteBuffer;
if (webSocketMessage instanceof TextMessage) {
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes());
if (webSocketMessage instanceof TextMessage textMessage) {
byteBuffer = ByteBuffer.wrap(textMessage.asBytes());
}
else if (webSocketMessage instanceof BinaryMessage) {
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload();
else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
byteBuffer = binaryMessage.getPayload();
}
else {
return result;

6
spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -94,8 +94,8 @@ public class WebLogicRequestUpgradeStrategy extends AbstractTyrusRequestUpgradeS @@ -94,8 +94,8 @@ public class WebLogicRequestUpgradeStrategy extends AbstractTyrusRequestUpgradeS
}
private static Object getNativeRequest(ServletRequest request) {
while (request instanceof ServletRequestWrapper) {
request = ((ServletRequestWrapper) request).getRequest();
while (request instanceof ServletRequestWrapper wrapper) {
request = wrapper.getRequest();
}
return request;
}

14
spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -176,8 +176,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life @@ -176,8 +176,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
}
protected void doStart() {
if (this.requestUpgradeStrategy instanceof Lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).start();
if (this.requestUpgradeStrategy instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
@ -190,8 +190,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life @@ -190,8 +190,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
}
protected void doStop() {
if (this.requestUpgradeStrategy instanceof Lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).stop();
if (this.requestUpgradeStrategy instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
@ -349,8 +349,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life @@ -349,8 +349,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler) {
WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler);
List<String> subProtocols = null;
if (handlerToCheck instanceof SubProtocolCapable) {
subProtocols = ((SubProtocolCapable) handlerToCheck).getSubProtocols();
if (handlerToCheck instanceof SubProtocolCapable subProtocolCapable) {
subProtocols = subProtocolCapable.getSubProtocols();
}
return (subProtocols != null ? subProtocols : Collections.emptyList());
}

6
spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2023 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 class DefaultHandshakeHandler extends AbstractHandshakeHandler implements @@ -43,8 +43,8 @@ public class DefaultHandshakeHandler extends AbstractHandshakeHandler implements
@Override
public void setServletContext(ServletContext servletContext) {
RequestUpgradeStrategy strategy = getRequestUpgradeStrategy();
if (strategy instanceof ServletContextAware) {
((ServletContextAware) strategy).setServletContext(servletContext);
if (strategy instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}

17
spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -61,8 +61,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements @@ -61,8 +61,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
@Override
protected void initServletContext(ServletContext servletContext) {
for (Object handler : getUrlMap().values()) {
if (handler instanceof ServletContextAware) {
((ServletContextAware) handler).setServletContext(servletContext);
if (handler instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
}
@ -73,8 +73,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements @@ -73,8 +73,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
if (!isRunning()) {
this.running = true;
for (Object handler : getUrlMap().values()) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).start();
if (handler instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
}
@ -85,8 +85,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements @@ -85,8 +85,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
if (isRunning()) {
this.running = false;
for (Object handler : getUrlMap().values()) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).stop();
if (handler instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}
@ -106,8 +106,7 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements @@ -106,8 +106,7 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
}
private boolean matchWebSocketUpgrade(@Nullable Object handler, HttpServletRequest request) {
handler = (handler instanceof HandlerExecutionChain ?
((HandlerExecutionChain) handler).getHandler() : handler);
handler = (handler instanceof HandlerExecutionChain chain ? chain.getHandler() : handler);
if (this.webSocketUpgradeMatch && handler instanceof WebSocketHttpRequestHandler) {
String header = request.getHeader(HttpHeaders.UPGRADE);
return (request.getMethod().equals("GET") &&

14
spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -124,8 +124,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl @@ -124,8 +124,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
@Override
public void setServletContext(ServletContext servletContext) {
if (this.handshakeHandler instanceof ServletContextAware) {
((ServletContextAware) this.handshakeHandler).setServletContext(servletContext);
if (this.handshakeHandler instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
@ -134,8 +134,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl @@ -134,8 +134,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
public void start() {
if (!isRunning()) {
this.running = true;
if (this.handshakeHandler instanceof Lifecycle) {
((Lifecycle) this.handshakeHandler).start();
if (this.handshakeHandler instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
}
@ -144,8 +144,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl @@ -144,8 +144,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
public void stop() {
if (isRunning()) {
this.running = false;
if (this.handshakeHandler instanceof Lifecycle) {
((Lifecycle) this.handshakeHandler).stop();
if (this.handshakeHandler instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}

4
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java

@ -155,14 +155,14 @@ public abstract class AbstractClientSockJsSession implements WebSocketSession { @@ -155,14 +155,14 @@ public abstract class AbstractClientSockJsSession implements WebSocketSession {
@Override
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
if (!(message instanceof TextMessage)) {
if (!(message instanceof TextMessage textMessage)) {
throw new IllegalArgumentException(this + " supports text messages only.");
}
if (this.state != State.OPEN) {
throw new IllegalStateException(this + " is not open: current state " + this.state);
}
String payload = ((TextMessage) message).getPayload();
String payload = textMessage.getPayload();
payload = getMessageCodec().encode(payload);
payload = payload.substring(1); // the client-side doesn't need message framing (letter "a")

6
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -183,8 +183,8 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport { @@ -183,8 +183,8 @@ public class RestTemplateXhrTransport extends AbstractXhrTransport {
public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getHeaders().putAll(this.headers);
if (this.body != null) {
if (request instanceof StreamingHttpOutputMessage) {
((StreamingHttpOutputMessage) request).setBody(outputStream ->
if (request instanceof StreamingHttpOutputMessage streamingOutputMessage) {
streamingOutputMessage.setBody(outputStream ->
StreamUtils.copy(this.body, SockJsFrame.CHARSET, outputStream));
}
else {

14
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -95,8 +95,8 @@ public class WebSocketTransport implements Transport, Lifecycle { @@ -95,8 +95,8 @@ public class WebSocketTransport implements Transport, Lifecycle {
@Override
public void start() {
if (!isRunning()) {
if (this.webSocketClient instanceof Lifecycle) {
((Lifecycle) this.webSocketClient).start();
if (this.webSocketClient instanceof Lifecycle lifecycle) {
lifecycle.start();
}
else {
this.running = true;
@ -107,8 +107,8 @@ public class WebSocketTransport implements Transport, Lifecycle { @@ -107,8 +107,8 @@ public class WebSocketTransport implements Transport, Lifecycle {
@Override
public void stop() {
if (isRunning()) {
if (this.webSocketClient instanceof Lifecycle) {
((Lifecycle) this.webSocketClient).stop();
if (this.webSocketClient instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
else {
this.running = false;
@ -118,8 +118,8 @@ public class WebSocketTransport implements Transport, Lifecycle { @@ -118,8 +118,8 @@ public class WebSocketTransport implements Transport, Lifecycle {
@Override
public boolean isRunning() {
if (this.webSocketClient instanceof Lifecycle) {
return ((Lifecycle) this.webSocketClient).isRunning();
if (this.webSocketClient instanceof Lifecycle lifecycle) {
return lifecycle.isRunning();
}
else {
return this.running;

18
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -91,8 +91,8 @@ public class SockJsHttpRequestHandler @@ -91,8 +91,8 @@ public class SockJsHttpRequestHandler
@Override
public void setServletContext(ServletContext servletContext) {
if (this.sockJsService instanceof ServletContextAware) {
((ServletContextAware) this.sockJsService).setServletContext(servletContext);
if (this.sockJsService instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
@ -101,8 +101,8 @@ public class SockJsHttpRequestHandler @@ -101,8 +101,8 @@ public class SockJsHttpRequestHandler
public void start() {
if (!isRunning()) {
this.running = true;
if (this.sockJsService instanceof Lifecycle) {
((Lifecycle) this.sockJsService).start();
if (this.sockJsService instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
}
@ -111,8 +111,8 @@ public class SockJsHttpRequestHandler @@ -111,8 +111,8 @@ public class SockJsHttpRequestHandler
public void stop() {
if (isRunning()) {
this.running = false;
if (this.sockJsService instanceof Lifecycle) {
((Lifecycle) this.sockJsService).stop();
if (this.sockJsService instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}
@ -147,8 +147,8 @@ public class SockJsHttpRequestHandler @@ -147,8 +147,8 @@ public class SockJsHttpRequestHandler
@Override
@Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
if (this.sockJsService instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) this.sockJsService).getCorsConfiguration(request);
if (this.sockJsService instanceof CorsConfigurationSource ccs) {
return ccs.getCorsConfiguration(request);
}
return null;
}

12
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java

@ -169,8 +169,8 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem @@ -169,8 +169,8 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
if (!isRunning()) {
this.running = true;
for (TransportHandler handler : this.handlers.values()) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).start();
if (handler instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
}
@ -181,8 +181,8 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem @@ -181,8 +181,8 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
if (isRunning()) {
this.running = false;
for (TransportHandler handler : this.handlers.values()) {
if (handler instanceof Lifecycle) {
((Lifecycle) handler).stop();
if (handler instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}
@ -199,7 +199,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem @@ -199,7 +199,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
WebSocketHandler handler) throws IOException {
TransportHandler transportHandler = this.handlers.get(TransportType.WEBSOCKET);
if (!(transportHandler instanceof HandshakeHandler)) {
if (!(transportHandler instanceof HandshakeHandler handshakeHandler)) {
logger.error("No handler configured for raw WebSocket messages");
response.setStatusCode(HttpStatus.NOT_FOUND);
return;
@ -213,7 +213,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem @@ -213,7 +213,7 @@ public class TransportHandlingSockJsService extends AbstractSockJsService implem
if (!chain.applyBeforeHandshake(request, response, attributes)) {
return;
}
((HandshakeHandler) transportHandler).doHandshake(request, response, handler, attributes);
handshakeHandler.doHandshake(request, response, handler, attributes);
chain.applyAfterHandshake(request, response, null);
}
catch (HandshakeFailureException ex) {

7
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -103,9 +103,10 @@ public class DefaultSockJsService extends TransportHandlingSockJsService impleme @@ -103,9 +103,10 @@ public class DefaultSockJsService extends TransportHandlingSockJsService impleme
@Override
public void setServletContext(ServletContext servletContext) {
for (TransportHandler handler : getTransportHandlers().values()) {
if (handler instanceof ServletContextAware) {
((ServletContextAware) handler).setServletContext(servletContext);
if (handler instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
}
}

6
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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,8 +69,8 @@ public class SockJsWebSocketHandler extends TextWebSocketHandler implements SubP @@ -69,8 +69,8 @@ public class SockJsWebSocketHandler extends TextWebSocketHandler implements SubP
this.sockJsSession = sockJsSession;
webSocketHandler = WebSocketHandlerDecorator.unwrap(webSocketHandler);
this.subProtocols = ((webSocketHandler instanceof SubProtocolCapable) ?
new ArrayList<>(((SubProtocolCapable) webSocketHandler).getSubProtocols()) : Collections.emptyList());
this.subProtocols = ((webSocketHandler instanceof SubProtocolCapable subProtocolCapable) ?
new ArrayList<>(subProtocolCapable.getSubProtocols()) : Collections.emptyList());
}
@Override

14
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -73,8 +73,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler @@ -73,8 +73,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
@Override
public void setServletContext(ServletContext servletContext) {
if (this.handshakeHandler instanceof ServletContextAware) {
((ServletContextAware) this.handshakeHandler).setServletContext(servletContext);
if (this.handshakeHandler instanceof ServletContextAware servletContextAware) {
servletContextAware.setServletContext(servletContext);
}
}
@ -83,8 +83,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler @@ -83,8 +83,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
public void start() {
if (!isRunning()) {
this.running = true;
if (this.handshakeHandler instanceof Lifecycle) {
((Lifecycle) this.handshakeHandler).start();
if (this.handshakeHandler instanceof Lifecycle lifecycle) {
lifecycle.start();
}
}
}
@ -93,8 +93,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler @@ -93,8 +93,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
public void stop() {
if (isRunning()) {
this.running = false;
if (this.handshakeHandler instanceof Lifecycle) {
((Lifecycle) this.handshakeHandler).stop();
if (this.handshakeHandler instanceof Lifecycle lifecycle) {
lifecycle.stop();
}
}
}

6
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -278,8 +278,8 @@ public abstract class AbstractHttpSockJsSession extends AbstractSockJsSession { @@ -278,8 +278,8 @@ public abstract class AbstractHttpSockJsSession extends AbstractSockJsSession {
}
private void disableShallowEtagHeaderFilter(ServerHttpRequest request) {
if (request instanceof ServletServerHttpRequest) {
ServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
if (request instanceof ServletServerHttpRequest servletServerHttpRequest) {
ServletRequest servletRequest = servletServerHttpRequest.getServletRequest();
ShallowEtagHeaderFilter.disableContentCaching(servletRequest);
}
}

10
spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -139,15 +139,15 @@ public class WebSocketServerSockJsSession extends AbstractSockJsSession implemen @@ -139,15 +139,15 @@ public class WebSocketServerSockJsSession extends AbstractSockJsSession implemen
@Override
public Object getNativeSession() {
Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
return (this.webSocketSession instanceof NativeWebSocketSession ?
((NativeWebSocketSession) this.webSocketSession).getNativeSession() : this.webSocketSession);
return (this.webSocketSession instanceof NativeWebSocketSession nativeWsSession ?
nativeWsSession.getNativeSession() : this.webSocketSession);
}
@Override
@Nullable
public <T> T getNativeSession(@Nullable Class<T> requiredType) {
return (this.webSocketSession instanceof NativeWebSocketSession ?
((NativeWebSocketSession) this.webSocketSession).getNativeSession(requiredType) : null);
return (this.webSocketSession instanceof NativeWebSocketSession nativeWsSession ?
nativeWsSession.getNativeSession(requiredType) : null);
}

Loading…
Cancel
Save