jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject svn commit: r1240674 - in /jmeter/trunk: src/components/org/apache/jmeter/reporters/MailerModel.java src/components/org/apache/jmeter/visualizers/MailerVisualizer.java xdocs/changes.xml
Date Sun, 05 Feb 2012 08:00:38 GMT
Author: pmouawad
Date: Sun Feb  5 08:00:38 2012
New Revision: 1240674

URL: http://svn.apache.org/viewvc?rev=1240674&view=rev
Log:
Bug 52603 - MailerVisualizer : Enable SSL , TLS and Authentication

Modified:
    jmeter/trunk/src/components/org/apache/jmeter/reporters/MailerModel.java
    jmeter/trunk/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java
    jmeter/trunk/xdocs/changes.xml

Modified: jmeter/trunk/src/components/org/apache/jmeter/reporters/MailerModel.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/reporters/MailerModel.java?rev=1240674&r1=1240673&r2=1240674&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/reporters/MailerModel.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/reporters/MailerModel.java Sun Feb  5 08:00:38
2012
@@ -24,8 +24,10 @@ import java.util.List;
 import java.util.Properties;
 import java.util.StringTokenizer;
 
+import javax.mail.Authenticator;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.AddressException;
@@ -34,6 +36,7 @@ import javax.mail.internet.MimeMessage;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.testelement.AbstractTestElement;
 import org.apache.jmeter.util.JMeterUtils;
@@ -45,13 +48,31 @@ import org.apache.log.Logger;
  *
  */
 public class MailerModel extends AbstractTestElement implements Serializable {
-
+    public static enum MailAuthType {
+        SSL("SSL"),
+        TLS("TLS"), 
+        NONE("");
+        
+        final private String value;
+        MailAuthType(String value) {
+            this.value = value;
+        }
+    }
+    
     private static final long serialVersionUID = 233L;
 
     private static final Logger log = LoggingManager.getLoggerForClass();
 
     private static final String MAIL_SMTP_HOST = "mail.smtp.host"; //$NON-NLS-1$
 
+    private static final String MAIL_SMTP_PORT = "mail.smtp.port"; //$NON-NLS-1$
+
+    private static final String MAIL_SMTP_AUTH = "mail.smtp.auth"; //$NON-NLS-1$
+
+    private static final String MAIL_SMTP_SOCKETFACTORY_CLASS = "mail.smtp.socketFactory.class";
//$NON-NLS-1$
+    
+    private static final String MAIL_SMTP_STARTTLS = "mail.smtp.starttls.enable"; //$NON-NLS-1$
+    
     private long failureCount = 0;
 
     private long successCount = 0;
@@ -68,6 +89,8 @@ public class MailerModel extends Abstrac
 
     private static final String HOST_KEY = "MailerModel.smtpHost"; //$NON-NLS-1$
 
+    private static final String PORT_KEY = "MailerModel.smtpPort"; //$NON-NLS-1$
+
     private static final String SUCCESS_SUBJECT = "MailerModel.successSubject"; //$NON-NLS-1$
 
     private static final String FAILURE_SUBJECT = "MailerModel.failureSubject"; //$NON-NLS-1$
@@ -76,8 +99,16 @@ public class MailerModel extends Abstrac
 
     private static final String SUCCESS_LIMIT_KEY = "MailerModel.successLimit"; //$NON-NLS-1$
 
+    private static final String LOGIN = "MailerModel.login"; //$NON-NLS-1$
+
+    private static final String PASSWORD = "MailerModel.password"; //$NON-NLS-1$
+
+    private static final String MAIL_AUTH_TYPE = "MailerModel.authType"; //$NON-NLS-1$
+
     private static final String DEFAULT_LIMIT = "2"; //$NON-NLS-1$
 
+    private static final int DEFAULT_SMTP_PORT = 25;
+
     /** The listener for changes. */
     private transient ChangeListener changeListener;
 
@@ -180,7 +211,9 @@ public class MailerModel extends Abstrac
             if (addressList.size() != 0) {
                 try {
                     sendMail(getFromAddress(), addressList, getFailureSubject(), "URL Failed:
"
-                            + sample.getSampleLabel(), getSmtpHost());
+                            + sample.getSampleLabel(), getSmtpHost(),
+                            getSmtpPort(), getLogin(), getPassword(),
+                            getMailAuthType());
                 } catch (Exception e) {
                     log.error("Problem sending mail: "+e);
                 }
@@ -215,6 +248,8 @@ public class MailerModel extends Abstrac
         notifyChangeListeners();
     }
 
+    
+
     /**
      * Resets the state of this object to its default. But: This method does not
      * reset any mail-specific attributes (like sender, mail-subject...) since
@@ -257,8 +292,35 @@ public class MailerModel extends Abstrac
      *            the message-body.
      * @param smtpHost
      *            the smtp-server used to send the mail.
+     * @throws MessagingException 
+     * @throws AddressException 
      */
