use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class DeleteHeaderTest method testDeleteHeaderXTestHeaderAll.
/*
* Delete all X-Test-Header
*/
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderXTestHeaderAll() {
String[] expected = { "Received: from edge01e.zimbra.com ([127.0.0.1])\r\n" + "\tby localhost (edge01e.zimbra.com [127.0.0.1]) (amavisd-new, port 10032)\r\n" + "\twith ESMTP id DN6rfD1RkHD7; Fri, 24 Jun 2016 01:45:31 -0400 (EDT)", "Received: from localhost (localhost [127.0.0.1])\r\n" + "\tby edge01e.zimbra.com (Postfix) with ESMTP id 9245B13575C;\r\n" + "\tFri, 24 Jun 2016 01:45:31 -0400 (EDT)", "X-Test-Header-non-ascii: =?utf-8?B?5pel5pys6Kqe44Gu5Lu25ZCN?=", "X-Numeric-Header: 2", "X-Numeric-Header: 3", "X-Numeric-Header: 4", "X-Dummy-Header: ABC", "X-Dummy-Header: 123", "X-Dummy-Header: abc", "X-Dummy-Header: \"\"", "X-Dummy-Header: xyz", "X-Dummy-Header: ", "X-Dummy-Header: test", "X-Dummy-Header: ''", "X-Dummy-Header: a1b2c3", "from: test2@zimbra.com", "Subject: example", "to: test@zimbra.com", "Content-Transfer-Encoding: 7bit", "MIME-Version: 1.0", "Message-ID:" };
try {
String filterScript = "require [\"editheader\"];\n" + " deleteheader \"X-Test-Header\" \r\n" + " ;\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
RuleManager.clearCachedRules(acct1);
acct1.setMailSieveScript(filterScript);
RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message message = mbox1.getMessageById(null, itemId);
boolean headerDeleted = true;
for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
Header temp = enumeration.nextElement();
if ("X-Test-Header".equals(temp.getName())) {
headerDeleted = false;
break;
}
}
Assert.assertTrue(headerDeleted);
// Verify the order of the message header
MimeMessage mm = message.getMimeMessage();
List<MPartInfo> parts = Mime.getParts(mm);
Set<MPartInfo> bodies = Mime.getBody(parts, false);
Assert.assertEquals(1, bodies.size());
for (MPartInfo body : bodies) {
Enumeration e = body.getMimePart().getAllHeaderLines();
int i = 0;
while (e.hasMoreElements()) {
String header = (String) e.nextElement();
if (header.startsWith("Message-ID:")) {
header = "Message-ID:";
}
Assert.assertEquals(expected[i++], header);
}
}
} catch (Exception e) {
fail("No exception should be thrown: " + e.getMessage());
}
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class DefangFilterTest method getHtmlPart.
/**
* 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 getHtmlPart(String fileName, int partNum) 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);
//Mime.getBody(msg.getMessageParts(), true);
List<MPartInfo> parts = msg.getMessageParts();
InputStream htmlStream = null;
for (MPartInfo body : parts) {
if (body.getPartNum() == partNum) {
htmlStream = body.getMimePart().getInputStream();
}
}
return htmlStream;
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class FeedManagerTest method atomEnabledOrg.
@Test
public void atomEnabledOrg() throws Exception {
long lastModified = 0;
String expectedCharset = MimeConstants.P_CHARSET_UTF8;
BufferedInputStream content = new BufferedInputStream(getClass().getResourceAsStream("atomEnabledOrg.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", 2, subs.size());
Object obj;
obj = subs.get(0);
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("Some content text", msgContent.indexOf("Rev 0.9 of the AtomAPI has just been posted") > 0);
} else {
Assert.fail("Expecting a ParsedMessage where is " + obj.getClass().getName());
}
obj = subs.get(1);
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("Some content text", msgContent.indexOf("AtomAPI at ApacheCon in Las Vegas") > 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 BodyTest method test.
private boolean test(MailAdapter mail, boolean caseSensitive, String substring) {
ZimbraMailAdapter zimbraMail = (ZimbraMailAdapter) mail;
ParsedMessage pm = zimbraMail.getParsedMessage();
if (pm == null) {
return false;
}
Account acct = null;
try {
acct = zimbraMail.getMailbox().getAccount();
} catch (ServiceException e) {
ZimbraLog.filter.warn("Error in getting account", e);
}
String defaultCharset = acct == null ? null : acct.getPrefMailDefaultCharset();
for (MPartInfo mpi : pm.getMessageParts()) {
String cType = mpi.getContentType();
// Check only parts that are text/plain or text/html and are not attachments.
if (!Part.ATTACHMENT.equals(mpi.getDisposition())) {
if (cType.equals(MimeConstants.CT_TEXT_PLAIN)) {
InputStream in = null;
try {
in = mpi.getMimePart().getInputStream();
String cthdr = mpi.getMimePart().getHeader("Content-Type", null);
String charset = null;
if (cthdr != null) {
charset = Mime.getCharset(cthdr);
}
if (charset == null || !Charset.isSupported(charset)) {
charset = defaultCharset;
}
Reader reader = charset == null ? new InputStreamReader(in) : new InputStreamReader(in, charset);
if (contains(reader, caseSensitive, substring)) {
return true;
}
} catch (Exception e) {
ZimbraLog.filter.warn("Unable to test text body for substring '%s'", substring, e);
} finally {
ByteUtil.closeStream(in);
}
} else if (cType.equals(MimeConstants.CT_TEXT_HTML)) {
InputStream in = null;
try {
// Extract up to 1MB of text and check for substring.
in = mpi.getMimePart().getInputStream();
String cthdr = mpi.getMimePart().getHeader("Content-Type", null);
Reader reader = Mime.getTextReader(in, cthdr, defaultCharset);
String text = HtmlTextExtractor.extract(reader, 1024 * 1024);
if (contains(new StringReader(text), caseSensitive, substring)) {
return true;
}
} catch (Exception e) {
ZimbraLog.filter.warn("Unable to test HTML body for substring '%s'", substring, e);
} finally {
ByteUtil.closeStream(in);
}
}
}
}
return false;
}
use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.
the class CalendarRequest method patchCalendarURLs.
protected static void patchCalendarURLs(MimeMessage mm, String htmlStr, String localURL, String orgAddress, String uid, String attendee, String invId) throws ServiceException {
try {
boolean changed = false;
String accept = buildUrl(localURL, orgAddress, uid, attendee, invId, "ACCEPT");
String decline = buildUrl(localURL, orgAddress, uid, attendee, invId, "DECLINE");
String tentative = buildUrl(localURL, orgAddress, uid, attendee, invId, "TENTATIVE");
for (MPartInfo mpi : Mime.getParts(mm)) {
if (mpi.getContentType().equals(MimeConstants.CT_TEXT_HTML)) {
String str = htmlStr;
str = str.replaceFirst("href=\"@@ACCEPT@@\"", accept);
str = str.replaceFirst("href=\"@@DECLINE@@\"", decline);
str = str.replaceFirst("href=\"@@TENTATIVE@@\"", tentative);
System.out.println(str);
mpi.getMimePart().setContent(str, MimeConstants.CT_TEXT_HTML);
changed = true;
// only match one part
break;
}
}
if (changed) {
mm.saveChanges();
}
} catch (IOException e) {
throw ServiceException.FAILURE("IOException " + e, e);
} catch (MessagingException e) {
throw ServiceException.FAILURE("MessagingException " + e, e);
}
}
Aggregations