1
1
/*
2
- * Copyright 2012-2019 the original author or authors.
2
+ * Copyright 2012-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
20
20
import java .util .Arrays ;
21
21
import java .util .Collection ;
22
22
import java .util .Collections ;
23
+ import java .util .LinkedHashSet ;
23
24
import java .util .List ;
24
- import java .util .Locale ;
25
25
import java .util .Set ;
26
- import java .util .stream .Collectors ;
27
26
28
27
import org .springframework .boot .actuate .endpoint .EndpointFilter ;
29
28
import org .springframework .boot .actuate .endpoint .EndpointId ;
35
34
36
35
/**
37
36
* {@link EndpointFilter} that will filter endpoints based on {@code include} and
38
- * {@code exclude} properties .
37
+ * {@code exclude} patterns .
39
38
*
40
39
* @param <E> the endpoint type
41
40
* @author Phillip Webb
@@ -45,11 +44,11 @@ public class ExposeExcludePropertyEndpointFilter<E extends ExposableEndpoint<?>>
45
44
46
45
private final Class <E > endpointType ;
47
46
48
- private final Set < String > include ;
47
+ private final EndpointPatterns include ;
49
48
50
- private final Set < String > exclude ;
49
+ private final EndpointPatterns exclude ;
51
50
52
- private final Set < String > exposeDefaults ;
51
+ private final EndpointPatterns exposeDefaults ;
53
52
54
53
public ExposeExcludePropertyEndpointFilter (Class <E > endpointType , Environment environment , String prefix ,
55
54
String ... exposeDefaults ) {
@@ -58,37 +57,22 @@ public ExposeExcludePropertyEndpointFilter(Class<E> endpointType, Environment en
58
57
Assert .hasText (prefix , "Prefix must not be empty" );
59
58
Binder binder = Binder .get (environment );
60
59
this .endpointType = endpointType ;
61
- this .include = bind (binder , prefix + ".include" );
62
- this .exclude = bind (binder , prefix + ".exclude" );
63
- this .exposeDefaults = asSet ( Arrays . asList ( exposeDefaults ) );
60
+ this .include = new EndpointPatterns ( bind (binder , prefix + ".include" ) );
61
+ this .exclude = new EndpointPatterns ( bind (binder , prefix + ".exclude" ) );
62
+ this .exposeDefaults = new EndpointPatterns ( exposeDefaults );
64
63
}
65
64
66
65
public ExposeExcludePropertyEndpointFilter (Class <E > endpointType , Collection <String > include ,
67
66
Collection <String > exclude , String ... exposeDefaults ) {
68
67
Assert .notNull (endpointType , "EndpointType Type must not be null" );
69
68
this .endpointType = endpointType ;
70
- this .include = asSet (include );
71
- this .exclude = asSet (exclude );
72
- this .exposeDefaults = asSet ( Arrays . asList ( exposeDefaults ) );
69
+ this .include = new EndpointPatterns (include );
70
+ this .exclude = new EndpointPatterns (exclude );
71
+ this .exposeDefaults = new EndpointPatterns ( exposeDefaults );
73
72
}
74
73
75
- private Set <String > bind (Binder binder , String name ) {
76
- return asSet (binder .bind (name , Bindable .listOf (String .class )).map (this ::cleanup ).orElseGet (ArrayList ::new ));
77
- }
78
-
79
- private List <String > cleanup (List <String > values ) {
80
- return values .stream ().map (this ::cleanup ).collect (Collectors .toList ());
81
- }
82
-
83
- private String cleanup (String value ) {
84
- return "*" .equals (value ) ? "*" : EndpointId .fromPropertyValue (value ).toLowerCaseString ();
85
- }
86
-
87
- private Set <String > asSet (Collection <String > items ) {
88
- if (items == null ) {
89
- return Collections .emptySet ();
90
- }
91
- return items .stream ().map ((item ) -> item .toLowerCase (Locale .ENGLISH )).collect (Collectors .toSet ());
74
+ private List <String > bind (Binder binder , String name ) {
75
+ return binder .bind (name , Bindable .listOf (String .class )).orElseGet (ArrayList ::new );
92
76
}
93
77
94
78
@ Override
@@ -101,20 +85,62 @@ public boolean match(E endpoint) {
101
85
102
86
private boolean isExposed (ExposableEndpoint <?> endpoint ) {
103
87
if (this .include .isEmpty ()) {
104
- return this .exposeDefaults .contains ( "*" ) || contains ( this .exposeDefaults , endpoint );
88
+ return this .exposeDefaults .matchesAll ( ) || this .exposeDefaults . matches ( endpoint );
105
89
}
106
- return this .include .contains ( "*" ) || contains ( this .include , endpoint );
90
+ return this .include .matchesAll ( ) || this .include . matches ( endpoint );
107
91
}
108
92
109
93
private boolean isExcluded (ExposableEndpoint <?> endpoint ) {
110
94
if (this .exclude .isEmpty ()) {
111
95
return false ;
112
96
}
113
- return this .exclude .contains ( "*" ) || contains ( this .exclude , endpoint );
97
+ return this .exclude .matchesAll ( ) || this .exclude . matches ( endpoint );
114
98
}
115
99
116
- private boolean contains (Set <String > items , ExposableEndpoint <?> endpoint ) {
117
- return items .contains (endpoint .getEndpointId ().toLowerCaseString ());
100
+ /**
101
+ * A set of endpoint patterns used to match IDs.
102
+ */
103
+ private static class EndpointPatterns {
104
+
105
+ private final boolean empty ;
106
+
107
+ private final boolean matchesAll ;
108
+
109
+ private final Set <EndpointId > endpointIds ;
110
+
111
+ EndpointPatterns (String [] patterns ) {
112
+ this ((patterns != null ) ? Arrays .asList (patterns ) : (Collection <String >) null );
113
+ }
114
+
115
+ EndpointPatterns (Collection <String > patterns ) {
116
+ patterns = (patterns != null ) ? patterns : Collections .emptySet ();
117
+ boolean matchesAll = false ;
118
+ Set <EndpointId > endpointIds = new LinkedHashSet <>();
119
+ for (String pattern : patterns ) {
120
+ if ("*" .equals (pattern )) {
121
+ matchesAll = true ;
122
+ }
123
+ else {
124
+ endpointIds .add (EndpointId .fromPropertyValue (pattern ));
125
+ }
126
+ }
127
+ this .empty = patterns .isEmpty ();
128
+ this .matchesAll = matchesAll ;
129
+ this .endpointIds = endpointIds ;
130
+ }
131
+
132
+ boolean isEmpty () {
133
+ return this .empty ;
134
+ }
135
+
136
+ boolean matchesAll () {
137
+ return this .matchesAll ;
138
+ }
139
+
140
+ boolean matches (ExposableEndpoint <?> endpoint ) {
141
+ return this .endpointIds .contains (endpoint .getEndpointId ());
142
+ }
143
+
118
144
}
119
145
120
146
}
0 commit comments