16
16
17
17
package org .springframework .cloud .netflix .eureka .http ;
18
18
19
+ import java .net .URI ;
19
20
import java .util .Collections ;
20
21
import java .util .HashMap ;
21
22
import java .util .List ;
40
41
import org .springframework .http .MediaType ;
41
42
import org .springframework .http .ResponseEntity ;
42
43
import org .springframework .web .client .RestTemplate ;
44
+ import org .springframework .web .util .UriComponentsBuilder ;
43
45
44
46
import static com .netflix .discovery .shared .transport .EurekaHttpResponse .anEurekaHttpResponse ;
45
47
@@ -68,36 +70,43 @@ public String getServiceUrl() {
68
70
69
71
@ Override
70
72
public EurekaHttpResponse <Void > register (InstanceInfo info ) {
71
- String urlPath = serviceUrl + "apps/" + info .getAppName ();
73
+ URI uri = UriComponentsBuilder .fromHttpUrl (serviceUrl ).path ("apps/{appName}" ).buildAndExpand (info .getAppName ())
74
+ .toUri ();
72
75
73
76
HttpHeaders headers = new HttpHeaders ();
74
77
headers .add (HttpHeaders .ACCEPT_ENCODING , "gzip" );
75
78
headers .add (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE );
76
79
77
- ResponseEntity <Void > response = restTemplate .exchange (urlPath , HttpMethod .POST , new HttpEntity <>(info , headers ),
80
+ ResponseEntity <Void > response = restTemplate .exchange (uri , HttpMethod .POST , new HttpEntity <>(info , headers ),
78
81
Void .class );
79
82
80
83
return anEurekaHttpResponse (response .getStatusCodeValue ()).headers (headersOf (response )).build ();
81
84
}
82
85
83
86
@ Override
84
87
public EurekaHttpResponse <Void > cancel (String appName , String id ) {
85
- String urlPath = serviceUrl + "apps/" + appName + '/' + id ;
88
+ URI uri = UriComponentsBuilder .fromHttpUrl (serviceUrl ).path ("apps/{appName}/{id}" ).buildAndExpand (appName , id )
89
+ .toUri ();
86
90
87
- ResponseEntity <Void > response = restTemplate .exchange (urlPath , HttpMethod .DELETE , null , Void .class );
91
+ ResponseEntity <Void > response = restTemplate .exchange (uri , HttpMethod .DELETE , null , Void .class );
88
92
89
93
return anEurekaHttpResponse (response .getStatusCodeValue ()).headers (headersOf (response )).build ();
90
94
}
91
95
92
96
@ Override
93
97
public EurekaHttpResponse <InstanceInfo > sendHeartBeat (String appName , String id , InstanceInfo info ,
94
98
InstanceStatus overriddenStatus ) {
95
- String urlPath = serviceUrl + "apps/" + appName + '/' + id + "?status=" + info . getStatus (). toString ( )
96
- + "&lastDirtyTimestamp=" + info .getLastDirtyTimestamp ().toString ()
97
- + ( overriddenStatus != null ? "&overriddenstatus=" + overriddenStatus . name () : "" );
99
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder . fromHttpUrl ( serviceUrl ). path ( "apps/{ appName}/{id}" )
100
+ . queryParam ( "status" , info .getStatus ().toString () )
101
+ . queryParam ( "lastDirtyTimestamp" , info . getLastDirtyTimestamp (). toString () );
98
102
99
- ResponseEntity <InstanceInfo > response = restTemplate .exchange (urlPath , HttpMethod .PUT , null ,
100
- InstanceInfo .class );
103
+ if (overriddenStatus != null ) {
104
+ uriBuilder = uriBuilder .queryParam ("overriddenstatus" , overriddenStatus .name ());
105
+ }
106
+
107
+ URI uri = uriBuilder .buildAndExpand (appName , id ).toUri ();
108
+
109
+ ResponseEntity <InstanceInfo > response = restTemplate .exchange (uri , HttpMethod .PUT , null , InstanceInfo .class );
101
110
102
111
EurekaHttpResponseBuilder <InstanceInfo > eurekaResponseBuilder = anEurekaHttpResponse (
103
112
response .getStatusCodeValue (), InstanceInfo .class ).headers (headersOf (response ));
@@ -112,20 +121,23 @@ public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id,
112
121
@ Override
113
122
public EurekaHttpResponse <Void > statusUpdate (String appName , String id , InstanceStatus newStatus ,
114
123
InstanceInfo info ) {
115
- String urlPath = serviceUrl + "apps/" + appName + '/' + id + "/status?value=" + newStatus .name ()
116
- + "&lastDirtyTimestamp=" + info .getLastDirtyTimestamp ().toString ();
124
+ URI uri = UriComponentsBuilder .fromHttpUrl (serviceUrl ).path ("apps/{appName}/{id}/status" )
125
+ .queryParam ("value" , newStatus .name ())
126
+ .queryParam ("lastDirtyTimestamp" , info .getLastDirtyTimestamp ().toString ()).buildAndExpand (appName , id )
127
+ .toUri ();
117
128
118
- ResponseEntity <Void > response = restTemplate .exchange (urlPath , HttpMethod .PUT , null , Void .class );
129
+ ResponseEntity <Void > response = restTemplate .exchange (uri , HttpMethod .PUT , null , Void .class );
119
130
120
131
return anEurekaHttpResponse (response .getStatusCodeValue ()).headers (headersOf (response )).build ();
121
132
}
122
133
123
134
@ Override
124
135
public EurekaHttpResponse <Void > deleteStatusOverride (String appName , String id , InstanceInfo info ) {
125
- String urlPath = serviceUrl + "apps/" + appName + '/' + id + "/status?lastDirtyTimestamp="
126
- + info .getLastDirtyTimestamp ().toString ();
136
+ URI uri = UriComponentsBuilder .fromHttpUrl (serviceUrl ).path ("apps/{appName}/{id}/status" )
137
+ .queryParam ("lastDirtyTimestamp" , info .getLastDirtyTimestamp ().toString ()).buildAndExpand (appName , id )
138
+ .toUri ();
127
139
128
- ResponseEntity <Void > response = restTemplate .exchange (urlPath , HttpMethod .DELETE , null , Void .class );
140
+ ResponseEntity <Void > response = restTemplate .exchange (uri , HttpMethod .DELETE , null , Void .class );
129
141
130
142
return anEurekaHttpResponse (response .getStatusCodeValue ()).headers (headersOf (response )).build ();
131
143
}
@@ -136,13 +148,15 @@ public EurekaHttpResponse<Applications> getApplications(String... regions) {
136
148
}
137
149
138
150
private EurekaHttpResponse <Applications > getApplicationsInternal (String urlPath , String [] regions ) {
139
- String url = serviceUrl + urlPath ;
151
+ UriComponentsBuilder uriBuilder = UriComponentsBuilder . fromHttpUrl ( serviceUrl ). path ( urlPath ) ;
140
152
141
153
if (regions != null && regions .length > 0 ) {
142
- url = url + ( urlPath . contains ( "?" ) ? "&" : "?" ) + " regions=" + StringUtil .join (regions );
154
+ uriBuilder = uriBuilder . queryParam ( " regions" , StringUtil .join (regions ) );
143
155
}
144
156
145
- ResponseEntity <EurekaApplications > response = restTemplate .exchange (url , HttpMethod .GET , null ,
157
+ URI uri = uriBuilder .build ().toUri ();
158
+
159
+ ResponseEntity <EurekaApplications > response = restTemplate .exchange (uri , HttpMethod .GET , null ,
146
160
EurekaApplications .class );
147
161
148
162
return anEurekaHttpResponse (response .getStatusCodeValue (),
@@ -167,9 +181,9 @@ public EurekaHttpResponse<Applications> getSecureVip(String secureVipAddress, St
167
181
168
182
@ Override
169
183
public EurekaHttpResponse <Application > getApplication (String appName ) {
170
- String urlPath = serviceUrl + "apps/" + appName ;
184
+ URI uri = UriComponentsBuilder . fromHttpUrl ( serviceUrl ). path ( "apps/{appName}" ). buildAndExpand ( appName ). toUri () ;
171
185
172
- ResponseEntity <Application > response = restTemplate .exchange (urlPath , HttpMethod .GET , null , Application .class );
186
+ ResponseEntity <Application > response = restTemplate .exchange (uri , HttpMethod .GET , null , Application .class );
173
187
174
188
Application application = response .getStatusCodeValue () == HttpStatus .OK .value () && response .hasBody ()
175
189
? response .getBody () : null ;
@@ -179,19 +193,18 @@ public EurekaHttpResponse<Application> getApplication(String appName) {
179
193
180
194
@ Override
181
195
public EurekaHttpResponse <InstanceInfo > getInstance (String appName , String id ) {
182
- return getInstanceInternal ("apps/" + appName + '/' + id );
196
+ return getInstanceInternal ("apps" , appName , id );
183
197
}
184
198
185
199
@ Override
186
200
public EurekaHttpResponse <InstanceInfo > getInstance (String id ) {
187
- return getInstanceInternal ("instances/" + id );
201
+ return getInstanceInternal ("instances" , id );
188
202
}
189
203
190
- private EurekaHttpResponse <InstanceInfo > getInstanceInternal (String urlPath ) {
191
- urlPath = serviceUrl + urlPath ;
204
+ private EurekaHttpResponse <InstanceInfo > getInstanceInternal (String ... pathSegments ) {
205
+ URI uri = UriComponentsBuilder . fromHttpUrl ( serviceUrl ). pathSegment ( pathSegments ). build (). toUri () ;
192
206
193
- ResponseEntity <InstanceInfo > response = restTemplate .exchange (urlPath , HttpMethod .GET , null ,
194
- InstanceInfo .class );
207
+ ResponseEntity <InstanceInfo > response = restTemplate .exchange (uri , HttpMethod .GET , null , InstanceInfo .class );
195
208
196
209
return anEurekaHttpResponse (response .getStatusCodeValue (),
197
210
response .getStatusCodeValue () == HttpStatus .OK .value () && response .hasBody () ? response .getBody ()
0 commit comments