diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java index 1db9dd2fc5..26de3ad5e5 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/rest/TbHttpClient.java @@ -27,10 +27,10 @@ import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.client.reactive.ReactorClientHttpConnector; -import org.springframework.web.client.RestClientResponseException; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec; +import org.springframework.web.reactive.function.client.WebClientResponseException; import org.springframework.web.util.UriComponentsBuilder; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.rule.engine.api.TbContext; @@ -132,6 +132,7 @@ public class TbHttpClient { this.webClient = WebClient.builder() .clientConnector(new ReactorClientHttpConnector(httpClient)) + .defaultHeader(HttpHeaders.CONNECTION, "close") //In previous realization this header was present! (Added for hotfix "Connection reset") .build(); } catch (SSLException e) { throw new TbNodeException(e); @@ -170,7 +171,7 @@ public class TbHttpClient { BiConsumer onFailure) { try { if (semaphore != null && !semaphore.tryAcquire(config.getReadTimeoutMs(), TimeUnit.MILLISECONDS)) { - ctx.tellFailure(msg, new RuntimeException("Timeout during waiting for reply!")); + onFailure.accept(msg, new RuntimeException("Timeout during waiting for reply!")); return; } @@ -183,10 +184,10 @@ public class TbHttpClient { .uri(uri) .headers(headers -> prepareHeaders(headers, msg)); - if (HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method) || - HttpMethod.PATCH.equals(method) || HttpMethod.DELETE.equals(method) || + if ((HttpMethod.POST.equals(method) || HttpMethod.PUT.equals(method) || + HttpMethod.PATCH.equals(method) || HttpMethod.DELETE.equals(method)) && !config.isIgnoreRequestBody()) { - request.body(BodyInserters.fromValue(getData(msg, config.isIgnoreRequestBody(), config.isParseToPlainText()))); + request.body(BodyInserters.fromValue(getData(msg, config.isParseToPlainText()))); } request @@ -236,11 +237,9 @@ public class TbHttpClient { return uri; } - private String getData(TbMsg tbMsg, boolean ignoreBody, boolean parseToPlainText) { - if (!ignoreBody && parseToPlainText) { - return JacksonUtil.toPlainText(tbMsg.getData()); - } - return tbMsg.getData(); + private Object getData(TbMsg tbMsg, boolean parseToPlainText) { + String data = tbMsg.getData(); + return parseToPlainText ? JacksonUtil.toPlainText(data) : JacksonUtil.toJsonNode(data); } private TbMsg processResponse(TbContext ctx, TbMsg origMsg, ResponseEntity response) { @@ -283,10 +282,9 @@ public class TbHttpClient { private TbMsg processException(TbMsg origMsg, Throwable e) { TbMsgMetaData metaData = origMsg.getMetaData(); metaData.putValue(ERROR, e.getClass() + ": " + e.getMessage()); - if (e instanceof RestClientResponseException) { - RestClientResponseException restClientResponseException = (RestClientResponseException) e; + if (e instanceof WebClientResponseException restClientResponseException) { metaData.putValue(STATUS, restClientResponseException.getStatusText()); - metaData.putValue(STATUS_CODE, restClientResponseException.getRawStatusCode() + ""); + metaData.putValue(STATUS_CODE, restClientResponseException.getStatusCode().value() + ""); metaData.putValue(ERROR_BODY, restClientResponseException.getResponseBodyAsString()); } return TbMsg.transformMsgMetadata(origMsg, metaData); diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeTest.java index 98695c1570..fad0ae66da 100644 --- a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeTest.java +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/rest/TbRestApiCallNodeTest.java @@ -165,7 +165,7 @@ public class TbRestApiCallNodeTest { try { assertEquals(path, request.getRequestLine().getUri(), "Request path matches"); assertTrue(request.containsHeader("Content-Type"), "Content-Type included"); - assertEquals("text/plain;charset=UTF-8", + assertEquals("application/json", request.getFirstHeader("Content-Type").getValue(), "Content-Type value"); assertTrue(request.containsHeader("Content-Length"), "Content-Length included"); assertEquals("2",