Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
449b42ff
Commit
449b42ff
authored
Nov 16, 2016
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish LinksEnhancer to use endpoint name
See gh-7164 See gh-7132
parent
ada02232
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
27 deletions
+37
-27
LinksEnhancer.java
...ngframework/boot/actuate/autoconfigure/LinksEnhancer.java
+18
-15
LinksEnhancerTests.java
...mework/boot/actuate/autoconfigure/LinksEnhancerTests.java
+19
-12
No files found.
spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancer.java
View file @
449b42ff
...
...
@@ -16,15 +16,14 @@
package
org
.
springframework
.
boot
.
actuate
.
autoconfigure
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
;
import
org.springframework.boot.actuate.endpoint.mvc.MvcEndpoints
;
import
org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint
;
import
org.springframework.hateoas.ResourceSupport
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.util.MultiValueMap
;
import
org.springframework.util.StringUtils
;
import
static
org
.
springframework
.
hateoas
.
mvc
.
ControllerLinkBuilder
.
linkTo
;
...
...
@@ -33,6 +32,7 @@ import static org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo;
* Adds endpoint links to {@link ResourceSupport}.
*
* @author Dave Syer
* @author Madhura Bhave
*/
class
LinksEnhancer
{
...
...
@@ -50,26 +50,29 @@ class LinksEnhancer {
resource
.
add
(
linkTo
(
LinksEnhancer
.
class
).
slash
(
this
.
rootPath
+
self
)
.
withSelfRel
());
}
M
ap
<
String
,
List
<
String
>>
added
=
new
HashMap
<
String
,
List
<
String
>
>();
M
ultiValueMap
<
String
,
String
>
added
=
new
LinkedMultiValueMap
<
String
,
String
>();
for
(
MvcEndpoint
endpoint
:
this
.
endpoints
.
getEndpoints
())
{
String
rel
=
getRel
(
endpoint
);
List
<
String
>
pathsForRel
=
added
.
get
(
rel
)
==
null
?
new
ArrayList
<
String
>()
:
added
.
get
(
rel
);
if
(!
endpoint
.
getPath
().
equals
(
self
)
&&
!
pathsForRel
.
contains
(
endpoint
.
getPath
()))
{
addEndpointLink
(
resource
,
endpoint
,
rel
);
pathsForRel
.
add
(
endpoint
.
getPath
());
added
.
put
(
rel
,
pathsForRel
);
if
(!
endpoint
.
getPath
().
equals
(
self
))
{
String
rel
=
getRel
(
endpoint
);
List
<
String
>
paths
=
added
.
get
(
rel
);
if
(
paths
==
null
||
!
paths
.
contains
(
endpoint
.
getPath
()))
{
addEndpointLink
(
resource
,
endpoint
,
rel
);
added
.
add
(
rel
,
endpoint
.
getPath
());
}
}
}
}
private
String
getRel
(
MvcEndpoint
endpoint
)
{
String
name
=
endpoint
instanceof
NamedMvcEndpoint
?
((
NamedMvcEndpoint
)
endpoint
).
getName
()
:
endpoint
.
getPath
();
return
(
name
.
startsWith
(
"/"
)
?
name
.
substring
(
1
)
:
name
);
if
(
endpoint
instanceof
NamedMvcEndpoint
)
{
return
((
NamedMvcEndpoint
)
endpoint
).
getName
();
}
String
path
=
endpoint
.
getPath
();
return
(
path
.
startsWith
(
"/"
)
?
path
.
substring
(
1
)
:
path
);
}
private
void
addEndpointLink
(
ResourceSupport
resource
,
MvcEndpoint
endpoint
,
String
rel
)
{
private
void
addEndpointLink
(
ResourceSupport
resource
,
MvcEndpoint
endpoint
,
String
rel
)
{
Class
<?>
type
=
endpoint
.
getEndpointType
();
type
=
(
type
==
null
?
Object
.
class
:
type
);
if
(
StringUtils
.
hasText
(
rel
))
{
...
...
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/LinksEnhancerTests.java
View file @
449b42ff
...
...
@@ -55,7 +55,8 @@ public class LinksEnhancerTests {
public
void
useNameAsRelIfAvailable
()
throws
Exception
{
TestMvcEndpoint
endpoint
=
new
TestMvcEndpoint
(
new
TestEndpoint
(
"a"
));
endpoint
.
setPath
(
"something-else"
);
LinksEnhancer
enhancer
=
getLinksEnhancer
(
Collections
.
singletonList
((
MvcEndpoint
)
endpoint
));
LinksEnhancer
enhancer
=
getLinksEnhancer
(
Collections
.
singletonList
((
MvcEndpoint
)
endpoint
));
ResourceSupport
support
=
new
ResourceSupport
();
enhancer
.
addEndpointLinks
(
support
,
""
);
assertThat
(
support
.
getLink
(
"a"
).
getHref
()).
contains
(
"/something-else"
);
...
...
@@ -86,20 +87,13 @@ public class LinksEnhancerTests {
endpoint
.
setPath
(
"endpoint"
);
TestMvcEndpoint
otherEndpoint
=
new
TestMvcEndpoint
(
new
TestEndpoint
(
"a"
));
otherEndpoint
.
setPath
(
"other-endpoint"
);
LinksEnhancer
enhancer
=
getLinksEnhancer
(
Arrays
.
asList
((
MvcEndpoint
)
endpoint
,
otherEndpoint
));
LinksEnhancer
enhancer
=
getLinksEnhancer
(
Arrays
.
asList
((
MvcEndpoint
)
endpoint
,
otherEndpoint
));
ResourceSupport
support
=
new
ResourceSupport
();
enhancer
.
addEndpointLinks
(
support
,
""
);
assertThat
(
support
.
getLinks
()).
haveExactly
(
1
,
getCondition
(
"a"
,
"endpoint"
));
assertThat
(
support
.
getLinks
()).
haveExactly
(
1
,
getCondition
(
"a"
,
"other-endpoint"
));
}
private
Condition
<
Link
>
getCondition
(
final
String
rel
,
final
String
href
)
{
return
new
Condition
<
Link
>()
{
@Override
public
boolean
matches
(
Link
link
)
{
return
link
.
getRel
().
equals
(
rel
)
&&
link
.
getHref
().
equals
(
"http://localhost/"
+
href
);
}
};
assertThat
(
support
.
getLinks
()).
haveExactly
(
1
,
getCondition
(
"a"
,
"other-endpoint"
));
}
private
LinksEnhancer
getLinksEnhancer
(
List
<
MvcEndpoint
>
endpoints
)
throws
Exception
{
...
...
@@ -114,6 +108,18 @@ public class LinksEnhancerTests {
return
new
LinksEnhancer
(
""
,
mvcEndpoints
);
}
private
Condition
<
Link
>
getCondition
(
final
String
rel
,
final
String
href
)
{
return
new
Condition
<
Link
>()
{
@Override
public
boolean
matches
(
Link
link
)
{
return
link
.
getRel
().
equals
(
rel
)
&&
link
.
getHref
().
equals
(
"http://localhost/"
+
href
);
}
};
}
private
static
class
TestEndpoint
extends
AbstractEndpoint
<
Object
>
{
TestEndpoint
(
String
id
)
{
...
...
@@ -140,6 +146,7 @@ public class LinksEnhancerTests {
NoNameTestMvcEndpoint
(
String
path
,
boolean
sensitive
)
{
super
(
path
,
sensitive
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment