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

See gh-30067
This commit is contained in:
Sam Brannen 2023-03-07 19:16:10 +01:00
parent 14973fd6f3
commit a31cfba992
26 changed files with 147 additions and 150 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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
logger.trace("Sending " + message + ", " + this); logger.trace("Sending " + message + ", " + this);
} }
if (message instanceof TextMessage) { if (message instanceof TextMessage textMessage) {
sendTextMessage((TextMessage) message); sendTextMessage(textMessage);
} }
else if (message instanceof BinaryMessage) { else if (message instanceof BinaryMessage binaryMessage) {
sendBinaryMessage((BinaryMessage) message); sendBinaryMessage(binaryMessage);
} }
else if (message instanceof PingMessage) { else if (message instanceof PingMessage pingMessage) {
sendPingMessage((PingMessage) message); sendPingMessage(pingMessage);
} }
else if (message instanceof PongMessage) { else if (message instanceof PongMessage pongMessage) {
sendPongMessage((PongMessage) message); sendPongMessage(pongMessage);
} }
else { else {
throw new IllegalStateException("Unexpected WebSocketMessage type: " + message); throw new IllegalStateException("Unexpected WebSocketMessage type: " + message);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -84,10 +84,8 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> {
* @see jakarta.websocket.Decoder#init(EndpointConfig) * @see jakarta.websocket.Decoder#init(EndpointConfig)
*/ */
public void init(EndpointConfig config) { public void init(EndpointConfig config) {
ApplicationContext applicationContext = getApplicationContext(); if (getApplicationContext() instanceof ConfigurableApplicationContext cac) {
if (applicationContext instanceof ConfigurableApplicationContext) { ConfigurableListableBeanFactory beanFactory = cac.getBeanFactory();
ConfigurableListableBeanFactory beanFactory =
((ConfigurableApplicationContext) applicationContext).getBeanFactory();
beanFactory.autowireBean(this); beanFactory.autowireBean(this);
} }
} }
@ -194,12 +192,12 @@ public abstract class ConvertingEncoderDecoderSupport<T, M> {
return (T) getConversionService().convert(message, getMessageType(), getType()); return (T) getConversionService().convert(message, getMessageType(), getType());
} }
catch (ConversionException ex) { catch (ConversionException ex) {
if (message instanceof String) { if (message instanceof String string) {
throw new DecodeException((String) message, throw new DecodeException(string,
"Unable to decode websocket message using ConversionService", ex); "Unable to decode websocket message using ConversionService", ex);
} }
if (message instanceof ByteBuffer) { if (message instanceof ByteBuffer byteBuffer) {
throw new DecodeException((ByteBuffer) message, throw new DecodeException(byteBuffer,
"Unable to decode websocket message using ConversionService", ex); "Unable to decode websocket message using ConversionService", ex);
} }
throw ex; throw ex;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -93,13 +93,13 @@ public class WebSocketMessageBrokerStats {
return null; return null;
} }
for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) { for (SubProtocolHandler handler : this.webSocketHandler.getProtocolHandlers()) {
if (handler instanceof StompSubProtocolHandler) { if (handler instanceof StompSubProtocolHandler stompHandler) {
return (StompSubProtocolHandler) handler; return stompHandler;
} }
} }
SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler(); SubProtocolHandler defaultHandler = this.webSocketHandler.getDefaultProtocolHandler();
if (defaultHandler instanceof StompSubProtocolHandler) { if (defaultHandler instanceof StompSubProtocolHandler stompHandler) {
return (StompSubProtocolHandler) defaultHandler; return stompHandler;
} }
return null; return null;
} }
@ -193,9 +193,8 @@ public class WebSocketMessageBrokerStats {
if (this.sockJsTaskScheduler == null) { if (this.sockJsTaskScheduler == null) {
return "null"; return "null";
} }
if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler) { if (this.sockJsTaskScheduler instanceof ThreadPoolTaskScheduler threadPoolTaskScheduler) {
return getExecutorStatsInfo(((ThreadPoolTaskScheduler) this.sockJsTaskScheduler) return getExecutorStatsInfo(threadPoolTaskScheduler.getScheduledThreadPoolExecutor());
.getScheduledThreadPoolExecutor());
} }
return "unknown"; return "unknown";
} }
@ -205,8 +204,8 @@ public class WebSocketMessageBrokerStats {
return "null"; return "null";
} }
if (executor instanceof ThreadPoolTaskExecutor) { if (executor instanceof ThreadPoolTaskExecutor threadPoolTaskScheduler) {
executor = ((ThreadPoolTaskExecutor) executor).getThreadPoolExecutor(); executor = threadPoolTaskScheduler.getThreadPoolExecutor();
} }
if (executor instanceof ThreadPoolExecutor) { if (executor instanceof ThreadPoolExecutor) {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private static SubProtocolWebSocketHandler unwrapSubProtocolWebSocketHandler(WebSocketHandler handler) {
WebSocketHandler actual = WebSocketHandlerDecorator.unwrap(handler); WebSocketHandler actual = WebSocketHandlerDecorator.unwrap(handler);
if (!(actual instanceof SubProtocolWebSocketHandler)) { if (!(actual instanceof SubProtocolWebSocketHandler subProtocolWebSocketHandler)) {
throw new IllegalArgumentException("No SubProtocolWebSocketHandler in " + handler); throw new IllegalArgumentException("No SubProtocolWebSocketHandler in " + handler);
} }
return (SubProtocolWebSocketHandler) actual; return subProtocolWebSocketHandler;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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(); WebSocketMessageBrokerStats stats = new WebSocketMessageBrokerStats();
stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler); stats.setSubProtocolWebSocketHandler((SubProtocolWebSocketHandler) subProtocolWebSocketHandler);
if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler) { if (stompBrokerRelayMessageHandler instanceof StompBrokerRelayMessageHandler sbrmh) {
stats.setStompBrokerRelay((StompBrokerRelayMessageHandler) stompBrokerRelayMessageHandler); stats.setStompBrokerRelay(sbrmh);
} }
stats.setInboundChannelExecutor(inboundExecutor); stats.setInboundChannelExecutor(inboundExecutor);
stats.setOutboundChannelExecutor(outboundExecutor); stats.setOutboundChannelExecutor(outboundExecutor);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {
if (message instanceof TextMessage) { if (message instanceof TextMessage textMessage) {
handleTextMessage(session, (TextMessage) message); handleTextMessage(session, textMessage);
} }
else if (message instanceof BinaryMessage) { else if (message instanceof BinaryMessage binaryMessage) {
handleBinaryMessage(session, (BinaryMessage) message); handleBinaryMessage(session, binaryMessage);
} }
else if (message instanceof PongMessage) { else if (message instanceof PongMessage pongMessage) {
handlePongMessage(session, (PongMessage) message); handlePongMessage(session, pongMessage);
} }
else { else {
throw new IllegalStateException("Unexpected WebSocket message type: " + message); throw new IllegalStateException("Unexpected WebSocket message type: " + message);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -48,8 +48,8 @@ public class BeanCreatingHandlerProvider<T> implements BeanFactoryAware {
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) { public void setBeanFactory(BeanFactory beanFactory) {
if (beanFactory instanceof AutowireCapableBeanFactory) { if (beanFactory instanceof AutowireCapableBeanFactory autowireCapableBeanFactory) {
this.beanFactory = (AutowireCapableBeanFactory) beanFactory; this.beanFactory = autowireCapableBeanFactory;
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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() { public WebSocketHandler getLastHandler() {
WebSocketHandler result = this.delegate; WebSocketHandler result = this.delegate;
while (result instanceof WebSocketHandlerDecorator) { while (result instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
result = ((WebSocketHandlerDecorator) result).getDelegate(); result = webSocketHandlerDecorator.getDelegate();
} }
return result; return result;
} }
public static WebSocketHandler unwrap(WebSocketHandler handler) { public static WebSocketHandler unwrap(WebSocketHandler handler) {
if (handler instanceof WebSocketHandlerDecorator) { if (handler instanceof WebSocketHandlerDecorator webSocketHandlerDecorator) {
return ((WebSocketHandlerDecorator) handler).getLastHandler(); return webSocketHandlerDecorator.getLastHandler();
} }
else { else {
return handler; return handler;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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() { public WebSocketSession getLastSession() {
WebSocketSession result = this.delegate; WebSocketSession result = this.delegate;
while (result instanceof WebSocketSessionDecorator) { while (result instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
result = ((WebSocketSessionDecorator) result).getDelegate(); result = webSocketSessionDecorator.getDelegate();
} }
return result; return result;
} }
public static WebSocketSession unwrap(WebSocketSession session) { public static WebSocketSession unwrap(WebSocketSession session) {
if (session instanceof WebSocketSessionDecorator) { if (session instanceof WebSocketSessionDecorator webSocketSessionDecorator) {
return ((WebSocketSessionDecorator) session).getLastSession(); return webSocketSessionDecorator.getLastSession();
} }
else { else {
return session; return session;

View File

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

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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() { public void start() {
if (!isRunning()) { if (!isRunning()) {
this.running = true; this.running = true;
if (getWebSocketClient() instanceof Lifecycle) { if (getWebSocketClient() instanceof Lifecycle lifecycle) {
((Lifecycle) getWebSocketClient()).start(); lifecycle.start();
} }
} }
@ -189,8 +189,8 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
public void stop() { public void stop() {
if (isRunning()) { if (isRunning()) {
this.running = false; this.running = false;
if (getWebSocketClient() instanceof Lifecycle) { if (getWebSocketClient() instanceof Lifecycle lifecycle) {
((Lifecycle) getWebSocketClient()).stop(); lifecycle.stop();
} }
} }
} }
@ -554,11 +554,11 @@ public class WebSocketStompClient extends StompClientSupport implements SmartLif
public List<Message<byte[]>> decode(WebSocketMessage<?> webSocketMessage) { public List<Message<byte[]>> decode(WebSocketMessage<?> webSocketMessage) {
List<Message<byte[]>> result = Collections.emptyList(); List<Message<byte[]>> result = Collections.emptyList();
ByteBuffer byteBuffer; ByteBuffer byteBuffer;
if (webSocketMessage instanceof TextMessage) { if (webSocketMessage instanceof TextMessage textMessage) {
byteBuffer = ByteBuffer.wrap(((TextMessage) webSocketMessage).asBytes()); byteBuffer = ByteBuffer.wrap(textMessage.asBytes());
} }
else if (webSocketMessage instanceof BinaryMessage) { else if (webSocketMessage instanceof BinaryMessage binaryMessage) {
byteBuffer = ((BinaryMessage) webSocketMessage).getPayload(); byteBuffer = binaryMessage.getPayload();
} }
else { else {
return result; return result;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private static Object getNativeRequest(ServletRequest request) {
while (request instanceof ServletRequestWrapper) { while (request instanceof ServletRequestWrapper wrapper) {
request = ((ServletRequestWrapper) request).getRequest(); request = wrapper.getRequest();
} }
return request; return request;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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() { protected void doStart() {
if (this.requestUpgradeStrategy instanceof Lifecycle) { if (this.requestUpgradeStrategy instanceof Lifecycle lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).start(); lifecycle.start();
} }
} }
@ -190,8 +190,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
} }
protected void doStop() { protected void doStop() {
if (this.requestUpgradeStrategy instanceof Lifecycle) { if (this.requestUpgradeStrategy instanceof Lifecycle lifecycle) {
((Lifecycle) this.requestUpgradeStrategy).stop(); lifecycle.stop();
} }
} }
@ -349,8 +349,8 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler) { protected final List<String> determineHandlerSupportedProtocols(WebSocketHandler handler) {
WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler); WebSocketHandler handlerToCheck = WebSocketHandlerDecorator.unwrap(handler);
List<String> subProtocols = null; List<String> subProtocols = null;
if (handlerToCheck instanceof SubProtocolCapable) { if (handlerToCheck instanceof SubProtocolCapable subProtocolCapable) {
subProtocols = ((SubProtocolCapable) handlerToCheck).getSubProtocols(); subProtocols = subProtocolCapable.getSubProtocols();
} }
return (subProtocols != null ? subProtocols : Collections.emptyList()); return (subProtocols != null ? subProtocols : Collections.emptyList());
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
RequestUpgradeStrategy strategy = getRequestUpgradeStrategy(); RequestUpgradeStrategy strategy = getRequestUpgradeStrategy();
if (strategy instanceof ServletContextAware) { if (strategy instanceof ServletContextAware servletContextAware) {
((ServletContextAware) strategy).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
protected void initServletContext(ServletContext servletContext) { protected void initServletContext(ServletContext servletContext) {
for (Object handler : getUrlMap().values()) { for (Object handler : getUrlMap().values()) {
if (handler instanceof ServletContextAware) { if (handler instanceof ServletContextAware servletContextAware) {
((ServletContextAware) handler).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }
} }
@ -73,8 +73,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
if (!isRunning()) { if (!isRunning()) {
this.running = true; this.running = true;
for (Object handler : getUrlMap().values()) { for (Object handler : getUrlMap().values()) {
if (handler instanceof Lifecycle) { if (handler instanceof Lifecycle lifecycle) {
((Lifecycle) handler).start(); lifecycle.start();
} }
} }
} }
@ -85,8 +85,8 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
if (isRunning()) { if (isRunning()) {
this.running = false; this.running = false;
for (Object handler : getUrlMap().values()) { for (Object handler : getUrlMap().values()) {
if (handler instanceof Lifecycle) { if (handler instanceof Lifecycle lifecycle) {
((Lifecycle) handler).stop(); lifecycle.stop();
} }
} }
} }
@ -106,8 +106,7 @@ public class WebSocketHandlerMapping extends SimpleUrlHandlerMapping implements
} }
private boolean matchWebSocketUpgrade(@Nullable Object handler, HttpServletRequest request) { private boolean matchWebSocketUpgrade(@Nullable Object handler, HttpServletRequest request) {
handler = (handler instanceof HandlerExecutionChain ? handler = (handler instanceof HandlerExecutionChain chain ? chain.getHandler() : handler);
((HandlerExecutionChain) handler).getHandler() : handler);
if (this.webSocketUpgradeMatch && handler instanceof WebSocketHttpRequestHandler) { if (this.webSocketUpgradeMatch && handler instanceof WebSocketHttpRequestHandler) {
String header = request.getHeader(HttpHeaders.UPGRADE); String header = request.getHeader(HttpHeaders.UPGRADE);
return (request.getMethod().equals("GET") && return (request.getMethod().equals("GET") &&

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
if (this.handshakeHandler instanceof ServletContextAware) { if (this.handshakeHandler instanceof ServletContextAware servletContextAware) {
((ServletContextAware) this.handshakeHandler).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }
@ -134,8 +134,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
public void start() { public void start() {
if (!isRunning()) { if (!isRunning()) {
this.running = true; this.running = true;
if (this.handshakeHandler instanceof Lifecycle) { if (this.handshakeHandler instanceof Lifecycle lifecycle) {
((Lifecycle) this.handshakeHandler).start(); lifecycle.start();
} }
} }
} }
@ -144,8 +144,8 @@ public class WebSocketHttpRequestHandler implements HttpRequestHandler, Lifecycl
public void stop() { public void stop() {
if (isRunning()) { if (isRunning()) {
this.running = false; this.running = false;
if (this.handshakeHandler instanceof Lifecycle) { if (this.handshakeHandler instanceof Lifecycle lifecycle) {
((Lifecycle) this.handshakeHandler).stop(); lifecycle.stop();
} }
} }
} }

View File

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

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { public void doWithRequest(ClientHttpRequest request) throws IOException {
request.getHeaders().putAll(this.headers); request.getHeaders().putAll(this.headers);
if (this.body != null) { if (this.body != null) {
if (request instanceof StreamingHttpOutputMessage) { if (request instanceof StreamingHttpOutputMessage streamingOutputMessage) {
((StreamingHttpOutputMessage) request).setBody(outputStream -> streamingOutputMessage.setBody(outputStream ->
StreamUtils.copy(this.body, SockJsFrame.CHARSET, outputStream)); StreamUtils.copy(this.body, SockJsFrame.CHARSET, outputStream));
} }
else { else {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public void start() { public void start() {
if (!isRunning()) { if (!isRunning()) {
if (this.webSocketClient instanceof Lifecycle) { if (this.webSocketClient instanceof Lifecycle lifecycle) {
((Lifecycle) this.webSocketClient).start(); lifecycle.start();
} }
else { else {
this.running = true; this.running = true;
@ -107,8 +107,8 @@ public class WebSocketTransport implements Transport, Lifecycle {
@Override @Override
public void stop() { public void stop() {
if (isRunning()) { if (isRunning()) {
if (this.webSocketClient instanceof Lifecycle) { if (this.webSocketClient instanceof Lifecycle lifecycle) {
((Lifecycle) this.webSocketClient).stop(); lifecycle.stop();
} }
else { else {
this.running = false; this.running = false;
@ -118,8 +118,8 @@ public class WebSocketTransport implements Transport, Lifecycle {
@Override @Override
public boolean isRunning() { public boolean isRunning() {
if (this.webSocketClient instanceof Lifecycle) { if (this.webSocketClient instanceof Lifecycle lifecycle) {
return ((Lifecycle) this.webSocketClient).isRunning(); return lifecycle.isRunning();
} }
else { else {
return this.running; return this.running;

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -91,8 +91,8 @@ public class SockJsHttpRequestHandler
@Override @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
if (this.sockJsService instanceof ServletContextAware) { if (this.sockJsService instanceof ServletContextAware servletContextAware) {
((ServletContextAware) this.sockJsService).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }
@ -101,8 +101,8 @@ public class SockJsHttpRequestHandler
public void start() { public void start() {
if (!isRunning()) { if (!isRunning()) {
this.running = true; this.running = true;
if (this.sockJsService instanceof Lifecycle) { if (this.sockJsService instanceof Lifecycle lifecycle) {
((Lifecycle) this.sockJsService).start(); lifecycle.start();
} }
} }
} }
@ -111,8 +111,8 @@ public class SockJsHttpRequestHandler
public void stop() { public void stop() {
if (isRunning()) { if (isRunning()) {
this.running = false; this.running = false;
if (this.sockJsService instanceof Lifecycle) { if (this.sockJsService instanceof Lifecycle lifecycle) {
((Lifecycle) this.sockJsService).stop(); lifecycle.stop();
} }
} }
} }
@ -147,8 +147,8 @@ public class SockJsHttpRequestHandler
@Override @Override
@Nullable @Nullable
public CorsConfiguration getCorsConfiguration(HttpServletRequest request) { public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
if (this.sockJsService instanceof CorsConfigurationSource) { if (this.sockJsService instanceof CorsConfigurationSource ccs) {
return ((CorsConfigurationSource) this.sockJsService).getCorsConfiguration(request); return ccs.getCorsConfiguration(request);
} }
return null; return null;
} }

View File

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

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
for (TransportHandler handler : getTransportHandlers().values()) { for (TransportHandler handler : getTransportHandlers().values()) {
if (handler instanceof ServletContextAware) { if (handler instanceof ServletContextAware servletContextAware) {
((ServletContextAware) handler).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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; this.sockJsSession = sockJsSession;
webSocketHandler = WebSocketHandlerDecorator.unwrap(webSocketHandler); webSocketHandler = WebSocketHandlerDecorator.unwrap(webSocketHandler);
this.subProtocols = ((webSocketHandler instanceof SubProtocolCapable) ? this.subProtocols = ((webSocketHandler instanceof SubProtocolCapable subProtocolCapable) ?
new ArrayList<>(((SubProtocolCapable) webSocketHandler).getSubProtocols()) : Collections.emptyList()); new ArrayList<>(subProtocolCapable.getSubProtocols()) : Collections.emptyList());
} }
@Override @Override

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,8 +73,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
@Override @Override
public void setServletContext(ServletContext servletContext) { public void setServletContext(ServletContext servletContext) {
if (this.handshakeHandler instanceof ServletContextAware) { if (this.handshakeHandler instanceof ServletContextAware servletContextAware) {
((ServletContextAware) this.handshakeHandler).setServletContext(servletContext); servletContextAware.setServletContext(servletContext);
} }
} }
@ -83,8 +83,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
public void start() { public void start() {
if (!isRunning()) { if (!isRunning()) {
this.running = true; this.running = true;
if (this.handshakeHandler instanceof Lifecycle) { if (this.handshakeHandler instanceof Lifecycle lifecycle) {
((Lifecycle) this.handshakeHandler).start(); lifecycle.start();
} }
} }
} }
@ -93,8 +93,8 @@ public class WebSocketTransportHandler extends AbstractTransportHandler
public void stop() { public void stop() {
if (isRunning()) { if (isRunning()) {
this.running = false; this.running = false;
if (this.handshakeHandler instanceof Lifecycle) { if (this.handshakeHandler instanceof Lifecycle lifecycle) {
((Lifecycle) this.handshakeHandler).stop(); lifecycle.stop();
} }
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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) { private void disableShallowEtagHeaderFilter(ServerHttpRequest request) {
if (request instanceof ServletServerHttpRequest) { if (request instanceof ServletServerHttpRequest servletServerHttpRequest) {
ServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); ServletRequest servletRequest = servletServerHttpRequest.getServletRequest();
ShallowEtagHeaderFilter.disableContentCaching(servletRequest); ShallowEtagHeaderFilter.disableContentCaching(servletRequest);
} }
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 @Override
public Object getNativeSession() { public Object getNativeSession() {
Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized"); Assert.state(this.webSocketSession != null, "WebSocketSession not yet initialized");
return (this.webSocketSession instanceof NativeWebSocketSession ? return (this.webSocketSession instanceof NativeWebSocketSession nativeWsSession ?
((NativeWebSocketSession) this.webSocketSession).getNativeSession() : this.webSocketSession); nativeWsSession.getNativeSession() : this.webSocketSession);
} }
@Override @Override
@Nullable @Nullable
public <T> T getNativeSession(@Nullable Class<T> requiredType) { public <T> T getNativeSession(@Nullable Class<T> requiredType) {
return (this.webSocketSession instanceof NativeWebSocketSession ? return (this.webSocketSession instanceof NativeWebSocketSession nativeWsSession ?
((NativeWebSocketSession) this.webSocketSession).getNativeSession(requiredType) : null); nativeWsSession.getNativeSession(requiredType) : null);
} }