Skip to content

Commit

Permalink
Improved ArgumentSanitizer (#4972)
Browse files Browse the repository at this point in the history
* Improved ArgumentSanitizer
* Added retries to wait for URL to be available because of delay in DNS.
* added .editorconfig
* added url check and retry before post.
  • Loading branch information
corneil committed Jun 30, 2022
1 parent 439f26a commit 7a47b64
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 219 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
@@ -0,0 +1,28 @@
root = true

[*.java]
indent_style = tab
indent_size = 4
continuation_indent_size = 8

[*.groovy]
indent_style = tab
indent_size = 4
continuation_indent_size = 8

[*.xml]
indent_style = tab
indent_size = 4
continuation_indent_size = 8

[*.yml]
indent_style = space
indent_size = 2

[*.yaml]
indent_style = space
indent_size = 2

[*.sh]
indent_style = space
indent_size = 4
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.dataflow.rest.client;

import java.time.Duration;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -97,16 +98,16 @@ public PagedModel<AppStatusResource> status() {
@Override
public AppStatusResource status(String deploymentId) {
return this.restTemplate.getForObject(
appStatusUriTemplate.expand(deploymentId).getHref(),
AppStatusResource.class
appStatusUriTemplate.expand(deploymentId).getHref(),
AppStatusResource.class
);
}

@Override
public PagedModel<StreamStatusResource> streamStatus(String... streamNames) {
return this.restTemplate.getForObject(
streamStatusUriTemplate.expand(streamNames).getHref(),
StreamStatusResource.Page.class
streamStatusUriTemplate.expand(streamNames).getHref(),
StreamStatusResource.Page.class
);
}

Expand All @@ -131,11 +132,27 @@ public Object postToActuator(String appId, String instanceId, String endpoint, M
public void postToUrl(String appId, String instanceId, byte[] data, HttpHeaders headers) {
Assert.notNull(appUrlPostUriTemplate, "post endpoint not found");
String uri = appUrlPostUriTemplate.expand(appId, instanceId).getHref();
waitForUrl(uri, Duration.ofSeconds(30));
HttpEntity<byte[]> entity = new HttpEntity<>(data, headers);
ResponseEntity<String> response = this.restTemplate.exchange(uri, HttpMethod.POST, entity, String.class);

if (!response.getStatusCode().is2xxSuccessful()) {
throw new RuntimeException("POST:exception:" + response.getStatusCode() + ":" + response.getBody());
}
}

private void waitForUrl(String uri, Duration timeout) {
// Check
final long waitUntilMillis = System.currentTimeMillis() + timeout.toMillis();
do {
ResponseEntity<String> response = this.restTemplate.getForEntity(uri, String.class);
if (response.getStatusCode().is2xxSuccessful()) {
break;
}
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
} while (waitUntilMillis <= System.currentTimeMillis());
}
}

0 comments on commit 7a47b64

Please sign in to comment.