2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.mail.internet;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.mail.Body;
|
|
|
|
import com.fsck.k9.mail.BodyPart;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO this is a close approximation of Message, need to update along with
|
|
|
|
* Message.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public class MimeBodyPart extends BodyPart {
|
2008-11-01 17:32:06 -04:00
|
|
|
protected MimeHeader mHeader = new MimeHeader();
|
|
|
|
protected Body mBody;
|
|
|
|
protected int mSize;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MimeBodyPart() throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MimeBodyPart(Body body) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
this(body, null);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MimeBodyPart(Body body, String mimeType) throws MessagingException {
|
|
|
|
if (mimeType != null) {
|
2009-09-26 18:47:13 -04:00
|
|
|
addHeader(MimeHeader.HEADER_CONTENT_TYPE, mimeType);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
setBody(body);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
protected String getFirstHeader(String name) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mHeader.getFirstHeader(name);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void addHeader(String name, String value) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.addHeader(name, value);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setHeader(String name, String value) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.setHeader(name, value);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String[] getHeader(String name) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mHeader.getHeader(name);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void removeHeader(String name) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.removeHeader(name);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Body getBody() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mBody;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setBody(Body body) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mBody = body;
|
2011-02-06 17:09:48 -05:00
|
|
|
if (body instanceof com.fsck.k9.mail.Multipart) {
|
2009-12-14 21:50:53 -05:00
|
|
|
com.fsck.k9.mail.Multipart multipart = ((com.fsck.k9.mail.Multipart)body);
|
2008-11-01 17:32:06 -04:00
|
|
|
multipart.setParent(this);
|
|
|
|
setHeader(MimeHeader.HEADER_CONTENT_TYPE, multipart.getContentType());
|
2011-02-06 17:09:48 -05:00
|
|
|
} else if (body instanceof TextBody) {
|
2008-11-01 17:32:06 -04:00
|
|
|
String contentType = String.format("%s;\n charset=utf-8", getMimeType());
|
|
|
|
String name = MimeUtility.getHeaderParameter(getContentType(), "name");
|
2011-02-06 17:09:48 -05:00
|
|
|
if (name != null) {
|
2008-11-01 17:32:06 -04:00
|
|
|
contentType += String.format(";\n name=\"%s\"", name);
|
|
|
|
}
|
|
|
|
setHeader(MimeHeader.HEADER_CONTENT_TYPE, contentType);
|
2010-02-05 09:26:09 -05:00
|
|
|
setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "quoted-printable");
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getContentType() throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
|
2011-06-16 22:13:32 -04:00
|
|
|
return (contentType == null) ? "text/plain" : contentType;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getDisposition() throws MessagingException {
|
2011-06-16 22:13:32 -04:00
|
|
|
return getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getContentId() throws MessagingException {
|
2010-07-11 09:44:16 -04:00
|
|
|
String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (contentId == null) {
|
2010-07-11 09:44:16 -04:00
|
|
|
return null;
|
|
|
|
}
|
2011-06-16 22:13:32 -04:00
|
|
|
|
|
|
|
int first = contentId.indexOf('<');
|
|
|
|
int last = contentId.lastIndexOf('>');
|
|
|
|
|
|
|
|
return (first != -1 && last != -1) ?
|
|
|
|
contentId.substring(first + 1, last) :
|
|
|
|
contentId;
|
2010-07-11 09:44:16 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getMimeType() throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
return MimeUtility.getHeaderParameter(getContentType(), null);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean isMimeType(String mimeType) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
return getMimeType().equals(mimeType);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public int getSize() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the MimeMessage out in MIME format.
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void writeTo(OutputStream out) throws IOException, MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
|
|
|
|
mHeader.writeTo(out);
|
|
|
|
writer.write("\r\n");
|
|
|
|
writer.flush();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (mBody != null) {
|
2008-11-01 17:32:06 -04:00
|
|
|
mBody.writeTo(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|