Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sending messages without a body #2393

Closed
asfimport opened this issue Aug 18, 2010 · 6 comments
Closed

Allow sending messages without a body #2393

asfimport opened this issue Aug 18, 2010 · 6 comments

Comments

@asfimport
Copy link
Collaborator

Luciana Moreira (Bug 49775):
Similar to #2387 I have created a patch to send messages without a body. The patch enhances the functionality of the SMTP Sampler in JMeter.

This patch is attached, and consists of a simple check box to determine if the body should be empty or not.

OS: Linux

@asfimport
Copy link
Collaborator Author

Luciana Moreira (migrated from Bugzilla):
Created attachment patch.txt: patch that allows sending messages without a body

patch.txt
### Eclipse Workspace Patch 1.0
#P jmeterSVN
Index: src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpSamplerGui.java
===================================================================
--- src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpSamplerGui.java	(revision 986649)
+++ src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpSamplerGui.java	(working copy)
@@ -74,6 +74,7 @@
         smtpPanel.setReceiverBCC(element.getPropertyAsString(SmtpSampler.RECEIVER_BCC));
 
         smtpPanel.setBody(element.getPropertyAsString(SmtpSampler.MESSAGE));
+        smtpPanel.setEmptyBody(element.getPropertyAsBoolean(SmtpSampler.EMPTY_MESSAGE));
         smtpPanel.setSubject(element.getPropertyAsString(SmtpSampler.SUBJECT));
         smtpPanel.setSuppressSubject(element.getPropertyAsBoolean(SmtpSampler.SUPPRESS_SUBJECT));
         smtpPanel.setIncludeTimestamp(element.getPropertyAsBoolean(SmtpSampler.INCLUDE_TIMESTAMP));
@@ -130,6 +131,7 @@
         te.setProperty(SmtpSampler.SUPPRESS_SUBJECT, Boolean.toString(smtpPanel.isSuppressSubject()));
         te.setProperty(SmtpSampler.INCLUDE_TIMESTAMP, Boolean.toString(smtpPanel.isIncludeTimestamp()));
         te.setProperty(SmtpSampler.MESSAGE, smtpPanel.getBody());
+        te.setProperty(SmtpSampler.EMPTY_MESSAGE, Boolean.toString(smtpPanel.isEmptyBody()));
         te.setProperty(SmtpSampler.ATTACH_FILE, smtpPanel.getAttachments());
         
         SecuritySettingsPanel secPanel = smtpPanel.getSecuritySettingsPanel();
Index: src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
===================================================================
--- src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java	(revision 986645)
+++ src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java	(working copy)
@@ -94,6 +94,8 @@
 
     private StringBuffer serverResponse = new StringBuffer();
 
+	private boolean emptyMessage;
+
     /**
      * Standard-Constructor
      */
@@ -172,18 +174,21 @@
             message = new MimeMessage(session);
             // handle body and attachments
             Multipart multipart = new MimeMultipart();
-            BodyPart body = new MimeBodyPart();
-            body.setText(mailBody);
-            multipart.addBodyPart(body);
+            if(!emptyMessage){
+                BodyPart body = new MimeBodyPart();
+                body.setText(mailBody);
+                multipart.addBodyPart(body);	
+            }
 
             for (File f : attachments) {
                 BodyPart attach = new MimeBodyPart();
                 attach.setFileName(f.getName());
-                attach.setDataHandler(new DataHandler(new FileDataSource(f)));
+                attach.setDataHandler(new DataHandler(new FileDataSource(f.getAbsolutePath())));
                 multipart.addBodyPart(attach);
             }
-
-            message.setContent(multipart);
+            if(!(emptyMessage && attachments.size() == 0)){
+            	message.setContent(multipart);	
+            }            
         }
 
         // set from field and subject
@@ -722,6 +727,15 @@
     public void setMailBody(String body){
         mailBody = body;
     }
