|
|
|
@ -1,5 +1,18 @@
|
|
|
|
|
package it.tref.liferay.portos.mailmanager.shared.messaging.util; |
|
|
|
|
|
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.messaging.dto.MailTemplateMessage; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.messaging.dto.TemplateMessage; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.model.FileAttachment; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailManagerConstants; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailUtil; |
|
|
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
import javax.mail.internet.AddressException; |
|
|
|
|
import javax.mail.internet.InternetAddress; |
|
|
|
|
|
|
|
|
|
import com.liferay.portal.kernel.exception.SystemException; |
|
|
|
|
import com.liferay.portal.kernel.json.JSONFactoryUtil; |
|
|
|
|
import com.liferay.portal.kernel.json.JSONObject; |
|
|
|
@ -14,64 +27,43 @@ import com.liferay.portal.kernel.util.StringPool;
|
|
|
|
|
import com.liferay.portal.kernel.util.StringUtil; |
|
|
|
|
import com.liferay.portal.service.ServiceContext; |
|
|
|
|
|
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.messaging.dto.MailTemplateMessage; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.messaging.dto.TemplateMessage; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.model.FileAttachment; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailManagerConstants; |
|
|
|
|
import it.tref.liferay.portos.mailmanager.shared.util.MailUtil; |
|
|
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
import javax.mail.internet.AddressException; |
|
|
|
|
import javax.mail.internet.InternetAddress; |
|
|
|
|
|
|
|
|
|
public class MailManagerUtil { |
|
|
|
|
|
|
|
|
|
private final static Log _log = LogFactoryUtil.getLog(MailManagerUtil.class); |
|
|
|
|
|
|
|
|
|
public static boolean sendMail(String messageId, String inReplyTo, String from, String[] to, String[] cc, |
|
|
|
|
String[] bcc, String subject, String body, boolean htmlFormat, boolean async, |
|
|
|
|
boolean checkPermission, ServiceContext serviceContext) throws AddressException, SystemException { |
|
|
|
|
String[] bcc, String subject, String body, boolean htmlFormat, boolean async, boolean checkPermission, |
|
|
|
|
ServiceContext serviceContext) throws AddressException, SystemException { |
|
|
|
|
|
|
|
|
|
InternetAddress fromInternetAddress = new InternetAddress(from, true); |
|
|
|
|
|
|
|
|
|
InternetAddress[] toInternetAddress = MailUtil.fromStringAddress(to); |
|
|
|
|
InternetAddress[] ccInternetAddress = MailUtil.fromStringAddress(cc); |
|
|
|
|
InternetAddress[] bccInternetAddress = MailUtil.fromStringAddress(bcc); |
|
|
|
|
|
|
|
|
|
MailMessage mailMessage = new MailMessage(fromInternetAddress, subject, body, htmlFormat); |
|
|
|
|
mailMessage.setTo(toInternetAddress); |
|
|
|
|
mailMessage.setCC(ccInternetAddress); |
|
|
|
|
mailMessage.setBCC(bccInternetAddress); |
|
|
|
|
|
|
|
|
|
Message message = new Message(); |
|
|
|
|
|
|
|
|
|
message.put("serviceContext", serviceContext); |
|
|
|
|
|
|
|
|
|
message.setPayload(JSONFactoryUtil.looseSerializeDeep(mailMessage)); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
if (async) { |
|
|
|
|
message.put("mode", MailManagerConstants.MESSAGING_SEND_MODE_ASYNC); |
|
|
|
|
MessageBusUtil.sendMessage(DestinationNames.SEND_MAIL, message); |
|
|
|
|
return true; |
|
|
|
|
} else { |
|
|
|
|
message.put("mode", MailManagerConstants.MESSAGING_SEND_MODE_SYNC); |
|
|
|
|
return GetterUtil.getBoolean(MessageBusUtil.sendSynchronousMessage( |
|
|
|
|
DestinationNames.SEND_MAIL_SYNC, message, TimeUnit.SECONDS.toMillis(30))); |
|
|
|
|
return GetterUtil.getBoolean(MessageBusUtil.sendSynchronousMessage(DestinationNames.SEND_MAIL_SYNC, |
|
|
|
|
message, TimeUnit.SECONDS.toMillis(30))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (MessageBusException e) { |
|
|
|
|
throw new SystemException(e); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean sendMailByTemplateId(String className, long classPK, long templateId, |
|
|
|
|
JSONObject templateVariables, ServiceContext serviceContext) throws SystemException { |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, templateId, StringPool.BLANK, null, null, null, |
|
|
|
|
templateVariables, Collections.<FileAttachment> emptyList(), true, true, serviceContext); |
|
|
|
|
} |
|
|
|
@ -79,6 +71,7 @@ public class MailManagerUtil {
|
|
|
|
|
public static boolean sendMailByTemplateId(String className, long classPK, long templateId, |
|
|
|
|
JSONObject templateVariables, List<FileAttachment> fileAttachments, ServiceContext serviceContext) |
|
|
|
|
throws SystemException { |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, templateId, StringPool.BLANK, null, null, null, |
|
|
|
|
templateVariables, fileAttachments, true, true, serviceContext); |
|
|
|
|
} |
|
|
|
@ -86,12 +79,14 @@ public class MailManagerUtil {
|
|
|
|
|
public static boolean sendMailByTemplateId(String className, long classPK, long templateId, String[] to, |
|
|
|
|
String[] cc, String[] ccn, JSONObject templateVariables, List<FileAttachment> fileAttachments, |
|
|
|
|
ServiceContext serviceContext) throws SystemException { |
|
|
|
|
return sendMailByTemplate(className, classPK, templateId, StringPool.BLANK, to, cc, ccn, |
|
|
|
|
templateVariables, fileAttachments, true, true, serviceContext); |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, templateId, StringPool.BLANK, to, cc, ccn, templateVariables, |
|
|
|
|
fileAttachments, true, true, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean sendMailByTemplateName(String className, long classPK, String templateName, |
|
|
|
|
JSONObject templateVariables, ServiceContext serviceContext) throws SystemException { |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, 0l, templateName, null, null, null, templateVariables, |
|
|
|
|
Collections.<FileAttachment> emptyList(), true, true, serviceContext); |
|
|
|
|
} |
|
|
|
@ -99,52 +94,45 @@ public class MailManagerUtil {
|
|
|
|
|
public static boolean sendMailByTemplateName(String className, long classPK, String templateName, |
|
|
|
|
JSONObject templateVariables, List<FileAttachment> fileAttachments, ServiceContext serviceContext) |
|
|
|
|
throws SystemException { |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, 0l, templateName, null, null, null, templateVariables, |
|
|
|
|
fileAttachments, true, true, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean sendMailByTemplatName(String className, long classPK, String templateName, |
|
|
|
|
String[] to, String[] cc, String[] ccn, JSONObject templateVariables, |
|
|
|
|
List<FileAttachment> fileAttachments, ServiceContext serviceContext) throws SystemException { |
|
|
|
|
public static boolean sendMailByTemplatName(String className, long classPK, String templateName, String[] to, |
|
|
|
|
String[] cc, String[] ccn, JSONObject templateVariables, List<FileAttachment> fileAttachments, |
|
|
|
|
ServiceContext serviceContext) throws SystemException { |
|
|
|
|
|
|
|
|
|
return sendMailByTemplate(className, classPK, 0l, templateName, to, cc, ccn, templateVariables, |
|
|
|
|
fileAttachments, true, true, serviceContext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean sendMailByTemplate(String className, long classPK, long templateId, |
|
|
|
|
String templateName, String[] to, String[] cc, String[] ccn, JSONObject templateVariables, |
|
|
|
|
List<FileAttachment> fileAttachments, boolean async, boolean checkPermission, |
|
|
|
|
ServiceContext serviceContext) throws SystemException { |
|
|
|
|
public static boolean sendMailByTemplate(String className, long classPK, long templateId, String templateName, |
|
|
|
|
String[] to, String[] cc, String[] ccn, JSONObject templateVariables, List<FileAttachment> fileAttachments, |
|
|
|
|
boolean async, boolean checkPermission, ServiceContext serviceContext) throws SystemException { |
|
|
|
|
|
|
|
|
|
JSONObject templateParam = JSONFactoryUtil.createJSONObject(); |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_VARIABLES_KEY, templateVariables); |
|
|
|
|
|
|
|
|
|
if (to != null && to.length > 0) { |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_TO_KEY, |
|
|
|
|
StringUtil.merge(to, StringPool.COMMA)); |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_TO_KEY, StringUtil.merge(to, StringPool.COMMA)); |
|
|
|
|
} |
|
|
|
|
if (cc != null && cc.length > 0) { |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_CC_KEY, |
|
|
|
|
StringUtil.merge(cc, StringPool.COMMA)); |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_CC_KEY, StringUtil.merge(cc, StringPool.COMMA)); |
|
|
|
|
} |
|
|
|
|
if (ccn != null && ccn.length > 0) { |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_CCN_KEY, |
|
|
|
|
StringUtil.merge(ccn, StringPool.COMMA)); |
|
|
|
|
templateParam.put(MailManagerConstants.TEMPLATE_PARAMETER_CCN_KEY, StringUtil.merge(ccn, StringPool.COMMA)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
MailTemplateMessage mailTemplateMessage = new MailTemplateMessage(); |
|
|
|
|
mailTemplateMessage.setTemplateId(templateId); |
|
|
|
|
mailTemplateMessage.setTemplateName(templateName); |
|
|
|
|
mailTemplateMessage.setTemplateParam(templateParam.toString()); |
|
|
|
|
mailTemplateMessage.setFileAttachments(fileAttachments); |
|
|
|
|
|
|
|
|
|
Message message = new Message(); |
|
|
|
|
message.put("serviceContext", serviceContext); |
|
|
|
|
message.put("className", className); |
|
|
|
|
message.put("classPK", classPK); |
|
|
|
|
message.setPayload(JSONFactoryUtil.looseSerializeDeep(mailTemplateMessage)); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
if (async) { |
|
|
|
|
message.put("mode", MailManagerConstants.MESSAGING_SEND_MODE_ASYNC); |
|
|
|
|
MessageBusUtil.sendMessage(DestinationNames.SEND_TEMPLATE_MAIL, message); |
|
|
|
@ -154,15 +142,15 @@ public class MailManagerUtil {
|
|
|
|
|
return GetterUtil.getBoolean(MessageBusUtil.sendSynchronousMessage( |
|
|
|
|
DestinationNames.SEND_TEMPLATE_MAIL_SYNC, message, TimeUnit.SECONDS.toMillis(30))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} catch (MessageBusException e) { |
|
|
|
|
throw new SystemException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean addMailTemplate(String templateName, String from, String[] to, String[] cc, |
|
|
|
|
String[] ccn, String subject, String body, boolean attachmentPermitted, |
|
|
|
|
ServiceContext serviceContext) throws SystemException { |
|
|
|
|
public static boolean addMailTemplate(String templateName, String from, String[] to, String[] cc, String[] ccn, |
|
|
|
|
String subject, String body, boolean attachmentPermitted, ServiceContext serviceContext) |
|
|
|
|
throws SystemException { |
|
|
|
|
|
|
|
|
|
return updateMailTemplate(0l, templateName, from, to, cc, ccn, subject, body, attachmentPermitted, |
|
|
|
|
serviceContext); |
|
|
|
|
} |
|
|
|
@ -181,17 +169,14 @@ public class MailManagerUtil {
|
|
|
|
|
templateMessage.setCcn(ccn); |
|
|
|
|
templateMessage.setSubject(subject); |
|
|
|
|
templateMessage.setBody(body); |
|
|
|
|
|
|
|
|
|
Message message = new Message(); |
|
|
|
|
message.put("serviceContext", serviceContext); |
|
|
|
|
message.setPayload(JSONFactoryUtil.looseSerializeDeep(templateMessage)); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
return GetterUtil.getBoolean(MessageBusUtil.sendSynchronousMessage( |
|
|
|
|
DestinationNames.EDIT_EMAIL_TEMPLATE, message, TimeUnit.SECONDS.toMillis(30))); |
|
|
|
|
return GetterUtil.getBoolean(MessageBusUtil.sendSynchronousMessage(DestinationNames.EDIT_EMAIL_TEMPLATE, |
|
|
|
|
message, TimeUnit.SECONDS.toMillis(30))); |
|
|
|
|
} catch (MessageBusException e) { |
|
|
|
|
throw new SystemException(e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|