This project has retired. For details please refer to its
Attic page.
SMTPNotifier xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juddi.subscription.notify;
18
19 import java.net.URISyntaxException;
20 import java.rmi.RemoteException;
21 import java.security.InvalidAlgorithmParameterException;
22 import java.security.InvalidKeyException;
23 import java.security.NoSuchAlgorithmException;
24 import java.util.Properties;
25
26 import javax.crypto.BadPaddingException;
27 import javax.crypto.IllegalBlockSizeException;
28 import javax.crypto.NoSuchPaddingException;
29 import javax.mail.Address;
30 import javax.mail.Message.RecipientType;
31 import javax.mail.PasswordAuthentication;
32 import javax.mail.Session;
33 import javax.mail.Transport;
34 import javax.mail.internet.InternetAddress;
35 import javax.mail.internet.MimeMessage;
36
37 import org.apache.commons.configuration.ConfigurationException;
38 import org.apache.commons.lang.StringEscapeUtils;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41 import org.apache.juddi.api_v3.AccessPointType;
42 import org.apache.juddi.config.AppConfig;
43 import org.apache.juddi.config.Property;
44 import org.apache.juddi.config.ResourceConfig;
45 import org.apache.juddi.cryptor.CryptorFactory;
46 import org.apache.juddi.jaxb.JAXBMarshaller;
47 import org.apache.juddi.model.BindingTemplate;
48 import org.uddi.api_v3.DispositionReport;
49 import org.uddi.api_v3.Result;
50 import org.uddi.subr_v3.NotifySubscriptionListener;
51 import org.uddi.v3_service.DispositionReportFaultMessage;
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class SMTPNotifier implements Notifier {
66
67 protected static final Log log = LogFactory.getLog(SMTPNotifier.class);
68 protected String notificationEmailAddress = null;
69
70 protected Session session = null;
71 protected Properties properties = null;
72
73 protected final static String[] mailProps = {"mail.smtp.from", "mail.smtp.host", "mail.smtp.port",
74 "mail.smtp.socketFactory.class", "mail.smtp.socketFactory.fallback", "mail.smtp.starttls.enable",
75 "mail.smtp.socketFactory.port","mail.smtp.auth","mail.smtp.user","mail.smtp.password","mail.debug"};
76
77 protected final Properties getEMailProperties() throws ConfigurationException {
78 if (properties==null || properties.isEmpty()) {
79 properties = new Properties();
80 String mailPrefix = AppConfig.getConfiguration().getString(Property.JUDDI_EMAIL_PREFIX, Property.DEFAULT_JUDDI_EMAIL_PREFIX);
81 if (! mailPrefix.endsWith(".")) mailPrefix = mailPrefix + ".";
82 for (String key: mailProps) {
83 if (AppConfig.getConfiguration().containsKey(mailPrefix + key)) {
84 properties.put(key, AppConfig.getConfiguration().getProperty(mailPrefix + key));
85 } else if (System.getProperty(mailPrefix + key) != null) {
86 properties.put(key, System.getProperty(mailPrefix + key));
87 }
88 }
89 }
90 return properties;
91 }
92
93 public SMTPNotifier(BindingTemplate bindingTemplate) throws URISyntaxException, Exception {
94 super();
95 if (!AccessPointType.END_POINT.toString().equalsIgnoreCase(bindingTemplate.getAccessPointType())) {
96 log.error("smtp enpoints only support AccessPointType " + AccessPointType.END_POINT);
97 }
98 String accessPointUrl = bindingTemplate.getAccessPointUrl().toLowerCase();
99 if (!accessPointUrl.startsWith("mailto:")) {
100 log.warn("smtp accessPointUrl for bindingTemplate " + bindingTemplate.getEntityKey() +
101 " should start with 'mailto'");
102
103 } else {
104 notificationEmailAddress = accessPointUrl.substring(accessPointUrl.indexOf(":")+1);
105 boolean auth=(getEMailProperties().getProperty("mail.smtp.auth", "false")).equalsIgnoreCase("true");
106 if (auth) {
107 final String username = getEMailProperties().getProperty("mail.smtp.user");
108 String pwd = getEMailProperties().getProperty("mail.smtp.password");
109
110 if (getEMailProperties().getProperty("mail.smtp.password" + Property.ENCRYPTED_ATTRIBUTE, "false").equalsIgnoreCase("true"))
111 {
112 try {
113 pwd = CryptorFactory.getCryptor().decrypt(pwd);
114 } catch (NoSuchPaddingException ex) {
115 log.error("Unable to decrypt settings",ex);
116 } catch (NoSuchAlgorithmException ex) {
117 log.error("Unable to decrypt settings",ex);
118 } catch (InvalidAlgorithmParameterException ex) {
119 log.error("Unable to decrypt settings",ex);
120 } catch (InvalidKeyException ex) {
121 log.error("Unable to decrypt settings",ex);
122 } catch (IllegalBlockSizeException ex) {
123 log.error("Unable to decrypt settings",ex);
124 } catch (BadPaddingException ex) {
125 log.error("Unable to decrypt settings",ex);
126 }
127 }
128 final String password = pwd;
129 log.debug("SMTP username = " + username + " from address = " + notificationEmailAddress);
130 Properties eMailProperties = getEMailProperties();
131 eMailProperties.remove("mail.smtp.user");
132 eMailProperties.remove("mail.smtp.password");
133 session = Session.getInstance(getEMailProperties(), new javax.mail.Authenticator() {
134 protected PasswordAuthentication getPasswordAuthentication() {
135 return new PasswordAuthentication(username, password);
136 }
137 });
138 } else {
139 Properties eMailProperties = getEMailProperties();
140 eMailProperties.remove("mail.smtp.user");
141 eMailProperties.remove("mail.smtp.password");
142 session = Session.getInstance(eMailProperties);
143 }
144 }
145 }
146
147 public DispositionReport notifySubscriptionListener(NotifySubscriptionListener body) throws DispositionReportFaultMessage, RemoteException {
148
149
150
151 try {
152 log.info("Sending notification email to " + notificationEmailAddress + " from " + getEMailProperties().getProperty("mail.smtp.from", "jUDDI"));
153 if (session !=null && notificationEmailAddress != null) {
154 MimeMessage message = new MimeMessage(session);
155 InternetAddress address = new InternetAddress(notificationEmailAddress);
156 Address[] to = {address};
157 message.setRecipients(RecipientType.TO, to);
158 message.setFrom(new InternetAddress(getEMailProperties().getProperty("mail.smtp.from", "jUDDI")));
159
160 String subscriptionResultXML = JAXBMarshaller.marshallToString(body, JAXBMarshaller.PACKAGE_SUBSCR_RES);
161 message.setText(subscriptionResultXML, "UTF-8");
162
163 message.setSubject(ResourceConfig.getGlobalMessage("notifications.smtp.default.subject") + " "
164 + StringEscapeUtils.escapeHtml(body.getSubscriptionResultsList().getSubscription().getSubscriptionKey()));
165 Transport.send(message);
166 }
167 else throw new DispositionReportFaultMessage("Session is null!", null);
168 } catch (Exception e) {
169 log.error(e.getMessage(),e);
170 throw new DispositionReportFaultMessage(e.getMessage(), null);
171 }
172
173 DispositionReport dr = new DispositionReport();
174 Result res = new Result();
175 dr.getResult().add(res);
176
177 return dr;
178 }
179 }