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;
018
019import org.glassfish.jersey.server.ResourceConfig;
020
021import org.springframework.boot.actuate.autoconfigure.endpoint.ExposeExcludePropertyEndpointFilter;
022import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
023import org.springframework.boot.actuate.endpoint.web.ExposableServletEndpoint;
024import org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar;
025import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
029import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
030import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath;
031import org.springframework.boot.autoconfigure.web.servlet.JerseyApplicationPath;
032import org.springframework.context.ApplicationContext;
033import org.springframework.context.annotation.Bean;
034import org.springframework.context.annotation.Configuration;
035import org.springframework.web.servlet.DispatcherServlet;
036
037/**
038 * {@link ManagementContextConfiguration} for servlet endpoints.
039 *
040 * @author Phillip Webb
041 * @author Andy Wilkinson
042 * @author Madhura Bhave
043 * @since 2.0.0
044 */
045@ManagementContextConfiguration
046@ConditionalOnWebApplication(type = Type.SERVLET)
047public class ServletEndpointManagementContextConfiguration {
048
049        @Bean
050        public ExposeExcludePropertyEndpointFilter<ExposableServletEndpoint> servletExposeExcludePropertyEndpointFilter(
051                        WebEndpointProperties properties) {
052                WebEndpointProperties.Exposure exposure = properties.getExposure();
053                return new ExposeExcludePropertyEndpointFilter<>(ExposableServletEndpoint.class,
054                                exposure.getInclude(), exposure.getExclude());
055        }
056
057        @Configuration
058        @ConditionalOnClass(DispatcherServlet.class)
059        public static class WebMvcServletEndpointManagementContextConfiguration {
060
061                private final ApplicationContext context;
062
063                public WebMvcServletEndpointManagementContextConfiguration(
064                                ApplicationContext context) {
065                        this.context = context;
066                }
067
068                @Bean
069                public ServletEndpointRegistrar servletEndpointRegistrar(
070                                WebEndpointProperties properties,
071                                ServletEndpointsSupplier servletEndpointsSupplier) {
072                        DispatcherServletPath dispatcherServletPath = this.context
073                                        .getBean(DispatcherServletPath.class);
074                        return new ServletEndpointRegistrar(
075                                        dispatcherServletPath.getRelativePath(properties.getBasePath()),
076                                        servletEndpointsSupplier.getEndpoints());
077                }
078
079        }
080
081        @Configuration
082        @ConditionalOnClass(ResourceConfig.class)
083        @ConditionalOnMissingClass("org.springframework.web.servlet.DispatcherServlet")
084        public static class JerseyServletEndpointManagementContextConfiguration {
085
086                private final ApplicationContext context;
087
088                public JerseyServletEndpointManagementContextConfiguration(
089                                ApplicationContext context) {
090                        this.context = context;
091                }
092
093                @Bean
094                public ServletEndpointRegistrar servletEndpointRegistrar(
095                                WebEndpointProperties properties,
096                                ServletEndpointsSupplier servletEndpointsSupplier) {
097                        JerseyApplicationPath jerseyApplicationPath = this.context
098                                        .getBean(JerseyApplicationPath.class);
099                        return new ServletEndpointRegistrar(
100                                        jerseyApplicationPath.getRelativePath(properties.getBasePath()),
101                                        servletEndpointsSupplier.getEndpoints());
102                }
103
104        }
105
106}