+    
+    /**
+     * Set the mail body.
+     *
+     * @param body
+     */
+    public void setEmptyBody(boolean emptyBody){
+    	emptyMessage = emptyBody;
+    }
 
     public StringBuffer getServerResponse() {
         return this.serverResponse;
Index: src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java
===================================================================
--- src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java	(revision 986647)
+++ src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/SmtpSampler.java	(working copy)
@@ -37,6 +37,7 @@
 import org.apache.jmeter.samplers.AbstractSampler;
 import org.apache.jmeter.samplers.Entry;
 import org.apache.jmeter.samplers.SampleResult;
+import org.apache.jmeter.services.FileServer;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.log.Logger;
@@ -68,6 +69,7 @@
     public final static String SUBJECT              = "SMTPSampler.subject"; // $NON-NLS-1$
     public final static String SUPPRESS_SUBJECT     = "SMTPSampler.suppressSubject"; // $NON-NLS-1$
     public final static String MESSAGE              = "SMTPSampler.message"; // $NON-NLS-1$
+    public final static String EMPTY_MESSAGE        = "SMTPSampler.emptyMessage"; // $NON-NLS-1$
     public final static String INCLUDE_TIMESTAMP    = "SMTPSampler.include_timestamp"; // $NON-NLS-1$
     public final static String ATTACH_FILE          = "SMTPSampler.attachFile"; // $NON-NLS-1$
     public final static String MESSAGE_SIZE_STATS   = "SMTPSampler.messageSizeStatistics"; // $NON-NLS-1$
@@ -149,11 +151,18 @@
             if (!getPropertyAsBoolean(USE_EML)) { // part is only needed if we
                 // don't send an .eml-file
                 instance.setMailBody(getPropertyAsString(MESSAGE));
+                instance.setEmptyBody(getPropertyAsBoolean(EMPTY_MESSAGE));
                 final String filesToAttach = getPropertyAsString(ATTACH_FILE);
                 if (!filesToAttach.equals("")) {
                     String[] attachments = filesToAttach.split(FILENAME_SEPARATOR);
                     for (String attachment : attachments) {
-                        instance.addAttachment(new File(attachment));
+                    	File file = new File(attachment);
+                    	if(!file.isAbsolute()){
+                            log.info("loading file with relative path: " +attachment);
+                            file = new File(FileServer.getFileServer().getBaseDir(), attachment);
+                            log.info("file path set to: "+attachment);
+                        }
+                        instance.addAttachment(file);
                     }
                 }
 
Index: src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpPanel.java
===================================================================
--- src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpPanel.java	(revision 986649)
+++ src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpPanel.java	(working copy)
@@ -63,6 +63,7 @@
     private JTextField tfAttachment;
     private JTextField tfEmlMessage;
     private JTextArea taMessage;
+    private JCheckBox cbEmptyBody;
 
     private JLabel jlAddressFrom;
     private JLabel jlAddressTo;
@@ -298,6 +299,26 @@
     }
 
     /**
+     * Returns true if message body should be empty
+     *
+     * @return Subject of e-mail
+     */
+    public boolean isEmptyBody() {
+        return cbEmptyBody.isSelected();
+    }
+
+    /**
+     * Sets the property that defines if the body should be null
+     *
+     * @param subject
+     *            Subject of e-mail
+     */
+    public void setEmptyBody(boolean emptyBody) {
+    	cbEmptyBody.setSelected(emptyBody);
+    	taMessage.setEnabled(!emptyBody);
+    }
+
+    /**
      * Returns if mail-server needs authentication (checkbox)
      *
      * @return true if authentication is used
@@ -484,6 +505,13 @@
 
         taMessage = new JTextArea(5, 20);
 
+        cbEmptyBody = new JCheckBox(JMeterUtils.getResString("smtp_emptybody")); // $NON-NLS-1$
+        cbEmptyBody.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent evt) {
+                emptyBodyActionPerformed(evt);
+            }
+        });
+        
         cbSuppressSubject = new JCheckBox(JMeterUtils.getResString("smtp_suppresssubj")); // $NON-NLS-1$
         cbSuppressSubject.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
@@ -742,6 +770,13 @@
         gridBagConstraints.gridy = 3;
         gridBagConstraints.fill = GridBagConstraints.BOTH;
         panelMessageSettings.add(taMessage, gridBagConstraints);
+        
+        cbEmptyBody.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
+        cbEmptyBody.setMargin(new java.awt.Insets(0, 0, 0, 0));
+        gridBagConstraints.gridx = 2;
+        gridBagConstraints.gridy = 3;
+        gridBagConstraints.fill = GridBagConstraints.NONE;
+        panelMessageSettings.add(cbEmptyBody, gridBagConstraints);
 
         gridBagConstraints.gridx = 0;
         gridBagConstraints.gridy = 4;
@@ -939,6 +974,7 @@
         tfMailToBCC.setText("");
         tfMailToCC.setText("");
         tfSubject.setText("");
+        cbEmptyBody.setSelected(false);
         cbSuppressSubject.setSelected(false);
         securitySettingsPanel.clear();
         clearHeaderFields();
@@ -1041,4 +1077,11 @@
             }
         }
     }
+	
+	private void emptyBodyActionPerformed(ActionEvent evt) {
+		final Object source = evt.getSource();
+    	if(source != null && source instanceof JCheckBox){
+			taMessage.setEnabled(!cbEmptyBody.isSelected());
+    	}		
+	}
 }
\ No newline at end of file
Index: src/core/org/apache/jmeter/resources/messages.properties
===================================================================
--- src/core/org/apache/jmeter/resources/messages.properties	(revision 986644)
+++ src/core/org/apache/jmeter/resources/messages.properties	(working copy)
@@ -789,6 +789,7 @@
 smime_assertion_signer_serial=Serial Number
 smime_assertion_title=SMIME Assertion
 smime_assertion_verify_signature=Verify signature
+smtp_emptybody=Supress Message Body
 smtp_additional_settings=Additional Settings
 smtp_attach_file=Attach file(s):
 smtp_attach_file_tooltip=Separate multiple files with ";"

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Why is the following change needed?

  • attach.setDataHandler(new DataHandler(new FileDataSource(f)));
  • attach.setDataHandler(new DataHandler(new FileDataSource(f.getAbsolutePath())));

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Also, I get

java.io.IOException: No content

if the message body is suppressed and there are no attached files.

Perhaps the checkbox should only be enabled if there is at least one file?

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
If there is no body and no files, it looks like the code needs to do the following to avoid the error:

message.setText("");

This results in an empty body, with the headers:

MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

I assume that this is what you were aiming for?

==

Given that it might be useful to send SMTP messages which are not multipart/mixed, maybe it would be better to change the code so that if the flag is set then the code uses:

message.setText(mailBody)

This would only make sense if there were no files.

Though if the mailBody was emtpy, perhaps one could allow a single file and send that as the body.

If you think this makes sense, I'm happy to make the changes (i.e. no need for you to redo the patch).

@asfimport
Copy link
Collaborator Author

Luciana Moreira (migrated from Bugzilla):
Sebb this week I am quite busy, but I will do my best to get back to you on these topics next week.

Good to see that you are looking into it ;)

@asfimport
Copy link
Collaborator Author

Sebb (migrated from Bugzilla):
Applied patch to SVN with some changes as described:

URL: http://svn.apache.org/viewvc?rev=991510&view=rev
Log:
#2393 - Allow sending messages without a body

Modified:
jakarta/jmeter/trunk/docs/images/screenshots/smtp_sampler.png
jakarta/jmeter/trunk/src/core/org/apache/jmeter/resources/messages.properties
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/gui/SmtpPanel.java
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/gui/SmtpSamplerGui.java
jakarta/jmeter/trunk/src/protocol/mail/org/apache/jmeter/protocol/smtp/sampler/protocol/SendMailCommand.java
jakarta/jmeter/trunk/xdocs/changes.xml
jakarta/jmeter/trunk/xdocs/images/screenshots/smtp_sampler.png
jakarta/jmeter/trunk/xdocs/usermanual/component_reference.xml

Please re-open the bug if there are any issues with the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant