private boolean hasEmptyBody(Part textPart) { return textPart.getBody() == null; } }
private boolean hasEmptyBody(Part textPart) { return textPart.getBody() == null; }
@Nullable private static MimeBodyPart getMultipartSignedContentPartIfAvailable(Part part) { MimeBodyPart replacementPart = NO_REPLACEMENT_PART; Body body = part.getBody(); if (body instanceof MimeMultipart) { MimeMultipart multipart = ((MimeMultipart) part.getBody()); if (multipart.getCount() >= 1) { replacementPart = (MimeBodyPart) multipart.getBodyPart(0); } } return replacementPart; }
public AttachmentViewInfo extractAttachmentInfoForDatabase(Part part) throws MessagingException { boolean isContentAvailable = part.getBody() != null; return extractAttachmentInfo(part, Uri.EMPTY, AttachmentViewInfo.UNKNOWN_SIZE, isContentAvailable); }
public static boolean isCompletePartAvailable(Part part) { Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(part); while (!partsToCheck.isEmpty()) { Part currentPart = partsToCheck.pop(); Body body = currentPart.getBody(); boolean isBodyMissing = body == null; if (isBodyMissing) { return false; } if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (BodyPart bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } } return true; }
@Override public Long getSizeForProgress() { Part part = currentCryptoPart.part; CryptoPartType cryptoPartType = currentCryptoPart.type; Body body; if (cryptoPartType == CryptoPartType.PGP_ENCRYPTED) { Multipart multipartEncryptedMultipart = (Multipart) part.getBody(); BodyPart encryptionPayloadPart = multipartEncryptedMultipart.getBodyPart(1); body = encryptionPayloadPart.getBody(); } else if (cryptoPartType == CryptoPartType.PGP_INLINE) { body = part.getBody(); } else { throw new IllegalStateException("part to stream must be encrypted or inline!"); } if (body instanceof SizeAware) { return ((SizeAware) body).getSize(); } return null; }
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); } }
private File writeBodyToDiskIfNecessary(Part part) throws MessagingException, IOException { Body body = part.getBody(); if (body instanceof BinaryTempFileBody) { return ((BinaryTempFileBody) body).getFile(); } else { return writeBodyToDisk(body); } }
public static Part findFirstPartByMimeType(Part part, String mimeType) { if (part.getBody() instanceof Multipart) { Multipart multipart = (Multipart)part.getBody(); for (BodyPart bodyPart : multipart.getBodyParts()) { Part ret = MimeUtility.findFirstPartByMimeType(bodyPart, mimeType); if (ret != null) { return ret; } } } else if (isSameMimeType(part.getMimeType(), mimeType)) { return part; } return null; }
public static boolean hasMissingParts(Part part) { Body body = part.getBody(); if (body == null) { return true; } if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part subPart : multipart.getBodyParts()) { if (hasMissingParts(subPart)) { return true; } } } return false; }
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 addChildrenToStack(Stack<PartContainer> stack, Part part, long parentMessageId) { Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (int i = multipart.getCount() - 1; i >= 0; i--) { BodyPart childPart = multipart.getBodyPart(i); stack.push(new PartContainer(parentMessageId, childPart)); } } else if (body instanceof Message) { Message innerMessage = (Message) body; stack.push(new PartContainer(parentMessageId, innerMessage)); } }
static Part getPart(Part searchRootPart, int... indexes) { Part part = searchRootPart; for (int index : indexes) { part = ((Multipart) part.getBody()).getBodyPart(index); } return part; } }
@Nullable public Part findFirstTextPart(@NonNull Part part) { String mimeType = part.getMimeType(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; if (isSameMimeType(mimeType, "multipart/alternative")) { return findTextPartInMultipartAlternative(multipart); } else { return findTextPartInMultipart(multipart); } } else if (isSameMimeType(mimeType, "text/plain") || isSameMimeType(mimeType, "text/html")) { return part; } return null; }
private static Part findPrimaryPartInAlternative(Part part) { Body body = part.getBody(); if (part.isMimeType("multipart/alternative") && body instanceof Multipart) { Multipart multipart = (Multipart) body; if (multipart.getCount() == 0) { return null; } BodyPart firstBodyPart = multipart.getBodyPart(0); if (isPartPgpInlineEncryptedOrSigned(firstBodyPart)) { return firstBodyPart; } } return null; }
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); }
private boolean containsPartWithMimeType(Part part, String... wantedMimeTypes) { String mimeType = part.getMimeType(); if (isMimeTypeAnyOf(mimeType, wantedMimeTypes)) { return true; } Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (BodyPart bodyPart : multipart.getBodyParts()) { if (containsPartWithMimeType(bodyPart, wantedMimeTypes)) { return true; } } } return false; }
@Override public void writeTo(OutputStream os) throws IOException { try { Multipart multipartSignedMultipart = (Multipart) signedPart.getBody(); BodyPart signatureBodyPart = multipartSignedMultipart.getBodyPart(0); Timber.d("signed data type: %s", signatureBodyPart.getMimeType()); signatureBodyPart.writeTo(os); } catch (MessagingException e) { Timber.e(e, "Exception while writing message to crypto provider"); } } };
private static boolean isPartMultipartSigned(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_SIGNED)) { return false; } if (! (part.getBody() instanceof MimeMultipart)) { return false; } MimeMultipart mimeMultipart = (MimeMultipart) part.getBody(); if (mimeMultipart.getCount() != 2) { return false; } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); // for partially downloaded messages the protocol parameter isn't yet available, so we'll just assume it's ok boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(0).getBody() == null; boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(1).getMimeType()); return dataUnavailable || protocolMatches; }
public static boolean isPartMultipartEncrypted(Part part) { if (!isSameMimeType(part.getMimeType(), MULTIPART_ENCRYPTED)) { return false; } if (! (part.getBody() instanceof MimeMultipart)) { return false; } MimeMultipart mimeMultipart = (MimeMultipart) part.getBody(); if (mimeMultipart.getCount() != 2) { return false; } String protocolParameter = MimeUtility.getHeaderParameter(part.getContentType(), PROTOCOL_PARAMETER); // for partially downloaded messages the protocol parameter isn't yet available, so we'll just assume it's ok boolean dataUnavailable = protocolParameter == null && mimeMultipart.getBodyPart(1).getBody() == null; boolean protocolMatches = isSameMimeType(protocolParameter, mimeMultipart.getBodyPart(0).getMimeType()); return dataUnavailable || protocolMatches; }