Skip to content

Commit bbe555a

Browse files
committed
Split Jersey management context configuration
This commit splits the management context configuration for jersey into two separate configurations depending on if the type is SAME or CHILD. The configuration for the SAME context should only kick in if there is no existing ResourceConfig bean. Fixes gh-15891
1 parent b645e0a commit bbe555a

File tree

9 files changed

+388
-166
lines changed

9 files changed

+388
-166
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/jersey/JerseyWebEndpointManagementContextConfiguration.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,9 +23,7 @@
2323
import java.util.List;
2424

2525
import org.glassfish.jersey.server.ResourceConfig;
26-
import org.glassfish.jersey.servlet.ServletContainer;
2726

28-
import org.springframework.beans.factory.ObjectProvider;
2927
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
3028
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
3129
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
@@ -42,14 +40,8 @@
4240
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4341
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
4442
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
45-
import org.springframework.boot.autoconfigure.jersey.JerseyProperties;
4643
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
47-
import org.springframework.boot.autoconfigure.web.servlet.DefaultJerseyApplicationPath;
48-
import org.springframework.boot.autoconfigure.web.servlet.JerseyApplicationPath;
49-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
50-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5144
import org.springframework.context.annotation.Bean;
52-
import org.springframework.context.annotation.Configuration;
5345

