Skip to content

Commit 975e986

Browse files
committed
Polish
1 parent f975024 commit 975e986

File tree

6 files changed

+83
-90
lines changed

6 files changed

+83
-90
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/EndpointRequest.java

Lines changed: 75 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -113,19 +113,56 @@ public static LinksRequestMatcher toLinks() {
113113
}
114114

115115
/**
116-
* The request matcher used to match against {@link Endpoint actuator endpoints}.
116+
* Base class for supported request matchers.
117117
*/
118-
public static final class EndpointRequestMatcher
118+
private static abstract class AbstractRequestMatcher
119119
extends ApplicationContextRequestMatcher<WebApplicationContext> {
120120

121+
private volatile RequestMatcher delegate;
122+
123+
public AbstractRequestMatcher() {
124+
super(WebApplicationContext.class);
125+
}
126+
127+
@Override
128+
protected final void initialized(Supplier<WebApplicationContext> context) {
129+
this.delegate = createDelegate(context.get());
130+
}
131+
132+
@Override
133+
protected final boolean matches(HttpServletRequest request,
134+
Supplier<WebApplicationContext> context) {
135+
return this.delegate.matches(request);
136+
}
137+
138+
private RequestMatcher createDelegate(WebApplicationContext context) {
139+
try {
140+
RequestMatcherFactory requestMatcherFactory = new RequestMatcherFactory(
141+
context.getBean(DispatcherServletPathProvider.class)
142+
.getServletPath());
143+
return createDelegate(context, requestMatcherFactory);
144+
}
145+
catch (NoSuchBeanDefinitionException ex) {
146+
return EMPTY_MATCHER;
147+
}
148+
}
149+
150+
protected abstract RequestMatcher createDelegate(WebApplicationContext context,
151+
RequestMatcherFactory requestMatcherFactory);
152+
153+
}
154+
155+
/**
156+
* The request matcher used to match against {@link Endpoint actuator endpoints}.
157+
*/
158+
public static final class EndpointRequestMatcher extends AbstractRequestMatcher {
159+
121160
private final List<Object> includes;
122161

123162
private final List<Object> excludes;
124163

125164
private final boolean includeLinks;
126165

127-
private volatile RequestMatcher delegate;
128-
129166
private EndpointRequestMatcher(boolean includeLinks) {
130167
this(Collections.emptyList(), Collections.emptyList(), includeLinks);
131168
}
@@ -142,7 +179,6 @@ private EndpointRequestMatcher(String[] endpoints, boolean includeLinks) {
142179

143180
private EndpointRequestMatcher(List<Object> includes, List<Object> excludes,
144181
boolean includeLinks) {
145-
super(WebApplicationContext.class);
146182
this.includes = includes;
147183
this.excludes = excludes;
148184
this.includeLinks = includeLinks;
@@ -165,40 +201,22 @@ public EndpointRequestMatcher excludingLinks() {
165201
}
166202

167203
@Override
168-
protected void initialized(
169-
Supplier<WebApplicationContext> webApplicationContext) {
170-
this.delegate = createDelegate(webApplicationContext);
171-
}
172-
173-
private RequestMatcher createDelegate(
174-
Supplier<WebApplicationContext> webApplicationContext) {
175-
try {
176-
WebApplicationContext context = webApplicationContext.get();
177-
PathMappedEndpoints pathMappedEndpoints = context
178-
.getBean(PathMappedEndpoints.class);
179-
DispatcherServletPathProvider pathProvider = context
180-
.getBean(DispatcherServletPathProvider.class);
181-
return createDelegate(pathMappedEndpoints, pathProvider.getServletPath());
182-
}
183-
catch (NoSuchBeanDefinitionException ex) {
184-
return EMPTY_MATCHER;
185-
}
186-
}
187-
188-
private RequestMatcher createDelegate(PathMappedEndpoints pathMappedEndpoints,
189-
String servletPath) {
204+
protected RequestMatcher createDelegate(WebApplicationContext context,
205+
RequestMatcherFactory requestMatcherFactory) {
206+
PathMappedEndpoints pathMappedEndpoints = context
207+
.getBean(PathMappedEndpoints.class);
190208
Set<String> paths = new LinkedHashSet<>();
191209
if (this.includes.isEmpty()) {
192210
paths.addAll(pathMappedEndpoints.getAllPaths());
193211
}
194212
streamPaths(this.includes, pathMappedEndpoints).forEach(paths::add);
195213
streamPaths(this.excludes, pathMappedEndpoints).forEach(paths::remove);
196-
List<RequestMatcher> delegateMatchers = getDelegateMatchers(servletPath,
197-
paths);
214+
List<RequestMatcher> delegateMatchers = getDelegateMatchers(
215+
requestMatcherFactory, paths);
198216
if (this.includeLinks
199217
&& StringUtils.hasText(pathMappedEndpoints.getBasePath())) {
200-
delegateMatchers.add(new AntPathRequestMatcher(
201-
computePath(servletPath, pathMappedEndpoints.getBasePath())));
218+
delegateMatchers.add(
219+
requestMatcherFactory.antPath(pathMappedEndpoints.getBasePath()));
202220
}
203221
return new OrRequestMatcher(delegateMatchers);
204222
}
@@ -226,75 +244,50 @@ private String getEndpointId(Class<?> source) {
226244
return annotation.id();
227245
}
228246

229-
private List<RequestMatcher> getDelegateMatchers(String servletPath,
230-
Set<String> paths) {
247+
private List<RequestMatcher> getDelegateMatchers(
248+
RequestMatcherFactory requestMatcherFactory, Set<String> paths) {
231249
return paths.stream()
232-
.map((path) -> new AntPathRequestMatcher(computePath(servletPath, path) + "/**"))
250+
.map((path) -> requestMatcherFactory.antPath(path, "/**"))
233251
.collect(Collectors.toList());
234252
}
235253

236-
private String computePath(String servletPath, String path) {
237-
if (servletPath.equals("/")) {
238-
return path;
239-
}
240-
return servletPath + path;
241-
}
242-
243-
@Override
244-
protected boolean matches(HttpServletRequest request,
245-
Supplier<WebApplicationContext> context) {
246-
return this.delegate.matches(request);
247-
}
248-
249254
}
250255

251256
/**
252257
* The request matcher used to match against the links endpoint.
253258
*/
254-
public static final class LinksRequestMatcher
255-
extends ApplicationContextRequestMatcher<WebApplicationContext> {
256-
257-
private RequestMatcher delegate;
258-
259-
private LinksRequestMatcher() {
260-
super(WebApplicationContext.class);
261-
}
259+
public static final class LinksRequestMatcher extends AbstractRequestMatcher {
262260

263261
@Override
264-
protected void initialized(
265-
Supplier<WebApplicationContext> webApplicationContext) {
266-
try {
267-
WebApplicationContext context = webApplicationContext.get();
268-
WebEndpointProperties properties = context
269-
.getBean(WebEndpointProperties.class);
270-
DispatcherServletPathProvider pathProvider = context
271-
.getBean(DispatcherServletPathProvider.class);
272-
this.delegate = createDelegate(pathProvider.getServletPath(), properties);
273-
}
274-
catch (NoSuchBeanDefinitionException ex) {
275-
this.delegate = EMPTY_MATCHER;
276-
}
277-
}
278-
279-
private RequestMatcher createDelegate(String path,
280-
WebEndpointProperties properties) {
262+
protected RequestMatcher createDelegate(WebApplicationContext context,
263+
RequestMatcherFactory requestMatcherFactory) {
264+
WebEndpointProperties properties = context
265+
.getBean(WebEndpointProperties.class);
281266
if (StringUtils.hasText(properties.getBasePath())) {
282-
return new AntPathRequestMatcher(computePath(path, properties.getBasePath()));
267+
return requestMatcherFactory.antPath(properties.getBasePath());
283268
}
284269
return EMPTY_MATCHER;
285270
}
286271

287-
private String computePath(String servletPath, String path) {
288-
if (servletPath.equals("/")) {
289-
return path;
290-
}
291-
return servletPath + path;
272+
}
273+
274+
/**
275+
* Factory used to create a {@link RequestMatcher}.
276+
*/
277+
private static class RequestMatcherFactory {
278+
279+
private final String servletPath;
280+
281+
RequestMatcherFactory(String servletPath) {
282+
this.servletPath = servletPath;
292283
}
293284

294-
@Override
295-
protected boolean matches(HttpServletRequest request,
296-
Supplier<WebApplicationContext> context) {
297-
return this.delegate.matches(request);
285+
public RequestMatcher antPath(String... parts) {
286+
String pattern = (this.servletPath.equals("/") ? "" : this.servletPath);
287+
for (String part : parts) {
288+
pattern += part;
289+
}
290+
return new AntPathRequestMatcher(pattern);
298291
}
299292

300293
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBean.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ private Object invoke(JmxOperation operation, Object[] params)
9999
String[] parameterNames = operation.getParameters().stream()
100100
.map(JmxOperationParameter::getName).toArray(String[]::new);
101101
Map<String, Object> arguments = getArguments(parameterNames, params);
102-
Object result = operation
103-
.invoke(new InvocationContext(SecurityContext.NONE, arguments));
102+
InvocationContext context = new InvocationContext(SecurityContext.NONE,
103+
arguments);
104+
Object result = operation.invoke(context);
104105
if (REACTOR_PRESENT) {
105106
result = ReactiveHandler.handle(result);
106107
}
@@ -119,9 +120,7 @@ private Exception translateIfNecessary(Exception exception) {
119120
if (exception.getClass().getName().startsWith("java.")) {
120121
return exception;
121122
}
122-
else {
123-
return new IllegalStateException(exception.getMessage());
124-
}
123+
return new IllegalStateException(exception.getMessage());
125124
}
126125

127126
private Map<String, Object> getArguments(String[] parameterNames, Object[] params) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/jmx/EndpointMBeanTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,12 @@ public void invokeWhenActionNameIsNotAnOperationShouldThrowException()
130130
public void invokeWhenOperationIsInvalidShouldThrowException()
131131
throws MBeanException, ReflectionException {
132132
TestJmxOperation operation = new TestJmxOperation() {
133+
133134
@Override
134135
public Object invoke(InvocationContext context) {
135136
throw new InvalidEndpointRequestException("test failure", "test");
136137
}
138+
137139
};
138140
TestExposableJmxEndpoint endpoint = new TestExposableJmxEndpoint(operation);
139141
EndpointMBean bean = new EndpointMBean(this.responseMapper, endpoint);

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfigurationTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ public void contentNegotiationStrategySkipsPathExtension() throws Exception {
831831
ContentNegotiationStrategy delegate = mock(ContentNegotiationStrategy.class);
832832
ContentNegotiationStrategy strategy = new WebMvcAutoConfiguration.OptionalPathExtensionContentNegotiationStrategy(
833833
delegate);
834-
835834
MockHttpServletRequest request = new MockHttpServletRequest();
836835
request.setAttribute(
837836
PathExtensionContentNegotiationStrategy.class.getName() + ".SKIP",

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,7 @@ the artifact yourself instead of overriding the property.
26002600
WARNING: Each Spring Boot release is designed and tested against this specific set of
26012601
third-party dependencies. Overriding versions may cause compatibility issues.
26022602

2603-
To override dependency versions in Gradle, see {spring-boot-gradle-plugin-reference}/#managing-dependencies-customizing[ this section ]
2603+
To override dependency versions in Gradle, see {spring-boot-gradle-plugin-reference}/#managing-dependencies-customizing[this section]
26042604
of the Gradle plugin's documentation.
26052605

26062606
[[howto-create-an-executable-jar-with-maven]]

0 commit comments

Comments
 (0)