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.
|
|
|
|
*/
|
2009-11-24 19:40:29 -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
|
|
|
|
|
|
|
public MimeBodyPart() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this(null);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public MimeBodyPart(Body body) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this(body, null);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -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);
|
|
|
|
}
|
|
|
|
|
2010-12-01 01:32:29 -05:00
|
|
|
protected String getFirstHeader(String name)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mHeader.getFirstHeader(name);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void addHeader(String name, String value) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.addHeader(name, value);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setHeader(String name, String value) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.setHeader(name, value);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public String[] getHeader(String name) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mHeader.getHeader(name);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void removeHeader(String name) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mHeader.removeHeader(name);
|
|
|
|
}
|
|
|
|
|
2010-12-01 01:32:29 -05:00
|
|
|
public Body getBody()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mBody;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public void setBody(Body body) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mBody = body;
|
2009-12-14 21:50:53 -05:00
|
|
|
if (body instanceof com.fsck.k9.mail.Multipart)
|
2009-11-24 19:40:29 -05:00
|
|
|
{
|
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());
|
|
|
|
}
|
2009-11-24 19:40:29 -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");
|
2009-11-24 19:40:29 -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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getContentType() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
String contentType = getFirstHeader(MimeHeader.HEADER_CONTENT_TYPE);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (contentType == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return "text/plain";
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-03-25 10:39:07 -04:00
|
|
|
return contentType;
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getDisposition() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
String contentDisposition = getFirstHeader(MimeHeader.HEADER_CONTENT_DISPOSITION);
|
2009-11-24 19:40:29 -05:00
|
|
|
if (contentDisposition == null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
2009-11-24 19:40:29 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return contentDisposition;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-11 09:44:16 -04:00
|
|
|
public String getContentId() throws MessagingException
|
|
|
|
{
|
|
|
|
String contentId = getFirstHeader(MimeHeader.HEADER_CONTENT_ID);
|
|
|
|
if (contentId == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-13 17:16:56 -04:00
|
|
|
int first = contentId.indexOf('<');
|
|
|
|
int last = contentId.lastIndexOf('>');
|
|
|
|
if (first != -1 && last != -1)
|
|
|
|
{
|
|
|
|
return contentId.substring(first+1, last);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return contentId;
|
|
|
|
}
|
2010-07-11 09:44:16 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public String getMimeType() throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return MimeUtility.getHeaderParameter(getContentType(), null);
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
public boolean isMimeType(String mimeType) throws MessagingException
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return getMimeType().equals(mimeType);
|
|
|
|
}
|
|
|
|
|
2010-12-01 01:32:29 -05:00
|
|
|
public int getSize()
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write the MimeMessage out in MIME format.
|
|
|
|
*/
|
2009-11-24 19:40:29 -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();
|
2009-11-24 19:40:29 -05:00
|
|
|
if (mBody != null)
|
|
|
|
{
|
2008-11-01 17:32:06 -04:00
|
|
|
mBody.writeTo(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|