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.jersey;
018
019import org.glassfish.jersey.server.ResourceConfig;
020import org.glassfish.jersey.servlet.ServletContainer;
021
022import org.springframework.beans.factory.ObjectProvider;
023import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
024import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
026import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
029import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
030import org.springframework.boot.web.servlet.ServletRegistrationBean;
031import org.springframework.context.annotation.Bean;
032
033/**
034 * {@link ManagementContextConfiguration} for Jersey infrastructure when a separate
035 * management context with a web server running on a different port is required.
036 *
037 * @author Stephane Nicoll
038 * @author Andy Wilkinson
039 * @author Phillip Webb
040 * @since 2.0.0
041 */
042@ManagementContextConfiguration(ManagementContextType.CHILD)
043@ConditionalOnWebApplication(type = Type.SERVLET)
044@ConditionalOnClass(ResourceConfig.class)
045@ConditionalOnMissingClass("org.springframework.web.servlet.DispatcherServlet")
046public class JerseyManagementChildContextConfiguration {
047
048        private final ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers;
049
050        public JerseyManagementChildContextConfiguration(
051                        ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers) {
052                this.resourceConfigCustomizers = resourceConfigCustomizers;
053        }
054
055        @Bean
056        public ServletRegistrationBean<ServletContainer> jerseyServletRegistration() {
057                return new ServletRegistrationBean<>(
058                                new ServletContainer(endpointResourceConfig()), "/*");
059        }
060
061        @Bean
062        public ResourceConfig endpointResourceConfig() {
063                ResourceConfig resourceConfig = new ResourceConfig();
064                this.resourceConfigCustomizers.orderedStream()
065                                .forEach((customizer) -> customizer.customize(resourceConfig));
066                return resourceConfig;
067        }
068
069}