| <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>SMTPNotifier.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">jUDDI Core Services</a> > <a href="index.source.html" class="el_package">org.apache.juddi.subscription.notify</a> > <span class="el_source">SMTPNotifier.java</span></div><h1>SMTPNotifier.java</h1><pre class="source lang-java linenums">/* |
| * Copyright 2001-2008 The Apache Software Foundation. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| */ |
| package org.apache.juddi.subscription.notify; |
| |
| import java.net.URISyntaxException; |
| import java.rmi.RemoteException; |
| import java.security.InvalidAlgorithmParameterException; |
| import java.security.InvalidKeyException; |
| import java.security.NoSuchAlgorithmException; |
| import java.util.Properties; |
| |
| import javax.crypto.BadPaddingException; |
| import javax.crypto.IllegalBlockSizeException; |
| import javax.crypto.NoSuchPaddingException; |
| import javax.mail.Address; |
| import javax.mail.Message.RecipientType; |
| import javax.mail.PasswordAuthentication; |
| import javax.mail.Session; |
| import javax.mail.Transport; |
| import javax.mail.internet.InternetAddress; |
| import javax.mail.internet.MimeMessage; |
| |
| import org.apache.commons.configuration.ConfigurationException; |
| import org.apache.commons.lang.StringEscapeUtils; |
| import org.apache.commons.logging.Log; |
| import org.apache.commons.logging.LogFactory; |
| import org.apache.juddi.api_v3.AccessPointType; |
| import org.apache.juddi.config.AppConfig; |
| import org.apache.juddi.config.Property; |
| import org.apache.juddi.config.ResourceConfig; |
| import org.apache.juddi.cryptor.CryptorFactory; |
| import org.apache.juddi.jaxb.JAXBMarshaller; |
| import org.apache.juddi.model.BindingTemplate; |
| import org.uddi.api_v3.DispositionReport; |
| import org.uddi.api_v3.Result; |
| import org.uddi.subr_v3.NotifySubscriptionListener; |
| import org.uddi.v3_service.DispositionReportFaultMessage; |
| |
| /** |
| * This class sends Email alerts when a specific subscription is tripped. |
| * The following properties can be set in the juddiv3.xml, or as System params. |
| * |
| * "mail.smtp.from", "mail.smtp.host", "mail.smtp.port", |
| * "mail.smtp.socketFactory.class", "mail.smtp.socketFactory.fallback", "mail.smtp.starttls.enable", |
| * "mail.smtp.socketFactory.port","mail.smtp.auth","mail.smtp.user","mail.smtp.password" |
| * |
| * The following properties can be set juddiv3.xml. |
| * |
| * @author Kurt Stam |
| */ |
| public class SMTPNotifier implements Notifier { |
| |
| <span class="fc" id="L67"> protected static final Log log = LogFactory.getLog(SMTPNotifier.class);</span> |
| <span class="fc" id="L68"> protected String notificationEmailAddress = null;</span> |
| //String from = null; |
| <span class="fc" id="L70"> protected Session session = null;</span> |
| <span class="fc" id="L71"> protected Properties properties = null;</span> |
| |
| <span class="fc" id="L73"> protected final static String[] mailProps = {"mail.smtp.from", "mail.smtp.host", "mail.smtp.port", </span> |
| "mail.smtp.socketFactory.class", "mail.smtp.socketFactory.fallback", "mail.smtp.starttls.enable", |
| "mail.smtp.socketFactory.port","mail.smtp.auth","mail.smtp.user","mail.smtp.password","mail.debug"}; |
| |
| protected final Properties getEMailProperties() throws ConfigurationException { |
| <span class="pc bpc" id="L78" title="1 of 4 branches missed."> if (properties==null || properties.isEmpty()) {</span> |
| <span class="fc" id="L79"> properties = new Properties();</span> |
| <span class="fc" id="L80"> String mailPrefix = AppConfig.getConfiguration().getString(Property.JUDDI_EMAIL_PREFIX, Property.DEFAULT_JUDDI_EMAIL_PREFIX);</span> |
| <span class="pc bpc" id="L81" title="1 of 2 branches missed."> if (! mailPrefix.endsWith(".")) mailPrefix = mailPrefix + ".";</span> |
| <span class="fc bfc" id="L82" title="All 2 branches covered."> for (String key: mailProps) {</span> |
| <span class="fc bfc" id="L83" title="All 2 branches covered."> if (AppConfig.getConfiguration().containsKey(mailPrefix + key)) {</span> |
| <span class="fc" id="L84"> properties.put(key, AppConfig.getConfiguration().getProperty(mailPrefix + key));</span> |
| <span class="pc bpc" id="L85" title="1 of 2 branches missed."> } else if (System.getProperty(mailPrefix + key) != null) {</span> |
| <span class="nc" id="L86"> properties.put(key, System.getProperty(mailPrefix + key));</span> |
| } |
| } |
| } |
| <span class="fc" id="L90"> return properties;</span> |
| } |
| |
| public SMTPNotifier(BindingTemplate bindingTemplate) throws URISyntaxException, Exception { |
| <span class="fc" id="L94"> super();</span> |
| <span class="pc bpc" id="L95" title="1 of 2 branches missed."> if (!AccessPointType.END_POINT.toString().equalsIgnoreCase(bindingTemplate.getAccessPointType())) {</span> |
| <span class="nc" id="L96"> log.error("smtp enpoints only support AccessPointType " + AccessPointType.END_POINT);</span> |
| } |
| <span class="fc" id="L98"> String accessPointUrl = bindingTemplate.getAccessPointUrl().toLowerCase();</span> |
| <span class="pc bpc" id="L99" title="1 of 2 branches missed."> if (!accessPointUrl.startsWith("mailto:")) {</span> |
| <span class="nc" id="L100"> log.warn("smtp accessPointUrl for bindingTemplate " + bindingTemplate.getEntityKey() + </span> |
| " should start with 'mailto'"); |
| //TODO maybe update the user's bindingTemplate with the error?, and also validate setting onsave |
| } else { |
| <span class="fc" id="L104"> notificationEmailAddress = accessPointUrl.substring(accessPointUrl.indexOf(":")+1);</span> |
| <span class="fc" id="L105"> boolean auth=(getEMailProperties().getProperty("mail.smtp.auth", "false")).equalsIgnoreCase("true");</span> |
| <span class="pc bpc" id="L106" title="1 of 2 branches missed."> if (auth) {</span> |
| <span class="nc" id="L107"> final String username = getEMailProperties().getProperty("mail.smtp.user");</span> |
| <span class="nc" id="L108"> String pwd = getEMailProperties().getProperty("mail.smtp.password");</span> |
| //decrypt if possible |
| <span class="nc bnc" id="L110" title="All 2 branches missed."> if (getEMailProperties().getProperty("mail.smtp.password" + Property.ENCRYPTED_ATTRIBUTE, "false").equalsIgnoreCase("true"))</span> |
| { |
| try { |
| <span class="nc" id="L113"> pwd = CryptorFactory.getCryptor().decrypt(pwd);</span> |
| <span class="nc" id="L114"> } catch (NoSuchPaddingException ex) {</span> |
| <span class="nc" id="L115"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L116"> } catch (NoSuchAlgorithmException ex) {</span> |
| <span class="nc" id="L117"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L118"> } catch (InvalidAlgorithmParameterException ex) {</span> |
| <span class="nc" id="L119"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L120"> } catch (InvalidKeyException ex) {</span> |
| <span class="nc" id="L121"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L122"> } catch (IllegalBlockSizeException ex) {</span> |
| <span class="nc" id="L123"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L124"> } catch (BadPaddingException ex) {</span> |
| <span class="nc" id="L125"> log.error("Unable to decrypt settings",ex);</span> |
| <span class="nc" id="L126"> }</span> |
| } |
| <span class="nc" id="L128"> final String password = pwd;</span> |
| <span class="nc" id="L129"> log.debug("SMTP username = " + username + " from address = " + notificationEmailAddress);</span> |
| <span class="nc" id="L130"> Properties eMailProperties = getEMailProperties();</span> |
| <span class="nc" id="L131"> eMailProperties.remove("mail.smtp.user");</span> |
| <span class="nc" id="L132"> eMailProperties.remove("mail.smtp.password");</span> |
| <span class="nc" id="L133"> session = Session.getInstance(getEMailProperties(), new javax.mail.Authenticator() {</span> |
| protected PasswordAuthentication getPasswordAuthentication() { |
| <span class="nc" id="L135"> return new PasswordAuthentication(username, password);</span> |
| } |
| }); |
| <span class="nc" id="L138"> } else {</span> |
| <span class="fc" id="L139"> Properties eMailProperties = getEMailProperties();</span> |
| <span class="fc" id="L140"> eMailProperties.remove("mail.smtp.user");</span> |
| <span class="fc" id="L141"> eMailProperties.remove("mail.smtp.password");</span> |
| <span class="fc" id="L142"> session = Session.getInstance(eMailProperties);</span> |
| } |
| } |
| <span class="fc" id="L145"> }</span> |
| |
| public DispositionReport notifySubscriptionListener(NotifySubscriptionListener body) throws DispositionReportFaultMessage, RemoteException { |
| |
| |
| |
| try { |
| <span class="fc" id="L152"> log.info("Sending notification email to " + notificationEmailAddress + " from " + getEMailProperties().getProperty("mail.smtp.from", "jUDDI"));</span> |
| <span class="pc bpc" id="L153" title="2 of 4 branches missed."> if (session !=null && notificationEmailAddress != null) {</span> |
| <span class="fc" id="L154"> MimeMessage message = new MimeMessage(session);</span> |
| <span class="fc" id="L155"> InternetAddress address = new InternetAddress(notificationEmailAddress);</span> |
| <span class="fc" id="L156"> Address[] to = {address};</span> |
| <span class="fc" id="L157"> message.setRecipients(RecipientType.TO, to);</span> |
| <span class="fc" id="L158"> message.setFrom(new InternetAddress(getEMailProperties().getProperty("mail.smtp.from", "jUDDI")));</span> |
| //maybe nice to use a template rather then sending raw xml. |
| <span class="fc" id="L160"> String subscriptionResultXML = JAXBMarshaller.marshallToString(body, JAXBMarshaller.PACKAGE_SUBSCR_RES);</span> |
| <span class="fc" id="L161"> message.setText(subscriptionResultXML, "UTF-8");</span> |
| //message.setContent(subscriptionResultXML, "text/xml; charset=UTF-8;"); |
| <span class="fc" id="L163"> message.setSubject(ResourceConfig.getGlobalMessage("notifications.smtp.default.subject") + " " </span> |
| <span class="fc" id="L164"> + StringEscapeUtils.escapeHtml(body.getSubscriptionResultsList().getSubscription().getSubscriptionKey()));</span> |
| <span class="fc" id="L165"> Transport.send(message);</span> |
| <span class="fc" id="L166"> }</span> |
| <span class="nc" id="L167"> else throw new DispositionReportFaultMessage("Session is null!", null);</span> |
| <span class="nc" id="L168"> } catch (Exception e) {</span> |
| <span class="nc" id="L169"> log.error(e.getMessage(),e);</span> |
| <span class="nc" id="L170"> throw new DispositionReportFaultMessage(e.getMessage(), null);</span> |
| <span class="fc" id="L171"> }</span> |
| |
| <span class="fc" id="L173"> DispositionReport dr = new DispositionReport();</span> |
| <span class="fc" id="L174"> Result res = new Result();</span> |
| <span class="fc" id="L175"> dr.getResult().add(res);</span> |
| |
| <span class="fc" id="L177"> return dr;</span> |
| } |
| } |
| </pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html> |