1 | /*
|
---|
2 | * Created on Jan 1, 2005
|
---|
3 | * Copyright (C) 2004, 2005 Andrea Schweer
|
---|
4 | *
|
---|
5 | * This file is part of the Greenstone Alerting Service.
|
---|
6 | * Refer to the COPYING file in the base directory of this package
|
---|
7 | * for licensing information.
|
---|
8 | */
|
---|
9 | package org.greenstone.gsdlas;
|
---|
10 |
|
---|
11 | import java.util.*;
|
---|
12 | import java.util.Map;
|
---|
13 | import java.util.Set;
|
---|
14 |
|
---|
15 | import javax.mail.MessagingException;
|
---|
16 |
|
---|
17 | import org.greenstone.gsdlas.profiles.Subscription;
|
---|
18 | import org.greenstone.gsdlas.util.Mailer;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * @author andrea
|
---|
22 | *
|
---|
23 | * TODO To change the template for this generated type comment go to
|
---|
24 | * Window - Preferences - Java - Code Style - Code Templates
|
---|
25 | *
|
---|
26 | * Singleton
|
---|
27 | */
|
---|
28 | public class Notifier {
|
---|
29 |
|
---|
30 | private static Notifier instance;
|
---|
31 |
|
---|
32 | private Notifier () {
|
---|
33 | // hide default constructor
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * @return
|
---|
38 | */
|
---|
39 | public static Notifier getInstance() {
|
---|
40 | if (instance == null) instance = new Notifier();
|
---|
41 | return instance;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * @param event
|
---|
46 | * @param matchedSubscriptions
|
---|
47 | */
|
---|
48 | public void sendNotifications(Map event, Set matchedSubscriptions) {
|
---|
49 | // TODO read smtp host and sender from configuration file
|
---|
50 | Mailer mailer = new Mailer(System.getProperty("user.name") + "@localhost", "localhost");
|
---|
51 | String eventText = eventToEmail(event);
|
---|
52 | String subject = "Event for your subscription";
|
---|
53 |
|
---|
54 | for (Iterator iter = matchedSubscriptions.iterator(); iter.hasNext();) {
|
---|
55 | Subscription sub = (Subscription) iter.next();
|
---|
56 | if (!sub.wantsEMailNotification()) continue;
|
---|
57 |
|
---|
58 | StringBuffer message = new StringBuffer();
|
---|
59 | // TODO formatting of notification email via velocity?
|
---|
60 | message.append("You've created a subscription at the Greenstone Alerting Service running on " +
|
---|
61 | "localhost:\n\n");
|
---|
62 | message.append(sub.toString());
|
---|
63 | message.append("\n");
|
---|
64 | message.append("An event has occurred that matches this subscription:\n\n");
|
---|
65 | message.append(eventText);
|
---|
66 | message.append("\n");
|
---|
67 | message.append("Go to http://localhost:8080/alerting/service?action=listSubscriptions to " +
|
---|
68 | "change your subscriptions, or to http://localhost:8080/alerting/service?action=showEvents&subscriptionID=" +
|
---|
69 | sub.getId() + " to see all events that match the subscription.\n");
|
---|
70 |
|
---|
71 | String to = sub.getMailAddress();
|
---|
72 |
|
---|
73 | try {
|
---|
74 | mailer.sendMail(to, subject, message.toString());
|
---|
75 | } catch (MessagingException e) {
|
---|
76 | System.err.println("Couldn't notify for subscription " + sub.getId() + ": " + e.getMessage());
|
---|
77 | e.printStackTrace();
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @param event
|
---|
84 | * @return
|
---|
85 | */
|
---|
86 | private String eventToEmail(Map event) {
|
---|
87 | StringBuffer message = new StringBuffer();
|
---|
88 | for (Iterator iter = event.keySet().iterator(); iter.hasNext();) {
|
---|
89 | String key = (String) iter.next();
|
---|
90 | String value = (String) event.get(key);
|
---|
91 | message.append(key + ": " + value + "\n");
|
---|
92 | }
|
---|
93 | return message.toString();
|
---|
94 | }
|
---|
95 |
|
---|
96 | }
|
---|