use of org.eclipse.hono.adapter.lora.providers.LoraProviderMalformedPayloadException in project hono by eclipse.
the class LoraProtocolAdapter method handleProviderRoute.
void handleProviderRoute(final HttpContext ctx, final LoraProvider provider) {
if (LOG.isDebugEnabled()) {
LOG.debug("processing request from provider [name: {}, URI: {}]", provider.getProviderName(), ctx.getRoutingContext().normalizedPath());
}
final Span currentSpan = TracingHelper.buildServerChildSpan(tracer, TracingHandler.serverSpanContext(ctx.getRoutingContext()), SPAN_NAME_PROCESS_MESSAGE, getClass().getSimpleName()).start();
TAG_LORA_PROVIDER.set(currentSpan, provider.getProviderName());
ctx.put(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER, provider.getProviderName());
if (!ctx.isDeviceAuthenticated()) {
logUnsupportedUserType(ctx.getRoutingContext(), currentSpan);
currentSpan.finish();
handle401(ctx.getRoutingContext());
return;
}
final Device gatewayDevice = ctx.getAuthenticatedDevice();
TracingHelper.setDeviceTags(currentSpan, gatewayDevice.getTenantId(), gatewayDevice.getDeviceId());
try {
final LoraMessage loraMessage = provider.getMessage(ctx.getRoutingContext());
final LoraMessageType type = loraMessage.getType();
currentSpan.log(Map.of("message type", type));
final String deviceId = loraMessage.getDevEUIAsString();
currentSpan.setTag(TAG_LORA_DEVICE_ID, deviceId);
switch(type) {
case UPLINK:
final UplinkLoraMessage uplinkMessage = (UplinkLoraMessage) loraMessage;
final Buffer payload = uplinkMessage.getPayload();
Optional.ofNullable(uplinkMessage.getMetaData()).ifPresent(metaData -> ctx.put(LoraConstants.APP_PROPERTY_META_DATA, metaData));
Optional.ofNullable(uplinkMessage.getAdditionalData()).ifPresent(additionalData -> ctx.put(LoraConstants.APP_PROPERTY_ADDITIONAL_DATA, additionalData));
final String contentType = payload.length() > 0 ? LoraConstants.CONTENT_TYPE_LORA_BASE + provider.getProviderName() : EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION;
// uploadTelemetryMessage will finish the root span, therefore finish child span here already
currentSpan.finish();
uploadTelemetryMessage(ctx, gatewayDevice.getTenantId(), deviceId, payload, contentType);
registerCommandConsumerIfNeeded(provider, gatewayDevice, currentSpan.context());
break;
default:
LOG.debug("discarding message of unsupported type [tenant: {}, device-id: {}, type: {}]", gatewayDevice.getTenantId(), deviceId, type);
currentSpan.log("discarding message of unsupported type");
currentSpan.finish();
// discard the message but return 202 to not cause errors on the LoRa provider side
handle202(ctx.getRoutingContext());
}
} catch (final LoraProviderMalformedPayloadException e) {
LOG.debug("error processing request from provider [name: {}]", provider.getProviderName(), e);
TracingHelper.logError(currentSpan, "error processing request", e);
currentSpan.finish();
handle400(ctx.getRoutingContext(), ERROR_MSG_INVALID_PAYLOAD);
}
}
use of org.eclipse.hono.adapter.lora.providers.LoraProviderMalformedPayloadException in project hono by eclipse.
the class LoraProtocolAdapterTest method handleProviderRouteCausesBadRequestForFailureToParseBody.
/**
* Verifies that the provider route rejects a request if the request body cannot
* be parsed.
*/
@Test
public void handleProviderRouteCausesBadRequestForFailureToParseBody() {
givenATelemetrySenderForAnyTenant();
final LoraProvider providerMock = getLoraProviderMock();
when(providerMock.getMessage(any(RoutingContext.class))).thenThrow(new LoraProviderMalformedPayloadException("no device ID"));
final HttpContext httpContext = newHttpContext();
adapter.handleProviderRoute(httpContext, providerMock);
verify(httpContext.getRoutingContext()).put(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER, TEST_PROVIDER);
assertNoTelemetryMessageHasBeenSentDownstream();
verifyBadRequest(httpContext.getRoutingContext());
verify(processMessageSpan).finish();
}
Aggregations