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.web.servlet;
018
019import javax.servlet.Servlet;
020
021import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
022import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
027import org.springframework.boot.web.servlet.filter.ApplicationContextHeaderFilter;
028import org.springframework.context.ApplicationContext;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031
032/**
033 * {@link EnableAutoConfiguration Auto-configuration} for Servlet-specific management
034 * context concerns.
035 *
036 * @author Phillip Webb
037 * @since 2.0.0
038 */
039@Configuration
040@ConditionalOnClass(Servlet.class)
041@ConditionalOnWebApplication(type = Type.SERVLET)
042public class ServletManagementContextAutoConfiguration {
043
044        @Bean
045        public ServletManagementContextFactory servletWebChildContextFactory() {
046                return new ServletManagementContextFactory();
047        }
048
049        @Bean
050        public ManagementServletContext managementServletContext(
051                        WebEndpointProperties properties) {
052                return properties::getBasePath;
053        }
054
055        // Put Servlets and Filters in their own nested class so they don't force early
056        // instantiation of ManagementServerProperties.
057        @Configuration
058        @ConditionalOnProperty(prefix = "management.server", name = "add-application-context-header", havingValue = "true")
059        protected static class ApplicationContextFilterConfiguration {
060
061                @Bean
062                public ApplicationContextHeaderFilter applicationContextIdFilter(
063                                ApplicationContext context) {
064                        return new ApplicationContextHeaderFilter(context);
065                }
066
067        }
068
069}