View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.java.job.notification;
20  
21  import javax.mail.internet.MimeMessage;
22  import org.apache.syncope.core.persistence.api.dao.TaskDAO;
23  import org.apache.syncope.core.persistence.api.entity.task.NotificationTask;
24  import org.apache.syncope.core.persistence.api.entity.task.TaskExec;
25  import org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory;
26  import org.apache.syncope.core.provisioning.api.AuditManager;
27  import org.apache.syncope.core.provisioning.api.notification.NotificationManager;
28  import org.springframework.context.ApplicationEventPublisher;
29  import org.springframework.mail.javamail.JavaMailSender;
30  import org.springframework.mail.javamail.MimeMessageHelper;
31  
32  public class MailNotificationJobDelegate extends AbstractNotificationJobDelegate {
33  
34      protected final JavaMailSender mailSender;
35  
36      public MailNotificationJobDelegate(
37              final TaskDAO taskDAO,
38              final TaskUtilsFactory taskUtilsFactory,
39              final AuditManager auditManager,
40              final NotificationManager notificationManager,
41              final ApplicationEventPublisher publisher,
42              final JavaMailSender mailSender) {
43  
44          super(taskDAO, taskUtilsFactory, auditManager, notificationManager, publisher);
45          this.mailSender = mailSender;
46      }
47  
48      @Override
49      protected void notify(
50              final String to,
51              final NotificationTask task,
52              final TaskExec<NotificationTask> execution) throws Exception {
53  
54          MimeMessage message = mailSender.createMimeMessage();
55          MimeMessageHelper helper = new MimeMessageHelper(message, true);
56          helper.setTo(to);
57          helper.setFrom(task.getSender());
58          helper.setSubject(task.getSubject());
59          helper.setText(task.getTextBody(), task.getHtmlBody());
60  
61          mailSender.send(message);
62  
63          execution.setStatus(NotificationJob.Status.SENT.name());
64  
65          StringBuilder report = new StringBuilder();
66          switch (task.getTraceLevel()) {
67              case ALL:
68                  report.append("FROM: ").append(task.getSender()).append('\n').
69                          append("TO: ").append(to).append('\n').
70                          append("SUBJECT: ").append(task.getSubject()).append('\n').append('\n').
71                          append(task.getTextBody()).append('\n').append('\n').
72                          append(task.getHtmlBody()).append('\n');
73                  break;
74  
75              case SUMMARY:
76                  report.append("E-mail sent to ").append(to).append('\n');
77                  break;
78  
79              case FAILURES:
80              case NONE:
81              default:
82          }
83          if (report.length() > 0) {
84              execution.setMessage(report.toString());
85          }
86      }
87  }