/jbpm-workitems/src/main/java/org/jbpm/process/workitem/email/EmailWorkItemHandler.java

https://github.com/mariofusco/jbpm · Java · 162 lines · 102 code · 22 blank · 38 comment · 28 complexity · 80c8a5f1dfeb7f35e94f664bdc414ae6 MD5 · raw file

  1. /**
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.process.workitem.email;
  17. import java.util.Arrays;
  18. import org.jbpm.process.workitem.AbstractLogOrThrowWorkItemHandler;
  19. import org.kie.api.runtime.process.WorkItem;
  20. import org.kie.api.runtime.process.WorkItemManager;
  21. /**
  22. * WorkItemHandler for sending email.
  23. *
  24. * Expects the following parameters:
  25. * - "From" (String): sends an email from the given the email address
  26. * - "To" (String): sends the email to the given email address(es),
  27. * multiple addresses must be separated using a semi-colon (';')
  28. * - "Subject" (String): the subject of the email
  29. * - "Text" (String): the body of the email (using HTML)
  30. * Is completed immediately and does not return any result parameters.
  31. *
  32. * Sending an email cannot be aborted.
  33. *
  34. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  35. */
  36. public class EmailWorkItemHandler extends AbstractLogOrThrowWorkItemHandler {
  37. private Connection connection;
  38. public EmailWorkItemHandler() {
  39. }
  40. public EmailWorkItemHandler(String host, String port, String userName, String password) {
  41. setConnection(host, port, userName, password);
  42. }
  43. public EmailWorkItemHandler(String host, String port, String userName, String password, String startTls) {
  44. setConnection(host, port, userName, password, startTls);
  45. }
  46. public void setConnection(String host, String port, String userName, String password) {
  47. connection = new Connection();
  48. connection.setHost(host);
  49. connection.setPort(port);
  50. connection.setUserName(userName);
  51. connection.setPassword(password);
  52. }
  53. public void setConnection(String host, String port, String userName, String password, String startTls) {
  54. connection = new Connection();
  55. connection.setHost(host);
  56. connection.setPort(port);
  57. connection.setUserName(userName);
  58. connection.setPassword(password);
  59. connection.setStartTls(Boolean.parseBoolean(startTls));
  60. }
  61. public Connection getConnection() {
  62. return connection;
  63. }
  64. public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
  65. if (connection == null) {
  66. throw new IllegalArgumentException("Connection not initialized for Email");
  67. }
  68. try {
  69. Email email = createEmail(workItem, connection);
  70. SendHtml.sendHtml(email);
  71. // avoid null pointer when used from deadline escalation handler
  72. if (manager != null) {
  73. manager.completeWorkItem(workItem.getId(), null);
  74. }
  75. } catch (Exception e) {
  76. handleException(e);
  77. }
  78. }
  79. protected static Email createEmail(WorkItem workItem, Connection connection) {
  80. Email email = new Email();
  81. Message message = new Message();
  82. message.setFrom((String) workItem.getParameter("From"));
  83. message.setReplyTo( (String) workItem.getParameter("Reply-To"));
  84. // Set recipients
  85. Recipients recipients = new Recipients();
  86. String to = (String) workItem.getParameter("To");
  87. if ( to == null || to.trim().length() == 0 ) {
  88. throw new RuntimeException( "Email must have one or more to adresses" );
  89. }
  90. for (String s: to.split(";")) {
  91. if (s != null && !"".equals(s)) {
  92. Recipient recipient = new Recipient();
  93. recipient.setEmail(s);
  94. recipient.setType( "To" );
  95. recipients.addRecipient(recipient);
  96. }
  97. }
  98. // Set cc
  99. String cc = (String) workItem.getParameter("Cc");
  100. if ( cc != null && cc.trim().length() > 0 ) {
  101. for (String s: cc.split(";")) {
  102. if (s != null && !"".equals(s)) {
  103. Recipient recipient = new Recipient();
  104. recipient.setEmail(s);
  105. recipient.setType( "Cc" );
  106. recipients.addRecipient(recipient);
  107. }
  108. }
  109. }
  110. // Set bcc
  111. String bcc = (String) workItem.getParameter("Bcc");
  112. if ( bcc != null && bcc.trim().length() > 0 ) {
  113. for (String s: bcc.split(";")) {
  114. if (s != null && !"".equals(s)) {
  115. Recipient recipient = new Recipient();
  116. recipient.setEmail(s);
  117. recipient.setType( "Bcc" );
  118. recipients.addRecipient(recipient);
  119. }
  120. }
  121. }
  122. // Fill message
  123. message.setRecipients(recipients);
  124. message.setSubject((String) workItem.getParameter("Subject"));
  125. message.setBody((String) workItem.getParameter("Body"));
  126. // fill attachments
  127. String attachmentList = (String) workItem.getParameter("Attachments");
  128. if (attachmentList != null) {
  129. String[] attachments = attachmentList.split(",");
  130. message.setAttachments(Arrays.asList(attachments));
  131. }
  132. // setup email
  133. email.setMessage(message);
  134. email.setConnection(connection);
  135. return email;
  136. }
  137. public void abortWorkItem(WorkItem arg0, WorkItemManager arg1) {
  138. // Do nothing, email cannot be aborted
  139. }
  140. }