Skip to content

Commit

Permalink
RestTemplateEurekaHttpClient - Encode special characters; fixes sprin…
Browse files Browse the repository at this point in the history
  • Loading branch information
NewAgeCZ committed Mar 5, 2023
1 parent 53d513f commit eddde9d
Showing 1 changed file with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.netflix.eureka.http;

import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -40,6 +41,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import static com.netflix.discovery.shared.transport.EurekaHttpResponse.anEurekaHttpResponse;

Expand Down Expand Up @@ -68,36 +70,43 @@ public String getServiceUrl() {

@Override
public EurekaHttpResponse<Void> register(InstanceInfo info) {
String urlPath = serviceUrl + "apps/" + info.getAppName();
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}").buildAndExpand(info.getAppName())
.toUri();

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.ACCEPT_ENCODING, "gzip");
headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.POST, new HttpEntity<>(info, headers),
ResponseEntity<Void> response = restTemplate.exchange(uri, HttpMethod.POST, new HttpEntity<>(info, headers),
Void.class);

return anEurekaHttpResponse(response.getStatusCodeValue()).headers(headersOf(response)).build();
}

@Override
public EurekaHttpResponse<Void> cancel(String appName, String id) {
String urlPath = serviceUrl + "apps/" + appName + '/' + id;
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}/{id}").buildAndExpand(appName, id)
.toUri();

ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.DELETE, null, Void.class);
ResponseEntity<Void> response = restTemplate.exchange(uri, HttpMethod.DELETE, null, Void.class);

return anEurekaHttpResponse(response.getStatusCodeValue()).headers(headersOf(response)).build();
}

@Override
public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id, InstanceInfo info,
InstanceStatus overriddenStatus) {
String urlPath = serviceUrl + "apps/" + appName + '/' + id + "?status=" + info.getStatus().toString()
+ "&lastDirtyTimestamp=" + info.getLastDirtyTimestamp().toString()
+ (overriddenStatus != null ? "&overriddenstatus=" + overriddenStatus.name() : "");
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}/{id}")
.queryParam("status", info.getStatus().toString())
.queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString());

ResponseEntity<InstanceInfo> response = restTemplate.exchange(urlPath, HttpMethod.PUT, null,
InstanceInfo.class);
if (overriddenStatus != null) {
uriBuilder = uriBuilder.queryParam("overriddenstatus", overriddenStatus.name());
}

URI uri = uriBuilder.buildAndExpand(appName, id).toUri();

ResponseEntity<InstanceInfo> response = restTemplate.exchange(uri, HttpMethod.PUT, null, InstanceInfo.class);

EurekaHttpResponseBuilder<InstanceInfo> eurekaResponseBuilder = anEurekaHttpResponse(
response.getStatusCodeValue(), InstanceInfo.class).headers(headersOf(response));
Expand All @@ -112,20 +121,23 @@ public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id,
@Override
public EurekaHttpResponse<Void> statusUpdate(String appName, String id, InstanceStatus newStatus,
InstanceInfo info) {
String urlPath = serviceUrl + "apps/" + appName + '/' + id + "/status?value=" + newStatus.name()
+ "&lastDirtyTimestamp=" + info.getLastDirtyTimestamp().toString();
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}/{id}/status")
.queryParam("value", newStatus.name())
.queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString()).buildAndExpand(appName, id)
.toUri();

ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.PUT, null, Void.class);
ResponseEntity<Void> response = restTemplate.exchange(uri, HttpMethod.PUT, null, Void.class);

return anEurekaHttpResponse(response.getStatusCodeValue()).headers(headersOf(response)).build();
}

@Override
public EurekaHttpResponse<Void> deleteStatusOverride(String appName, String id, InstanceInfo info) {
String urlPath = serviceUrl + "apps/" + appName + '/' + id + "/status?lastDirtyTimestamp="
+ info.getLastDirtyTimestamp().toString();
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}/{id}/status")
.queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString()).buildAndExpand(appName, id)
.toUri();

ResponseEntity<Void> response = restTemplate.exchange(urlPath, HttpMethod.DELETE, null, Void.class);
ResponseEntity<Void> response = restTemplate.exchange(uri, HttpMethod.DELETE, null, Void.class);

return anEurekaHttpResponse(response.getStatusCodeValue()).headers(headersOf(response)).build();
}
Expand All @@ -136,13 +148,15 @@ public EurekaHttpResponse<Applications> getApplications(String... regions) {
}

private EurekaHttpResponse<Applications> getApplicationsInternal(String urlPath, String[] regions) {
String url = serviceUrl + urlPath;
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromHttpUrl(serviceUrl).path(urlPath);

if (regions != null && regions.length > 0) {
url = url + (urlPath.contains("?") ? "&" : "?") + "regions=" + StringUtil.join(regions);
uriBuilder = uriBuilder.queryParam("regions", StringUtil.join(regions));
}

ResponseEntity<EurekaApplications> response = restTemplate.exchange(url, HttpMethod.GET, null,
URI uri = uriBuilder.build().toUri();

ResponseEntity<EurekaApplications> response = restTemplate.exchange(uri, HttpMethod.GET, null,
EurekaApplications.class);

return anEurekaHttpResponse(response.getStatusCodeValue(),
Expand All @@ -167,9 +181,9 @@ public EurekaHttpResponse<Applications> getSecureVip(String secureVipAddress, St

@Override
public EurekaHttpResponse<Application> getApplication(String appName) {
String urlPath = serviceUrl + "apps/" + appName;
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).path("apps/{appName}").buildAndExpand(appName).toUri();

ResponseEntity<Application> response = restTemplate.exchange(urlPath, HttpMethod.GET, null, Application.class);
ResponseEntity<Application> response = restTemplate.exchange(uri, HttpMethod.GET, null, Application.class);

Application application = response.getStatusCodeValue() == HttpStatus.OK.value() && response.hasBody()
? response.getBody() : null;
Expand All @@ -179,19 +193,18 @@ public EurekaHttpResponse<Application> getApplication(String appName) {

@Override
public EurekaHttpResponse<InstanceInfo> getInstance(String appName, String id) {
return getInstanceInternal("apps/" + appName + '/' + id);
return getInstanceInternal("apps", appName, id);
}

@Override
public EurekaHttpResponse<InstanceInfo> getInstance(String id) {
return getInstanceInternal("instances/" + id);
return getInstanceInternal("instances", id);
}

private EurekaHttpResponse<InstanceInfo> getInstanceInternal(String urlPath) {
urlPath = serviceUrl + urlPath;
private EurekaHttpResponse<InstanceInfo> getInstanceInternal(String... pathSegments) {
URI uri = UriComponentsBuilder.fromHttpUrl(serviceUrl).pathSegment(pathSegments).build().toUri();

ResponseEntity<InstanceInfo> response = restTemplate.exchange(urlPath, HttpMethod.GET, null,
InstanceInfo.class);
ResponseEntity<InstanceInfo> response = restTemplate.exchange(uri, HttpMethod.GET, null, InstanceInfo.class);

return anEurekaHttpResponse(response.getStatusCodeValue(),
response.getStatusCodeValue() == HttpStatus.OK.value() && response.hasBody() ? response.getBody()
Expand Down

0 comments on commit eddde9d

Please sign in to comment.