blob: ee4fbe2bc3dd102ba2809a9327a0a9d9a5509e04 [file] [log] [blame]
<?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> &gt; <a href="index.source.html" class="el_package">org.apache.juddi.subscription.notify</a> &gt; <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 &quot;License&quot;);
* 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 &quot;AS IS&quot; 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.
*
* &quot;mail.smtp.from&quot;, &quot;mail.smtp.host&quot;, &quot;mail.smtp.port&quot;,
* &quot;mail.smtp.socketFactory.class&quot;, &quot;mail.smtp.socketFactory.fallback&quot;, &quot;mail.smtp.starttls.enable&quot;,
* &quot;mail.smtp.socketFactory.port&quot;,&quot;mail.smtp.auth&quot;,&quot;mail.smtp.user&quot;,&quot;mail.smtp.password&quot;
*
* 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 = {&quot;mail.smtp.from&quot;, &quot;mail.smtp.host&quot;, &quot;mail.smtp.port&quot;, </span>
&quot;mail.smtp.socketFactory.class&quot;, &quot;mail.smtp.socketFactory.fallback&quot;, &quot;mail.smtp.starttls.enable&quot;,
&quot;mail.smtp.socketFactory.port&quot;,&quot;mail.smtp.auth&quot;,&quot;mail.smtp.user&quot;,&quot;mail.smtp.password&quot;,&quot;mail.debug&quot;};
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(&quot;.&quot;)) mailPrefix = mailPrefix + &quot;.&quot;;</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(&quot;smtp enpoints only support AccessPointType &quot; + 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(&quot;mailto:&quot;)) {</span>
<span class="nc" id="L100"> log.warn(&quot;smtp accessPointUrl for bindingTemplate &quot; + bindingTemplate.getEntityKey() + </span>
&quot; should start with 'mailto'&quot;);
//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(&quot;:&quot;)+1);</span>
<span class="fc" id="L105"> boolean auth=(getEMailProperties().getProperty(&quot;mail.smtp.auth&quot;, &quot;false&quot;)).equalsIgnoreCase(&quot;true&quot;);</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(&quot;mail.smtp.user&quot;);</span>
<span class="nc" id="L108"> String pwd = getEMailProperties().getProperty(&quot;mail.smtp.password&quot;);</span>
//decrypt if possible
<span class="nc bnc" id="L110" title="All 2 branches missed."> if (getEMailProperties().getProperty(&quot;mail.smtp.password&quot; + Property.ENCRYPTED_ATTRIBUTE, &quot;false&quot;).equalsIgnoreCase(&quot;true&quot;))</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(&quot;Unable to decrypt settings&quot;,ex);</span>
<span class="nc" id="L116"> } catch (NoSuchAlgorithmException ex) {</span>
<span class="nc" id="L117"> log.error(&quot;Unable to decrypt settings&quot;,ex);</span>
<span class="nc" id="L118"> } catch (InvalidAlgorithmParameterException ex) {</span>
<span class="nc" id="L119"> log.error(&quot;Unable to decrypt settings&quot;,ex);</span>
<span class="nc" id="L120"> } catch (InvalidKeyException ex) {</span>
<span class="nc" id="L121"> log.error(&quot;Unable to decrypt settings&quot;,ex);</span>
<span class="nc" id="L122"> } catch (IllegalBlockSizeException ex) {</span>
<span class="nc" id="L123"> log.error(&quot;Unable to decrypt settings&quot;,ex);</span>
<span class="nc" id="L124"> } catch (BadPaddingException ex) {</span>
<span class="nc" id="L125"> log.error(&quot;Unable to decrypt settings&quot;,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(&quot;SMTP username = &quot; + username + &quot; from address = &quot; + notificationEmailAddress);</span>
<span class="nc" id="L130"> Properties eMailProperties = getEMailProperties();</span>
<span class="nc" id="L131"> eMailProperties.remove(&quot;mail.smtp.user&quot;);</span>
<span class="nc" id="L132"> eMailProperties.remove(&quot;mail.smtp.password&quot;);</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(&quot;mail.smtp.user&quot;);</span>
<span class="fc" id="L141"> eMailProperties.remove(&quot;mail.smtp.password&quot;);</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(&quot;Sending notification email to &quot; + notificationEmailAddress + &quot; from &quot; + getEMailProperties().getProperty(&quot;mail.smtp.from&quot;, &quot;jUDDI&quot;));</span>
<span class="pc bpc" id="L153" title="2 of 4 branches missed."> if (session !=null &amp;&amp; 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(&quot;mail.smtp.from&quot;, &quot;jUDDI&quot;)));</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, &quot;UTF-8&quot;);</span>
//message.setContent(subscriptionResultXML, &quot;text/xml; charset=UTF-8;&quot;);
<span class="fc" id="L163"> message.setSubject(ResourceConfig.getGlobalMessage(&quot;notifications.smtp.default.subject&quot;) + &quot; &quot; </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(&quot;Session is null!&quot;, 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>