private byte[] getBodyBytes(Body body) throws IOException, MessagingException { ByteArrayOutputStream output = new ByteArrayOutputStream(); body.writeTo(output); return output.toByteArray(); }
@Override public void writeTo(OutputStream os) throws IOException { try { if (writeBodyContentOnly) { Body body = bodyPart.getBody(); InputStream inputStream = body.getInputStream(); IOUtils.copy(inputStream, os); } else { bodyPart.writeTo(os); } } catch (MessagingException e) { throw new IOException(e); } } };
@Override public void setEncoding(String encoding) throws MessagingException { if (mBody != null) { mBody.setEncoding(encoding); } setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); }
private File writeBodyToDisk(Body body) throws IOException, MessagingException { File file = File.createTempFile("body", null, BinaryTempFileBody.getTempDirectory()); OutputStream out = new FileOutputStream(file); try { body.writeTo(out); } finally { out.close(); } return file; }
inputStream = body.getInputStream();
@Override public void setEncoding(String encoding) throws MessagingException { if (mBody != null) { mBody.setEncoding(encoding); } setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); }
/** * Write the MimeMessage out in MIME format. */ @Override public void writeTo(OutputStream out) throws IOException, MessagingException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); mHeader.writeTo(out); writer.write("\r\n"); writer.flush(); if (mBody != null) { mBody.writeTo(out); } }
@Test public void getTextFromPart_withExceptionThrownGettingInputStream_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); Body body = mock(Body.class); when(body.getInputStream()).thenThrow(new MessagingException("Test")); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
public static void setEncoding(Part part, String encoding) throws MessagingException { Body body = part.getBody(); if (body != null) { body.setEncoding(encoding); } part.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, encoding); } }
@Override public void writeTo(OutputStream out) throws IOException, MessagingException { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024); mHeader.writeTo(out); writer.write("\r\n"); writer.flush(); if (mBody != null) { mBody.writeTo(out); } }
private void mimeBuildInlineMessage(@NonNull Body inlineBodyPart) throws MessagingException { if (!cryptoStatus.isPgpInlineModeEnabled()) { throw new IllegalStateException("call to mimeBuildInlineMessage while pgp/inline isn't enabled!"); } boolean isCleartextSignature = !cryptoStatus.isEncryptionEnabled(); if (isCleartextSignature) { inlineBodyPart.setEncoding(MimeUtil.ENC_QUOTED_PRINTABLE); } MimeMessageHelper.setBody(currentProcessedMimeMessage, inlineBodyPart); }
public static byte[] getSignatureData(Part part) throws IOException, MessagingException { if (isPartMultipartSigned(part)) { Body body = part.getBody(); if (body instanceof Multipart) { Multipart multi = (Multipart) body; BodyPart signatureBody = multi.getBodyPart(1); ByteArrayOutputStream bos = new ByteArrayOutputStream(); signatureBody.getBody().writeTo(bos); return bos.toByteArray(); } } return null; }
private void processEncryptedMessageAndCaptureMocks(Message message, Body encryptedBody, OutputStream outputStream) throws Exception { messageCryptoHelper.asyncStartOrResumeProcessingMessage(message, messageCryptoCallback, null, false); ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); ArgumentCaptor<OpenPgpDataSource> dataSourceCaptor = ArgumentCaptor.forClass(OpenPgpDataSource.class); ArgumentCaptor<IOpenPgpSinkResultCallback> callbackCaptor = ArgumentCaptor.forClass(IOpenPgpSinkResultCallback.class); verify(openPgpApi).executeApiAsync(intentCaptor.capture(), dataSourceCaptor.capture(), any(OpenPgpDataSink.class), callbackCaptor.capture()); capturedApiIntent = intentCaptor.getValue(); capturedCallback = callbackCaptor.getValue(); OpenPgpDataSource dataSource = dataSourceCaptor.getValue(); dataSource.writeTo(outputStream); verify(encryptedBody).writeTo(outputStream); }
@Override @WorkerThread public void writeTo(OutputStream os) throws IOException { try { Part part = currentCryptoPart.part; CryptoPartType cryptoPartType = currentCryptoPart.type; if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) { Multipart multipartEncryptedMultipart = (Multipart) part.getBody(); BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1); Body encryptionPayloadBody = encryptionPayloadPart.getBody(); encryptionPayloadBody.writeTo(os); } else if (cryptoPartType == CryptoPartType.PGP_INLINE) { String text = MessageExtractor.getTextFromPart(part); os.write(text.getBytes()); } else { throw new IllegalStateException("part to stream must be encrypted or inline!"); } } catch (MessagingException e) { Timber.e(e, "MessagingException while writing message to crypto provider"); } } };
private void writeRawBodyToStream(Cursor cursor, SQLiteDatabase db, OutputStream outputStream) throws IOException, MessagingException { long partId = cursor.getLong(ATTACH_PART_ID_INDEX); String rootPart = cursor.getString(ATTACH_ROOT_INDEX); LocalMessage message = loadLocalMessageByRootPartId(db, rootPart); if (message == null) { throw new MessagingException("Unable to find message for attachment!"); } Part part = findPartById(message, partId); if (part == null) { throw new MessagingException("Unable to find attachment part in associated message (db integrity error?)"); } Body body = part.getBody(); if (body == null) { throw new MessagingException("Attachment part isn't available!"); } body.writeTo(outputStream); }
@Test public void fetch_withBodyProfile_setsContentOfMessage() throws MessagingException, IOException { InputStream messageInputStream = new ByteArrayInputStream(( "From: <adam@example.org>\r\n" + "To: <eva@example.org>\r\n" + "Subject: Testmail\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: text/plain\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + "this is some test text.").getBytes()); folder.open(Folder.OPEN_MODE_RW); List<Pop3Message> messageList = setupMessageFromServer(); FetchProfile fetchProfile = new FetchProfile(); fetchProfile.add(Item.BODY); when(mockConnection.readLine()).thenReturn("1 100").thenReturn("."); when(mockConnection.getInputStream()).thenReturn(messageInputStream); folder.fetch(messageList, fetchProfile, mockListener); ByteArrayOutputStream bodyData = new ByteArrayOutputStream(); messageList.get(0).getBody().writeTo(bodyData); assertEquals("this is some test text.", new String(bodyData.toByteArray(), "UTF-8")); }
@Test public void fetchPart_withTextSection_shouldProcessImapResponses() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); ImapMessage message = createImapMessage("1"); Part part = createPlainTextPart("1.1"); setupSingleFetchResponseToCallback(); folder.fetchPart(message, part, null, new DefaultBodyFactory()); ArgumentCaptor<Body> bodyArgumentCaptor = ArgumentCaptor.forClass(Body.class); verify(part).setBody(bodyArgumentCaptor.capture()); Body body = bodyArgumentCaptor.getValue(); Buffer buffer = new Buffer(); body.writeTo(buffer.outputStream()); assertEquals("text", buffer.readUtf8()); }