Class AttachmentUtil

java.lang.Object
com.mirth.connect.server.userutil.AttachmentUtil

public class AttachmentUtil extends Object
Provides utility methods for creating, retrieving, and re-attaching message attachments.
  • Method Details

    • reAttachMessage

      public static byte[] reAttachMessage(String raw, ImmutableConnectorMessage connectorMessage, String charsetEncoding, boolean binary)
      Replaces any unique attachment tokens (e.g. "${ATTACH:id}") with the corresponding attachment content, and returns the full post-replacement message as a byte array.
      Parameters:
      raw - The raw message string to replace tokens from.
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      charsetEncoding - If binary mode is not used, the resulting byte array will be encoded using this charset.
      binary - If enabled, the raw data is assumed to be Base64 encoded. The resulting byte array will be the raw Base64 decoded bytes.
      Returns:
      The resulting message as a byte array, with all applicable attachment content re-inserted.
    • reAttachMessage

      public static byte[] reAttachMessage(String raw, ImmutableConnectorMessage connectorMessage, String charsetEncoding, boolean binary, boolean reattach, boolean localOnly)
      Replaces any unique attachment tokens (e.g. "${ATTACH:id}") with the corresponding attachment content, and returns the full post-replacement message as a byte array.
      Parameters:
      raw - The raw message string to replace tokens from.
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      charsetEncoding - If binary mode is not used, the resulting byte array will be encoded using this charset.
      binary - If enabled, the raw data is assumed to be Base64 encoded. The resulting byte array will be the raw Base64 decoded bytes.
      reattach - If true, attachment tokens will be replaced with the actual attachment content. Otherwise, local attachment tokens will be replaced only with the corresponding expanded tokens.
      localOnly - If true, only local attachment tokens will be replaced, and expanded tokens will be ignored.
      Returns:
      The resulting message as a byte array, with all applicable attachment content re-inserted.
    • reAttachMessage

      public static String reAttachMessage(ImmutableConnectorMessage connectorMessage)
      Replaces any unique attachment tokens (e.g. "${ATTACH:id}") with the corresponding attachment content, and returns the full post-replacement message.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID. The message string will be either the encoded or raw content.
      Returns:
      The resulting message with all applicable attachment content re-inserted.
    • reAttachMessage

      public static String reAttachMessage(String raw, ImmutableConnectorMessage connectorMessage)
      Replaces any unique attachment tokens (e.g. "${ATTACH:id}") with the corresponding attachment content, and returns the full post-replacement message.
      Parameters:
      raw - The raw message string to replace tokens from.
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      Returns:
      The resulting message with all applicable attachment content re-inserted.
    • getMessageAttachmentIds

      public static List<String> getMessageAttachmentIds(ImmutableConnectorMessage connectorMessage) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Returns a List of attachment IDs associated with the current channel / message.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      Returns:
      A List of attachment IDs associated with the current channel / message.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment IDs could be retrieved.
    • getMessageAttachmentIds

      public static List<String> getMessageAttachmentIds(String channelId, Long messageId) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Returns a List of attachment IDs associated with the current channel / message.
      Parameters:
      channelId - The ID of the channel the attachments are associated with.
      messageId - The ID of the message the attachments are associated with.
      Returns:
      A List of attachment IDs associated with the current channel / message.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment IDs could be retrieved.
    • getMessageAttachments

      public static List<Attachment> getMessageAttachments(ImmutableConnectorMessage connectorMessage) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves all attachments associated with a connector message.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      Returns:
      A list of attachments associated with the connector message.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • getMessageAttachments

      public static List<Attachment> getMessageAttachments(ImmutableConnectorMessage connectorMessage, boolean base64Decode) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves all attachments associated with a connector message.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      base64Decode - If true, the content of each attachment will first be Base64 decoded for convenient use.
      Returns:
      A list of attachments associated with the connector message.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • getMessageAttachments

      public static List<Attachment> getMessageAttachments(String channelId, Long messageId) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves all attachments associated with a specific channel/message ID.
      Parameters:
      channelId - The ID of the channel to retrieve the attachments from.
      messageId - The ID of the message to retrieve the attachments from.
      Returns:
      A list of attachments associated with the channel/message ID.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • getMessageAttachments

      public static List<Attachment> getMessageAttachments(String channelId, Long messageId, boolean base64Decode) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves all attachments associated with a specific channel/message ID.
      Parameters:
      channelId - The ID of the channel to retrieve the attachments from.
      messageId - The ID of the message to retrieve the attachments from.
      base64Decode - If true, the content of each attachment will first be Base64 decoded for convenient use.
      Returns:
      A list of attachments associated with the channel/message ID.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • getMessageAttachment

      public static Attachment getMessageAttachment(ImmutableConnectorMessage connectorMessage, String attachmentId) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from the current channel/message ID.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      attachmentId - The ID of the attachment to retrieve.
      Returns:
      The attachment associated with the given IDs, or null if none was found.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment could not be retrieved.
    • getMessageAttachment

      public static Attachment getMessageAttachment(ImmutableConnectorMessage connectorMessage, String attachmentId, boolean base64Decode) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from the current channel/message ID.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message, used to identify the channel/message ID.
      attachmentId - The ID of the attachment to retrieve.
      base64Decode - If true, the content of each attachment will first be Base64 decoded for convenient use.
      Returns:
      The attachment associated with the given IDs, or null if none was found.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment could not be retrieved.
    • getMessageAttachment

      public static Attachment getMessageAttachment(String channelId, Long messageId, String attachmentId) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from a specific channel/message ID.
      Parameters:
      channelId - The ID of the channel to retrieve the attachment from.
      messageId - The ID of the message to retrieve the attachment from.
      attachmentId - The ID of the attachment to retrieve.
      Returns:
      The attachment associated with the given IDs, or null if none was found.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment could not be retrieved.
    • getMessageAttachment

      public static Attachment getMessageAttachment(String channelId, Long messageId, String attachmentId, boolean base64Decode) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from a specific channel/message ID.
      Parameters:
      channelId - The ID of the channel to retrieve the attachment from.
      messageId - The ID of the message to retrieve the attachment from.
      attachmentId - The ID of the attachment to retrieve.
      base64Decode - If true, the content of each attachment will first be Base64 decoded for convenient use.
      Returns:
      The attachment associated with the given IDs, or null if none was found.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachment could not be retrieved.
    • getMessageAttachmentsFromSourceChannel

      public static List<Attachment> getMessageAttachmentsFromSourceChannel(ImmutableConnectorMessage connectorMessage) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from an upstream channel that sent a message to the current channel.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message. The channel ID and message ID will be retrieved from the source map.
      Returns:
      A list of attachments associated with the source channel/message IDs.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • getMessageAttachmentsFromSourceChannel

      public static List<Attachment> getMessageAttachmentsFromSourceChannel(ImmutableConnectorMessage connectorMessage, boolean base64Decode) throws com.mirth.connect.donkey.model.message.MessageSerializerException
      Retrieves an attachment from an upstream channel that sent a message to the current channel.
      Parameters:
      connectorMessage - The ConnectorMessage associated with this message. The channel ID and message ID will be retrieved from the source map.
      base64Decode - If true, the content of each attachment will first be Base64 decoded for convenient use.
      Returns:
      A list of attachments associated with the source channel/message IDs.
      Throws:
      com.mirth.connect.donkey.model.message.MessageSerializerException - If the attachments could not be retrieved.
    • addAttachment

      public static Attachment addAttachment(List<Attachment> attachments, Object content, String type) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Creates an Attachment and adds it to the provided list.
      Parameters:
      attachments - The list of attachments to add to.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      Returns:
      The attachment added to the list.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • addAttachment

      public static Attachment addAttachment(List<Attachment> attachments, Object content, String type, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Creates an Attachment and adds it to the provided list.
      Parameters:
      attachments - The list of attachments to add to.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment added to the list.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • createAttachment

      public static Attachment createAttachment(ImmutableConnectorMessage connectorMessage, Object content, String type) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Creates an attachment associated with a given connector message, and inserts it into the database.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      Returns:
      The attachment that was created and inserted.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • createAttachment

      public static Attachment createAttachment(ImmutableConnectorMessage connectorMessage, Object content, String type, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Creates an attachment associated with a given connector message, and inserts it into the database.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment that was created and inserted.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(ImmutableConnectorMessage connectorMessage, String attachmentId, Object content, String type) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      attachmentId - The unique ID of the attachment to update.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(ImmutableConnectorMessage connectorMessage, String attachmentId, Object content, String type, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      attachmentId - The unique ID of the attachment to update.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(ImmutableConnectorMessage connectorMessage, Attachment attachment) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      attachment - The Attachment object to update.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(ImmutableConnectorMessage connectorMessage, Attachment attachment, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      connectorMessage - The connector message to be associated with the attachment.
      attachment - The Attachment object to update.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(String channelId, Long messageId, Attachment attachment) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      channelId - The ID of the channel the attachment is associated with.
      messageId - The ID of the message the attachment is associated with.
      attachment - The Attachment object to update.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(String channelId, Long messageId, Attachment attachment, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      channelId - The ID of the channel the attachment is associated with.
      messageId - The ID of the message the attachment is associated with.
      attachment - The Attachment object to update.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(String channelId, Long messageId, String attachmentId, Object content, String type) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      channelId - The ID of the channel the attachment is associated with.
      messageId - The ID of the message the attachment is associated with.
      attachmentId - The unique ID of the attachment to update.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.
    • updateAttachment

      public static Attachment updateAttachment(String channelId, Long messageId, String attachmentId, Object content, String type, boolean base64Encode) throws com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException
      Updates an attachment associated with a given connector message.
      Parameters:
      channelId - The ID of the channel the attachment is associated with.
      messageId - The ID of the message the attachment is associated with.
      attachmentId - The unique ID of the attachment to update.
      content - The attachment content (must be a string or byte array).
      type - The MIME type of the attachment.
      base64Encode - If true, the content of each attachment will first be Base64 encoded for convenience.
      Returns:
      The attachment that was updated.
      Throws:
      com.mirth.connect.donkey.server.controllers.UnsupportedDataTypeException - If the attachment content is not a String or byte array.