MimeHeader.java revision ca5089efb84fd3c60cf317de474f8e313b8119e5
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.email.mail.internet;
18
19import java.io.BufferedWriter;
20import java.io.IOException;
21import java.io.OutputStream;
22import java.io.OutputStreamWriter;
23import java.util.ArrayList;
24import java.util.regex.Pattern;
25
26import com.android.email.Utility;
27import com.android.email.mail.MessagingException;
28
29public class MimeHeader {
30    /**
31     * Application specific header that contains Store specific information about an attachment.
32     * In IMAP this contains the IMAP BODYSTRUCTURE part id so that the ImapStore can later
33     * retrieve the attachment at will from the server.
34     * The info is recorded from this header on LocalStore.appendMessages and is put back
35     * into the MIME data by LocalStore.fetch.
36     */
37    public static final String HEADER_ANDROID_ATTACHMENT_STORE_DATA = "X-Android-Attachment-StoreData";
38
39    public static final String HEADER_CONTENT_TYPE = "Content-Type";
40    public static final String HEADER_CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";
41    public static final String HEADER_CONTENT_DISPOSITION = "Content-Disposition";
42    public static final String HEADER_CONTENT_ID = "Content-ID";
43
44    /**
45     * Fields that should be omitted when writing the header using writeTo()
46     */
47    private static final String[] writeOmitFields = {
48//        HEADER_ANDROID_ATTACHMENT_DOWNLOADED,
49//        HEADER_ANDROID_ATTACHMENT_ID,
50        HEADER_ANDROID_ATTACHMENT_STORE_DATA
51    };
52
53    protected ArrayList<Field> mFields = new ArrayList<Field>();
54
55    public void clear() {
56        mFields.clear();
57    }
58
59    public String getFirstHeader(String name) throws MessagingException {
60        String[] header = getHeader(name);
61        if (header == null) {
62            return null;
63        }
64        return header[0];
65    }
66
67    public void addHeader(String name, String value) throws MessagingException {
68        mFields.add(new Field(name, value));
69    }
70
71    public void setHeader(String name, String value) throws MessagingException {
72        if (name == null || value == null) {
73            return;
74        }
75        removeHeader(name);
76        addHeader(name, value);
77    }
78
79    public String[] getHeader(String name) throws MessagingException {
80        ArrayList<String> values = new ArrayList<String>();
81        for (Field field : mFields) {
82            if (field.name.equalsIgnoreCase(name)) {
83                values.add(field.value);
84            }
85        }
86        if (values.size() == 0) {
87            return null;
88        }
89        return values.toArray(new String[] {});
90    }
91
92    public void removeHeader(String name) throws MessagingException {
93        ArrayList<Field> removeFields = new ArrayList<Field>();
94        for (Field field : mFields) {
95            if (field.name.equalsIgnoreCase(name)) {
96                removeFields.add(field);
97            }
98        }
99        mFields.removeAll(removeFields);
100    }
101
102    /**
103     * Write header into String
104     *
105     * @return CR-NL separated header string except the headers in writeOmitFields
106     * null if header is empty
107     */
108    public String writeToString() {
109        if (mFields.size() == 0) {
110            return null;
111        }
112        StringBuilder builder = new StringBuilder();
113        for (Field field : mFields) {
114            if (!Utility.arrayContains(writeOmitFields, field.name)) {
115                builder.append(field.name + ": " + field.value + "\r\n");
116            }
117        }
118        return builder.toString();
119    }
120
121    public void writeTo(OutputStream out) throws IOException, MessagingException {
122        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out), 1024);
123        for (Field field : mFields) {
124            if (!Utility.arrayContains(writeOmitFields, field.name)) {
125                writer.write(field.name + ": " + field.value + "\r\n");
126            }
127        }
128        writer.flush();
129    }
130
131    class Field {
132        String name;
133
134        String value;
135
136        public Field(String name, String value) {
137            this.name = name;
138            this.value = value;
139        }
140
141        @Override
142        public String toString() {
143            return name + "=" + value;
144        }
145    }
146}
147