-    public void sendMail(String from, List<String> vEmails, String subject, String
attText, String smtpHost)
+    public void sendMail(String from, List<String> vEmails, String subject, String
attText, String smtpHost) 
+            throws AddressException, MessagingException {
+        sendMail(from, vEmails, subject, attText, smtpHost, null, null, null, null);   
+    }
+    
+    /**
+     * Sends a mail with the given parameters using SMTP.
+     *
+     * @param from
+     *            the sender of the mail as shown in the mail-client.
+     * @param vEmails
+     *            all receivers of the mail. The receivers are seperated by
+     *            commas.
+     * @param subject
+     *            the subject of the mail.
+     * @param attText
+     *            the message-body.
+     * @param smtpHost
+     *            the smtp-server used to send the mail.
+     */
+    public void sendMail(String from, List<String> vEmails, String subject,
+            String attText, String smtpHost, 
+            Integer smtpPort,
+            final String user,
+            final String password,
+            MailAuthType mailAuthType)
             throws AddressException, MessagingException {
         String host = smtpHost;
         boolean debug = Boolean.valueOf(host).booleanValue();
@@ -274,15 +336,39 @@ public class MailerModel extends Abstrac
         Properties props = new Properties();
 
         props.put(MAIL_SMTP_HOST, host);
-        Session session = Session.getDefaultInstance(props, null);
+        if(smtpPort != null) {
+            props.put(MAIL_SMTP_PORT, smtpPort);
+        }
+        Authenticator authenticator = null;
+        if(mailAuthType != MailAuthType.NONE) {
+            props.put(MAIL_SMTP_AUTH, "true");
+            switch (mailAuthType) {
+                case SSL:
+                    props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, 
+                            "javax.net.ssl.SSLSocketFactory");
+                    break;
+                case TLS:
+                    props.put(MAIL_SMTP_STARTTLS, 
+                            "true");
+                    break;
+    
+                default:
+                    break;
+                }
+        }
+        
+        if(!StringUtils.isEmpty(user)) {
+            authenticator = 
+                    new javax.mail.Authenticator() {
+                        protected PasswordAuthentication getPasswordAuthentication() {
+                            return new PasswordAuthentication(user,password);
+                        }
+                    };
+        }
+        Session session = Session.getInstance(props, authenticator);
         // N.B. properties are only used when the default session is first
         // created
         // so check if the mail host needs to be reset...
-        props = session.getProperties();
-        if (!host.equalsIgnoreCase(props.getProperty(MAIL_SMTP_HOST))) {
-            props.setProperty(MAIL_SMTP_HOST, host);
-        }
-
         session.setDebug(debug);
 
         // create a message
@@ -305,7 +391,11 @@ public class MailerModel extends Abstrac
 
         log.info(attText);
 
-        sendMail(from, getAddressList(), subject, attText, smtpHost);
+        sendMail(from, getAddressList(), subject, attText, smtpHost,
+                getSmtpPort(), 
+                getLogin(), 
+                getPassword(),
+                getMailAuthType());
         log.info("Test mail sent successfully!!");
     }
 
@@ -327,6 +417,26 @@ public class MailerModel extends Abstrac
         setProperty(HOST_KEY, str);
     }
 
+    public void setSmtpPort(Integer str) {
+        if(str== null) {
+            setProperty(PORT_KEY, DEFAULT_SMTP_PORT);
+        } else {
+            setProperty(PORT_KEY, str);            
+        }
+    }
+    
+    public void setLogin(String login) {
+        setProperty(LOGIN, login);
+    }
+    
+    public void setPassword(String password) {
+        setProperty(PASSWORD, password);
+    }
+    
+    public void setMailAuthType(String value) {
+        setProperty(MAIL_AUTH_TYPE, value, "");
+    }
+    
     public void setFailureSubject(String str) {
         setProperty(FAILURE_SUBJECT, str);
     }
@@ -365,6 +475,10 @@ public class MailerModel extends Abstrac
         return getPropertyAsString(HOST_KEY);
     }
 
+    public int getSmtpPort() {
+        return getPropertyAsInt(PORT_KEY, DEFAULT_SMTP_PORT);
+    }
+
     public String getFailureSubject() {
         return getPropertyAsString(FAILURE_SUBJECT);
     }
@@ -388,4 +502,17 @@ public class MailerModel extends Abstrac
     public long getFailureCount() {
         return this.failureCount;
     }
-}
+
+    public String getLogin() {
+        return getPropertyAsString(LOGIN);
+    }
+
+    public String getPassword() {
+        return getPropertyAsString(PASSWORD);
+    }
+
+    public MailAuthType getMailAuthType() {
+        String authType = getPropertyAsString(MAIL_AUTH_TYPE, MailAuthType.NONE.toString());
+        return MailAuthType.valueOf(authType);
+    }
+}
\ No newline at end of file

Modified: jmeter/trunk/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java?rev=1240674&r1=1240673&r2=1240674&view=diff
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java (original)
+++ jmeter/trunk/src/components/org/apache/jmeter/visualizers/MailerVisualizer.java Sun Feb
 5 08:00:38 2012
