|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.boot.autoconfigure.rsocket; |
| 18 | + |
| 19 | +import java.util.stream.Collectors; |
| 20 | + |
| 21 | +import io.netty.buffer.PooledByteBufAllocator; |
| 22 | +import io.rsocket.RSocketFactory; |
| 23 | + |
| 24 | +import org.springframework.beans.factory.ObjectProvider; |
| 25 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 26 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 27 | +import org.springframework.boot.autoconfigure.condition.AllNestedConditions; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 32 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 34 | +import org.springframework.boot.context.properties.PropertyMapper; |
| 35 | +import org.springframework.boot.rsocket.netty.NettyRSocketBootstrap; |
| 36 | +import org.springframework.boot.rsocket.netty.NettyRSocketServerFactory; |
| 37 | +import org.springframework.boot.rsocket.server.RSocketServerFactory; |
| 38 | +import org.springframework.boot.rsocket.server.ServerRSocketFactoryCustomizer; |
| 39 | +import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory; |
| 40 | +import org.springframework.boot.web.server.WebServerFactoryCustomizer; |
| 41 | +import org.springframework.context.annotation.Bean; |
| 42 | +import org.springframework.context.annotation.Conditional; |
| 43 | +import org.springframework.context.annotation.Configuration; |
| 44 | +import org.springframework.http.client.reactive.ReactorResourceFactory; |
| 45 | +import org.springframework.messaging.rsocket.MessageHandlerAcceptor; |
| 46 | +import org.springframework.messaging.rsocket.RSocketStrategies; |
| 47 | + |
| 48 | +/** |
| 49 | + * {@link EnableAutoConfiguration Auto-configuration} for RSocket servers. In the case of |
| 50 | + * {@link org.springframework.boot.WebApplicationType#REACTIVE}, the RSocket server is |
| 51 | + * added as a WebSocket endpoint on the existing |
| 52 | + * {@link org.springframework.boot.web.embedded.netty.NettyWebServer}. If a specific |
| 53 | + * server port is configured, a new standalone RSocket server is created. |
| 54 | + * |
| 55 | + * @author Brian Clozel |
| 56 | + * @since 2.2.0 |
| 57 | + */ |
| 58 | +@Configuration(proxyBeanMethods = false) |
| 59 | +@ConditionalOnClass({ RSocketFactory.class, RSocketStrategies.class, |
| 60 | + PooledByteBufAllocator.class }) |
| 61 | +@ConditionalOnBean(MessageHandlerAcceptor.class) |
| 62 | +@AutoConfigureAfter(RSocketStrategiesAutoConfiguration.class) |
| 63 | +@EnableConfigurationProperties(RSocketProperties.class) |
| 64 | +public class RSocketServerAutoConfiguration { |
| 65 | + |
| 66 | + @Conditional(OnRSocketWebServerCondition.class) |
| 67 | + @Configuration(proxyBeanMethods = false) |
| 68 | + static class WebFluxServerAutoConfiguration { |
| 69 | + |
| 70 | + @Bean |
| 71 | + public WebServerFactoryCustomizer<NettyReactiveWebServerFactory> rSocketWebsocketCustomizer( |
| 72 | + RSocketProperties properties, |
| 73 | + MessageHandlerAcceptor messageHandlerAcceptor) { |
| 74 | + RSocketNettyServerCustomizer customizer = new RSocketNettyServerCustomizer( |
| 75 | + properties.getServer().getMappingPath(), messageHandlerAcceptor); |
| 76 | + return (factory) -> factory.addServerCustomizers(customizer); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "port") |
| 82 | + @Configuration(proxyBeanMethods = false) |
| 83 | + static class EmbeddedServerAutoConfiguration { |
| 84 | + |
| 85 | + @Bean |
| 86 | + @ConditionalOnMissingBean |
| 87 | + public ReactorResourceFactory reactorServerResourceFactory() { |
| 88 | + return new ReactorResourceFactory(); |
| 89 | + } |
| 90 | + |
| 91 | + @Bean |
| 92 | + @ConditionalOnMissingBean |
| 93 | + public RSocketServerFactory rSocketServerFactory(RSocketProperties properties, |
| 94 | + ReactorResourceFactory resourceFactory, |
| 95 | + ObjectProvider<ServerRSocketFactoryCustomizer> customizers) { |
| 96 | + NettyRSocketServerFactory factory = new NettyRSocketServerFactory(); |
| 97 | + factory.setResourceFactory(resourceFactory); |
| 98 | + PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); |
| 99 | + map.from(properties.getServer().getAddress()).to(factory::setAddress); |
| 100 | + map.from(properties.getServer().getPort()).to(factory::setPort); |
| 101 | + factory.setServerCustomizers( |
| 102 | + customizers.orderedStream().collect(Collectors.toList())); |
| 103 | + return factory; |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public NettyRSocketBootstrap nettyRSocketBootstrap( |
| 108 | + RSocketServerFactory rSocketServerFactory, |
| 109 | + MessageHandlerAcceptor messageHandlerAcceptor) { |
| 110 | + return new NettyRSocketBootstrap(rSocketServerFactory, |
| 111 | + messageHandlerAcceptor); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + static class OnRSocketWebServerCondition extends AllNestedConditions { |
| 117 | + |
| 118 | + OnRSocketWebServerCondition() { |
| 119 | + super(ConfigurationPhase.REGISTER_BEAN); |
| 120 | + } |
| 121 | + |
| 122 | + @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) |
| 123 | + static class IsReactiveWebApplication { |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "port", |
| 128 | + matchIfMissing = true) |
| 129 | + static class HasNoPortConfigured { |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "mapping-path") |
| 134 | + static class HasMappingPathConfigured { |
| 135 | + |
| 136 | + } |
| 137 | + |
| 138 | + @ConditionalOnProperty(prefix = "spring.rsocket.server", name = "transport", |
| 139 | + havingValue = "websocket") |
| 140 | + static class HasWebsocketTransportConfigured { |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments