1717package de .codecentric .boot .admin .server .utils .jackson ;
1818
1919import java .io .IOException ;
20- import java .util .HashMap ;
2120import java .util .Iterator ;
2221import java .util .Map ;
2322
2423import com .fasterxml .jackson .core .JsonParser ;
25- import com .fasterxml .jackson .core .JsonToken ;
2624import com .fasterxml .jackson .databind .DeserializationContext ;
2725import com .fasterxml .jackson .databind .JsonNode ;
2826import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
29- import org .springframework .util .ObjectUtils ;
3027
3128import de .codecentric .boot .admin .server .domain .values .Registration ;
3229
@@ -41,26 +38,18 @@ public RegistrationDeserializer() {
4138 @ Override
4239 public Registration deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
4340 JsonNode node = p .readValueAsTree ();
44- Map <String , String > normalizedKvPair = getNormalizedKvPair (node );
4541 Registration .Builder builder = Registration .builder ();
4642
47- if (node .hasNonNull ("name" )) {
48- builder .name (node .get ("name" ).asText ());
49- }
43+ builder .name (firstNonNullAsText (node , "name" ));
44+
5045 if (node .hasNonNull ("url" )) {
51- String url = node . get ( "url" ). asText ( );
46+ String url = firstNonNullAsText ( node , "url" );
5247 builder .healthUrl (url .replaceFirst ("/+$" , "" ) + "/health" ).managementUrl (url );
5348 }
5449 else {
55- if (!ObjectUtils .isEmpty (normalizedKvPair .get ("healthurl" ))) {
56- builder .healthUrl (normalizedKvPair .get ("healthurl" ));
57- }
58- if (!ObjectUtils .isEmpty (normalizedKvPair .get ("managementurl" ))) {
59- builder .managementUrl (normalizedKvPair .get ("managementurl" ));
60- }
61- if (!ObjectUtils .isEmpty (normalizedKvPair .get ("serviceurl" ))) {
62- builder .serviceUrl (normalizedKvPair .get ("serviceurl" ));
63- }
50+ builder .healthUrl (firstNonNullAsText (node , "healthUrl" , "health_url" ));
51+ builder .managementUrl (firstNonNullAsText (node , "managementUrl" , "management_url" ));
52+ builder .serviceUrl (firstNonNullAsText (node , "serviceUrl" , "service_url" ));
6453 }
6554
6655 if (node .has ("metadata" )) {
@@ -71,29 +60,18 @@ public Registration deserialize(JsonParser p, DeserializationContext ctxt) throw
7160 }
7261 }
7362
74- if (node .hasNonNull ("source" )) {
75- builder .source (node .get ("source" ).asText ());
76- }
63+ builder .source (firstNonNullAsText (node , "source" ));
7764
7865 return builder .build ();
7966 }
8067
81- private Map <String , String > getNormalizedKvPair (JsonNode jn ) throws IOException {
82- Map <String , String > normalizedKvPair = new HashMap <>();
83- JsonParser jp = jn .traverse ();
84- while (!jp .isClosed ()) {
85- if (jp .nextToken () == JsonToken .FIELD_NAME ) {
86- String fieldName = jp .currentName ();
87- if (!ObjectUtils .isEmpty (fieldName )) {
88- JsonToken jsonValueToken = jp .nextValue ();
89- if (jsonValueToken == JsonToken .VALUE_STRING ) {
90- normalizedKvPair .putIfAbsent (fieldName .replaceAll ("[_-]" , "" ).toLowerCase (),
91- jp .getValueAsString ());
92- }
93- }
68+ private String firstNonNullAsText (JsonNode node , String ... fieldNames ) {
69+ for (String fieldName : fieldNames ) {
70+ if (node .hasNonNull (fieldName )) {
71+ return node .get (fieldName ).asText ();
9472 }
9573 }
96- return normalizedKvPair ;
74+ return null ;
9775 }
9876
9977}
0 commit comments