@@ -24,19 +24,23 @@ import java.awt.GridBagLayout;
 import java.awt.Insets;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+
 import javax.mail.MessagingException;
 import javax.mail.internet.AddressException;
 import javax.swing.BorderFactory;
 import javax.swing.JButton;
+import javax.swing.JComboBox;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
+import javax.swing.JPasswordField;
 import javax.swing.JTextField;
 import javax.swing.border.Border;
 import javax.swing.border.EmptyBorder;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.jmeter.gui.util.VerticalPanel;
 import org.apache.jmeter.reporters.MailerModel;
 import org.apache.jmeter.reporters.MailerResultCollector;
@@ -72,6 +76,8 @@ public class MailerVisualizer extends Ab
 
     private JTextField smtpHostField;
 
+    private JTextField smtpPortField;
+
     private JTextField failureSubjectField;
 
     private JTextField successSubjectField;
@@ -82,6 +88,13 @@ public class MailerVisualizer extends Ab
 
     private JTextField successLimitField;
 
+    private JTextField smtpLoginField;
+
+    private JTextField smtpPasswordField;
+
+    private JComboBox authTypeCombo;
+
+
     // private JPanel mainPanel;
     // private JLabel panelTitleLabel;
 
@@ -180,6 +193,45 @@ public class MailerVisualizer extends Ab
         mailerPanel.add(smtpHostField);
 
         c.gridwidth = 1;
+        mailerPanel.add(new JLabel("SMTP Port:"));
+
+        smtpPortField = new JTextField(25);
+        smtpPortField.setEditable(true);
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        g.setConstraints(smtpPortField, c);
+        mailerPanel.add(smtpPortField);
+
+        c.gridwidth = 1;
+        mailerPanel.add(new JLabel("SMTP Login:"));
+
+        smtpLoginField = new JTextField(25);
+        smtpLoginField.setEditable(true);
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        g.setConstraints(smtpLoginField, c);
+        mailerPanel.add(smtpLoginField);
+
+        c.gridwidth = 1;
+        mailerPanel.add(new JLabel("SMTP Password:"));
+
+        smtpPasswordField = new JPasswordField(25);
+        smtpPasswordField.setEditable(true);
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        g.setConstraints(smtpPasswordField, c);
+        mailerPanel.add(smtpPasswordField);
+
+        c.gridwidth = 1;
+        mailerPanel.add(new JLabel("AUTH TYPE"));
+        
+        authTypeCombo = new JComboBox(new Object[] { 
+                MailerModel.MailAuthType.NONE.toString(), 
+                MailerModel.MailAuthType.SSL.toString(),
+                MailerModel.MailAuthType.TLS.toString()});
+        c.gridwidth = GridBagConstraints.REMAINDER;
+        g.setConstraints(authTypeCombo, c);
+        mailerPanel.add(authTypeCombo);
+        
+        
+        c.gridwidth = 1;
         mailerPanel.add(new JLabel("Failure Subject:"));
 
         failureSubjectField = new JTextField(25);
@@ -322,6 +374,13 @@ public class MailerVisualizer extends Ab
         mailerModel.setFailureSubject(failureSubjectField.getText());
         mailerModel.setFromAddress(fromField.getText());
         mailerModel.setSmtpHost(smtpHostField.getText());
+        mailerModel.setSmtpPort(
+                StringUtils.isEmpty(smtpPortField.getText()) ?
+                        null : Integer.valueOf(smtpPortField.getText()));
+        mailerModel.setLogin(smtpLoginField.getText());
+        mailerModel.setPassword(smtpPasswordField.getText());
+        mailerModel.setMailAuthType(
+                authTypeCombo.getSelectedItem().toString());
         mailerModel.setSuccessLimit(successLimitField.getText());
         mailerModel.setSuccessSubject(successSubjectField.getText());
         mailerModel.setToAddress(addressField.getText());
@@ -341,6 +400,10 @@ public class MailerVisualizer extends Ab
         addressField.setText(model.getToAddress());
         fromField.setText(model.getFromAddress());
         smtpHostField.setText(model.getSmtpHost());
+        smtpPortField.setText(Integer.toString(model.getSmtpPort()));
+        smtpLoginField.setText(model.getLogin());
+        smtpPasswordField.setText(model.getPassword());
+        authTypeCombo.setSelectedItem(model.getMailAuthType().toString());
         successSubjectField.setText(model.getSuccessSubject());
         failureSubjectField.setText(model.getFailureSubject());
         failureLimitField.setText(String.valueOf(model.getFailureLimit()));

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1240674&r1=1240673&r2=1240674&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml (original)
+++ jmeter/trunk/xdocs/changes.xml Sun Feb  5 08:00:38 2012
@@ -118,6 +118,7 @@ or a Debug Sampler with all fields set t
 
 <h3>Listeners</h3>
 <ul>
+<li>Bug 52603 - MailerVisualizer : Enable SSL , TLS and Authentication</li>
 </ul>
 
 <h3>Timers, Assertions, Config, Pre- &amp; Post-Processors</h3>



Mime
View raw message