/** * Processes an ExternalMessage to a Signal. * * @param message the message * @return the signal */ Optional<InboundExternalMessage> process(final ExternalMessage message) { final StartedTimer overAllProcessingTimer = startNewTimer().tag(DIRECTION_TAG_NAME, INBOUND); return withTimer(overAllProcessingTimer, () -> convertMessage(message, overAllProcessingTimer)); }
/** * Processes a Signal to an ExternalMessage. * * @param signal the signal * @return the message */ Optional<ExternalMessage> process(final Signal<?> signal) { final StartedTimer overAllProcessingTimer = startNewTimer().tag(DIRECTION_TAG_NAME, OUTBOUND); return withTimer(overAllProcessingTimer, () -> convertToExternalMessage(() -> protocolAdapter.toAdaptable(signal), overAllProcessingTimer)); }
private Optional<ExternalMessage> mapToExternalMessage(final Signal<?> signal) { try { return processor.process(signal); } catch (final DittoRuntimeException e) { log.info("Got DittoRuntimeException during processing Signal: {} - {}", e.getMessage(), e.getDescription().orElse("")); } catch (final Exception e) { log.warning("Got unexpected exception during processing Signal: {}", e.getMessage()); } return Optional.empty(); }
private Optional<ExternalMessage> convertToExternalMessage(final Supplier<Adaptable> adaptableSupplier, final StartedTimer overAllProcessingTimer) { checkNotNull(adaptableSupplier); try { final Adaptable adaptable = withTimer(overAllProcessingTimer.startNewSegment(PROTOCOL_SEGMENT_NAME), adaptableSupplier); enhanceLogFromAdaptable(adaptable); return withTimer(overAllProcessingTimer.startNewSegment(PAYLOAD_SEGMENT_NAME), () -> getMapper(adaptable).map(adaptable)); } catch (final DittoRuntimeException e) { throw e; } catch (final Exception e) { final Optional<DittoHeaders> headers = adaptableSupplier.get() .getHeaders(); final String contentType = headers .map(h -> h.get(ExternalMessage.CONTENT_TYPE_HEADER)) .orElse(""); throw MessageMappingFailedException.newBuilder(contentType) .description("Could not map Adaptable due to unknown problem: " + e.getMessage()) .dittoHeaders(headers.orElseGet(DittoHeaders::empty)) .cause(e) .build(); } }
try { processor = MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); } catch (final DittoRuntimeException dre) { log.info( processor.getRegistry());
private CompletionStage<Status.Status> testMessageMappingProcessor(@Nullable final MappingContext mappingContext) { try { // this one throws DittoRuntimeExceptions when the mapper could not be configured MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); return CompletableFuture.completedFuture(new Status.Success("mapping")); } catch (final DittoRuntimeException dre) { log.info("Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}", dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse("")); getSender().tell(dre, getSelf()); return CompletableFuture.completedFuture(new Status.Failure(dre)); } }
private MessageMapper getMapper(final Adaptable adaptable) { enhanceLogFromAdaptable(adaptable); return registry.getMapper().orElseGet(() -> { log.debug("Falling back to Default MessageMapper for mapping Adaptable as no MessageMapper was present: {}", adaptable); return registry.getDefaultMapper(); }); }
/** * Initializes a new command processor with mappers defined in mapping mappingContext. The dynamic access is needed * to instantiate message mappers for an actor system * * @param mappingContext the mapping Context * @param actorSystem the dynamic access used for message mapper instantiation * @param log the log adapter * @return the processor instance * @throws org.eclipse.ditto.model.connectivity.MessageMapperConfigurationInvalidException if the configuration of * one of the {@code mappingContext} is invalid * @throws org.eclipse.ditto.model.connectivity.MessageMapperConfigurationFailedException if the configuration of * one of the {@code mappingContext} failed for a mapper specific reason */ public static MessageMappingProcessor of(final String connectionId, @Nullable final MappingContext mappingContext, final ActorSystem actorSystem, final DiagnosticLoggingAdapter log) { final MessageMapperRegistry registry = DefaultMessageMapperFactory.of(actorSystem, MessageMappers.class, log) .registryOf(DittoMessageMapper.CONTEXT, mappingContext); final ProtocolConfigReader protocolConfigReader = ProtocolConfigReader.fromRawConfig(actorSystem.settings().config()); final ProtocolAdapter protocolAdapter = protocolConfigReader.loadProtocolAdapterProvider(actorSystem).getProtocolAdapter(null); return new MessageMappingProcessor(connectionId, registry, log, protocolAdapter); }
private Optional<ExternalMessage> convertToExternalMessage(final Supplier<Adaptable> adaptableSupplier, final StartedTimer overAllProcessingTimer) { checkNotNull(adaptableSupplier); try { final Adaptable adaptable = withTimer(overAllProcessingTimer.startNewSegment(PROTOCOL_SEGMENT_NAME), adaptableSupplier); enhanceLogFromAdaptable(adaptable); return withTimer(overAllProcessingTimer.startNewSegment(PAYLOAD_SEGMENT_NAME), () -> getMapper(adaptable).map(adaptable)); } catch (final DittoRuntimeException e) { throw e; } catch (final Exception e) { final Optional<DittoHeaders> headers = adaptableSupplier.get() .getHeaders(); final String contentType = headers .map(h -> h.get(ExternalMessage.CONTENT_TYPE_HEADER)) .orElse(""); throw MessageMappingFailedException.newBuilder(contentType) .description("Could not map Adaptable due to unknown problem: " + e.getMessage()) .dittoHeaders(headers.orElseGet(DittoHeaders::empty)) .cause(e) .build(); } }
try { processor = MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); } catch (final DittoRuntimeException dre) { log.info( processor.getRegistry());
private CompletionStage<Status.Status> testMessageMappingProcessor(@Nullable final MappingContext mappingContext) { try { // this one throws DittoRuntimeExceptions when the mapper could not be configured MessageMappingProcessor.of(connectionId(), mappingContext, getContext().getSystem(), log); return CompletableFuture.completedFuture(new Status.Success("mapping")); } catch (final DittoRuntimeException dre) { log.info("Got DittoRuntimeException during initialization of MessageMappingProcessor: {} {} - desc: {}", dre.getClass().getSimpleName(), dre.getMessage(), dre.getDescription().orElse("")); getSender().tell(dre, getSelf()); return CompletableFuture.completedFuture(new Status.Failure(dre)); } }
private MessageMapper getMapper(final Adaptable adaptable) { enhanceLogFromAdaptable(adaptable); return registry.getMapper().orElseGet(() -> { log.debug("Falling back to Default MessageMapper for mapping Adaptable as no MessageMapper was present: {}", adaptable); return registry.getDefaultMapper(); }); }
/** * Initializes a new command processor with mappers defined in mapping mappingContext. The dynamic access is needed * to instantiate message mappers for an actor system * * @param mappingContext the mapping Context * @param actorSystem the dynamic access used for message mapper instantiation * @param log the log adapter * @return the processor instance * @throws org.eclipse.ditto.model.connectivity.MessageMapperConfigurationInvalidException if the configuration of * one of the {@code mappingContext} is invalid * @throws org.eclipse.ditto.model.connectivity.MessageMapperConfigurationFailedException if the configuration of * one of the {@code mappingContext} failed for a mapper specific reason */ public static MessageMappingProcessor of(final String connectionId, @Nullable final MappingContext mappingContext, final ActorSystem actorSystem, final DiagnosticLoggingAdapter log) { final MessageMapperRegistry registry = DefaultMessageMapperFactory.of(actorSystem, MessageMappers.class, log) .registryOf(DittoMessageMapper.CONTEXT, mappingContext); final ProtocolConfigReader protocolConfigReader = ProtocolConfigReader.fromRawConfig(actorSystem.settings().config()); final ProtocolAdapter protocolAdapter = protocolConfigReader.loadProtocolAdapterProvider(actorSystem).getProtocolAdapter(null); return new MessageMappingProcessor(connectionId, registry, log, protocolAdapter); }
private Optional<InboundExternalMessage> convertMessage(final ExternalMessage message, final StartedTimer overAllProcessingTimer) { checkNotNull(message); try { final Optional<Adaptable> adaptableOpt = withTimer( overAllProcessingTimer.startNewSegment(PAYLOAD_SEGMENT_NAME), () -> getMapper(message).map(message)); return adaptableOpt.map(adaptable -> { enhanceLogFromAdaptable(adaptable); final Signal<?> signal = this.<Signal<?>>withTimer( overAllProcessingTimer.startNewSegment(PROTOCOL_SEGMENT_NAME), () -> protocolAdapter.fromAdaptable(adaptable)); return MappedInboundExternalMessage.of(message, adaptable.getTopicPath(), signal); }); } catch (final DittoRuntimeException e) { throw e; } catch (final Exception e) { throw MessageMappingFailedException.newBuilder(message.findContentType().orElse("")) .description("Could not map ExternalMessage due to unknown problem: " + e.getClass().getSimpleName() + " " + e.getMessage()) .dittoHeaders(DittoHeaders.of(message.getHeaders())) .cause(e) .build(); } }
/** * Processes an ExternalMessage to a Signal. * * @param message the message * @return the signal */ Optional<InboundExternalMessage> process(final ExternalMessage message) { final StartedTimer overAllProcessingTimer = startNewTimer().tag(DIRECTION_TAG_NAME, INBOUND); return withTimer(overAllProcessingTimer, () -> convertMessage(message, overAllProcessingTimer)); }
/** * Processes a Signal to an ExternalMessage. * * @param signal the signal * @return the message */ Optional<ExternalMessage> process(final Signal<?> signal) { final StartedTimer overAllProcessingTimer = startNewTimer().tag(DIRECTION_TAG_NAME, OUTBOUND); return withTimer(overAllProcessingTimer, () -> convertToExternalMessage(() -> protocolAdapter.toAdaptable(signal), overAllProcessingTimer)); }
private Optional<ExternalMessage> mapToExternalMessage(final Signal<?> signal) { try { return processor.process(signal); } catch (final DittoRuntimeException e) { log.info("Got DittoRuntimeException during processing Signal: {} - {}", e.getMessage(), e.getDescription().orElse("")); } catch (final Exception e) { log.warning("Got unexpected exception during processing Signal: {}", e.getMessage()); } return Optional.empty(); }
private Optional<InboundExternalMessage> convertMessage(final ExternalMessage message, final StartedTimer overAllProcessingTimer) { checkNotNull(message); try { final Optional<Adaptable> adaptableOpt = withTimer( overAllProcessingTimer.startNewSegment(PAYLOAD_SEGMENT_NAME), () -> getMapper(message).map(message)); return adaptableOpt.map(adaptable -> { enhanceLogFromAdaptable(adaptable); final Signal<?> signal = this.<Signal<?>>withTimer( overAllProcessingTimer.startNewSegment(PROTOCOL_SEGMENT_NAME), () -> protocolAdapter.fromAdaptable(adaptable)); return MappedInboundExternalMessage.of(message, adaptable.getTopicPath(), signal); }); } catch (final DittoRuntimeException e) { throw e; } catch (final Exception e) { throw MessageMappingFailedException.newBuilder(message.findContentType().orElse("")) .description("Could not map ExternalMessage due to unknown problem: " + e.getClass().getSimpleName() + " " + e.getMessage()) .dittoHeaders(DittoHeaders.of(message.getHeaders())) .cause(e) .build(); } }
private void handle(final ExternalMessage externalMessage) { ConditionChecker.checkNotNull(externalMessage); final String correlationId = externalMessage.getHeaders().get(DittoHeaderDefinition.CORRELATION_ID.getKey()); LogUtil.enhanceLogWithCorrelationId(log, correlationId); LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId); log.debug("Handling ExternalMessage: {}", externalMessage); try { final ExternalMessage messageWithAuthSubject = placeholderSubstitution.apply(externalMessage); final Optional<InboundExternalMessage> inboundMessageOpt = processor.process(messageWithAuthSubject); inboundMessageOpt.ifPresent(inboundMessage -> { final Signal<?> signal = inboundMessage.getSignal(); enhanceLogUtil(signal); applySignalIdEnforcement.accept(messageWithAuthSubject, signal); final Signal<?> adjustedSignal = mapHeaders .andThen(mappedHeaders -> adjustHeaders.apply(messageWithAuthSubject, mappedHeaders)) .andThen(signal::setDittoHeaders) .apply(inboundMessage); startTrace(adjustedSignal); // This message is important to check if a command is accepted for a specific connection, as this // happens quite a lot this is going to the debug level. Use best with a connection-id filter. log.debug("Message successfully mapped to signal: '{}'. Passing to conciergeForwarder", adjustedSignal .getType()); conciergeForwarder.tell(adjustedSignal, getSelf()); }); } catch (final DittoRuntimeException e) { handleDittoRuntimeException(e, externalMessage.getHeaders()); } catch (final Exception e) { log.warning("Got <{}> when message was processed: <{}>", e.getClass().getSimpleName(), e.getMessage()); } }
private void handle(final ExternalMessage externalMessage) { ConditionChecker.checkNotNull(externalMessage); final String correlationId = externalMessage.getHeaders().get(DittoHeaderDefinition.CORRELATION_ID.getKey()); LogUtil.enhanceLogWithCorrelationId(log, correlationId); LogUtil.enhanceLogWithCustomField(log, BaseClientData.MDC_CONNECTION_ID, connectionId); log.debug("Handling ExternalMessage: {}", externalMessage); try { final ExternalMessage messageWithAuthSubject = placeholderSubstitution.apply(externalMessage); final Optional<InboundExternalMessage> inboundMessageOpt = processor.process(messageWithAuthSubject); inboundMessageOpt.ifPresent(inboundMessage -> { final Signal<?> signal = inboundMessage.getSignal(); enhanceLogUtil(signal); applySignalIdEnforcement.accept(messageWithAuthSubject, signal); final Signal<?> adjustedSignal = mapHeaders .andThen(mappedHeaders -> adjustHeaders.apply(messageWithAuthSubject, mappedHeaders)) .andThen(signal::setDittoHeaders) .apply(inboundMessage); startTrace(adjustedSignal); // This message is important to check if a command is accepted for a specific connection, as this // happens quite a lot this is going to the debug level. Use best with a connection-id filter. log.debug("Message successfully mapped to signal: '{}'. Passing to conciergeForwarder", adjustedSignal .getType()); conciergeForwarder.tell(adjustedSignal, getSelf()); }); } catch (final DittoRuntimeException e) { handleDittoRuntimeException(e, externalMessage.getHeaders()); } catch (final Exception e) { log.warning("Got <{}> when message was processed: <{}>", e.getClass().getSimpleName(), e.getMessage()); } }