use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class ZMimeParserTest method repetition.
@Test
public void repetition() 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");
for (int i = 0; i < 100; i++) {
bb.append("--").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");
try {
MimeMessage mm = ZMimeParser.parse(getSession(), new SharedByteArrayInputStream(bb.toByteArray()));
Object content = mm.getContent();
Assert.assertTrue("content is multipart", content instanceof MimeMultipart);
MimeMultipart mp = (MimeMultipart) content;
Assert.assertEquals("count reduced??", 100, mp.getCount());
} catch (ClassCastException e) {
Assert.fail("mishandled double Content-Type headers");
}
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class CalendarItem method decomposeBlob.
/**
* Break up a multipart/digest blob into separate MimeMessages keyed by InvId header value.
* @param digestStream
* @return
* @throws MessagingException
*/
public static Map<Integer, MimeMessage> decomposeBlob(byte[] digestBlob) throws ServiceException {
Map<Integer, MimeMessage> map = new HashMap<Integer, MimeMessage>();
try {
InputStream bais = new SharedByteArrayInputStream(digestBlob);
FixedMimeMessage digestMm = new FixedMimeMessage(JMSession.getSession(), bais);
// It should be multipart/digest.
MimeMultipart mmp;
Object obj = digestMm.getContent();
if (obj instanceof MimeMultipart)
mmp = (MimeMultipart) obj;
else
throw ServiceException.FAILURE("Expected MimeMultipart, but got " + obj.getClass().getName() + ": " + obj.toString(), null);
int numParts = mmp.getCount();
for (int i = 0; i < numParts; i++) {
MimeBodyPart mbp = (MimeBodyPart) mmp.getBodyPart(i);
int invId = 0;
String[] hdrs = mbp.getHeader("invId");
if (hdrs != null && hdrs.length > 0) {
invId = Integer.parseInt(hdrs[0]);
MimeMessage mm;
Object objMbp = mbp.getContent();
if (objMbp instanceof MimeMessage)
mm = (MimeMessage) objMbp;
else
throw ServiceException.FAILURE("Expected MimeMessage, but got " + objMbp.getClass().getName() + ": " + objMbp.toString(), null);
map.put(invId, mm);
}
}
} catch (MessagingException e) {
throw ServiceException.FAILURE("Can't parse calendar item blob", e);
} catch (IOException e) {
throw ServiceException.FAILURE("Can't parse calendar item blob", e);
} catch (NumberFormatException e) {
throw ServiceException.FAILURE("Can't parse calendar item blob", e);
}
return map;
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class ParsedMessage method initialize.
private void initialize(byte[] rawData, Long receivedDate, boolean indexAttachments) throws ServiceException {
if (rawData == null || rawData.length == 0) {
throw ServiceException.FAILURE("Message data cannot be null or empty.", null);
}
sharedStream = new SharedByteArrayInputStream(rawData);
initialize(receivedDate, indexAttachments);
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class ParseMimeMessage method importMsgSoap.
public static MimeMessage importMsgSoap(Element msgElem) throws ServiceException {
/* msgElem == "<m>" E_MSG */
assert (msgElem.getName().equals(MailConstants.E_MSG));
Element contentElement = msgElem.getElement(MailConstants.E_CONTENT);
byte[] content;
try {
// Convert LF to CRLF because the XML parser normalizes element text to LF.
String text = StringUtil.lfToCrlf(contentElement.getText());
content = text.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
throw ServiceException.FAILURE("encoding error", e);
}
long maxSize = Provisioning.getInstance().getConfig().getLongAttr(Provisioning.A_zimbraMtaMaxMessageSize, DEFAULT_MAX_SIZE);
if ((maxSize != 0) && (content.length > maxSize)) {
throw ServiceException.INVALID_REQUEST("inline message too large", null);
}
InputStream messageStream = new SharedByteArrayInputStream(content);
try {
return new Mime.FixedMimeMessage(JMSession.getSession(), messageStream);
} catch (MessagingException me) {
throw ServiceException.FAILURE("MessagingExecption", me);
}
}
use of javax.mail.util.SharedByteArrayInputStream in project zm-mailbox by Zimbra.
the class TestContentTransferEncoding method testSimpleMimeMessage.
/*
* This tests the CTE header of a forwarded message being inferred from the existing message when the message is a simple MIME message
*/
@Ignore("disabled until bug 98015 is fixed")
@Test
public void testSimpleMimeMessage() throws Exception {
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
MimeMessage mimeMsg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(getSimpleMimeString().getBytes()));
ParsedMessage pm = new ParsedMessage(mimeMsg, true);
DeliveryOptions dopt = new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX);
Message msg = mbox.addMessage(null, pm, dopt, null);
MsgToSend msgToSend = new MsgToSend();
msgToSend.setOrigId(String.valueOf(msg.getId()));
msgToSend.setReplyType("w");
msgToSend.setSubject("Fwd: Simple Test");
msgToSend.setMimePart(MimePartInfo.createForContentTypeAndContent("text/plain", "simple test"));
SendMsgRequest req = new SendMsgRequest();
req.setMsg(msgToSend);
MimeMessage parsed = sendForwardedMessage(req, msg);
assertEquals("test", parsed.getHeader("Content-Transfer-Encoding")[0]);
}
Aggregations