use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class DefangFilterTest method getHtmlBody.
/**
* Utility method that gets the html body part from a mime message and returns its input stream
* @param fileName The name of the email file to load from the unit test data dir
* @return The input stream for the html body if successful
* @throws Exception
*/
private InputStream getHtmlBody(String fileName) throws Exception {
// Get an input stream of a test pdf to test with
InputStream inputStream = new FileInputStream(EMAIL_BASE_DIR + fileName);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteUtil.copy(inputStream, true, baos, true);
ParsedMessage msg = new ParsedMessage(baos.toByteArray(), false);
Set<MPartInfo> bodyparts = Mime.getBody(msg.getMessageParts(), true);
InputStream htmlStream = null;
for (MPartInfo body : bodyparts) {
if (body.getContentType().contains("html")) {
htmlStream = body.getMimePart().getInputStream();
}
}
return htmlStream;
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testNotify_mimeVariables.
/**
* Tests a sieve rule with mime variable parameters.
*/
@Test
public void testNotify_mimeVariables() {
String filterScript = "require [\"enotify\", \"tag\"];\n" + "if envelope :matches [\"To\"] \"*\" {set \"rcptto\" \"${1}\";}\n" + "if envelope :matches [\"From\"] \"*\" {set \"mailfrom\" \"${1}\";}\n" + "if anyof(not envelope :is [\"From\"] \"\") {\n" + " set \"subjectparam\" \"Notification\";\n" + " set \"bodyparam\" text:\r\n" + "Hello ${rcptto},\n" + "A new massage has arrived.\n" + "Sent: ${Date}\n" + "From: ${From}\n" + "Subject: ${Subject}\r\n" + ".\r\n" + ";\n" + " notify :message \"${subjectparam}\"\n" + " :from \"${rcptto}\"\n" + " \"mailto:test2@zimbra.com?body=${bodyparam}\";\n" + "}";
String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Date: Tue, 11 Oct 2016 12:01:37 +0900\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: foo@example.com, baz@example.com\n" + "cc: qux@example.com\n";
String expectedNotifyMsg = "Hello test1@zimbra.com,\n" + "A new massage has arrived.\n" + "Sent: Tue, 11 Oct 2016 12:01:37 0900\n" + "From: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
RuleManager.clearCachedRules(acct1);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript);
acct1.setMail("test1@zimbra.com");
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// The triggered message should be delivered to the target mailbox
Assert.assertEquals(1, ids.size());
// Notification message should be delivered to mailto addresses
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
// Verify the subject line of the notification message
Assert.assertEquals("Notification", notifyMsg.getSubject());
// Verify the from header of the notification message
String[] headers = notifyMsg.getMimeMessage().getHeader("from");
Assert.assertTrue(headers.length == 1);
Assert.assertEquals("test1@zimbra.com", headers[0]);
// Verify the message body of the notification message
MimeMessage mm = notifyMsg.getMimeMessage();
List<MPartInfo> parts = Mime.getParts(mm);
Set<MPartInfo> bodies = Mime.getBody(parts, false);
Assert.assertEquals(1, bodies.size());
for (MPartInfo body : bodies) {
Object mimeContent = body.getMimePart().getContent();
Assert.assertTrue(mimeContent instanceof String);
String deliveredNotifyMsg = (String) mimeContent;
Assert.assertEquals(expectedNotifyMsg, deliveredNotifyMsg);
}
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class NotifyMailtoTest method testNotify_variable.
/**
* Tests a sieve rule with variable parameters.
*/
@Test
public void testNotify_variable() {
String filterScript = "require [\"enotify\", \"tag\"];\n" + "if envelope :matches [\"To\"] \"*\" {set \"rcptto\" \"${1}\";}\n" + "if envelope :matches [\"From\"] \"*\" {set \"mailfrom\" \"${1}\";}\n" + "if header :matches \"Date\" \"*\" {set \"dateheader\" \"${1}\";}\n" + "if header :matches \"From\" \"*\" {set \"fromheader\" \"${1}\";}\n" + "if header :matches \"Subject\" \"*\" {set \"subjectheader\" \"${1}\";}\n" + "if anyof(not envelope :is [\"From\"] \"\") {\n" + " set \"subjectparam\" \"Notification\";\n" + " set \"bodyparam\" text:\r\n" + "Hello ${rcptto},\n" + "A new massage has arrived.\n" + "Sent: ${dateheader}\n" + "From: ${fromheader}\n" + "Subject: ${subjectheader}\r\n" + ".\r\n" + ";\n" + " notify :message \"${subjectparam}\"\n" + " :from \"${rcptto}\"\n" + " \"mailto:test2@zimbra.com?body=${bodyparam}\";\n" + "}";
String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Date: Tue, 11 Oct 2016 12:01:37 +0900\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: foo@example.com, baz@example.com\n" + "cc: qux@example.com\n";
String expectedNotifyMsg = "Hello test1@zimbra.com,\n" + "A new massage has arrived.\n" + "Sent: Tue, 11 Oct 2016 12:01:37 0900\n" + "From: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out";
try {
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
RuleManager.clearCachedRules(acct1);
LmtpEnvelope env = new LmtpEnvelope();
LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
env.setSender(sender);
env.addLocalRecipient(recipient);
acct1.setMailSieveScript(filterScript);
acct1.setMail("test1@zimbra.com");
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
// The triggered message should be delivered to the target mailbox
Assert.assertEquals(1, ids.size());
// Notification message should be delivered to mailto addresses
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
// Verify the subject line of the notification message
Assert.assertEquals("Notification", notifyMsg.getSubject());
// Verify the from header of the notification message
String[] headers = notifyMsg.getMimeMessage().getHeader("from");
Assert.assertTrue(headers.length == 1);
Assert.assertEquals("test1@zimbra.com", headers[0]);
// Verify the message body of the notification message
MimeMessage mm = notifyMsg.getMimeMessage();
List<MPartInfo> parts = Mime.getParts(mm);
Set<MPartInfo> bodies = Mime.getBody(parts, false);
Assert.assertEquals(1, bodies.size());
for (MPartInfo body : bodies) {
Object mimeContent = body.getMimePart().getContent();
Assert.assertTrue(mimeContent instanceof String);
String deliveredNotifyMsg = (String) mimeContent;
Assert.assertEquals(expectedNotifyMsg, deliveredNotifyMsg);
}
} catch (Exception e) {
fail("No exception should be thrown");
}
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class FeedManagerTest method socialcastAtomFeed.
@Test
public void socialcastAtomFeed() throws Exception {
long lastModified = 0;
String expectedCharset = MimeConstants.P_CHARSET_UTF8;
BufferedInputStream content = new BufferedInputStream(getClass().getResourceAsStream("socialcastAtomFeed.xml"));
RemoteDataInfo rdi = new RemoteDataInfo(HttpStatus.OK_200, 0, content, expectedCharset, lastModified);
SubscriptionData<?> subsData = FeedManager.retrieveRemoteDatasource(null, rdi, null);
List<?> subs = subsData.getItems();
Assert.assertNotNull("List of subscriptions", subs);
Assert.assertEquals("Number of items", 1, subs.size());
for (Object obj : subs) {
if (obj instanceof ParsedMessage) {
ParsedMessage pm = (ParsedMessage) obj;
List<MPartInfo> parts = pm.getMessageParts();
Assert.assertEquals("Number of message parts", 1, parts.size());
String msgContent = streamToString(parts.get(0).getMimePart().getInputStream(), Charsets.UTF_8);
Assert.assertTrue("Text from inside <div>", msgContent.indexOf("Congratulations for passing!") > 0);
Assert.assertTrue("Article reference", msgContent.indexOf("https://pink.socialcast.com/messages/15629747-active-learner-thanks-to-cccc") > 0);
} else {
Assert.fail("Expecting a ParsedMessage where is " + obj.getClass().getName());
}
}
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class FilterUtil method getVarsMap.
@VisibleForTesting
static Map<String, String> getVarsMap(Mailbox mailbox, ParsedMessage parsedMessage, MimeMessage mimeMessage) throws MessagingException, ServiceException {
Map<String, String> vars = new HashMap<String, String>() {
@Override
public String get(Object key) {
return super.get(((String) key).toLowerCase());
}
};
Enumeration enumeration = mimeMessage.getAllHeaders();
while (enumeration.hasMoreElements()) {
Header header = (Header) enumeration.nextElement();
vars.put(header.getName().toLowerCase(), ZInternetHeader.decode(mimeMessage.getHeader(header.getName(), ",")));
}
// raw subject could be encoded, so get the parsed subject
vars.put("subject", parsedMessage.getSubject());
MPartInfo bodyPart = Mime.getTextBody(parsedMessage.getMessageParts(), false);
if (bodyPart != null) {
try {
vars.put("body", Mime.getStringContent(bodyPart.getMimePart(), mailbox.getAccount().getPrefMailDefaultCharset()));
} catch (IOException e) {
ZimbraLog.filter.warn("Error in reading text body", e);
}
}
return vars;
}
Aggregations