private static String extractPayloadAsString(final ExternalMessage message) { final Optional<String> payload; if (message.isTextMessage()) { payload = message.getTextPayload(); } else if (message.isBytesMessage()) { final Charset charset = Optional.ofNullable(message.getHeaders() .get(ExternalMessage.CONTENT_TYPE_HEADER)) .map(MessageMappers::determineCharset) .orElse(StandardCharsets.UTF_8); payload = message.getBytePayload().map(charset::decode).map(CharBuffer::toString); } else { payload = Optional.empty(); } return payload.filter(s -> !s.isEmpty()).orElseThrow(() -> MessageMappingFailedException.newBuilder(message.findContentType().orElse("")) .description( "As payload was absent or empty, please make sure to send payload in your messages.") .dittoHeaders(DittoHeaders.of((message.getHeaders()))) .build()); }
@Override protected MessageMappingFailedException doBuild(final DittoHeaders dittoHeaders, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new MessageMappingFailedException(dittoHeaders, message, description, cause, href); } }
/** * Constructs a new {@code MessageMappingFailedException} object with the exception message extracted from the given * JSON object. * * @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from. * @param dittoHeaders the headers of the command which resulted in this exception. * @return the new MessageMappingFailedException. * @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link * JsonFields#MESSAGE} field. */ public static MessageMappingFailedException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new Builder() .dittoHeaders(dittoHeaders) .message(readMessage(jsonObject)) .description(readDescription(jsonObject).orElse(DEFAULT_DESCRIPTION)) .href(readHRef(jsonObject).orElse(null)) .build(); }
private static String extractPayloadAsString(final ExternalMessage message) { final Optional<String> payload; if (message.isTextMessage()) { payload = message.getTextPayload(); } else if (message.isBytesMessage()) { final Charset charset = Optional.ofNullable(message.getHeaders() .get(ExternalMessage.CONTENT_TYPE_HEADER)) .map(MessageMappers::determineCharset) .orElse(StandardCharsets.UTF_8); payload = message.getBytePayload().map(charset::decode).map(CharBuffer::toString); } else { payload = Optional.empty(); } return payload.filter(s -> !s.isEmpty()).orElseThrow(() -> MessageMappingFailedException.newBuilder(message.findContentType().orElse("")) .description( "As payload was absent or empty, please make sure to send payload in your messages.") .dittoHeaders(DittoHeaders.of((message.getHeaders()))) .build()); }
/** * Constructs a new {@code MessageMappingFailedException} object with the exception message extracted from the given * JSON object. * * @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from. * @param dittoHeaders the headers of the command which resulted in this exception. * @return the new MessageMappingFailedException. * @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link * JsonFields#MESSAGE} field. */ public static MessageMappingFailedException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new Builder() .dittoHeaders(dittoHeaders) .message(readMessage(jsonObject)) .description(readDescription(jsonObject).orElse(DEFAULT_DESCRIPTION)) .href(readHRef(jsonObject).orElse(null)) .build(); }
@Override protected MessageMappingFailedException doBuild(final DittoHeaders dittoHeaders, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new MessageMappingFailedException(dittoHeaders, message, description, cause, href); } }
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(); } }
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(); } }
messageBuilder.withText(((CharSequence) textPayload).toString()); } else { throw MessageMappingFailedException.newBuilder("") .description("Neither <bytePayload> nor <textPayload> were defined in the outgoing script") .dittoHeaders(adaptable.getHeaders().orElse(DittoHeaders.empty())) adaptable.getHeaders().orElseGet(DittoHeaders::empty)); } catch (final Throwable e) { throw MessageMappingFailedException.newBuilder(MessageMapper.findContentType(adaptable).orElse("")) .description(e.getMessage()) .dittoHeaders(adaptable.getHeaders().orElseGet(DittoHeaders::empty))
messageBuilder.withText(((CharSequence) textPayload).toString()); } else { throw MessageMappingFailedException.newBuilder("") .description("Neither <bytePayload> nor <textPayload> were defined in the outgoing script") .dittoHeaders(adaptable.getHeaders().orElse(DittoHeaders.empty())) adaptable.getHeaders().orElseGet(DittoHeaders::empty)); } catch (final Throwable e) { throw MessageMappingFailedException.newBuilder(MessageMapper.findContentType(adaptable).orElse("")) .description(e.getMessage()) .dittoHeaders(adaptable.getHeaders().orElseGet(DittoHeaders::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 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(); } }
/** * Build {@link MessageMappingFailedException} from a {@link RhinoException}. * @param e the original exception thrown by the rhino engine * @param contentType the content type of the message which could not be mapped * @param dittoHeaders the {@link DittoHeaders} of the original message * @return a {@link MessageMappingFailedException} containing information about the javascript error that occurred */ default MessageMappingFailedException buildMessageMappingFailedException(final RhinoException e, final String contentType, final DittoHeaders dittoHeaders) { final boolean sourceExists = e.lineSource() != null && !e.lineSource().isEmpty(); final String lineSource = sourceExists ? (", source:\n" + e.lineSource()) : ""; final boolean stackExists = e.getScriptStackTrace() != null && !e.getScriptStackTrace().isEmpty(); final String scriptStackTrace = stackExists ? (", stack:\n" + e.getScriptStackTrace()) : ""; return MessageMappingFailedException.newBuilder(contentType) .description(e.getMessage() + " - in line/column #" + e.lineNumber() + "/" + e.columnNumber() + lineSource + scriptStackTrace) .dittoHeaders(dittoHeaders) .cause(e) .build(); } }
/** * Build {@link MessageMappingFailedException} from a {@link RhinoException}. * @param e the original exception thrown by the rhino engine * @param contentType the content type of the message which could not be mapped * @param dittoHeaders the {@link DittoHeaders} of the original message * @return a {@link MessageMappingFailedException} containing information about the javascript error that occurred */ default MessageMappingFailedException buildMessageMappingFailedException(final RhinoException e, final String contentType, final DittoHeaders dittoHeaders) { final boolean sourceExists = e.lineSource() != null && !e.lineSource().isEmpty(); final String lineSource = sourceExists ? (", source:\n" + e.lineSource()) : ""; final boolean stackExists = e.getScriptStackTrace() != null && !e.getScriptStackTrace().isEmpty(); final String scriptStackTrace = stackExists ? (", stack:\n" + e.getScriptStackTrace()) : ""; return MessageMappingFailedException.newBuilder(contentType) .description(e.getMessage() + " - in line/column #" + e.lineNumber() + "/" + e.columnNumber() + lineSource + scriptStackTrace) .dittoHeaders(dittoHeaders) .cause(e) .build(); } }
DittoHeaders.of(message.getHeaders())); } catch (final Throwable e) { throw MessageMappingFailedException.newBuilder(message.findContentType().orElse(null)) .description(e.getMessage()) .dittoHeaders(DittoHeaders.of(message.getHeaders()))
DittoHeaders.of(message.getHeaders())); } catch (final Throwable e) { throw MessageMappingFailedException.newBuilder(message.findContentType().orElse(null)) .description(e.getMessage()) .dittoHeaders(DittoHeaders.of(message.getHeaders()))