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
|
|
|
|
2010-05-19 14:17:06 -04:00
|
|
|
import com.fsck.k9.helper.Utility;
|
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;
|
2010-12-23 04:58:13 -05:00
|
|
|
import java.nio.charset.Charset;
|
2010-05-21 11:34:29 -04:00
|
|
|
import java.util.*;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public class MimeHeader {
|
2010-08-07 11:10:07 -04:00
|
|
|
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
/**
|
|
|
|
* Application specific header that contains Store specific information about an attachment.
|
|
|
|
* In IMAP this contains the IMAP BODYSTRUCTURE part id so that the ImapStore can later
|
|
|
|
* retrieve the attachment at will from the server.
|
|
|
|
* The info is recorded from this header on LocalStore.appendMessages and is put back
|
|
|
|
* into the MIME data by LocalStore.fetch.
|
|
|
|
*/
|
|
|
|
public static final String HEADER_ANDROID_ATTACHMENT_STORE_DATA = "X-Android-Attachment-StoreData";
|
|
|
|
|
|
|
|
public static final String HEADER_CONTENT_TYPE = "Content-Type";
|
|
|
|
public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
|
|
|
|
public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
|
2010-07-11 09:44:16 -04:00
|
|
|
public static final String HEADER_CONTENT_ID = "Content-ID";
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fields that should be omitted when writing the header using writeTo()
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
private static final String[] writeOmitFields = {
|
2008-11-01 17:32:06 -04:00
|
|
|
// HEADER_ANDROID_ATTACHMENT_DOWNLOADED,
|
|
|
|
// HEADER_ANDROID_ATTACHMENT_ID,
|
|
|
|
HEADER_ANDROID_ATTACHMENT_STORE_DATA
|
|
|
|
};
|
|
|
|
|
|
|
|
protected ArrayList<Field> mFields = new ArrayList<Field>();
|
2010-12-23 04:58:13 -05:00
|
|
|
private String mCharset = null;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void clear() {
|
2008-11-01 17:32:06 -04:00
|
|
|
mFields.clear();
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getFirstHeader(String name) {
|
2008-11-01 17:32:06 -04:00
|
|
|
String[] header = getHeader(name);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (header == null) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return header[0];
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void addHeader(String name, String value) {
|
2008-11-01 17:32:06 -04:00
|
|
|
mFields.add(new Field(name, MimeUtility.foldAndEncode(value)));
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setHeader(String name, String value) {
|
|
|
|
if (name == null || value == null) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
removeHeader(name);
|
|
|
|
addHeader(name, value);
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Set<String> getHeaderNames() {
|
2010-05-21 11:34:29 -04:00
|
|
|
Set<String> names = new HashSet<String>();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Field field : mFields) {
|
2009-06-08 23:11:35 -04:00
|
|
|
names.add(field.name);
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String[] getHeader(String name) {
|
2010-11-26 23:03:15 -05:00
|
|
|
ArrayList<String> values = new ArrayList<String>();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Field field : mFields) {
|
|
|
|
if (field.name.equalsIgnoreCase(name)) {
|
2010-11-26 23:03:15 -05:00
|
|
|
values.add(field.value);
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (values.size() == 0) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return null;
|
|
|
|
}
|
2010-08-07 11:10:07 -04:00
|
|
|
return values.toArray(EMPTY_STRING_ARRAY);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void removeHeader(String name) {
|
2010-11-26 23:03:15 -05:00
|
|
|
ArrayList<Field> removeFields = new ArrayList<Field>();
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Field field : mFields) {
|
|
|
|
if (field.name.equalsIgnoreCase(name)) {
|
2010-11-26 23:03:15 -05:00
|
|
|
removeFields.add(field);
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2010-11-26 23:03:15 -05:00
|
|
|
mFields.removeAll(removeFields);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void writeTo(OutputStream out) throws IOException {
|
2008-11-01 17:32:06 -04:00
|
|
|
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
|
2011-02-06 17:09:48 -05:00
|
|
|
for (Field field : mFields) {
|
|
|
|
if (!Utility.arrayContains(writeOmitFields, field.name)) {
|
2009-07-02 01:49:09 -04:00
|
|
|
String v = field.value;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
if (hasToBeEncoded(v)) {
|
2011-02-28 11:50:56 -05:00
|
|
|
Charset charset = null;
|
|
|
|
|
|
|
|
if (mCharset != null) {
|
|
|
|
charset = Charset.forName(mCharset);
|
|
|
|
}
|
|
|
|
v = EncoderUtil.encodeEncodedWord(field.value, charset);
|
2009-07-02 01:49:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
writer.write(field.name + ": " + v + "\r\n");
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
writer.flush();
|
|
|
|
}
|
|
|
|
|
2009-07-02 01:49:09 -04:00
|
|
|
// encode non printable characters except LF/CR codes.
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean hasToBeEncoded(String text) {
|
|
|
|
for (int i = 0; i < text.length(); i++) {
|
2009-07-02 01:49:09 -04:00
|
|
|
char c = text.charAt(i);
|
2011-02-06 17:09:48 -05:00
|
|
|
if (c < 0x20 || 0x7e < c) { // non printable
|
|
|
|
if (c != 0x0a && c != 0x0d) { // non LF/CR
|
2009-07-02 01:49:09 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
static class Field {
|
2008-11-01 17:32:06 -04:00
|
|
|
String name;
|
|
|
|
|
|
|
|
String value;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Field(String name, String value) {
|
2008-11-01 17:32:06 -04:00
|
|
|
this.name = name;
|
|
|
|
this.value = value;
|
|
|
|
}
|
2010-08-18 10:13:37 -04:00
|
|
|
|
2010-11-03 23:11:34 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public String toString() {
|
2010-08-29 12:57:13 -04:00
|
|
|
StringBuilder sb = new StringBuilder("(");
|
|
|
|
sb.append(name).append('=').append(value).append(')');
|
|
|
|
return sb.toString();
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
2010-12-23 04:58:13 -05:00
|
|
|
|
|
|
|
public void setCharset(String charset)
|
|
|
|
{
|
|
|
|
mCharset = charset;
|
|
|
|
}
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|