Class MessageHelper


  • public final class MessageHelper
    extends Object
    Some helper methods when working with Message.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void copyBody​(org.apache.camel.Message source, org.apache.camel.Message target)
      Copies the body of the source message to the body of the target message while preserving the data type if the messages are both of type DataTypeAware.
      static void copyHeaders​(org.apache.camel.Message source, org.apache.camel.Message target, boolean override)
      Copies the headers from the source to the target message.
      static void copyHeaders​(org.apache.camel.Message source, org.apache.camel.Message target, org.apache.camel.spi.HeaderFilterStrategy strategy, boolean override)
      Copies the headers from the source to the target message.
      static String dumpAsJSon​(org.apache.camel.Message message)
      Dumps the message as a generic JSon structure.
      static String dumpAsJSon​(org.apache.camel.Message message, boolean includeBody)
      Dumps the message as a generic JSon structure.
      static String dumpAsJSon​(org.apache.camel.Message message, boolean includeBody, int indent)
      Dumps the message as a generic JSon structure.
      static String dumpAsJSon​(org.apache.camel.Message message, boolean includeBody, int indent, boolean allowCachedStreams, boolean allowStreams, boolean allowFiles, int maxChars, boolean pretty)
      Dumps the message as a generic JSon structure.
      static String dumpAsJSon​(org.apache.camel.Message message, boolean includeBody, int indent, boolean allowStreams, boolean allowFiles, int maxChars, boolean pretty)
      Dumps the message as a generic JSon structure.
      static String dumpAsXml​(org.apache.camel.Message message)
      Dumps the message as a generic XML structure.
      static String dumpAsXml​(org.apache.camel.Message message, boolean includeBody)
      Dumps the message as a generic XML structure.
      static String dumpAsXml​(org.apache.camel.Message message, boolean includeBody, int indent)
      Dumps the message as a generic XML structure.
      static String dumpAsXml​(org.apache.camel.Message message, boolean includeBody, int indent, boolean allowCachedStreams, boolean allowStreams, boolean allowFiles, int maxChars)
      Dumps the message as a generic XML structure.
      static String dumpAsXml​(org.apache.camel.Message message, boolean includeBody, int indent, boolean allowStreams, boolean allowFiles, int maxChars)
      Dumps the message as a generic XML structure.
      static String dumpMessageHistoryStacktrace​(org.apache.camel.Exchange exchange, org.apache.camel.spi.ExchangeFormatter exchangeFormatter, boolean logStackTrace)
      Dumps the MessageHistory from the Exchange in a human readable format.
      static String extractBodyAsString​(org.apache.camel.Message message)
      Extracts the given body and returns it as a String, that can be used for logging etc.
      static String extractBodyForLogging​(org.apache.camel.Message message)
      Extracts the body for logging purpose.
      static String extractBodyForLogging​(org.apache.camel.Message message, String prepend)
      Extracts the body for logging purpose.
      static String extractBodyForLogging​(org.apache.camel.Message message, String prepend, boolean allowStreams, boolean allowFiles)
      Extracts the body for logging purpose.
      static String extractBodyForLogging​(org.apache.camel.Message message, String prepend, boolean allowCachedStreams, boolean allowStreams, boolean allowFiles, int maxChars)
      Extracts the body for logging purpose.
      static String extractBodyForLogging​(org.apache.camel.Message message, String prepend, boolean allowStreams, boolean allowFiles, int maxChars)
      Extracts the body for logging purpose.
      static String extractValueForLogging​(Object value, org.apache.camel.Message message)
      Extracts the value for logging purpose.
      static String extractValueForLogging​(Object obj, org.apache.camel.Message message, boolean allowCachedStreams, boolean allowStreams, boolean allowFiles, int maxChars)
      Extracts the value for logging purpose.
      static String extractValueForLogging​(Object obj, org.apache.camel.Message message, boolean allowStreams, boolean allowFiles, int maxChars)
      Extracts the value for logging purpose.
      static String getBodyTypeName​(org.apache.camel.Message message)
      Gets the given body class type name as a String.
      static String getContentEncoding​(org.apache.camel.Message message)
      Returns the MIME content encoding on the message or null if none defined
      static String getContentType​(org.apache.camel.Message message)
      Returns the MIME content type on the message or null if none defined
      static void resetStreamCache​(org.apache.camel.Message message)
      If the message body contains a StreamCache instance, reset the cache to enable reading from it again.
    • Method Detail

      • extractBodyAsString

        public static String extractBodyAsString​(org.apache.camel.Message message)
        Extracts the given body and returns it as a String, that can be used for logging etc.

        Will handle stream based bodies wrapped in StreamCache.

        Parameters:
        message - the message with the body
        Returns:
        the body as String, can return null if no body
      • getBodyTypeName

        public static String getBodyTypeName​(org.apache.camel.Message message)
        Gets the given body class type name as a String.

        Will skip java.lang. for the build in Java types.

        Parameters:
        message - the message with the body
        Returns:
        the body type name as String, can return null if no body
      • resetStreamCache

        public static void resetStreamCache​(org.apache.camel.Message message)
        If the message body contains a StreamCache instance, reset the cache to enable reading from it again.
        Parameters:
        message - the message for which to reset the body
      • getContentType

        public static String getContentType​(org.apache.camel.Message message)
        Returns the MIME content type on the message or null if none defined
      • getContentEncoding

        public static String getContentEncoding​(org.apache.camel.Message message)
        Returns the MIME content encoding on the message or null if none defined
      • extractBodyForLogging

        public static String extractBodyForLogging​(org.apache.camel.Message message)
        Extracts the body for logging purpose.

        Will clip the body if its too big for logging. Will prepend the message with Message:

        Parameters:
        message - the message
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_STREAMS, Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractValueForLogging

        public static String extractValueForLogging​(Object value,
                                                    org.apache.camel.Message message)
        Extracts the value for logging purpose.

        Will clip the value if its too big for logging.

        Parameters:
        value - the value
        message - the message
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_STREAMS, Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractBodyForLogging

        public static String extractBodyForLogging​(org.apache.camel.Message message,
                                                   String prepend)
        Extracts the body for logging purpose.

        Will clip the body if its too big for logging.

        Parameters:
        message - the message
        prepend - a message to prepend
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_STREAMS, Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractBodyForLogging

        public static String extractBodyForLogging​(org.apache.camel.Message message,
                                                   String prepend,
                                                   boolean allowStreams,
                                                   boolean allowFiles)
        Extracts the body for logging purpose.

        Will clip the body if its too big for logging.

        Parameters:
        message - the message
        prepend - a message to prepend
        allowStreams - whether or not streams is allowed
        allowFiles - whether or not files is allowed (currently not in use)
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_STREAMS, Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractBodyForLogging

        public static String extractBodyForLogging​(org.apache.camel.Message message,
                                                   String prepend,
                                                   boolean allowStreams,
                                                   boolean allowFiles,
                                                   int maxChars)
        Extracts the body for logging purpose.

        Will clip the body if its too big for logging.

        Parameters:
        message - the message
        prepend - a message to prepend (optional)
        allowStreams - whether or not streams is allowed
        allowFiles - whether or not files is allowed (currently not in use)
        maxChars - limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractBodyForLogging

        public static String extractBodyForLogging​(org.apache.camel.Message message,
                                                   String prepend,
                                                   boolean allowCachedStreams,
                                                   boolean allowStreams,
                                                   boolean allowFiles,
                                                   int maxChars)
        Extracts the body for logging purpose.

        Will clip the body if its too big for logging.

        Parameters:
        message - the message
        prepend - a message to prepend (optional)
        allowCachedStreams - whether or not cached streams is allowed
        allowStreams - whether or not streams is allowed
        allowFiles - whether or not files is allowed (currently not in use)
        maxChars - limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractValueForLogging

        public static String extractValueForLogging​(Object obj,
                                                    org.apache.camel.Message message,
                                                    boolean allowStreams,
                                                    boolean allowFiles,
                                                    int maxChars)
        Extracts the value for logging purpose.

        Will clip the value if its too big for logging.

        Parameters:
        obj - the value
        message - the message
        allowStreams - whether or not streams is allowed
        allowFiles - whether or not files is allowed (currently not in use)
        maxChars - limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • extractValueForLogging

        public static String extractValueForLogging​(Object obj,
                                                    org.apache.camel.Message message,
                                                    boolean allowCachedStreams,
                                                    boolean allowStreams,
                                                    boolean allowFiles,
                                                    int maxChars)
        Extracts the value for logging purpose.

        Will clip the value if its too big for logging.

        Parameters:
        obj - the value
        message - the message
        allowCachedStreams - whether or not cached streams is allowed
        allowStreams - whether or not streams is allowed
        allowFiles - whether or not files is allowed (currently not in use)
        maxChars - limit to maximum number of chars. Use 0 for not limit, and -1 for turning logging message body off.
        Returns:
        the logging message
        See Also:
        Exchange.LOG_DEBUG_BODY_MAX_CHARS
      • dumpAsXml

        public static String dumpAsXml​(org.apache.camel.Message message)
        Dumps the message as a generic XML structure.
        Parameters:
        message - the message
        Returns:
        the XML
      • dumpAsXml

        public static String dumpAsXml​(org.apache.camel.Message message,
                                       boolean includeBody)
        Dumps the message as a generic XML structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        Returns:
        the XML
      • dumpAsXml

        public static String dumpAsXml​(org.apache.camel.Message message,
                                       boolean includeBody,
                                       int indent)
        Dumps the message as a generic XML structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        Returns:
        the XML
      • dumpAsXml

        public static String dumpAsXml​(org.apache.camel.Message message,
                                       boolean includeBody,
                                       int indent,
                                       boolean allowStreams,
                                       boolean allowFiles,
                                       int maxChars)
        Dumps the message as a generic XML structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        allowStreams - whether to include message body if they are stream based
        allowFiles - whether to include message body if they are file based
        maxChars - clip body after maximum chars (to avoid very big messages). Use 0 or negative value to not limit at all.
        Returns:
        the XML
      • dumpAsXml

        public static String dumpAsXml​(org.apache.camel.Message message,
                                       boolean includeBody,
                                       int indent,
                                       boolean allowCachedStreams,
                                       boolean allowStreams,
                                       boolean allowFiles,
                                       int maxChars)
        Dumps the message as a generic XML structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        allowCachedStreams - whether to include message body if they are stream cache based
        allowStreams - whether to include message body if they are stream based
        allowFiles - whether to include message body if they are file based
        maxChars - clip body after maximum chars (to avoid very big messages). Use 0 or negative value to not limit at all.
        Returns:
        the XML
      • copyBody

        public static void copyBody​(org.apache.camel.Message source,
                                    org.apache.camel.Message target)
        Copies the body of the source message to the body of the target message while preserving the data type if the messages are both of type DataTypeAware. .
        Parameters:
        source - the source message from which the body must be extracted.
        target - the target message that will receive the body.
      • copyHeaders

        public static void copyHeaders​(org.apache.camel.Message source,
                                       org.apache.camel.Message target,
                                       boolean override)
        Copies the headers from the source to the target message.
        Parameters:
        source - the source message
        target - the target message
        override - whether to override existing headers
      • copyHeaders

        public static void copyHeaders​(org.apache.camel.Message source,
                                       org.apache.camel.Message target,
                                       org.apache.camel.spi.HeaderFilterStrategy strategy,
                                       boolean override)
        Copies the headers from the source to the target message.
        Parameters:
        source - the source message
        target - the target message
        strategy - the header filter strategy which could help us to filter the protocol message headers
        override - whether to override existing headers
      • dumpMessageHistoryStacktrace

        public static String dumpMessageHistoryStacktrace​(org.apache.camel.Exchange exchange,
                                                          org.apache.camel.spi.ExchangeFormatter exchangeFormatter,
                                                          boolean logStackTrace)
        Dumps the MessageHistory from the Exchange in a human readable format.
        Parameters:
        exchange - the exchange
        exchangeFormatter - if provided then information about the exchange is included in the dump
        logStackTrace - whether to include a header for the stacktrace, to be added (not included in this dump).
        Returns:
        a human readable message history as a table
      • dumpAsJSon

        public static String dumpAsJSon​(org.apache.camel.Message message)
        Dumps the message as a generic JSon structure.
        Parameters:
        message - the message
        Returns:
        the JSon
      • dumpAsJSon

        public static String dumpAsJSon​(org.apache.camel.Message message,
                                        boolean includeBody)
        Dumps the message as a generic JSon structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        Returns:
        the JSon
      • dumpAsJSon

        public static String dumpAsJSon​(org.apache.camel.Message message,
                                        boolean includeBody,
                                        int indent)
        Dumps the message as a generic JSon structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        Returns:
        the JSon
      • dumpAsJSon

        public static String dumpAsJSon​(org.apache.camel.Message message,
                                        boolean includeBody,
                                        int indent,
                                        boolean allowStreams,
                                        boolean allowFiles,
                                        int maxChars,
                                        boolean pretty)
        Dumps the message as a generic JSon structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        allowStreams - whether to include message body if they are stream based
        allowFiles - whether to include message body if they are file based
        maxChars - clip body after maximum chars (to avoid very big messages). Use 0 or negative value to not limit at all.
        Returns:
        the JSon
      • dumpAsJSon

        public static String dumpAsJSon​(org.apache.camel.Message message,
                                        boolean includeBody,
                                        int indent,
                                        boolean allowCachedStreams,
                                        boolean allowStreams,
                                        boolean allowFiles,
                                        int maxChars,
                                        boolean pretty)
        Dumps the message as a generic JSon structure.
        Parameters:
        message - the message
        includeBody - whether or not to include the message body
        indent - number of spaces to indent
        allowCachedStreams - whether to include message body if they are stream cached based
        allowStreams - whether to include message body if they are stream based
        allowFiles - whether to include message body if they are file based
        maxChars - clip body after maximum chars (to avoid very big messages). Use 0 or negative value to not limit at all.
        Returns:
        the JSon