~ubuntu-branches/ubuntu/utopic/apache-mime4j/utopic

« back to all changes in this revision

Viewing changes to core/src/main/java/org/apache/james/mime4j/stream/FallbackBodyDescriptorBuilder.java

  • Committer: Package Import Robot
  • Author(s): David Paleino
  • Date: 2013-11-03 11:13:27 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20131103111327-09huep00ex05z113
Tags: 0.7.2-2
* Upload to unstable
* Fixed Vcs-* fields in debian/control
* Updated debian/watch
* Standards-Version bump to 3.9.5, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************
 
2
 * Licensed to the Apache Software Foundation (ASF) under one   *
 
3
 * or more contributor license agreements.  See the NOTICE file *
 
4
 * distributed with this work for additional information        *
 
5
 * regarding copyright ownership.  The ASF licenses this file   *
 
6
 * to you under the Apache License, Version 2.0 (the            *
 
7
 * "License"); you may not use this file except in compliance   *
 
8
 * with the License.  You may obtain a copy of the License at   *
 
9
 *                                                              *
 
10
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 
11
 *                                                              *
 
12
 * Unless required by applicable law or agreed to in writing,   *
 
13
 * software distributed under the License is distributed on an  *
 
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 
15
 * KIND, either express or implied.  See the License for the    *
 
16
 * specific language governing permissions and limitations      *
 
17
 * under the License.                                           *
 
18
 ****************************************************************/
 
19
 
 
20
package org.apache.james.mime4j.stream;
 
21
 
 
22
import java.util.HashMap;
 
23
import java.util.Locale;
 
24
import java.util.Map;
 
25
 
 
26
import org.apache.james.mime4j.MimeException;
 
27
import org.apache.james.mime4j.codec.DecodeMonitor;
 
28
import org.apache.james.mime4j.util.MimeUtil;
 
29
 
 
30
/**
 
31
 * Encapsulates the values of the MIME-specific header fields
 
32
 * (which starts with <code>Content-</code>).
 
33
 */
 
