source: contrib/MailArchiver/sources/vendor/mime4j/custom/dom/src/main/java/org/apache/james/mime4j/field/ContentDispositionFieldImpl.java @ 6785

Revision 6785, 8.2 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.field;
21
22import java.io.StringReader;
23import java.util.Collections;
24import java.util.Date;
25import java.util.HashMap;
26import java.util.List;
27import java.util.Locale;
28import java.util.Map;
29
30import org.apache.james.mime4j.codec.DecodeMonitor;
31import org.apache.james.mime4j.field.contentdisposition.parser.ContentDispositionParser;
32import org.apache.james.mime4j.field.contentdisposition.parser.ParseException;
33import org.apache.james.mime4j.field.contentdisposition.parser.TokenMgrError;
34import org.apache.james.mime4j.field.datetime.parser.DateTimeParser;
35import org.apache.james.mime4j.util.ByteSequence;
36
37/**
38 * Represents a <code>Content-Disposition</code> field.
39 */
40public class ContentDispositionFieldImpl extends AbstractField implements org.apache.james.mime4j.dom.field.ContentDispositionField {
41
42    private boolean parsed = false;
43
44    private String dispositionType = "";
45    private Map<String, String> parameters = new HashMap<String, String>();
46    private ParseException parseException;
47
48    private boolean creationDateParsed;
49    private Date creationDate;
50
51    private boolean modificationDateParsed;
52    private Date modificationDate;
53
54    private boolean readDateParsed;
55    private Date readDate;
56
57    ContentDispositionFieldImpl(String name, String body, ByteSequence raw, DecodeMonitor monitor) {
58        super(name, body, raw, monitor);
59    }
60
61    /**
62     * Gets the exception that was raised during parsing of the field value, if
63     * any; otherwise, null.
64     */
65    @Override
66    public ParseException getParseException() {
67        if (!parsed)
68            parse();
69
70        return parseException;
71    }
72
73    /**
74     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getDispositionType()
75     */
76    public String getDispositionType() {
77        if (!parsed)
78            parse();
79
80        return dispositionType;
81    }
82
83    /**
84     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getParameter(java.lang.String)
85     */
86    public String getParameter(String name) {
87        if (!parsed)
88            parse();
89
90        return parameters.get(name.toLowerCase());
91    }
92
93    /**
94     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getParameters()
95     */
96    public Map<String, String> getParameters() {
97        if (!parsed)
98            parse();
99
100        return Collections.unmodifiableMap(parameters);
101    }
102
103    /**
104     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#isDispositionType(java.lang.String)
105     */
106    public boolean isDispositionType(String dispositionType) {
107        if (!parsed)
108            parse();
109
110        return this.dispositionType.equalsIgnoreCase(dispositionType);
111    }
112
113    /**
114     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#isInline()
115     */
116    public boolean isInline() {
117        if (!parsed)
118            parse();
119
120        return dispositionType.equals(DISPOSITION_TYPE_INLINE);
121    }
122
123    /**
124     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#isAttachment()
125     */
126    public boolean isAttachment() {
127        if (!parsed)
128            parse();
129
130        return dispositionType.equals(DISPOSITION_TYPE_ATTACHMENT);
131    }
132
133    /**
134     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getFilename()
135     */
136    public String getFilename() {
137        return getParameter(PARAM_FILENAME);
138    }
139
140    /**
141     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getCreationDate()
142     */
143    public Date getCreationDate() {
144        if (!creationDateParsed) {
145            creationDate = parseDate(PARAM_CREATION_DATE);
146            creationDateParsed = true;
147        }
148
149        return creationDate;
150    }
151
152    /**
153     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getModificationDate()
154     */
155    public Date getModificationDate() {
156        if (!modificationDateParsed) {
157            modificationDate = parseDate(PARAM_MODIFICATION_DATE);
158            modificationDateParsed = true;
159        }
160
161        return modificationDate;
162    }
163
164    /**
165     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getReadDate()
166     */
167    public Date getReadDate() {
168        if (!readDateParsed) {
169            readDate = parseDate(PARAM_READ_DATE);
170            readDateParsed = true;
171        }
172
173        return readDate;
174    }
175
176    /**
177     * @see org.apache.james.mime4j.dom.field.ContentDispositionField#getSize()
178     */
179    public long getSize() {
180        String value = getParameter(PARAM_SIZE);
181        if (value == null)
182            return -1;
183
184        try {
185            long size = Long.parseLong(value);
186            return size < 0 ? -1 : size;
187        } catch (NumberFormatException e) {
188            return -1;
189        }
190    }
191
192    private Date parseDate(String paramName) {
193        String value = getParameter(paramName);
194        if (value == null) {
195            monitor.warn("Parsing " + paramName + " null", "returning null");
196            return null;
197        }
198
199        try {
200            return new DateTimeParser(new StringReader(value)).parseAll()
201                    .getDate();
202        } catch (org.apache.james.mime4j.field.datetime.parser.ParseException e) {
203            monitor.warn("Parsing " + paramName + " '" + value + "': "
204                    + e.getMessage(), "returning null");
205            return null;
206        } catch (org.apache.james.mime4j.field.datetime.parser.TokenMgrError e) {
207            monitor.warn("Parsing " + paramName + " '" + value + "': "
208                    + e.getMessage(), "returning null");
209            return null;
210        }
211    }
212
213    private void parse() {
214        String body = getBody();
215
216        ContentDispositionParser parser = new ContentDispositionParser(
217                new StringReader(body));
218        try {
219            parser.parseAll();
220        } catch (ParseException e) {
221            parseException = e;
222        } catch (TokenMgrError e) {
223            parseException = new ParseException(e.getMessage());
224        }
225
226        final String dispositionType = parser.getDispositionType();
227
228        if (dispositionType != null) {
229            this.dispositionType = dispositionType.toLowerCase(Locale.US);
230
231            List<String> paramNames = parser.getParamNames();
232            List<String> paramValues = parser.getParamValues();
233
234            if (paramNames != null && paramValues != null) {
235                final int len = Math.min(paramNames.size(), paramValues.size());
236                for (int i = 0; i < len; i++) {
237                    String paramName = paramNames.get(i).toLowerCase(Locale.US);
238                    String paramValue = paramValues.get(i);
239                    parameters.put(paramName, paramValue);
240                }
241            }
242        }
243
244        parsed = true;
245    }
246
247    static final FieldParser<ContentDispositionFieldImpl> PARSER = new FieldParser<ContentDispositionFieldImpl>() {
248        public ContentDispositionFieldImpl parse(final String name, final String body,
249                final ByteSequence raw, DecodeMonitor monitor) {
250            return new ContentDispositionFieldImpl(name, body, raw, monitor);
251        }
252    };
253}
Note: See TracBrowser for help on using the repository browser.