public byte[] asBytes(Message message) { try { return DefaultMessageWriter.asBytes(message); } catch (IOException e) { throw new RuntimeException(e); } }
private byte[] getBytes(Body body) throws IOException { DefaultMessageWriter messageWriter = new DefaultMessageWriter(); ByteArrayOutputStream out = new ByteArrayOutputStream(); messageWriter.writeBody(body, out); return out.toByteArray(); }
private InputStream textHeaders(MailboxMessage message) throws MimeIOException, IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); new DefaultMessageWriter() .writeHeader(buildTextHeaders(message), out); return new ByteArrayInputStream(out.toByteArray()); }
public static byte[] asBytes(Message message) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DefaultMessageWriter writer = new DefaultMessageWriter(); writer.writeMessage(message, buffer); return buffer.toByteArray(); }
/** * Write the specified <code>Entity</code> to the specified * <code>OutputStream</code>. * * @param entity * the <code>Entity</code> to write. * @param out * the OutputStream to write to. * @throws IOException * if an I/O error occurs. */ public void writeEntity(Entity entity, OutputStream out) throws IOException { final Header header = entity.getHeader(); if (header == null) throw new IllegalArgumentException("Missing header"); writeHeader(header, out); final Body body = entity.getBody(); if (body == null) throw new IllegalArgumentException("Missing body"); boolean binaryBody = body instanceof BinaryBody; OutputStream encOut = encodeStream(out, entity .getContentTransferEncoding(), binaryBody); writeBody(body, encOut); // close if wrapped (base64 or quoted-printable) if (encOut != out) encOut.close(); }
@Override public MessageWriter newMessageWriter() { return new DefaultMessageWriter(); }
ContentTypeField contentType = getContentType(multipart); ByteSequence boundary = getBoundary(contentType); writeBytes(preamble, out); out.write(CRLF); writeBytes(boundary, out); out.write(CRLF); writeEntity(bodyPart, out); out.write(CRLF); writeBytes(boundary, out); out.write(DASHES); out.write(CRLF); if (epilogue != null) { writeBytes(epilogue, out);
public static void main(String[] args) throws Exception { // 1) start with an empty message Message message = MessageBuilder.create() // 2) set header fields // Date and From are required fields // Message-ID should be present .setFrom("John Doe <jdoe@machine.example>") .setTo("Mary Smith <mary@example.net>") .setSubject("Saying Hello") .setDate(new Date()) .generateMessageId(InetAddress.getLocalHost().getCanonicalHostName()) .setBody("This is a message just to say hello.\r\nSo, \"Hello\".", Charsets.ISO_8859_1) .build(); try { // 4) print message to standard output MessageWriter writer = new DefaultMessageWriter(); writer.writeMessage(message, System.out); } finally { // 5) message is no longer needed and should be disposed of message.dispose(); } } }
public byte[] asBytes(Message message) throws MailboxException { try { return DefaultMessageWriter.asBytes(message); } catch (IOException e) { throw new MailboxException("Could not write message as bytes", e); } }
try { MessageWriter writer = new DefaultMessageWriter(); writer.writeMessage(message, System.out); } finally {
public AppendCommand build(Message message) throws IOException { return build(DefaultMessageWriter.asBytes(message)); }
MessageWriter writer = new DefaultMessageWriter();
@Override public int getConstantMessageSize() { try { return DefaultMessageWriter.asBytes(message).length; } catch (IOException e) { throw new RuntimeException(e); } } }
private String asString(Message message) throws Exception { return new String(DefaultMessageWriter.asBytes(message), StandardCharsets.UTF_8); } }
@Test public void getMessageListShouldExcludeMessagesWhenAttachmentFilterDoesntMatch() throws Exception { mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, ALICE, "mailbox"); byte[] attachmentContent = ClassLoaderUtils.getSystemResourceAsByteArray("eml/attachment.pdf"); Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(BodyPartBuilder.create() .setBody(attachmentContent, "application/pdf") .setContentDisposition("attachment")) .addBodyPart(BodyPartBuilder.create() .setBody("The message has a PDF attachment.", "plain", StandardCharsets.UTF_8)) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); mailboxProbe.appendMessage(ALICE, MailboxPath.forUser(ALICE, "mailbox"), new ByteArrayInputStream(DefaultMessageWriter.asBytes(message)), new Date(), false, new Flags()); await(); given() .header("Authorization", aliceAccessToken.serialize()) .body("[[\"getMessageList\", {\"filter\":{\"attachments\":\"no apple inside\"}}, \"#0\"]]") .when() .post("/jmap") .then() .statusCode(200) .body(ARGUMENTS + ".messageIds", empty()); }
@Test public void getMessageListShouldIncludeMessagesWhenAttachmentFilterMatches() throws Exception { mailboxProbe.createMailbox(MailboxConstants.USER_NAMESPACE, ALICE, "mailbox"); byte[] attachmentContent = ClassLoaderUtils.getSystemResourceAsByteArray("eml/attachment.pdf"); Multipart multipart = MultipartBuilder.create("mixed") .addBodyPart(BodyPartBuilder.create() .setBody(attachmentContent, "application/pdf") .setContentDisposition("attachment")) .addBodyPart(BodyPartBuilder.create() .setBody("The message has a PDF attachment.", "plain", StandardCharsets.UTF_8)) .build(); Message message = Message.Builder.of() .setBody(multipart) .build(); ComposedMessageId composedMessageId = mailboxProbe.appendMessage(ALICE, MailboxPath.forUser(ALICE, "mailbox"), new ByteArrayInputStream(DefaultMessageWriter.asBytes(message)), new Date(), false, new Flags()); await(); given() .header("Authorization", aliceAccessToken.serialize()) .body("[[\"getMessageList\", {\"filter\":{\"attachments\":\"beautiful banana\"}}, \"#0\"]]") .when() .post("/jmap") .then() .statusCode(200) .body(ARGUMENTS + ".messageIds", contains(composedMessageId.getMessageId().serialize())); }