001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.socket.config.annotation; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.beans.factory.annotation.Autowired; 023import org.springframework.context.annotation.Configuration; 024import org.springframework.messaging.converter.MessageConverter; 025import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver; 026import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler; 027import org.springframework.messaging.simp.config.ChannelRegistration; 028import org.springframework.messaging.simp.config.MessageBrokerRegistry; 029import org.springframework.util.CollectionUtils; 030 031/** 032 * A {@link WebSocketMessageBrokerConfigurationSupport} extension that detects 033 * beans of type {@link WebSocketMessageBrokerConfigurer} and delegates to all 034 * of them allowing callback style customization of the configuration provided 035 * in {@link WebSocketMessageBrokerConfigurationSupport}. 036 * 037 * <p>This class is typically imported via {@link EnableWebSocketMessageBroker}. 038 * 039 * @author Rossen Stoyanchev 040 * @since 4.0 041 */ 042@Configuration 043public class DelegatingWebSocketMessageBrokerConfiguration extends WebSocketMessageBrokerConfigurationSupport { 044 045 private final List<WebSocketMessageBrokerConfigurer> configurers = new ArrayList<>(); 046 047 048 @Autowired(required = false) 049 public void setConfigurers(List<WebSocketMessageBrokerConfigurer> configurers) { 050 if (!CollectionUtils.isEmpty(configurers)) { 051 this.configurers.addAll(configurers); 052 } 053 } 054 055 056 @Override 057 protected void registerStompEndpoints(StompEndpointRegistry registry) { 058 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 059 configurer.registerStompEndpoints(registry); 060 } 061 } 062 063 @Override 064 protected void configureWebSocketTransport(WebSocketTransportRegistration registration) { 065 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 066 configurer.configureWebSocketTransport(registration); 067 } 068 } 069 070 @Override 071 protected void configureClientInboundChannel(ChannelRegistration registration) { 072 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 073 configurer.configureClientInboundChannel(registration); 074 } 075 } 076 077 @Override 078 protected void configureClientOutboundChannel(ChannelRegistration registration) { 079 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 080 configurer.configureClientOutboundChannel(registration); 081 } 082 } 083 084 @Override 085 protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { 086 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 087 configurer.addArgumentResolvers(argumentResolvers); 088 } 089 } 090 091 @Override 092 protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) { 093 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 094 configurer.addReturnValueHandlers(returnValueHandlers); 095 } 096 } 097 098 @Override 099 protected boolean configureMessageConverters(List<MessageConverter> messageConverters) { 100 boolean registerDefaults = true; 101 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 102 if (!configurer.configureMessageConverters(messageConverters)) { 103 registerDefaults = false; 104 } 105 } 106 return registerDefaults; 107 } 108 109 @Override 110 protected void configureMessageBroker(MessageBrokerRegistry registry) { 111 for (WebSocketMessageBrokerConfigurer configurer : this.configurers) { 112 configurer.configureMessageBroker(registry); 113 } 114 } 115 116}