2010-07-13 17:49:28 -04:00
|
|
|
|
|
|
|
package com.fsck.k9.mail;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2014-09-14 05:13:34 -04:00
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.List;
|
2010-07-13 17:49:28 -04:00
|
|
|
|
Recursively convert attachments of type message/rfc822 to 7bit if necessary.
The preceding commit resulted in attachments of type message/rfc822 being
sent with 8bit encoding even when the SMTP server did not support
8BITMIME. This commit assures that messages will be converted to 7bit
when necessary.
A new interface CompositeBody was created that extends Body, and classes
Message and Multipart were changed from implementing Body to
CompositeBody. Additional classes BinaryTempFileMessageBody and
LocalAttachmentMessageBody were created (by extending BinaryTempFileBody
and LocalAttachmentBody, respectively), and they too implement
CompositeBody.
A CompositeBody is a Body containing a composite-type that can contain
subparts that may require recursive processing when converting from 8bit
to 7bit. The Part to which a CompositeBody belongs is only permitted to
use 8bit or 7bit encoding for the CompositeBody.
Previously, a Message was created so that it was 7bit clean by default
(even though that meant base64 encoding all attachments, including
messages). Then, if the SMTP server supported 8BITMIME,
Message.setEncoding("8bit") was called so that bodies of type TextBody
would been transmitted using 8bit encoding rather than quoted-printable.
Now, messages are created with 8bit encoding by default. Then, if the
SMTP server does not support 8BITMIME, Message.setUsing7bitTransport is
called to recursively convert the message and its subparts to 7bit. The
method setUsing7bitTransport was added to the interfaces Part and
CompositeBody.
setEncoding no longer iterates over parts in Multipart. That task belongs
to setUsing7bitTransport, which may in turn call setEncoding on the parts.
MimeUtility.getEncodingforType was created as a helper function for
choosing a default encoding that should be used for a given MIME type when
an attachment is added to a message (either while composing or when
retrieving from LocalStore).
setEncoding was implemented in MimeBodyPart to assure that the encoding
set in the Part's headers was the same as set for the Part's Body. (The
method already existed in MimeMessage, which has similarities with
MimeBodyPart.)
MimeMessage.parse(InputStream in, boolean recurse) was implemented so that
the parser could be told to recursively process nested messages read from
the InputStream, thus giving access to all subparts at any level that may
need to be converted from 8bit to 7bit.
2013-09-02 23:49:28 -04:00
|
|
|
import org.apache.james.mime4j.util.MimeUtil;
|
|
|
|
|
2014-12-15 06:05:21 -05:00
|
|
|
import com.fsck.k9.mail.internet.CharsetSupport;
|
2010-07-13 17:49:28 -04:00
|
|
|
import com.fsck.k9.mail.internet.TextBody;
|
|
|
|
|
Recursively convert attachments of type message/rfc822 to 7bit if necessary.
The preceding commit resulted in attachments of type message/rfc822 being
sent with 8bit encoding even when the SMTP server did not support
8BITMIME. This commit assures that messages will be converted to 7bit
when necessary.
A new interface CompositeBody was created that extends Body, and classes
Message and Multipart were changed from implementing Body to
CompositeBody. Additional classes BinaryTempFileMessageBody and
LocalAttachmentMessageBody were created (by extending BinaryTempFileBody
and LocalAttachmentBody, respectively), and they too implement
CompositeBody.
A CompositeBody is a Body containing a composite-type that can contain
subparts that may require recursive processing when converting from 8bit
to 7bit. The Part to which a CompositeBody belongs is only permitted to
use 8bit or 7bit encoding for the CompositeBody.
Previously, a Message was created so that it was 7bit clean by default
(even though that meant base64 encoding all attachments, including
messages). Then, if the SMTP server supported 8BITMIME,
Message.setEncoding("8bit") was called so that bodies of type TextBody
would been transmitted using 8bit encoding rather than quoted-printable.
Now, messages are created with 8bit encoding by default. Then, if the
SMTP server does not support 8BITMIME, Message.setUsing7bitTransport is
called to recursively convert the message and its subparts to 7bit. The
method setUsing7bitTransport was added to the interfaces Part and
CompositeBody.
setEncoding no longer iterates over parts in Multipart. That task belongs
to setUsing7bitTransport, which may in turn call setEncoding on the parts.
MimeUtility.getEncodingforType was created as a helper function for
choosing a default encoding that should be used for a given MIME type when
an attachment is added to a message (either while composing or when
retrieving from LocalStore).
setEncoding was implemented in MimeBodyPart to assure that the encoding
set in the Part's headers was the same as set for the Part's Body. (The
method already existed in MimeMessage, which has similarities with
MimeBodyPart.)
MimeMessage.parse(InputStream in, boolean recurse) was implemented so that
the parser could be told to recursively process nested messages read from
the InputStream, thus giving access to all subparts at any level that may
need to be converted from 8bit to 7bit.
2013-09-02 23:49:28 -04:00
|
|
|
public abstract class Multipart implements CompositeBody {
|
2014-09-22 15:52:59 -04:00
|
|
|
private Part mParent;
|
2010-07-13 17:49:28 -04:00
|
|
|
|
2014-10-04 06:45:45 -04:00
|
|
|
private final List<BodyPart> mParts = new ArrayList<BodyPart>();
|
2010-07-13 17:49:28 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void addBodyPart(BodyPart part) {
|
2010-07-13 17:49:28 -04:00
|
|
|
mParts.add(part);
|
2011-01-03 13:07:44 -05:00
|
|
|
part.setParent(this);
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public BodyPart getBodyPart(int index) {
|
2010-07-13 17:49:28 -04:00
|
|
|
return mParts.get(index);
|
|
|
|
}
|
|
|
|
|
2014-09-14 05:13:34 -04:00
|
|
|
public List<BodyPart> getBodyParts() {
|
|
|
|
return Collections.unmodifiableList(mParts);
|
|
|
|
}
|
|
|
|
|
2015-01-02 22:39:25 -05:00
|
|
|
public abstract String getContentType();
|
2010-07-13 17:49:28 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public int getCount() {
|
2010-07-13 17:49:28 -04:00
|
|
|
return mParts.size();
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Part getParent() {
|
2010-07-13 17:49:28 -04:00
|
|
|
return mParent;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setParent(Part parent) {
|
2010-07-13 17:49:28 -04:00
|
|
|
this.mParent = parent;
|
|
|
|
}
|
|
|
|
|
2014-09-14 05:13:34 -04:00
|
|
|
@Override
|
Recursively convert attachments of type message/rfc822 to 7bit if necessary.
The preceding commit resulted in attachments of type message/rfc822 being
sent with 8bit encoding even when the SMTP server did not support
8BITMIME. This commit assures that messages will be converted to 7bit
when necessary.
A new interface CompositeBody was created that extends Body, and classes
Message and Multipart were changed from implementing Body to
CompositeBody. Additional classes BinaryTempFileMessageBody and
LocalAttachmentMessageBody were created (by extending BinaryTempFileBody
and LocalAttachmentBody, respectively), and they too implement
CompositeBody.
A CompositeBody is a Body containing a composite-type that can contain
subparts that may require recursive processing when converting from 8bit
to 7bit. The Part to which a CompositeBody belongs is only permitted to
use 8bit or 7bit encoding for the CompositeBody.
Previously, a Message was created so that it was 7bit clean by default
(even though that meant base64 encoding all attachments, including
messages). Then, if the SMTP server supported 8BITMIME,
Message.setEncoding("8bit") was called so that bodies of type TextBody
would been transmitted using 8bit encoding rather than quoted-printable.
Now, messages are created with 8bit encoding by default. Then, if the
SMTP server does not support 8BITMIME, Message.setUsing7bitTransport is
called to recursively convert the message and its subparts to 7bit. The
method setUsing7bitTransport was added to the interfaces Part and
CompositeBody.
setEncoding no longer iterates over parts in Multipart. That task belongs
to setUsing7bitTransport, which may in turn call setEncoding on the parts.
MimeUtility.getEncodingforType was created as a helper function for
choosing a default encoding that should be used for a given MIME type when
an attachment is added to a message (either while composing or when
retrieving from LocalStore).
setEncoding was implemented in MimeBodyPart to assure that the encoding
set in the Part's headers was the same as set for the Part's Body. (The
method already existed in MimeMessage, which has similarities with
MimeBodyPart.)
MimeMessage.parse(InputStream in, boolean recurse) was implemented so that
the parser could be told to recursively process nested messages read from
the InputStream, thus giving access to all subparts at any level that may
need to be converted from 8bit to 7bit.
2013-09-02 23:49:28 -04:00
|
|
|
public void setEncoding(String encoding) throws MessagingException {
|
|
|
|
if (!MimeUtil.ENC_7BIT.equalsIgnoreCase(encoding)
|
|
|
|
&& !MimeUtil.ENC_8BIT.equalsIgnoreCase(encoding)) {
|
|
|
|
throw new MessagingException(
|
|
|
|
"Incompatible content-transfer-encoding applied to a CompositeBody");
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|
|
|
|
|
Recursively convert attachments of type message/rfc822 to 7bit if necessary.
The preceding commit resulted in attachments of type message/rfc822 being
sent with 8bit encoding even when the SMTP server did not support
8BITMIME. This commit assures that messages will be converted to 7bit
when necessary.
A new interface CompositeBody was created that extends Body, and classes
Message and Multipart were changed from implementing Body to
CompositeBody. Additional classes BinaryTempFileMessageBody and
LocalAttachmentMessageBody were created (by extending BinaryTempFileBody
and LocalAttachmentBody, respectively), and they too implement
CompositeBody.
A CompositeBody is a Body containing a composite-type that can contain
subparts that may require recursive processing when converting from 8bit
to 7bit. The Part to which a CompositeBody belongs is only permitted to
use 8bit or 7bit encoding for the CompositeBody.
Previously, a Message was created so that it was 7bit clean by default
(even though that meant base64 encoding all attachments, including
messages). Then, if the SMTP server supported 8BITMIME,
Message.setEncoding("8bit") was called so that bodies of type TextBody
would been transmitted using 8bit encoding rather than quoted-printable.
Now, messages are created with 8bit encoding by default. Then, if the
SMTP server does not support 8BITMIME, Message.setUsing7bitTransport is
called to recursively convert the message and its subparts to 7bit. The
method setUsing7bitTransport was added to the interfaces Part and
CompositeBody.
setEncoding no longer iterates over parts in Multipart. That task belongs
to setUsing7bitTransport, which may in turn call setEncoding on the parts.
MimeUtility.getEncodingforType was created as a helper function for
choosing a default encoding that should be used for a given MIME type when
an attachment is added to a message (either while composing or when
retrieving from LocalStore).
setEncoding was implemented in MimeBodyPart to assure that the encoding
set in the Part's headers was the same as set for the Part's Body. (The
method already existed in MimeMessage, which has similarities with
MimeBodyPart.)
MimeMessage.parse(InputStream in, boolean recurse) was implemented so that
the parser could be told to recursively process nested messages read from
the InputStream, thus giving access to all subparts at any level that may
need to be converted from 8bit to 7bit.
2013-09-02 23:49:28 -04:00
|
|
|
/* Nothing else to do. Each subpart has its own separate encoding */
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|
2011-01-04 08:25:59 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setCharset(String charset) throws MessagingException {
|
2011-01-04 08:25:59 -05:00
|
|
|
if (mParts.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
BodyPart part = mParts.get(0);
|
|
|
|
Body body = part.getBody();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (body instanceof TextBody) {
|
2014-12-15 06:05:21 -05:00
|
|
|
CharsetSupport.setCharset(charset, part);
|
2011-01-04 08:25:59 -05:00
|
|
|
((TextBody)body).setCharset(charset);
|
|
|
|
}
|
|
|
|
}
|
2010-07-13 17:49:28 -04:00
|
|
|
}
|