001/*
002 * Copyright 2012-2018 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 *      http://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.boot.actuate.autoconfigure.endpoint.web.reactive;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.List;
022
023import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
024import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
025import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
026import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
027import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
028import org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver;
029import org.springframework.boot.actuate.endpoint.web.EndpointMapping;
030import org.springframework.boot.actuate.endpoint.web.EndpointMediaTypes;
031import org.springframework.boot.actuate.endpoint.web.ExposableWebEndpoint;
032import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
033import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
034import org.springframework.boot.actuate.endpoint.web.reactive.ControllerEndpointHandlerMapping;
035import org.springframework.boot.actuate.endpoint.web.reactive.WebFluxEndpointHandlerMapping;
036import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
037import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
038import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
039import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
040import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
041import org.springframework.boot.context.properties.EnableConfigurationProperties;
042import org.springframework.context.annotation.Bean;
043import org.springframework.http.server.reactive.HttpHandler;
044import org.springframework.web.reactive.DispatcherHandler;
045
046/**
047 * {@link ManagementContextConfiguration} for Reactive {@link Endpoint} concerns.
048 *
049 * @author Andy Wilkinson
050 * @author Phillip Webb
051 * @since 2.0.0
052 */
053@ManagementContextConfiguration
054@ConditionalOnWebApplication(type = Type.REACTIVE)
055@ConditionalOnClass({ DispatcherHandler.class, HttpHandler.class })
056@ConditionalOnBean(WebEndpointsSupplier.class)
057@EnableConfigurationProperties(CorsEndpointProperties.class)
058public class WebFluxEndpointManagementContextConfiguration {
059
060        @Bean
061        @ConditionalOnMissingBean
062        public WebFluxEndpointHandlerMapping webEndpointReactiveHandlerMapping(
063                        WebEndpointsSupplier webEndpointsSupplier,
064                        ControllerEndpointsSupplier controllerEndpointsSupplier,
065                        EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
066                        WebEndpointProperties webEndpointProperties) {
067                EndpointMapping endpointMapping = new EndpointMapping(
068                                webEndpointProperties.getBasePath());
069                Collection<ExposableWebEndpoint> endpoints = webEndpointsSupplier.getEndpoints();
070                List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
071                allEndpoints.addAll(endpoints);
072                allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
073                return new WebFluxEndpointHandlerMapping(endpointMapping, endpoints,
074                                endpointMediaTypes, corsProperties.toCorsConfiguration(),
075                                new EndpointLinksResolver(allEndpoints,
076                                                webEndpointProperties.getBasePath()));
077        }
078
079        @Bean
080        @ConditionalOnMissingBean
081        public ControllerEndpointHandlerMapping controllerEndpointHandlerMapping(
082                        ControllerEndpointsSupplier controllerEndpointsSupplier,
083                        CorsEndpointProperties corsProperties,
084                        WebEndpointProperties webEndpointProperties) {
085                EndpointMapping endpointMapping = new EndpointMapping(
086                                webEndpointProperties.getBasePath());
087                return new ControllerEndpointHandlerMapping(endpointMapping,
088                                controllerEndpointsSupplier.getEndpoints(),
089                                corsProperties.toCorsConfiguration());
090        }
091
092}