34
class FallbackBodyDescriptorBuilder implements BodyDescriptorBuilder {
 
35
 
 
36
    private static final String US_ASCII = "us-ascii";
 
37
    private static final String SUB_TYPE_EMAIL = "rfc822";
 
38
    private static final String MEDIA_TYPE_TEXT = "text";
 
39
    private static final String MEDIA_TYPE_MESSAGE = "message";
 
40
    private static final String EMAIL_MESSAGE_MIME_TYPE = MEDIA_TYPE_MESSAGE + "/" + SUB_TYPE_EMAIL;
 
41
    private static final String DEFAULT_SUB_TYPE = "plain";
 
42
    private static final String DEFAULT_MEDIA_TYPE = MEDIA_TYPE_TEXT;
 
43
    private static final String DEFAULT_MIME_TYPE = DEFAULT_MEDIA_TYPE + "/" + DEFAULT_SUB_TYPE;
 
44
 
 
45
    private final String parentMimeType;
 
46
    private final DecodeMonitor monitor;
 
47
 
 
48
    private String mediaType;
 
49
    private String subType;
 
50
    private String mimeType;
 
51
    private String boundary;
 
52
    private String charset;
 
53
    private String transferEncoding;
 
54
    private long contentLength;
 
55
 
 
56
    /**
 
57
     * Creates a new root <code>BodyDescriptor</code> instance.
 
58
     */
 
59
    public FallbackBodyDescriptorBuilder() {
 
60
        this(null, null);
 
61
    }
 
62
 
 
63
    /**
 
64
     * Creates a new <code>BodyDescriptor</code> instance.
 
65
     *
 
66
     * @param parent the descriptor of the parent or <code>null</code> if this
 
67
     *        is the root descriptor.
 
68
     */
 
69
    public FallbackBodyDescriptorBuilder(final String parentMimeType, final DecodeMonitor monitor) {
 
70
        super();
 
71
        this.parentMimeType = parentMimeType;
 
72
        this.monitor = monitor != null ? monitor : DecodeMonitor.SILENT;
 
73
        reset();
 
74
    }
 
75
 
 
76
    public void reset() {
 
77
        mimeType = null;
 
78
        subType = null;
 
79
        mediaType = null;
 
80
        boundary = null;
 
81
        charset = null;
 
82
        transferEncoding = null;
 
83
        contentLength = -1;
 
84
    }
 
85
 
 
86
    public BodyDescriptorBuilder newChild() {
 
87
        return new FallbackBodyDescriptorBuilder(mimeType, monitor);
 
88
    }
 
89
 
 
90
    public BodyDescriptor build() {
 
91
        String actualMimeType = mimeType;
 
92
        String actualMediaType = mediaType;
 
93
        String actualSubType = subType;
 
94
        String actualCharset = charset;
 
95
        if (actualMimeType == null) {
 
96
            if (MimeUtil.isSameMimeType("multipart/digest", parentMimeType)) {
 
97
                actualMimeType = EMAIL_MESSAGE_MIME_TYPE;
 
98
                actualMediaType = MEDIA_TYPE_MESSAGE;
 
99
                actualSubType = SUB_TYPE_EMAIL;
 
100
            } else {
 
101
                actualMimeType = DEFAULT_MIME_TYPE;
 
102
                actualMediaType = DEFAULT_MEDIA_TYPE;
 
103
                actualSubType = DEFAULT_SUB_TYPE;
 
104
            }
 
105
        }
 
106
        if (actualCharset == null && MEDIA_TYPE_TEXT.equals(actualMediaType)) {
 
107
            actualCharset = US_ASCII;
 
108
        }
 
109
        return new BasicBodyDescriptor(actualMimeType, actualMediaType, actualSubType,
 
110
                boundary, actualCharset,
 
111
                transferEncoding != null ? transferEncoding : "7bit",
 
112
                contentLength);
 
113
    }
 
114
 
 
115
    /**
 
116
     * Should be called for each <code>Content-</code> header field of
 
117
     * a MIME message or part.
 
118
     *
 
119
     * @param field the MIME field.
 
120
     */
 
121
    public Field addField(RawField field) throws MimeException {
 
122
        String name = field.getName().toLowerCase(Locale.US);
 
123
 
 
124
        if (name.equals("content-transfer-encoding") && transferEncoding == null) {
 
125
            String value = field.getBody();
 
126
            if (value != null) {
 
127
                value = value.trim().toLowerCase(Locale.US);
 
128
                if (value.length() > 0) {
 
129
                    transferEncoding = value;
 
130
                }
 
131
            }
 
132
        } else if (name.equals("content-length") && contentLength == -1) {
 
133
            String value = field.getBody();
 
134
            if (value != null) {
 
135
                value = value.trim();
 
136
                try {
 
137
                    contentLength = Long.parseLong(value.trim());
 
138
                } catch (NumberFormatException e) {
 
139
                    if (monitor.warn("Invalid content length: " + value,
 
140
                            "ignoring Content-Length header")) {
 
141
                        throw new MimeException("Invalid Content-Length header: " + value);
 
142
                    }
 
143
                }
 
144
            }
 
145
        } else if (name.equals("content-type") && mimeType == null) {
 
146
            parseContentType(field);
 
147
        }
 
148
        return null;
 
149
    }
 
150
 
 
151
    private void parseContentType(Field field) throws MimeException {
 
152
        RawField rawfield;
 
153
        if (field instanceof RawField) {
 
154
            rawfield = ((RawField) field);
 
155
        } else {
 
156
            rawfield = new RawField(field.getName(), field.getBody());
 
157
        }
 
158
        RawBody body = RawFieldParser.DEFAULT.parseRawBody(rawfield);
 
159
        String main = body.getValue();
 
160
        Map<String, String> params = new HashMap<String, String>();
 
161
        for (NameValuePair nmp: body.getParams()) {
 
162
            String name = nmp.getName().toLowerCase(Locale.US);
 
163
            params.put(name, nmp.getValue());
 
164
        }
 
165
 
 
166
        String type = null;
 
167
        String subtype = null;
 
168
        if (main != null) {
 
169
            main = main.toLowerCase().trim();
 
170
            int index = main.indexOf('/');
 
171
            boolean valid = false;
 
172
            if (index != -1) {
 
173
                type = main.substring(0, index).trim();
 
174
                subtype = main.substring(index + 1).trim();
 
175
                if (type.length() > 0 && subtype.length() > 0) {
 
176
                    main = type + "/" + subtype;
 
177
                    valid = true;
 
178
                }
 
179
            }
 
180
 
 
181
            if (!valid) {
 
182
                main = null;
 
183
                type = null;
 
184
                subtype = null;
 
185
            }
 
186
        }
 
187
        String b = params.get("boundary");
 
188
 
 
189
        if (main != null
 
190
                && ((main.startsWith("multipart/") && b != null)
 
191
                        || !main.startsWith("multipart/"))) {
 
192
            mimeType = main;
 
193
            mediaType = type;
 
194
            subType = subtype;
 
195
        }
 
196
 
 
197
        if (MimeUtil.isMultipart(mimeType)) {
 
198
            boundary = b;
 
199
        }
 
200
 
 
201
        String c = params.get("charset");
 
202
        charset = null;
 
203
        if (c != null) {
 
204
            c = c.trim();
 
205
            if (c.length() > 0) {
 
206
                charset = c;
 
207
            }
 
208
        }
 
209
    }
 
210
 
 
211
}