| 1 |
|
package com.atlassian.mail.server.impl.util; |
| 2 |
|
|
| 3 |
|
import com.atlassian.mail.Email; |
| 4 |
|
import com.atlassian.mail.MailException; |
| 5 |
|
import com.atlassian.mail.MailUtils; |
| 6 |
|
import com.opensymphony.util.TextUtils; |
| 7 |
|
import alt.javax.mail.internet.MimeMessage; |
| 8 |
|
|
| 9 |
|
import javax.mail.MessagingException; |
| 10 |
|
import javax.mail.Multipart; |
| 11 |
|
import javax.mail.Part; |
| 12 |
|
import javax.mail.internet.InternetAddress; |
| 13 |
|
import javax.mail.internet.MimeBodyPart; |
| 14 |
|
import java.io.UnsupportedEncodingException; |
| 15 |
|
import java.util.Map; |
| 16 |
|
import java.util.Calendar; |
| 17 |
|
import java.util.Iterator; |
| 18 |
|
|
|
|
|
| 74.5% |
Uncovered Elements: 25 (98) |
Complexity: 23 |
Complexity Density: 0.38 |
|
| 19 |
|
public class MessageCreator |
| 20 |
|
{ |
|
|
|
| 74.2% |
Uncovered Elements: 25 (97) |
Complexity: 23 |
Complexity Density: 0.38 |
|
| 21 |
8
|
public void updateMimeMessage(Email email, String defaultFrom, String prefix, MimeMessage message) throws MailException, MessagingException, UnsupportedEncodingException... |
| 22 |
|
{ |
| 23 |
8
|
String from = TextUtils.noNull(email.getFrom()).trim(); |
| 24 |
8
|
String fromName = email.getFromName(); |
| 25 |
8
|
String to = email.getTo(); |
| 26 |
8
|
String cc = email.getCc(); |
| 27 |
8
|
String bcc = email.getBcc(); |
| 28 |
8
|
String replyTo = email.getReplyTo(); |
| 29 |
8
|
String inReplyTo = email.getInReplyTo(); |
| 30 |
8
|
String subject = email.getSubject(); |
| 31 |
8
|
String body = email.getBody(); |
| 32 |
8
|
String mimeType = email.getMimeType(); |
| 33 |
8
|
String encoding = email.getEncoding(); |
| 34 |
8
|
Map headers = email.getHeaders(); |
| 35 |
8
|
Multipart multipart = email.getMultipart(); |
| 36 |
|
|
| 37 |
8
|
if (!TextUtils.stringSet(TextUtils.noNull(to).trim()) && !TextUtils.stringSet(TextUtils.noNull(cc).trim()) && !TextUtils.stringSet(TextUtils.noNull(bcc).trim())) |
| 38 |
|
{ |
| 39 |
0
|
throw new MailException("Tried to send mail (" + subject + ") with no recipients."); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
8
|
message.setSentDate(Calendar.getInstance().getTime()); |
| 43 |
8
|
if (to != null) |
| 44 |
8
|
message.setRecipients(javax.mail.Message.RecipientType.TO, MailUtils.parseAddresses(to)); |
| 45 |
|
|
| 46 |
8
|
if (cc != null) |
| 47 |
1
|
message.setRecipients(javax.mail.Message.RecipientType.CC, MailUtils.parseAddresses(cc)); |
| 48 |
|
|
| 49 |
8
|
if (bcc != null) |
| 50 |
0
|
message.setRecipients(javax.mail.Message.RecipientType.BCC, MailUtils.parseAddresses(bcc)); |
| 51 |
|
|
| 52 |
8
|
if (replyTo != null) |
| 53 |
0
|
message.setReplyTo(MailUtils.parseAddresses(replyTo)); |
| 54 |
|
|
| 55 |
8
|
if (inReplyTo != null) |
| 56 |
0
|
message.setHeader("In-Reply-To", inReplyTo); |
| 57 |
|
|
| 58 |
8
|
if (TextUtils.stringSet(from)) |
| 59 |
|
{ |
| 60 |
|
|
| 61 |
5
|
InternetAddress internetAddress = new InternetAddress(from); |
| 62 |
5
|
if (TextUtils.stringSet(fromName) && internetAddress.getPersonal() == null) |
| 63 |
|
{ |
| 64 |
0
|
if(encoding != null) |
| 65 |
|
{ |
| 66 |
0
|
internetAddress.setPersonal(fromName, encoding); |
| 67 |
|
} |
| 68 |
|
else |
| 69 |
|
{ |
| 70 |
0
|
internetAddress.setPersonal(fromName); |
| 71 |
|
} |
| 72 |
|
} |
| 73 |
5
|
message.setFrom(internetAddress); |
| 74 |
|
} |
| 75 |
3
|
else if (TextUtils.stringSet(defaultFrom)) |
| 76 |
|
{ |
| 77 |
|
|
| 78 |
2
|
InternetAddress internetAddress = new InternetAddress(defaultFrom); |
| 79 |
2
|
if (TextUtils.stringSet(fromName) && internetAddress.getPersonal() == null) |
| 80 |
|
{ |
| 81 |
0
|
if(encoding != null) |
| 82 |
|
{ |
| 83 |
0
|
internetAddress.setPersonal(fromName, encoding); |
| 84 |
|
} |
| 85 |
|
else |
| 86 |
|
{ |
| 87 |
0
|
internetAddress.setPersonal(fromName); |
| 88 |
|
} |
| 89 |
|
} |
| 90 |
2
|
message.setFrom(internetAddress); |
| 91 |
|
} |
| 92 |
|
else |
| 93 |
|
{ |
| 94 |
1
|
throw new MailException("Tried to send mail (" + subject + ") from no one (no 'from' and 'default from' specified)."); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
7
|
String fullSubject = subject; |
| 98 |
7
|
if (TextUtils.stringSet(prefix)) |
| 99 |
4
|
fullSubject = prefix + " " + fullSubject; |
| 100 |
|
|
| 101 |
7
|
if (encoding != null) |
| 102 |
7
|
message.setSubject(fullSubject, encoding); |
| 103 |
|
else |
| 104 |
0
|
message.setSubject(fullSubject); |
| 105 |
|
|
| 106 |
7
|
String mimeTypeAndEncoding = mimeType; |
| 107 |
|
|
| 108 |
7
|
if (encoding != null) |
| 109 |
7
|
mimeTypeAndEncoding += "; charset=" + encoding + ""; |
| 110 |
|
|
| 111 |
7
|
if (multipart != null) |
| 112 |
|
{ |
| 113 |
|
|
| 114 |
1
|
MimeBodyPart messageBodyPart = new MimeBodyPart(); |
| 115 |
1
|
messageBodyPart.setContent(body, mimeTypeAndEncoding); |
| 116 |
1
|
messageBodyPart.setDisposition(Part.INLINE); |
| 117 |
|
|
| 118 |
|
|
| 119 |
1
|
multipart.addBodyPart(messageBodyPart, 0); |
| 120 |
|
|
| 121 |
1
|
message.setContent(multipart); |
| 122 |
|
} |
| 123 |
|
else |
| 124 |
|
{ |
| 125 |
6
|
message.setContent(body, mimeTypeAndEncoding); |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
|
| 129 |
7
|
if (headers != null) |
| 130 |
|
{ |
| 131 |
14
|
for (Iterator iterator = headers.entrySet().iterator(); iterator.hasNext();) |
| 132 |
|
{ |
| 133 |
7
|
Map.Entry entry = (Map.Entry) iterator.next(); |
| 134 |
7
|
message.addHeader((String) entry.getKey(), (String) entry.getValue()); |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
} |