Author: sebb
Date: Thu Jul 1 15:43:30 2010
New Revision: 959699
URL: http://svn.apache.org/viewvc?rev=959699&view=rev
Log:
Tidyup; consolidate duplicated code
Removed:
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/MailBodyProvider.java
Modified:
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
Modified:
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
URL:
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java?rev=959699&r1=959698&r2=959699&view=diff
==============================================================================
---
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
(original)
+++
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
Thu Jul 1 15:43:30 2010
@@ -21,9 +21,9 @@ package org.apache.jmeter.protocol.smtp.
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.util.ArrayList;
import java.util.Date;
import java.util.List;
-import java.util.Vector;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
@@ -31,10 +31,8 @@ import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
-import org.apache.jmeter.protocol.smtp.sampler.protocol.MailBodyProvider;
import org.apache.jmeter.protocol.smtp.sampler.protocol.SendMailCommand;
import org.apache.jmeter.protocol.smtp.sampler.tools.CounterOutputStream;
-
import org.apache.jmeter.samplers.AbstractSampler;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.SampleResult;
@@ -124,52 +122,21 @@ public class SmtpSampler extends Abstrac
}
try {
+
+ // Process address lists
+
instance.setReceiverTo(getPropNameAsAddresses(SmtpSampler.RECEIVER_TO));
+
instance.setReceiverCC(getPropNameAsAddresses(SmtpSampler.RECEIVER_CC));
+
instance.setReceiverBCC(getPropNameAsAddresses(SmtpSampler.RECEIVER_BCC));
+
+ instance.setSubject(getPropertyAsString(SUBJECT)
+ + (getPropertyAsBoolean(INCLUDE_TIMESTAMP) ?
+ " <<< current timestamp: " + new Date().getTime()
+ " >>>"
+ : ""
+ ));
+
if (!getPropertyAsBoolean(USE_EML)) { // part is only needed if we
// don't send an .eml-file
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- List<InternetAddress> receiversTo = new
Vector<InternetAddress>();
- if
(getPropertyAsString(SmtpSampler.RECEIVER_TO).matches("....@.*")) {
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_TO))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversTo.add(new
InternetAddress(strReceivers[i].trim()));
- }
- instance.setReceiverTo(receiversTo);
- }
-
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- if
(getPropertyAsString(SmtpSampler.RECEIVER_CC).matches("....@.*")) {
- List<InternetAddress> receiversCC = new
Vector<InternetAddress>();
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_CC))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversCC.add(new
InternetAddress(strReceivers[i].trim()));
- }
- instance.setReceiverCC(receiversCC);
- }
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- if
(getPropertyAsString(SmtpSampler.RECEIVER_BCC).matches("....@.*")) {
- List<InternetAddress> receiversBCC = new
Vector<InternetAddress>();
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_BCC))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversBCC.add(new
InternetAddress(strReceivers[i].trim()));
- }
- instance.setReceiverBCC(receiversBCC);
- }
-
- MailBodyProvider mb = new MailBodyProvider();
- if (getPropertyAsString(MESSAGE) != null
- && !getPropertyAsString(MESSAGE).equals(""))
- mb.setBody(getPropertyAsString(MESSAGE));
- instance.setMbProvider(mb);
-
+ instance.setMailBody(getPropertyAsString(MESSAGE));
if (!getAttachments().equals("")) {
String[] attachments =
getAttachments().split(FILENAME_SEPARATOR);
for (String attachment : attachments) {
@@ -177,60 +144,8 @@ public class SmtpSampler extends Abstrac
}
}
- instance.setSubject(getPropertyAsString(SUBJECT)
- + (getPropertyAsBoolean(INCLUDE_TIMESTAMP) ? "
<<< current timestamp: "
- + new Date().getTime() + " >>>"
- : ""));
- } else {
-
- // send an .eml-file
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- if
(getPropertyAsString(SmtpSampler.RECEIVER_TO).matches("....@.*")) {
- List<InternetAddress> receiversTo = new
Vector<InternetAddress>();
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_TO))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversTo.add(new
InternetAddress(strReceivers[i].trim()));
- }
- instance.setReceiverTo(receiversTo);
- }
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- if
(getPropertyAsString(SmtpSampler.RECEIVER_CC).matches("....@.*")) {
- List<InternetAddress> receiversCC = new
Vector<InternetAddress>();
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_CC))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversCC.add(new
InternetAddress(strReceivers[i].trim()));
- }
- instance.setReceiverCC(receiversCC);
- }
-
- // check if there are really mail-addresses in the fields and
if
- // there are multiple ones
- if (getPropertyAsString(SmtpSampler.RECEIVER_BCC).matches(
- "....@.*")) {
- List<InternetAddress> receiversBCC = new
Vector<InternetAddress>();
- String[] strReceivers =
(getPropertyAsString(SmtpSampler.RECEIVER_BCC))
- .split(";");
- for (int i = 0; i < strReceivers.length; i++) {
- receiversBCC.add(new InternetAddress(strReceivers[i]
- .trim()));
- }
- instance.setReceiverBCC(receiversBCC);
- }
-
- String subj = getPropertyAsString(SUBJECT);
- if (subj.trim().length() > 0) {
- instance.setSubject(subj
- + (getPropertyAsBoolean(INCLUDE_TIMESTAMP) ? " <<<
current timestamp: "
- + new Date().getTime() + " >>>"
- : ""));
- }
}
+
// needed for measuring sending time
instance.setSynchronousMode(true);
@@ -247,13 +162,19 @@ public class SmtpSampler extends Abstrac
} catch (AddressException ex) {
log.warn("Error while preparing message", ex);
+ res.setResponseCode("500");
+ res.setResponseMessage(ex.toString());
return res;
} catch (IOException ex) {
- // TODO Auto-generated catch block
- ex.printStackTrace();
+ log.warn("Error while preparing message", ex);
+ res.setResponseCode("500");
+ res.setResponseMessage(ex.toString());
+ return res;
} catch (MessagingException ex) {
- // TODO Auto-generated catch block
- ex.printStackTrace();
+ log.warn("Error while preparing message", ex);
+ res.setResponseCode("500");
+ res.setResponseMessage(ex.toString());
+ return res;
}
// Perform the sampling
@@ -284,15 +205,12 @@ public class SmtpSampler extends Abstrac
res.setResponseCode("500");
res.setResponseMessage("AuthenticationFailedException:
authentication failed - wrong username / password!\n"
+ afex);
- }
// SSL not supported, startTLS not supported, other messagingException
- catch (MessagingException mex) {
+ } catch (MessagingException mex) {
log.warn("",mex);
res.setResponseCode("500");
- if (mex.getMessage().matches(
- ".*Could not connect to SMTP host.*465.*")
- && mex.getCause().getMessage().matches(
- ".*Connection timed out.*")) {
+ if (mex.getMessage().matches(".*Could not connect to SMTP
host.*465.*")
+ && mex.getCause().getMessage().matches(".*Connection timed
out.*")) {
res.setResponseMessage("MessagingException: Probably, SSL is
not supported by the SMTP-Server!\n"
+ mex);
} else if (mex.getMessage().matches(".*StartTLS failed.*")) {
@@ -304,12 +222,10 @@ public class SmtpSampler extends Abstrac
res.setResponseMessage("MessagingException: Server certificate
not trusted - perhaps you have to restart JMeter!\n"
+ mex);
} else {
- res.setResponseMessage("Other MessagingException: "
- + mex.toString());
+ res.setResponseMessage("Other MessagingException: " +
mex.toString());
}
- }
- // general exception
- catch (Exception ex) {
+ } catch (Exception ex) { // general exception
+ log.warn("",ex);
res.setResponseCode("500");
if (null != ex.getMessage()
&& ex.getMessage().matches("Failed to build truststore")) {
@@ -344,6 +260,26 @@ public class SmtpSampler extends Abstrac
}
/**
+ * Get the list of addresses or null.
+ * Null is treated differently from an empty list.
+ * @param propName name of property containing addresses separated by ";"
+ * @return the list or null
+ * @throws AddressException
+ */
+ private List<InternetAddress> getPropNameAsAddresses(String propName)
throws AddressException{
+ final String propValue = getPropertyAsString(propName).trim();
+ if (propValue.length() > 0){ // we have at least one potential address
+ List<InternetAddress> addresses = new ArrayList<InternetAddress>();
+ for (String address : propValue.split(";")){
+ addresses.add(new InternetAddress(address.trim()));
+ }
+ return addresses;
+ } else {
+ return null;
+ }
+ }
+
+ /**
* @return FQDN or IP of mailserver
*/
public String getServer() {
Modified:
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
URL:
http://svn.apache.org/viewvc/jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java?rev=959699&r1=959698&r2=959699&view=diff
==============================================================================
---
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
(original)
+++
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
Thu Jul 1 15:43:30 2010
@@ -27,9 +27,10 @@ import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.security.Security;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
-import java.util.Vector;
+import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
@@ -56,17 +57,11 @@ import org.apache.log.Logger;
*/
public class SendMailCommand {
- /**
- * Standard-Constructor
- */
- public SendMailCommand() {
- headers = new HashMap<String, String>();
- attachments = new Vector<File>();
- }
-
// local vars
private static final Logger logger = LoggingManager.getLoggerForClass();
- public static final String TRUST_ALL_SOCKET_FACTORY =
"jmeter.smtpsampler.protocol.TrustAllSSLSocketFactory";
+
+ // Use the actual class so the name must be correct.
+ private static final String TRUST_ALL_SOCKET_FACTORY =
TrustAllSSLSocketFactory.class.getName();
private boolean useSSL = false;
private boolean useStartTLS = false;
@@ -93,7 +88,7 @@ public class SendMailCommand {
private List<File> attachments;
- private MailBodyProvider mbProvider;
+ private String mailBody;
// needed to check starttls functionality
private PrintStream debugOutStream;
@@ -108,6 +103,14 @@ public class SendMailCommand {
private StringBuffer serverResponse = new StringBuffer();
/**
+ * Standard-Constructor
+ */
+ public SendMailCommand() {
+ headers = new HashMap<String, String>();
+ attachments = new ArrayList<File>();
+ }
+
+ /**
* Prepares message prior to be sent via execute()-method, i.e. sets
* properties such as protocol, authentication, etc.
*
@@ -117,7 +120,7 @@ public class SendMailCommand {
*/
public Message prepareMessage() throws MessagingException, IOException {
- java.util.Properties props = new java.util.Properties();
+ Properties props = new Properties();
String protocol = getProtocol();
@@ -148,7 +151,7 @@ public class SendMailCommand {
// handle body and attachments
Multipart multipart = new MimeMultipart();
BodyPart body = new MimeBodyPart();
- body.setText(mbProvider.getMailBody());
+ body.setText(mailBody);
multipart.addBodyPart(body);
for (File f : attachments) {
@@ -203,9 +206,11 @@ public class SendMailCommand {
*
* @param message
* Message prior prepared by prepareMessage()
- * @throws Exception
+ * @throws MessagingException
+ * @throws IOException
+ * @throws InterruptedException
*/
- public void execute(Message message) throws Exception {
+ public void execute(Message message) throws MessagingException,
IOException, InterruptedException {
System.clearProperty("javax.net.ssl.trustStore");
@@ -358,28 +363,6 @@ public class SendMailCommand {
}
/**
- * Returns MailBodyProvider-Object for currend message
- *
- * @see MailBodyProvider
- * @return Current MailBody-Provider-Object
- */
- public MailBodyProvider getMbProvider() {
- return mbProvider;
- }
-
- /**
- * Sets MailBodyProvider-Object for currend message - to be called by
- * SmtpSampler-object
- *
- * @see MailBodyProvider
- * @param mbProvider
- * MailBody-Provider to be used
- */
- public void setMbProvider(MailBodyProvider mbProvider) {
- this.mbProvider = mbProvider;
- }
-
- /**
* Returns username to authenticate at the mailserver - standard getter
*
* @return Username for mailserver
@@ -420,16 +403,6 @@ public class SendMailCommand {
}
/**
- * Returns receivers of current message <InternetAddress> ("to") - standard
- * getter
- *
- * @return List of receivers
- */
- public List<InternetAddress> getReceiverTo() {
- return receiverTo;
- }
-
- /**
* Sets receivers of current message ("to") - to be called by
* SmtpSampler-object
*
@@ -761,6 +734,15 @@ public class SendMailCommand {
this.emlMessage = emlMessage;
}
+ /**
+ * Set the mail body.
+ *
+ * @param body
+ */
+ public void setMailBody(String body){
+ mailBody = body;
+ }
+
public StringBuffer getServerResponse() {
return this.serverResponse;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]