2

我有一个包含加密部分的 MimeMessage 实例。

原始内容类型为“multipart/encrypted;protocol="application/pgp-encrypted";boundary="EncryptedBoundary12312345654654"

解密每个部分后,我希望多部分标头更改为:

"multipart/mixed; boundary="EncryptedBoundary12312345654654"

边界数显然是动态的,那我不能只做

mime.setHeader("Content-Type", "multipart/mixed;" );

您对该案例的最佳实践有什么想法吗?

4

2 回答 2

2

当您说“希望多部分标题更改”时,我不明白您的意思。您是否尝试“就地”解密消息?这可能不会很好地工作。

您可以使用原始消息的解密内容创建新消息。如果“边界”值保持不变对您很重要,您可能需要继承 MimeMultipart 并使用 ContentType 类来构造新的内容类型值。

于 2012-01-31T18:26:49.950 回答
1

我回答发布我的解决方案的代码:

// source is the encrypted MimeMessage 
// MimeMessageWrapper is a wrapper which can copy a messgae but keep the message ID unchanged
boolean keepMessageId = true;
MimeMessageWrapper newMime = new MimeMessageWrapper(source, keepMessageId); 

MimeMultipart mmp = new MimeMultipart("mixed");

List<MimePart> parts = MimeMultipartUtils.findPartsByMimeType(mime, "*");

for (MimePart part : parts) {

    // Do some part processing
    // Decrypt Adn verify individual parts
    // End of processing 

    ContentType type = new ContentType(part.getContentType());
    String encoding = part.getEncoding();
    String name = type.getParameter("name");

    part.setContent(new String(decPart.toByteArray()), type.toString());

    // Add the part to the brand new MimeMultipart
    mmp.addBodyPart((BodyPart) part);

}

// Set the original copy Message with the new modified content (decrypted parts)
mime.setContent(mmp);
mime.saveChanges();

事实上,似乎没有其他方法可以更改原始消息,但创建副本对我来说就足够了。重要的一点是创建一个新的 MimeMultipart 对象,该对象将包含解密的部分,然后作为内容提供给 MimeMessage(Wrapper)。这将“自动”生成新的内容类型值。

作为信息,我们确实使用了 MimeMessageWrapper,它只是一个包装类,可以使消息 ID 保持不变(或不变)到副本。一种可能的实现是在Apache James项目上。

另一个重要的一点,最后在那个解决方案中,底层部分被改变了,但边界也被调整了(不再说 EncryptedXXXX),这对我们的案例来说更加清晰。

于 2012-03-13T21:39:27.207 回答