Domanda

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?

È stato utile?

Soluzione 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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top