更改multipart/XXX内容类型而不更改基础部分 [英] Altering a multipart/XXX content type without altering the underlying parts

查看:96
本文介绍了更改multipart/XXX内容类型而不更改基础部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MimeMessage实例,其中包含加密的部分.

I have an instance of MimeMessage which contains encrypted Parts.

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

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?

推荐答案

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

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

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

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

有关信息,我们确实使用了MimeMessageWrapper,它只是一个包装类,可以使副本的消息ID保持不变(或不保持不变).一种可能的实现方式是 Apache James 项目.

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.

另一个重要的观点是,最终在该解决方案中,底层部分被更改,但边界也进行了修改(不再说EncryptedXXXX),这对于我们的案例而言更加干净.

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.

这篇关于更改multipart/XXX内容类型而不更改基础部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