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