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.servlet;
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.annotation.ServletEndpointsSupplier;
035import org.springframework.boot.actuate.endpoint.web.servlet.ControllerEndpointHandlerMapping;
036import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
037import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
038import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
039import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
040import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
041import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
042import org.springframework.boot.context.properties.EnableConfigurationProperties;
043import org.springframework.context.annotation.Bean;
044import org.springframework.web.servlet.DispatcherServlet;
045
046/**
047 * {@link ManagementContextConfiguration} for Spring MVC {@link Endpoint} concerns.
048 *
049 * @author Andy Wilkinson
050 * @author Phillip Webb
051 * @since 2.0.0
052 */
053@ManagementContextConfiguration
054@ConditionalOnWebApplication(type = Type.SERVLET)
055@ConditionalOnClass(DispatcherServlet.class)
056@ConditionalOnBean({ DispatcherServlet.class, WebEndpointsSupplier.class })
057@EnableConfigurationProperties(CorsEndpointProperties.class)
058public class WebMvcEndpointManagementContextConfiguration {
059
060        @Bean
061        @ConditionalOnMissingBean
062        public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
063                        WebEndpointsSupplier webEndpointsSupplier,
064                        ServletEndpointsSupplier servletEndpointsSupplier,
065                        ControllerEndpointsSupplier controllerEndpointsSupplier,
066                        EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties,
067                        WebEndpointProperties webEndpointProperties) {
068                List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
069                Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier
070                                .getEndpoints();
071                allEndpoints.addAll(webEndpoints);
072                allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
073                allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
074                EndpointMapping endpointMapping = new EndpointMapping(
075                                webEndpointProperties.getBasePath());
076                return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints,
077                                endpointMediaTypes, corsProperties.toCorsConfiguration(),
078                                new EndpointLinksResolver(allEndpoints,
079                                                webEndpointProperties.getBasePath()));
080        }
081
082        @Bean
083        @ConditionalOnMissingBean
084        public ControllerEndpointHandlerMapping controllerEndpointHandlerMapping(
085                        ControllerEndpointsSupplier controllerEndpointsSupplier,
086                        CorsEndpointProperties corsProperties,
087                        WebEndpointProperties webEndpointProperties) {
088                EndpointMapping endpointMapping = new EndpointMapping(
089                                webEndpointProperties.getBasePath());
090                return new ControllerEndpointHandlerMapping(endpointMapping,
091                                controllerEndpointsSupplier.getEndpoints(),
092                                corsProperties.toCorsConfiguration());
093        }
094
095}