use of org.eclipse.hono.util.Strings 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());
});
}
Aggregations