Class MailAttachmentsManagerImpl

java.lang.Object
com.atlassian.jira.mail.util.MailAttachmentsManagerImpl
All Implemented Interfaces:
MailAttachmentsManager

public class MailAttachmentsManagerImpl extends Object implements MailAttachmentsManager
Stateful, created per single email sent to recipient
  • Method Details

    • withoutJwtTokens

      public static MailAttachmentsManagerImpl withoutJwtTokens(AvatarService avatarService, AvatarTranscoder avatarTranscoder, UserManager userManager, AvatarManager avatarManager, ApplicationProperties applicationProperties, ImageAttachmentJwtTokenService imageAttachmentJwtTokenService, ImageAttachmentAltAttributeService imageAttachmentAltAttributeService, AttachmentManager attachmentManager, ThumbnailManager thumbnailManager)
      Created this way it won't try to add JWT tokens to secured image paths. Only internal resources will be inlined with src='cid:...' and displayed in emails.
    • withJwtTokens

      public static MailAttachmentsManagerImpl withJwtTokens(AvatarService avatarService, AvatarTranscoder avatarTranscoder, UserManager userManager, AvatarManager avatarManager, ApplicationProperties applicationProperties, ImageAttachmentJwtTokenService imageAttachmentJwtTokenService, ImageAttachmentAltAttributeService imageAttachmentAltAttributeService, String recipientUserName, AttachmentManager attachmentManager, ThumbnailManager thumbnailManager)
      Created this way it will try to add JWT tokens to secured image paths. Internal resources will be inlined with scr='cid:...' and displayed in emails. Image attachments starting with /secured/ will have JWT token parameter added to the
    • getAvatarUrl

      public String getAvatarUrl(String username)
      Description copied from interface: MailAttachmentsManager
      Returns a link to access user avatar in email message. If avatar can be attached to email, the cid link is returned, otherwise avatar URL is returned and no image is attached. (E.g. If Gravatar is enabled)
      Specified by:
      getAvatarUrl in interface MailAttachmentsManager
      Parameters:
      username -
      Returns:
      cid link or Avatar URL
    • getAvatarUrl

      public String getAvatarUrl(TemplateUser templateUser)
      Description copied from interface: MailAttachmentsManager
      Returns a link to access user avatar in email message. If avatar can be attached to email, the cid link is returned, otherwise avatar URL is returned and no image is attached. (E.g. If Gravatar is enabled)
      Specified by:
      getAvatarUrl in interface MailAttachmentsManager
      Parameters:
      templateUser -
      Returns:
      cid link or Avatar URL
    • getAvatarUrl

      public String getAvatarUrl(ApplicationUser user)
      Description copied from interface: MailAttachmentsManager
      Returns a link to access user avatar in email message. If avatar can be attached to email, the cid link is returned, otherwise avatar URL is returned and no image is attached. (E.g. If Gravatar is enabled)
      Specified by:
      getAvatarUrl in interface MailAttachmentsManager
      Parameters:
      user -
      Returns:
      cid link or Avatar URL
    • getAvatarUrl

      public String getAvatarUrl(@Nonnull Avatar avatar, @Nonnull Avatar.Size size, String avatarOwnerDescription)
      Description copied from interface: MailAttachmentsManager
      Returns a link to access Avatar (icon for project or some other entity in Jira) in email message.
      Specified by:
      getAvatarUrl in interface MailAttachmentsManager
      Parameters:
      avatar - Avatar to display in email
      size - avatar size
      avatarOwnerDescription - Description of avatar
      Returns:
      cid link
    • getIssueTypeIconUrl

      public String getIssueTypeIconUrl(IssueType issueType)
    • addAttachmentAndReturnCid

      public String addAttachmentAndReturnCid(MailAttachment mailAttachment)
      Description copied from interface: MailAttachmentsManager
      Adds an attachment to the mail attachments manager's collection of attachments. This collection will eventually be attached to the email. This method will return a CID that can be used to reference the attachment in the body of an email. Given the same mail attachment multiple times, this method will return the same CID.
      Specified by:
      addAttachmentAndReturnCid in interface MailAttachmentsManager
      Parameters:
      mailAttachment -
      Returns:
      The CID for an attachment
    • extractAttachmentId

      public static Long extractAttachmentId(String path)
    • getImageUrl

      public String getImageUrl(String path)
      Description copied from interface: MailAttachmentsManager
      Tries to inline image specified by path and add it to email attachments or add JWT token
      Specified by:
      getImageUrl in interface MailAttachmentsManager
      Parameters:
      path -
      Returns:
      • image cid link if it's an internal resource
      • image link appended with JWT token if it's /secure/thumbnail/* or /secure/attachment/*
      • unchanged otherwise
    • processImages

      public String processImages(String html)
      Description copied from interface: MailAttachmentsManager

      - Looks for tags and tries to inline image if it's an internal resource. Otherwise adds JWT token for every /secure/* image or leaves unchanged for other paths.

      - Counts how many images there are that start with /secure/* so MailAttachmentsManager.hasSecuredImage() can calculate the result needed to construct email footer

      - Adds default 'alt' attribute which is an attachment file name in case img has none

      Specified by:
      processImages in interface MailAttachmentsManager
      Parameters:
      html - HTML to change.
      Returns:
      The same HTML with the images inlined or JWT tokens parameter added to images' src attribute.
    • inlineImages

      public String inlineImages(String html)
      Specified by:
      inlineImages in interface MailAttachmentsManager
    • hasSecuredImage

      public boolean hasSecuredImage()
      Description copied from interface: MailAttachmentsManager
      Needed to attach/or not Use only after MailAttachmentsManager.inlineImages(String) as it will count found images in rendered html.
      Specified by:
      hasSecuredImage in interface MailAttachmentsManager
      Returns:
      true when there is at least one image with contains /secure/* as src found in html
    • removeBaseUrl

      public String removeBaseUrl(String url)
      Description copied from interface: MailAttachmentsManager
      Tries to remove the baseUrl (eg: http://example.com/jira) or the basePath (/jira) from an URL, and return the result. Some quick examples (let's assume baseUrl = http://example.com/jira)

      • http://example.com/jira/images/icon.png --> /images/icon.png
      • /jira/images/icon.png --> /images/icon.png
      • /images/icon.png --> /images/icon.png
      • http://example.com/test/images/icon.png --> http://example.com/test/images/icon.png
      • http://google.com/jira/images/icon.png --> http://google.com/jira/images/icon.png
      Specified by:
      removeBaseUrl in interface MailAttachmentsManager
      Parameters:
      url - the url to remove the baseUrl from
      Returns:
      the url without the baseUrl nor the basePath
    • getExternalImageUrl

      public String getExternalImageUrl(String path)
      Description copied from interface: MailAttachmentsManager
      If the path is a relative url tries to add the image specified as an attachment. Returns a cid link if successful, or unchanged path if not
      Specified by:
      getExternalImageUrl in interface MailAttachmentsManager
    • getAbsoluteUrl

      protected String getAbsoluteUrl(String path)
    • getAttachmentsCount

      public int getAttachmentsCount()
      Description copied from interface: MailAttachmentsManager
      Returns the number of attachments added to this manager
      Specified by:
      getAttachmentsCount in interface MailAttachmentsManager
      Returns:
      number of attachments
    • buildAttachmentsBodyParts

      public Iterable<jakarta.mail.BodyPart> buildAttachmentsBodyParts()
      Description copied from interface: MailAttachmentsManager
      Builds bodyPart for each image (including avatars) added to this manager
      Specified by:
      buildAttachmentsBodyParts in interface MailAttachmentsManager
      Returns:
      Added attachments as list of BodyParts