diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java index 63a403e46cf..20ac35e4516 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/AbstractWebSocketSession.java @@ -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 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); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java index 0e49d4c36e7..20bd7cfb5a9 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/adapter/standard/ConvertingEncoderDecoderSupport.java @@ -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 { * @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 { 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; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java index 16e1b2f5139..e5aa7675b3a 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/WebSocketMessageBrokerStats.java @@ -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 { 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 { 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 { return "null"; } - if (executor instanceof ThreadPoolTaskExecutor) { - executor = ((ThreadPoolTaskExecutor) executor).getThreadPoolExecutor(); + if (executor instanceof ThreadPoolTaskExecutor threadPoolTaskScheduler) { + executor = threadPoolTaskScheduler.getThreadPoolExecutor(); } if (executor instanceof ThreadPoolExecutor) { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java index d00634d9843..7b3dd2291e1 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebMvcStompEndpointRegistry.java @@ -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 { 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; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java index 162b3ab52f9..2b84d011afa 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/config/annotation/WebSocketMessageBrokerConfigurationSupport.java @@ -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 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); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java index bd365da614f..64a1ae1fdb3 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/handler/AbstractWebSocketHandler.java @@ -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 { @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); diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java b/spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java index 4845f6b18b0..74c0a16203e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/handler/BeanCreatingHandlerProvider.java @@ -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 implements BeanFactoryAware { @Override public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof AutowireCapableBeanFactory) { - this.beanFactory = (AutowireCapableBeanFactory) beanFactory; + if (beanFactory instanceof AutowireCapableBeanFactory autowireCapableBeanFactory) { + this.beanFactory = autowireCapableBeanFactory; } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java b/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java index 4ec39f8f762..7668f2babd3 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketHandlerDecorator.java @@ -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 { 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; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java b/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java index 06c1faf5115..115ee5ea0c3 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/handler/WebSocketSessionDecorator.java @@ -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 { 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; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java index 9ad19bdd70e..b06bea9b5bb 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/StompSubProtocolHandler.java @@ -230,11 +230,11 @@ public class StompSubProtocolHandler implements SubProtocolHandler, ApplicationE List> 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 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 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 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(); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java index 4a53f6e04bf..cf599692d08 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/messaging/WebSocketStompClient.java @@ -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 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 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 public List> decode(WebSocketMessage webSocketMessage) { List> 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; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java index 8174297a5c4..23409116acc 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/standard/WebLogicRequestUpgradeStrategy.java @@ -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 } private static Object getNativeRequest(ServletRequest request) { - while (request instanceof ServletRequestWrapper) { - request = ((ServletRequestWrapper) request).getRequest(); + while (request instanceof ServletRequestWrapper wrapper) { + request = wrapper.getRequest(); } return request; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java index 527054eb755..48cb47eebae 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java @@ -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 } 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 } 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 protected final List determineHandlerSupportedProtocols(WebSocketHandler handler) { WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler); List subProtocols = null; - if (handlerToCheck instanceof SubProtocolCapable) { - subProtocols = ((SubProtocolCapable) handlerToCheck).getSubProtocols(); + if (handlerToCheck instanceof SubProtocolCapable subProtocolCapable) { + subProtocols = subProtocolCapable.getSubProtocols(); } return (subProtocols != null ? subProtocols : Collections.emptyList()); } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java index a3701fc0997..1ad972ab904 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/DefaultHandshakeHandler.java @@ -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 @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); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java index a6d5bfb7f91..ebd03280c60 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHandlerMapping.java @@ -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 @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 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 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 } 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") && diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java index 1d238281e3b..e03cc0da923 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/WebSocketHttpRequestHandler.java @@ -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 @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 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 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(); } } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java index e0591fe5fa4..c042721a43e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/AbstractClientSockJsSession.java @@ -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") diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java index f3b2a765470..eb14d15f932 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/RestTemplateXhrTransport.java @@ -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 { 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 { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java index 716a2ab5587..fe1b5013409 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/client/WebSocketTransport.java @@ -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 { @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 { @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 { @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; diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java index 555f8f8903e..97356666947 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/SockJsHttpRequestHandler.java @@ -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 @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 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 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 @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; } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java index 6d76f3cf936..2dfd9e452db 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java @@ -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 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 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 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) { diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java index 8a33978475e..8de7cdb518c 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/DefaultSockJsService.java @@ -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 @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); } } } + } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java index fadfc905c31..5293580a02e 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/SockJsWebSocketHandler.java @@ -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 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 diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java index 8f58315f80a..7a5b23d88a2 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/handler/WebSocketTransportHandler.java @@ -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 @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 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 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(); } } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java index 2f198ce4b19..211f8063b5b 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/AbstractHttpSockJsSession.java @@ -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 { } 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); } } diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java index 611d51f739b..ded5f410eb2 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/session/WebSocketServerSockJsSession.java @@ -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 @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 getNativeSession(@Nullable Class requiredType) { - return (this.webSocketSession instanceof NativeWebSocketSession ? - ((NativeWebSocketSession) this.webSocketSession).getNativeSession(requiredType) : null); + return (this.webSocketSession instanceof NativeWebSocketSession nativeWsSession ? + nativeWsSession.getNativeSession(requiredType) : null); }