|
1 | 1 | /*
|
2 |
| - * Copyright 2021 the original author or authors. |
| 2 | + * Copyright 2021-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
34 | 34 | import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistration;
|
35 | 35 | import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry;
|
36 | 36 | import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
|
| 37 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry; |
37 | 38 |
|
38 | 39 | /**
|
39 | 40 | * The {@link ServletWebSocketHandlerRegistry} extension for Spring Integration purpose, especially
|
|
46 | 47 | class IntegrationServletWebSocketHandlerRegistry extends ServletWebSocketHandlerRegistry
|
47 | 48 | implements ApplicationContextAware, DestructionAwareBeanPostProcessor {
|
48 | 49 |
|
49 |
| - private final ThreadLocal<IntegrationDynamicWebSocketHandlerRegistration> currentRegistration = new ThreadLocal<>(); |
50 |
| - |
51 | 50 | private final Map<WebSocketHandler, List<String>> dynamicRegistrations = new HashMap<>();
|
52 | 51 |
|
53 | 52 | private ApplicationContext applicationContext;
|
@@ -78,30 +77,17 @@ public AbstractHandlerMapping getHandlerMapping() {
|
78 | 77 | return originHandlerMapping;
|
79 | 78 | }
|
80 | 79 |
|
81 |
| - @Override |
82 |
| - public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) { |
83 |
| - if (this.dynamicHandlerMapping != null) { |
84 |
| - IntegrationDynamicWebSocketHandlerRegistration registration = |
85 |
| - new IntegrationDynamicWebSocketHandlerRegistration(); |
86 |
| - registration.addHandler(handler, paths); |
87 |
| - this.currentRegistration.set(registration); |
88 |
| - return registration; |
89 |
| - } |
90 |
| - else { |
91 |
| - return super.addHandler(handler, paths); |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 | 80 | @Override
|
96 | 81 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
97 |
| - if (this.dynamicHandlerMapping != null && bean instanceof ServerWebSocketContainer) { |
98 |
| - ServerWebSocketContainer serverWebSocketContainer = (ServerWebSocketContainer) bean; |
| 82 | + if (this.dynamicHandlerMapping != null && bean instanceof ServerWebSocketContainer serverWebSocketContainer) { |
99 | 83 | if (serverWebSocketContainer.getSockJsTaskScheduler() == null) {
|
100 | 84 | serverWebSocketContainer.setSockJsTaskScheduler(this.sockJsTaskScheduler);
|
101 | 85 | }
|
102 |
| - serverWebSocketContainer.registerWebSocketHandlers(this); |
103 |
| - IntegrationDynamicWebSocketHandlerRegistration registration = this.currentRegistration.get(); |
104 |
| - this.currentRegistration.remove(); |
| 86 | + |
| 87 | + DynamicHandlerRegistrationProxy dynamicHandlerRegistrationProxy = new DynamicHandlerRegistrationProxy(); |
| 88 | + |
| 89 | + serverWebSocketContainer.registerWebSocketHandlers(dynamicHandlerRegistrationProxy); |
| 90 | + IntegrationDynamicWebSocketHandlerRegistration registration = dynamicHandlerRegistrationProxy.registration; |
105 | 91 | MultiValueMap<HttpRequestHandler, String> mappings = registration.getMapping();
|
106 | 92 | for (Map.Entry<HttpRequestHandler, List<String>> entry : mappings.entrySet()) {
|
107 | 93 | HttpRequestHandler httpHandler = entry.getKey();
|
@@ -136,11 +122,30 @@ void removeRegistration(ServerWebSocketContainer serverWebSocketContainer) {
|
136 | 122 | }
|
137 | 123 | }
|
138 | 124 |
|
| 125 | + private static final class DynamicHandlerRegistrationProxy implements WebSocketHandlerRegistry { |
| 126 | + |
| 127 | + private IntegrationDynamicWebSocketHandlerRegistration registration; |
| 128 | + |
| 129 | + DynamicHandlerRegistrationProxy() { |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public WebSocketHandlerRegistration addHandler(WebSocketHandler webSocketHandler, String... paths) { |
| 134 | + this.registration = new IntegrationDynamicWebSocketHandlerRegistration(); |
| 135 | + this.registration.addHandler(webSocketHandler, paths); |
| 136 | + return this.registration; |
| 137 | + } |
| 138 | + |
| 139 | + } |
| 140 | + |
139 | 141 | private static final class IntegrationDynamicWebSocketHandlerRegistration
|
140 | 142 | extends ServletWebSocketHandlerRegistration {
|
141 | 143 |
|
142 | 144 | private WebSocketHandler handler;
|
143 | 145 |
|
| 146 | + IntegrationDynamicWebSocketHandlerRegistration() { |
| 147 | + } |
| 148 | + |
144 | 149 | @Override
|
145 | 150 | public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) {
|
146 | 151 | // The IntegrationWebSocketContainer comes only with a single WebSocketHandler
|
|
0 commit comments