Browse Source

Accept the response header prefix in any case in HTTP integrations

Downlink metadata keys starting with "header:" (as the request headers
are passed to the downlink converter) are now returned as response
headers, not only keys starting with "Header:".
pull/16538/head
Serhii Titenko 1 month ago
parent
commit
bdbb1a3670
  1. 60
      application/src/test/java/org/thingsboard/server/service/integration/HttpIntegrationTest.java
  2. 2
      common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/basic/BasicHttpIntegration.java

60
application/src/test/java/org/thingsboard/server/service/integration/HttpIntegrationTest.java

@ -20,6 +20,8 @@ import org.springframework.context.annotation.Primary;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EventInfo;
@ -30,9 +32,14 @@ import org.thingsboard.server.common.data.event.LifeCycleEventFilter;
import org.thingsboard.server.common.data.integration.IntegrationType;
import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.msg.TbMsgType;
import org.thingsboard.server.common.data.secret.Secret;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import org.thingsboard.server.common.msg.queue.TbCallback;
import org.thingsboard.server.dao.secret.SecretService;
import org.thingsboard.server.dao.service.DaoSqlTest;
import org.thingsboard.server.gen.transport.TransportProtos;
import org.thingsboard.server.service.cache.DefaultIntegrationExecutorTenantProfileCache;
import org.thingsboard.server.service.cache.IntegrationExecutorTenantProfileCache;
@ -314,6 +321,59 @@ public class HttpIntegrationTest extends AbstractIntegrationTest {
});
}
@Test
public void testResponseHeadersFromDownlinkMetadata_thenHeaderPrefixIsCaseInsensitive() throws Exception {
loginTenantAdmin();
ObjectNode uplinkConfiguration = JacksonUtil.newObjectNode();
uplinkConfiguration.put("scriptLang", "TBEL");
uplinkConfiguration.put("decoder", "return {};");
uplinkConfiguration.put("tbelDecoder", "return {deviceName: 'http_device', deviceType: 'http', telemetry: {test: 1}};");
createConverter(HTTP_UPLINK_CONVERTER_NAME, ConverterType.UPLINK, uplinkConfiguration);
ObjectNode downlinkConfiguration = JacksonUtil.newObjectNode();
downlinkConfiguration.put("scriptLang", "TBEL");
downlinkConfiguration.put("encoder", "return {};");
downlinkConfiguration.put("tbelEncoder", "return {contentType: 'JSON', data: JSON.stringify(msg), " +
"metadata: {'Header:X-Upper-Case': 'upper', 'header:X-Lower-Case': 'lower'}};");
createConverter("Test downlink converter", ConverterType.DOWNLINK, downlinkConfiguration);
createIntegration("Test HTTP integration", IntegrationType.HTTP);
enableIntegration();
waitUntilIntegrationStarted(tenantId, integration.getId());
Device device = new Device();
device.setName("http_device");
device.setType("http");
device = doPost("/api/device", device, Device.class);
TbMsg downlink = TbMsg.newMsg()
.type(TbMsgType.ATTRIBUTES_UPDATED)
.originator(device.getId())
.copyMetaData(new TbMsgMetaData())
.data("{\"uploadFrequency\":60}")
.build();
downlinkService.onRuleEngineDownlinkMsg(tenantId, integration.getId(), TransportProtos.IntegrationDownlinkMsgProto.newBuilder()
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits())
.setTenantIdMSB(tenantId.getId().getMostSignificantBits())
.setIntegrationIdLSB(integration.getId().getId().getLeastSignificantBits())
.setIntegrationIdMSB(integration.getId().getId().getMostSignificantBits())
.setDataProto(TbMsg.toProto(downlink))
.build(), TbCallback.EMPTY);
MvcResult response = await("uplink response with the pending downlink")
.atMost(TIMEOUT, TimeUnit.SECONDS)
.until(() -> postUplink().andReturn(), result -> result.getResponse().getStatus() == 200);
Assert.assertEquals("{\"uploadFrequency\":60}", response.getResponse().getContentAsString());
Assert.assertEquals("upper", response.getResponse().getHeader("X-Upper-Case"));
Assert.assertEquals("lower", response.getResponse().getHeader("X-Lower-Case"));
}
private ResultActions postUplink() throws Exception {
return doPostAsync("/api/v1/integrations/http/{routingKey}", TEST_DATA, TimeUnit.SECONDS.toMillis(TIMEOUT), integration.getRoutingKey());
}
private void createIntegration(boolean isDevice) throws Exception {
loginTenantAdmin();

2
common/integration/http-integrations/src/main/java/org/thingsboard/integration/http/basic/BasicHttpIntegration.java

@ -137,7 +137,7 @@ public class BasicHttpIntegration<T extends HttpIntegrationMsg<?>> extends Abstr
boolean contentTypePresent = false;
for (Entry<String, String> kv : downlink.getMetadata().entrySet()) {
if (kv.getKey().startsWith(HEADER)) {
if (kv.getKey().regionMatches(true, 0, HEADER, 0, HEADER.length())) {
String headerName = kv.getKey().substring(HEADER.length());
responseHeaders.add(headerName, kv.getValue());
if (headerName.equalsIgnoreCase("Content-Type")) {

Loading…
Cancel
Save