|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.websocket.config; |
| 18 | + |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.springframework.beans.BeansException; |
| 24 | +import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; |
| 25 | +import org.springframework.context.ApplicationContext; |
| 26 | +import org.springframework.context.ApplicationContextAware; |
| 27 | +import org.springframework.integration.websocket.ServerWebSocketContainer; |
| 28 | +import org.springframework.scheduling.TaskScheduler; |
| 29 | +import org.springframework.util.CollectionUtils; |
| 30 | +import org.springframework.util.MultiValueMap; |
| 31 | +import org.springframework.web.HttpRequestHandler; |
| 32 | +import org.springframework.web.servlet.handler.AbstractHandlerMapping; |
| 33 | +import org.springframework.web.socket.WebSocketHandler; |
| 34 | +import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistration; |
| 35 | +import org.springframework.web.socket.config.annotation.ServletWebSocketHandlerRegistry; |
| 36 | +import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration; |
| 37 | + |
| 38 | +/** |
| 39 | + * The {@link ServletWebSocketHandlerRegistry} extension for Spring Integration purpose, especially |
| 40 | + * a dynamic WebSocket endpoint registrations. |
| 41 | + * |
| 42 | + * @author Artem Bilan |
| 43 | + * |
| 44 | + * @since 5.5 |
| 45 | + */ |
| 46 | +class IntegrationServletWebSocketHandlerRegistry extends ServletWebSocketHandlerRegistry |
| 47 | + implements ApplicationContextAware, DestructionAwareBeanPostProcessor { |
| 48 | + |
| 49 | + private final Map<WebSocketHandler, List<String>> dynamicRegistrations = new HashMap<>(); |
| 50 | + |
| 51 | + private ApplicationContext applicationContext; |
| 52 | + |
| 53 | + private volatile IntegrationDynamicWebSocketHandlerMapping dynamicHandlerMapping; |
| 54 | + |
| 55 | + IntegrationServletWebSocketHandlerRegistry() { |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 60 | + this.applicationContext = applicationContext; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + @Override |
| 65 | + protected boolean requiresTaskScheduler() { // NOSONAR visibility |
| 66 | + return super.requiresTaskScheduler(); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void setTaskScheduler(TaskScheduler scheduler) { // NOSONAR visibility |
| 71 | + super.setTaskScheduler(scheduler); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public AbstractHandlerMapping getHandlerMapping() { |
| 76 | + AbstractHandlerMapping originHandlerMapping = super.getHandlerMapping(); |
| 77 | + originHandlerMapping.setApplicationContext(this.applicationContext); |
| 78 | + this.dynamicHandlerMapping = this.applicationContext.getBean(IntegrationDynamicWebSocketHandlerMapping.class); |
| 79 | + return originHandlerMapping; |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public WebSocketHandlerRegistration addHandler(WebSocketHandler handler, String... paths) { |
| 84 | + if (this.dynamicHandlerMapping != null) { |
| 85 | + IntegrationDynamicWebSocketHandlerRegistration registration = |
| 86 | + new IntegrationDynamicWebSocketHandlerRegistration(); |
| 87 | + registration.addHandler(handler, paths); |
| 88 | + MultiValueMap<HttpRequestHandler, String> mappings = registration.getMapping(); |
| 89 | + for (Map.Entry<HttpRequestHandler, List<String>> entry : mappings.entrySet()) { |
| 90 | + HttpRequestHandler httpHandler = entry.getKey(); |
| 91 | + List<String> patterns = entry.getValue(); |
| 92 | + this.dynamicRegistrations.put(handler, patterns); |
| 93 | + for (String pattern : patterns) { |
| 94 | + this.dynamicHandlerMapping.registerHandler(pattern, httpHandler); |
| 95 | + } |
| 96 | + } |
| 97 | + return registration; |
| 98 | + } |
| 99 | + else { |
| 100 | + return super.addHandler(handler, paths); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { |
| 106 | + if (this.dynamicHandlerMapping != null && bean instanceof ServerWebSocketContainer) { |
| 107 | + ((ServerWebSocketContainer) bean).registerWebSocketHandlers(this); |
| 108 | + } |
| 109 | + return bean; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean requiresDestruction(Object bean) { |
| 114 | + return bean instanceof ServerWebSocketContainer; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException { |
| 119 | + if (requiresDestruction(bean)) { |
| 120 | + removeRegistration((ServerWebSocketContainer) bean); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + void removeRegistration(ServerWebSocketContainer serverWebSocketContainer) { |
| 125 | + List<String> patterns = this.dynamicRegistrations.remove(serverWebSocketContainer.getWebSocketHandler()); |
| 126 | + if (this.dynamicHandlerMapping != null && !CollectionUtils.isEmpty(patterns)) { |
| 127 | + for (String pattern : patterns) { |
| 128 | + this.dynamicHandlerMapping.unregisterHandler(pattern); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static final class IntegrationDynamicWebSocketHandlerRegistration |
| 134 | + extends ServletWebSocketHandlerRegistration { |
| 135 | + |
| 136 | + MultiValueMap<HttpRequestHandler, String> getMapping() { |
| 137 | + return getMappings(); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments