Altering a multipart/XXX content type without altering the underlying parts

大憨熊 提交于 2020-01-04 08:21:09

问题


I have an instance of MimeMessage which contains encrypted Parts.

The original content type is "multipart/encrypted; protocol="application/pgp-encrypted"; boundary="EncryptedBoundary12312345654654"

After decryption of each parts, I want the multipart header to change as:

"multipart/mixed; boundary="EncryptedBoundary12312345654654"

The boundary number is obviously dynamic, then I cannot just make

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

Do you have an idea about the best practice for that case?


回答1:


I don't understand what you mean when you say you "want the multipart header to change". Are you trying to decrypt the message "in place"? That's probably not going to work well.

You can create a new message using the decrypted contents of the original message. If it's important to you that things like the "boundary" value remain the same, you'll probably need to subclass MimeMultipart and use the ContentType class to construct a new content type value.




回答2:


I answer to publish the code of my solution:

// 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();

In fact it seems there is no another way to alter the original message but create a copy was enough for me. The important point was just to create a new MimeMultipart object which will contains the decrypted parts and then given as the content to the MimeMessage(Wrapper). This will generate the new content type values "automagically".

For information, we did use a MimeMessageWrapper which is just a wrapper class that enable to keep the message ID unchanged (or not) to the copies. One possible implementation is on the Apache James project.

Another important point, finally in that solution, the underlying parts were changed but the boundary was adapted as well (it is not said EncryptedXXXX anymore) which is even cleaner for our case.



来源:https://stackoverflow.com/questions/9081739/altering-a-multipart-xxx-content-type-without-altering-the-underlying-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!