5446
/**
5547
* {@link ManagementContextConfiguration} for Jersey {@link Endpoint} concerns.
@@ -88,37 +80,4 @@ public ResourceConfigCustomizer webEndpointRegistrar(
8880
};
8981
}
9082

91-
@Configuration
92-
@ConditionalOnMissingBean(ResourceConfig.class)
93-
@EnableConfigurationProperties(JerseyProperties.class)
94-
static class ResourceConfigConfiguration {
95-
96-
@Bean
97-
public ResourceConfig resourceConfig(
98-
ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers) {
99-
ResourceConfig resourceConfig = new ResourceConfig();
100-
resourceConfigCustomizers.orderedStream()
101-
.forEach((customizer) -> customizer.customize(resourceConfig));
102-
return resourceConfig;
103-
}
104-
105-
@Bean
106-
@ConditionalOnMissingBean
107-
public JerseyApplicationPath jerseyApplicationPath(JerseyProperties properties,
108-
ResourceConfig config) {
109-
return new DefaultJerseyApplicationPath(properties.getApplicationPath(),
110-
config);
111-
}
112-
113-
@Bean
114-
public ServletRegistrationBean<ServletContainer> jerseyServletRegistration(
115-
ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers,
116-
JerseyApplicationPath jerseyApplicationPath) {
117-
return new ServletRegistrationBean<>(
118-
new ServletContainer(resourceConfig(resourceConfigCustomizers)),
119-
jerseyApplicationPath.getUrlMapping());
120-
}
121-
122-
}
123-
12483
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,57 +13,35 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.boot.actuate.autoconfigure.web.jersey;
1817

1918
import org.glassfish.jersey.server.ResourceConfig;
20-
import org.glassfish.jersey.servlet.ServletContainer;
2119

22-
import org.springframework.beans.factory.ObjectProvider;
2320
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
2421
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
2522
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2623
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
2724
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
28-
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
29-
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
30-
import org.springframework.boot.web.servlet.ServletRegistrationBean;
25+
import org.springframework.boot.autoconfigure.web.servlet.JerseyApplicationPath;
3126
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Import;
3228

3329
/**
3430
* {@link ManagementContextConfiguration} for Jersey infrastructure when a separate
3531
* management context with a web server running on a different port is required.
3632
*
37-
* @author Stephane Nicoll
38-
* @author Andy Wilkinson
39-
* @author Phillip Webb
40-
* @since 2.0.0
33+
* @author Madhura Bhave
4134
*/
4235
@ManagementContextConfiguration(ManagementContextType.CHILD)
43-
@ConditionalOnWebApplication(type = Type.SERVLET)
36+
@Import(JerseyManagementContextConfiguration.class)
37+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
4438
@ConditionalOnClass(ResourceConfig.class)
4539
@ConditionalOnMissingClass("org.springframework.web.servlet.DispatcherServlet")
46-
public class JerseyManagementChildContextConfiguration {
47-
48-
private final ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers;
49-
50-
public JerseyManagementChildContextConfiguration(
51-
ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers) {
52-
this.resourceConfigCustomizers = resourceConfigCustomizers;
53-
}
54-
55-
@Bean
56-
public ServletRegistrationBean<ServletContainer> jerseyServletRegistration() {
57-
return new ServletRegistrationBean<>(
58-
new ServletContainer(endpointResourceConfig()), "/*");
59-
}
40+
public class JerseyChildManagementContextConfiguration {
6041

6142
@Bean
62-
public ResourceConfig endpointResourceConfig() {
63-
ResourceConfig resourceConfig = new ResourceConfig();
64-
this.resourceConfigCustomizers.orderedStream()
65-
.forEach((customizer) -> customizer.customize(resourceConfig));
66-
return resourceConfig;
43+
public JerseyApplicationPath jerseyApplicationPath() {
44+
return () -> "/";
6745
}
6846

6947
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.web.jersey;
17+
18+
import org.glassfish.jersey.server.ResourceConfig;
19+
import org.glassfish.jersey.servlet.ServletContainer;
20+
21+
import org.springframework.beans.factory.ObjectProvider;
22+
import org.springframework.boot.autoconfigure.jersey.ResourceConfigCustomizer;
23+
import org.springframework.boot.autoconfigure.web.servlet.JerseyApplicationPath;
24+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
25+
import org.springframework.context.annotation.Bean;
26+
27+
/**
28+
* Shared configuration for Jersey-based actuators regardless of management context type.
29+
*
30+
* @author Madhura Bhave
31+
*/
32+
class JerseyManagementContextConfiguration {
33+
34+
@Bean
35+
public ServletRegistrationBean<ServletContainer> jerseyServletRegistration(
36+
JerseyApplicationPath jerseyApplicationPath, ResourceConfig resourceConfig) {
37+
return new ServletRegistrationBean<>(new ServletContainer(resourceConfig),
38+
jerseyApplicationPath.getUrlMapping());
39+
}
40+
41+
@Bean
42+
public ResourceConfig resourceConfig(
43+
ObjectProvider<ResourceConfigCustomizer> resourceConfigCustomizers) {
44+
ResourceConfig resourceConfig = new ResourceConfig();
45+
resourceConfigCustomizers.orderedStream()
46+
.forEach((customizer) -> customizer.customize(resourceConfig));
47+
return resourceConfig;
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.actuate.autoconfigure.web.jersey;
17+
18+
import org.glassfish.jersey.server.ResourceConfig;
19+
20+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration;
21+
import org.springframework.boot.actuate.autoconfigure.web.ManagementContextType;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
26+
import org.springframework.boot.autoconfigure.jersey.JerseyProperties;
27+
import org.springframework.boot.autoconfigure.web.servlet.DefaultJerseyApplicationPath;
28+
import org.springframework.boot.autoconfigure.web.servlet.JerseyApplicationPath;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Import;
32+
33+
/**
34+
* {@link ManagementContextConfiguration} for Jersey infrastructure when the management
35+
* context is the same as the main application context.
36+
*
37+
* @author Madhura Bhave
38+
*/
39+
@ManagementContextConfiguration(ManagementContextType.SAME)
40+
@ConditionalOnMissingBean(ResourceConfig.class)
41+
@Import(JerseyManagementContextConfiguration.class)
42+
@EnableConfigurationProperties(JerseyProperties.class)
43+
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
44+
@ConditionalOnClass(ResourceConfig.class)
45+
@ConditionalOnMissingClass("org.springframework.web.servlet.DispatcherServlet")
46+
public class JerseySameManagementContextConfiguration {
47+
48+
@Bean
49+
@ConditionalOnMissingBean(JerseyApplicationPath.class)
50+
public JerseyApplicationPath jerseyApplicationPath(JerseyProperties properties,
51+
ResourceConfig config) {
52+
return new DefaultJerseyApplicationPath(properties.getApplicationPath(), config);
53+
}
54+
55+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ org.springframework.boot.actuate.autoconfigure.endpoint.web.ServletEndpointManag
9494
org.springframework.boot.actuate.autoconfigure.endpoint.web.reactive.WebFluxEndpointManagementContextConfiguration,\
9595
org.springframework.boot.actuate.autoconfigure.endpoint.web.servlet.WebMvcEndpointManagementContextConfiguration,\
9696
org.springframework.boot.actuate.autoconfigure.endpoint.web.jersey.JerseyWebEndpointManagementContextConfiguration,\
97-
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyManagementChildContextConfiguration,\
97+
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseySameManagementContextConfiguration,\
98+
org.springframework.boot.actuate.autoconfigure.web.jersey.JerseyChildManagementContextConfiguration,\
9899
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementChildContextConfiguration,\
99100
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementChildContextConfiguration,\
100101
org.springframework.boot.actuate.autoconfigure.web.servlet.WebMvcEndpointChildContextConfiguration

0 commit comments

Comments
 (0)