use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class HttpBasedMessageMapping method mapDownstreamMessageRequest.
private void mapDownstreamMessageRequest(final MqttContext ctx, final ResourceIdentifier targetAddress, final RegistrationAssertion registrationInfo, final MapperEndpoint mapperEndpoint, final Handler<AsyncResult<MappedMessage>> resultHandler) {
final MultiMap headers = MultiMap.caseInsensitiveMultiMap();
JsonObject.mapFrom(registrationInfo).forEach(property -> {
final Object value = property.getValue();
if (value instanceof String) {
// prevent strings from being enclosed in quotes
headers.add(property.getKey(), (String) value);
} else {
headers.add(property.getKey(), Json.encode(value));
}
});
headers.add(MessageHelper.APP_PROPERTY_ORIG_ADDRESS, ctx.message().topicName());
if (ctx.contentType() != null) {
headers.add(HttpHeaders.CONTENT_TYPE.toString(), ctx.contentType());
}
final Promise<MappedMessage> result = Promise.promise();
webClient.post(mapperEndpoint.getPort(), mapperEndpoint.getHost(), mapperEndpoint.getUri()).putHeaders(headers).ssl(mapperEndpoint.isTlsEnabled()).sendBuffer(ctx.message().payload(), httpResponseAsyncResult -> {
if (httpResponseAsyncResult.failed()) {
LOG.debug("failed to map message [origin: {}] using mapping service [host: {}, port: {}, URI: {}]", ctx.authenticatedDevice(), mapperEndpoint.getHost(), mapperEndpoint.getPort(), mapperEndpoint.getUri(), httpResponseAsyncResult.cause());
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, httpResponseAsyncResult.cause()));
} else {
final HttpResponse<Buffer> httpResponse = httpResponseAsyncResult.result();
if (httpResponse.statusCode() == HttpURLConnection.HTTP_OK) {
final Map<String, String> additionalProperties = new HashMap<>();
httpResponse.headers().forEach(entry -> additionalProperties.put(entry.getKey(), entry.getValue()));
final String mappedDeviceId = Optional.ofNullable(additionalProperties.remove(MessageHelper.APP_PROPERTY_DEVICE_ID)).map(id -> {
LOG.debug("original {} has been mapped to [device-id: {}]", ctx.authenticatedDevice(), id);
return id;
}).orElseGet(() -> targetAddress.getResourceId());
result.complete(new MappedMessage(ResourceIdentifier.from(targetAddress.getEndpoint(), targetAddress.getTenantId(), mappedDeviceId), httpResponse.bodyAsBuffer(), additionalProperties));
} else {
LOG.debug("mapping service [host: {}, port: {}, URI: {}] returned unexpected status code: {}", mapperEndpoint.getHost(), mapperEndpoint.getPort(), mapperEndpoint.getUri(), httpResponse.statusCode());
result.fail(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "could not invoke configured mapping service"));
}
}
resultHandler.handle(result.future());
});
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapMessageSucceedsIfNoMapperIsSet.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no downstream mapper has been defined for
* the gateway.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapMessageSucceedsIfNoMapperIsSet(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, "gateway");
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final MqttContext context = newContext(message, span, new Device(TEST_TENANT_ID, "gateway"));
messageMapping.mapDownstreamMessage(context, targetAddress, new RegistrationAssertion("gateway")).onComplete(ctx.succeeding(mappedMessage -> {
ctx.verify(() -> {
assertThat(mappedMessage.getTargetAddress()).isEqualTo(targetAddress);
assertThat(mappedMessage.getPayload()).isEqualTo(message.payload());
assertThat(mappedMessage.getAdditionalProperties()).isEmpty();
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapCommandSucceedsIfNoMapperIsSet.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no upstream mapper has been defined for
* the gateway.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapCommandSucceedsIfNoMapperIsSet(final VertxTestContext ctx) {
config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
final Command command = mock(Command.class);
final Buffer payload = Buffer.buffer("payload");
when(command.getPayload()).thenReturn(payload);
messageMapping.mapUpstreamMessage(new RegistrationAssertion("gateway"), command).onComplete(ctx.succeeding(mappedBuffer -> {
ctx.verify(() -> {
assertThat(mappedBuffer).isEqualTo(payload);
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapMessageSucceedsIfNoMapperEndpointIsConfigured.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no downstream mapper endpoint has been configured
* for the adapter.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapMessageSucceedsIfNoMapperEndpointIsConfigured(final VertxTestContext ctx) {
final ResourceIdentifier targetAddress = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, "gateway");
final MqttPublishMessage message = newMessage(MqttQoS.AT_LEAST_ONCE, TelemetryConstants.TELEMETRY_ENDPOINT);
final MqttContext context = newContext(message, span, new Device(TEST_TENANT_ID, "gateway"));
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setDownstreamMessageMapper("mapper");
messageMapping.mapDownstreamMessage(context, targetAddress, assertion).onComplete(ctx.succeeding(mappedMessage -> {
ctx.verify(() -> {
assertThat(mappedMessage.getTargetAddress()).isEqualTo(targetAddress);
assertThat(mappedMessage.getPayload()).isEqualTo(message.payload());
assertThat(mappedMessage.getAdditionalProperties()).isEmpty();
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.
the class HttpBasedMessageMappingTest method testMapCommandSucceedsIfNoMapperEndpointIsConfigured.
/**
* Verifies that the result returned by the mapping service contains the
* original payload and target address if no upstream mapper endpoint has been configured
* for the adapter.
*
* @param ctx The helper to use for running tests on vert.x.
*/
@Test
public void testMapCommandSucceedsIfNoMapperEndpointIsConfigured(final VertxTestContext ctx) {
final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setUpstreamMessageMapper("mapper");
final Command command = mock(Command.class);
final Buffer payload = Buffer.buffer("payload");
when(command.getPayload()).thenReturn(payload);
messageMapping.mapUpstreamMessage(assertion, command).onComplete(ctx.succeeding(mappedBuffer -> {
ctx.verify(() -> {
assertThat(mappedBuffer).isEqualTo(payload);
verify(mapperWebClient, never()).post(anyInt(), anyString(), anyString());
});
ctx.completeNow();
}));
}
Aggregations