use of com.zimbra.common.zmime.ZMimeUtility.ByteBuilder in project zm-mailbox by Zimbra.
the class ZMimeMultipartTest method encoded.
@Test
public void encoded() throws Exception {
final String boundary = "dfghjkl";
final String preamble = "when in the course of human events...\r\n";
final String plain = "The Rain in Spain.";
final String html = "The <u>Rain</u> in <em>Spain</em>.";
ByteBuilder bbheader = new ByteBuilder();
bbheader.append("From: test@example.com\r\n");
bbheader.append("To: rcpt@example.com\r\n");
bbheader.append("Subject: message subject\r\n");
bbheader.append("Message-ID: <11e1-b0c4-0800200c9a66@example.com>\r\n");
bbheader.append("Content-Transfer-Encoding: base64\r\n");
bbheader.append("Content-Type: multipart/alternative; boundary=").append(boundary).append("\r\n");
bbheader.append("\r\n");
ByteBuilder bbmulti = new ByteBuilder();
bbmulti.append(preamble);
bbmulti.append("--").append(boundary).append("\r\n");
bbmulti.append("Content-Type: text/plain\r\n");
bbmulti.append("\r\n");
bbmulti.append(plain).append("\r\n");
bbmulti.append("--").append(boundary).append("\r\n");
bbmulti.append("Content-Type: text/html\r\n");
bbmulti.append("\r\n");
bbmulti.append(html).append("\r\n");
bbmulti.append("--").append(boundary).append("--\r\n");
// message with CTE header and base64-encoded body
ByteBuilder bb = new ByteBuilder();
bb.append(bbheader);
bb.append(ByteUtil.getContent(new Base64EncoderStream(new ByteArrayInputStream(bbmulti.toByteArray())), -1));
Session s = Session.getDefaultInstance(new Properties());
ZMimeMessage mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
Object o = mm.getContent();
Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
ZMimeMultipart multi = (ZMimeMultipart) o;
Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
Assert.assertEquals("2 subparts", 2, multi.getCount());
Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
// message with CTE header and nonencoded body
bb = new ByteBuilder();
bb.append(bbheader);
bb.append(bbmulti);
mm = new ZMimeMessage(s, new SharedByteArrayInputStream(bb.toByteArray()));
o = mm.getContent();
Assert.assertTrue("content is ZMimeMultipart", o instanceof ZMimeMultipart);
multi = (ZMimeMultipart) o;
Assert.assertEquals("preamble matches", preamble, multi.getPreamble());
Assert.assertEquals("2 subparts", 2, multi.getCount());
Assert.assertEquals("part 1 content match", plain, multi.getBodyPart(0).getContent());
Assert.assertEquals("part 2 content match", html, multi.getBodyPart(1).getContent());
}
use of com.zimbra.common.zmime.ZMimeUtility.ByteBuilder in project zm-mailbox by Zimbra.
the class ZMimeParserTest method detectBoundary.
@Test
public void detectBoundary() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
bb.append("From: <foo@example.com\r\n");
bb.append("Subject: sample\r\n");
appendMultipartWithoutBoundary(bb);
MimeMessage mm = new ZMimeMessage(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
Assert.assertTrue("content is multipart", mm.getContent() instanceof ZMimeMultipart);
testMultipartWithoutBoundary((ZMimeMultipart) mm.getContent());
bb.reset();
bb.append("From: <foo@example.com\r\n");
bb.append("Subject: sample\r\n");
bb.append("Content-Type: multipart/alternative\r\n");
bb.append("\r\n");
bb.append("prologue text goes here\r\n");
bb.append("--").append(BOUNDARY2).append("\r\n");
appendMultipartWithoutBoundary(bb);
bb.append("--").append(BOUNDARY2).append("--\r\n");
mm = new ZMimeMessage(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
Assert.assertTrue("content is multipart", mm.getContent() instanceof ZMimeMultipart);
ZMimeMultipart mmp = (ZMimeMultipart) mm.getContent();
Assert.assertEquals("multipart/alternative", "alternative", new ZContentType(mmp.getContentType()).getSubType());
Assert.assertEquals("toplevel multipart has 1 subpart", 1, mmp.getCount());
Assert.assertEquals("implicit boundary detection", BOUNDARY2, mmp.getBoundary());
Assert.assertEquals("first part is multipart/mixed", "multipart/mixed", new ZContentType(mmp.getBodyPart(0).getContentType()).getBaseType());
testMultipartWithoutBoundary((ZMimeMultipart) mmp.getBodyPart(0).getContent());
}
use of com.zimbra.common.zmime.ZMimeUtility.ByteBuilder in project zm-mailbox by Zimbra.
the class ZMimeParserTest method recursion.
@Test
public void recursion() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
String boundary = BOUNDARY1;
bb.append("From: <foo@example.com\r\n");
bb.append("Subject: sample\r\n");
bb.append("Content-Type: multipart/mixed; boundary=").append(boundary).append("\r\n");
bb.append("Content-Type: text/plain\r\n");
bb.append("\r\n");
bb.append("foo! bar! loud noises\r\n\r\n");
bb.append("--").append(boundary).append("\r\n");
addChildren(bb, 0);
bb.append("--").append(boundary).append("--\r\n");
try {
MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
Object content = mm.getContent();
Assert.assertTrue("content is multipart", content instanceof ZMimeMultipart);
ZMimeMultipart zmp = (ZMimeMultipart) content;
Assert.assertEquals("top count", 1, zmp.getCount());
traverseChildren((ZMimeMultipart) zmp.getBodyPart(0).getContent(), LC.mime_max_recursion.intValue() - 1);
} catch (ClassCastException e) {
Assert.fail("mishandled double Content-Type headers");
}
}
use of com.zimbra.common.zmime.ZMimeUtility.ByteBuilder in project zm-mailbox by Zimbra.
the class ZMimeParserTest method parse.
@Test
public void parse() throws Exception {
ByteBuilder bb = new ByteBuilder(CharsetUtil.UTF_8);
bb.append("Content-Type: text/plain\r\n");
bb.append("From: <foo@example.com\r\n");
bb.append("Subject: sample\r\n");
bb.append("Content-Type: multipart/alternative; boundary=").append(BOUNDARY1).append("\r\n");
bb.append("\r\n");
bb.append("--").append(BOUNDARY1).append("\r\n");
bb.append("Content-Type: text/plain\r\n");
bb.append("\r\n");
bb.append("foo! bar! loud noises\r\n\r\n");
bb.append("--").append(BOUNDARY1).append("--\r\n");
try {
MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
Assert.assertFalse("content isn't multipart", mm.getContent() instanceof MimeMultipart);
Assert.assertEquals("text/plain", "text/plain", new ZContentType(mm.getContentType()).getBaseType());
} catch (ClassCastException e) {
Assert.fail("mishandled double Content-Type headers");
}
}
use of com.zimbra.common.zmime.ZMimeUtility.ByteBuilder in project zm-mailbox by Zimbra.
the class ZCompoundHeader method reserialize.
protected void reserialize() {
ByteBuilder line = new ByteBuilder();
line.append(name.getBytes()).append(':').append(' ');
this.valueStart = line.length();
if (primaryValue != null) {
line.append(primaryValue);
}
int position = line.length();
for (Map.Entry<String, String> param : params.entrySet()) {
String key = param.getKey(), value = param.getValue();
if (value == null || key == null || key.isEmpty()) {
continue;
}
line.append(';');
position++;
boolean quoted = false, nonascii = false;
for (int i = 0, max = value.length(); i < max; i++) {
char c = value.charAt(i);
if (c >= 0x7F || c < 0x20 && c != '\t') {
nonascii = true;
break;
} else if (TSPECIALS[c]) {
quoted = true;
}
}
String paramCharset = nonascii ? CharsetUtil.checkCharset(value, this.charset) : "us-ascii";
ByteBuilder bb = new ByteBuilder();
if (!nonascii) {
if (quoted || value.isEmpty()) {
bb.append(param.getKey()).append('=').append('"');
for (int i = 0, max = value.length(); i < max; i++) {
char c = value.charAt(i);
if (c == '"' || c == '\\') {
bb.append('\\');
}
bb.append(c);
}
bb.append('"');
} else {
bb.append(param.getKey()).append('=').append(value);
}
} else if (use2231Encoding) {
try {
String encoded = new URLEncoder().encode(value, paramCharset);
bb.append(param.getKey()).append('*').append('=').append(paramCharset).append('\'').append('\'').append(encoded);
} catch (UnsupportedEncodingException e) {
}
} else {
String encoded = EncodedWord.encode(value, CharsetUtil.toCharset(paramCharset));
bb.append(param.getKey()).append('=').append('"').append(encoded).append('"');
}
if (position + bb.length() > LINE_WRAP_LENGTH) {
line.append('\r').append('\n').append(' ');
position = 1;
} else {
line.append(' ');
position++;
}
line.append(bb);
position += bb.length();
}
this.content = line.append("\r\n").toByteArray();
}
Aggregations