001/*
002 * Copyright 2002-2017 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.reactive.config;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Objects;
022import java.util.function.Function;
023import java.util.stream.Collectors;
024
025import org.springframework.format.FormatterRegistry;
026import org.springframework.http.codec.ServerCodecConfigurer;
027import org.springframework.lang.Nullable;
028import org.springframework.util.CollectionUtils;
029import org.springframework.validation.MessageCodesResolver;
030import org.springframework.validation.Validator;
031import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
032import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
033
034/**
035 * A {@link WebFluxConfigurer} that delegates to one or more others.
036 *
037 * @author Brian Clozel
038 * @author Rossen Stoyanchev
039 * @since 5.0
040 */
041public class WebFluxConfigurerComposite implements WebFluxConfigurer {
042
043        private final List<WebFluxConfigurer> delegates = new ArrayList<>();
044
045
046        public void addWebFluxConfigurers(List<WebFluxConfigurer> configurers) {
047                if (!CollectionUtils.isEmpty(configurers)) {
048                        this.delegates.addAll(configurers);
049                }
050        }
051
052
053        @Override
054        public void configureContentTypeResolver(RequestedContentTypeResolverBuilder builder) {
055                this.delegates.forEach(delegate -> delegate.configureContentTypeResolver(builder));
056        }
057
058        @Override
059        public void addCorsMappings(CorsRegistry registry) {
060                this.delegates.forEach(delegate -> delegate.addCorsMappings(registry));
061        }
062
063        @Override
064        public void configurePathMatching(PathMatchConfigurer configurer) {
065                this.delegates.forEach(delegate -> delegate.configurePathMatching(configurer));
066        }
067
068        @Override
069        public void addResourceHandlers(ResourceHandlerRegistry registry) {
070                this.delegates.forEach(delegate -> delegate.addResourceHandlers(registry));
071        }
072
073        @Override
074        public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
075                this.delegates.forEach(delegate -> delegate.configureArgumentResolvers(configurer));
076        }
077
078        @Override
079        public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
080                this.delegates.forEach(delegate -> delegate.configureHttpMessageCodecs(configurer));
081        }
082
083        @Override
084        public void addFormatters(FormatterRegistry registry) {
085                this.delegates.forEach(delegate -> delegate.addFormatters(registry));
086        }
087
088        @Override
089        public Validator getValidator() {
090                return createSingleBean(WebFluxConfigurer::getValidator, Validator.class);
091        }
092
093        @Override
094        public MessageCodesResolver getMessageCodesResolver() {
095                return createSingleBean(WebFluxConfigurer::getMessageCodesResolver, MessageCodesResolver.class);
096        }
097
098        @Override
099        public void configureViewResolvers(ViewResolverRegistry registry) {
100                this.delegates.forEach(delegate -> delegate.configureViewResolvers(registry));
101        }
102
103        @Nullable
104        private <T> T createSingleBean(Function<WebFluxConfigurer, T> factory, Class<T> beanType) {
105                List<T> result = this.delegates.stream().map(factory).filter(Objects::nonNull).collect(Collectors.toList());
106                if (result.isEmpty()) {
107                        return null;
108                }
109                else if (result.size() == 1) {
110                        return result.get(0);
111                }
112                else {
113                        throw new IllegalStateException("More than one WebFluxConfigurer implements " +
114                                        beanType.getSimpleName() + " factory method.");
115                }
116        }
117
118}