@Override public MessageMappingProcessorActor create() { return new MessageMappingProcessorActor(publisherActor, conciergeForwarder, processor, connectionId); } });
private void handleOutboundSignal(final OutboundSignal outbound) { final Signal<?> signal = outbound.getSource(); enhanceLogUtil(signal); log.debug("Handling outbound signal: {}", signal); mapToExternalMessage(signal) .map(message -> OutboundSignalFactory.newMappedOutboundSignal(outbound, message)) .ifPresent(outboundSignal -> publisherActor.forward(outboundSignal, getContext())); }
private void finishTrace(final WithDittoHeaders<? extends Signal> response) { if (ThingErrorResponse.class.isAssignableFrom(response.getClass())) { finishTrace(response, ((ThingErrorResponse) response).getDittoRuntimeException()); } else { finishTrace(response, null); } }
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 handleDittoRuntimeException(final DittoRuntimeException exception, final Map<String, String> dittoHeaders) { final ThingErrorResponse errorResponse = ThingErrorResponse.of(exception, DittoHeaders.newBuilder(exception.getDittoHeaders()) .putHeaders(dittoHeaders) .build()); enhanceLogUtil(exception); final String stackTrace = stackTraceAsString(exception); log.info("Got DittoRuntimeException '{}' when ExternalMessage was processed: {} - {}. StackTrace: {}", exception.getErrorCode(), exception.getMessage(), exception.getDescription().orElse(""), stackTrace); handleCommandResponse(errorResponse); }
private void handleCommandResponse(final CommandResponse<?> response) { enhanceLogUtil(response); finishTrace(response); if (response.getDittoHeaders().isResponseRequired()) { if (response.getStatusCodeValue() < HttpStatusCode.BAD_REQUEST.toInt()) { log.debug("Received response: {}", response); } else { log.debug("Received error response: {}", response.toJsonString()); } handleSignal(response); } else { log.debug("Requester did not require response (via DittoHeader '{}') - not mapping back to ExternalMessage", DittoHeaderDefinition.RESPONSE_REQUIRED); } }
/** * Is called for responses or errors which were directly sent to the mapping actor as a response. * * @param signal the response/error */ private void handleSignal(final Signal<?> signal) { // map to outbound signal without authorized target (responses and errors are only sent to its origin) log.debug("Handling raw signal: {}", signal); handleOutboundSignal(OutboundSignalFactory.newOutboundSignal(signal, Collections.emptySet())); }
private void handleDittoRuntimeException(final DittoRuntimeException exception) { handleDittoRuntimeException(exception, exception.getDittoHeaders()); }
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 handleDittoRuntimeException(final DittoRuntimeException exception, final Map<String, String> dittoHeaders) { final ThingErrorResponse errorResponse = ThingErrorResponse.of(exception, DittoHeaders.newBuilder(exception.getDittoHeaders()) .putHeaders(dittoHeaders) .build()); enhanceLogUtil(exception); final String stackTrace = stackTraceAsString(exception); log.info("Got DittoRuntimeException '{}' when ExternalMessage was processed: {} - {}. StackTrace: {}", exception.getErrorCode(), exception.getMessage(), exception.getDescription().orElse(""), stackTrace); handleCommandResponse(errorResponse); }
private void handleCommandResponse(final CommandResponse<?> response) { enhanceLogUtil(response); finishTrace(response); if (response.getDittoHeaders().isResponseRequired()) { if (response.getStatusCodeValue() < HttpStatusCode.BAD_REQUEST.toInt()) { log.debug("Received response: {}", response); } else { log.debug("Received error response: {}", response.toJsonString()); } handleSignal(response); } else { log.debug("Requester did not require response (via DittoHeader '{}') - not mapping back to ExternalMessage", DittoHeaderDefinition.RESPONSE_REQUIRED); } }
/** * Is called for responses or errors which were directly sent to the mapping actor as a response. * * @param signal the response/error */ private void handleSignal(final Signal<?> signal) { // map to outbound signal without authorized target (responses and errors are only sent to its origin) log.debug("Handling raw signal: {}", signal); handleOutboundSignal(OutboundSignalFactory.newOutboundSignal(signal, Collections.emptySet())); }
private void handleDittoRuntimeException(final DittoRuntimeException exception) { handleDittoRuntimeException(exception, exception.getDittoHeaders()); }
private void handleOutboundSignal(final OutboundSignal outbound) { final Signal<?> signal = outbound.getSource(); enhanceLogUtil(signal); log.debug("Handling outbound signal: {}", signal); mapToExternalMessage(signal) .map(message -> OutboundSignalFactory.newMappedOutboundSignal(outbound, message)) .ifPresent(outboundSignal -> publisherActor.forward(outboundSignal, getContext())); }
@Override public MessageMappingProcessorActor create() { return new MessageMappingProcessorActor(publisherActor, conciergeForwarder, processor, connectionId); } });
private void finishTrace(final WithDittoHeaders<? extends Signal> response) { if (ThingErrorResponse.class.isAssignableFrom(response.getClass())) { finishTrace(response, ((ThingErrorResponse) response).getDittoRuntimeException()); } else { finishTrace(response, null); } }
/** * Creates Akka configuration object for this actor. * * @param publisherActor actor that handles/publishes outgoing messages. * @param conciergeForwarder the actor used to send signals to the concierge service. * @param processor the MessageMappingProcessor to use. * @param connectionId the connection id. * @return the Akka configuration Props object. */ public static Props props(final ActorRef publisherActor, final ActorRef conciergeForwarder, final MessageMappingProcessor processor, final String connectionId) { return Props.create(MessageMappingProcessorActor.class, new Creator<MessageMappingProcessorActor>() { private static final long serialVersionUID = 1L; @Override public MessageMappingProcessorActor create() { return new MessageMappingProcessorActor(publisherActor, conciergeForwarder, processor, connectionId); } }); }
private void finishTrace(final WithDittoHeaders<? extends Signal> response, @Nullable final Throwable cause) { response.getDittoHeaders().getCorrelationId().ifPresent(correlationId -> { try { finishTrace(correlationId, cause); } catch (final IllegalArgumentException e) { log.debug("Trace missing for response: '{}'", response); } }); }
/** * Creates Akka configuration object for this actor. * * @param publisherActor actor that handles/publishes outgoing messages. * @param conciergeForwarder the actor used to send signals to the concierge service. * @param processor the MessageMappingProcessor to use. * @param connectionId the connection id. * @return the Akka configuration Props object. */ public static Props props(final ActorRef publisherActor, final ActorRef conciergeForwarder, final MessageMappingProcessor processor, final String connectionId) { return Props.create(MessageMappingProcessorActor.class, new Creator<MessageMappingProcessorActor>() { private static final long serialVersionUID = 1L; @Override public MessageMappingProcessorActor create() { return new MessageMappingProcessorActor(publisherActor, conciergeForwarder, processor, connectionId); } }); }
private void finishTrace(final WithDittoHeaders<? extends Signal> response, @Nullable final Throwable cause) { response.getDittoHeaders().getCorrelationId().ifPresent(correlationId -> { try { finishTrace(correlationId, cause); } catch (final IllegalArgumentException e) { log.debug("Trace missing for response: '{}'", response); } }); }