source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/main/java/org/apache/james/mime4j/message/MimeBuilder.java @ 6785

Revision 6785, 13.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.message;
21
22import java.io.IOException;
23import java.io.InputStream;
24
25import org.apache.james.mime4j.MimeException;
26import org.apache.james.mime4j.MimeIOException;
27import org.apache.james.mime4j.codec.DecodeMonitor;
28import org.apache.james.mime4j.dom.Body;
29import org.apache.james.mime4j.dom.Disposable;
30import org.apache.james.mime4j.dom.Entity;
31import org.apache.james.mime4j.dom.Header;
32import org.apache.james.mime4j.dom.Message;
33import org.apache.james.mime4j.dom.Multipart;
34import org.apache.james.mime4j.dom.SingleBody;
35import org.apache.james.mime4j.dom.field.Field;
36import org.apache.james.mime4j.field.DefaultFieldParser;
37import org.apache.james.mime4j.parser.AbstractContentHandler;
38import org.apache.james.mime4j.parser.MimeStreamParser;
39import org.apache.james.mime4j.stream.MimeEntityConfig;
40import org.apache.james.mime4j.stream.MutableBodyDescriptorFactory;
41import org.apache.james.mime4j.stream.RawField;
42
43/**
44 * Utility class for copying message and parsing message elements.
45 */
46public class MimeBuilder {
47
48    public static final MimeBuilder DEFAULT = new MimeBuilder();
49   
50    protected MimeBuilder() {
51        super();
52    }
53
54    /**
55     * Creates a new <code>Header</code> from the specified
56     * <code>Header</code>. The <code>Header</code> instance is initialized
57     * with a copy of the list of {@link Field}s of the specified
58     * <code>Header</code>. The <code>Field</code> objects are not copied
59     * because they are immutable and can safely be shared between headers.
60     *
61     * @param other
62     *            header to copy.
63     */
64    public Header copy(Header other) {
65        HeaderImpl copy = new HeaderImpl();
66        for (Field otherField : other.getFields()) {
67            copy.addField(otherField);
68        }
69        return copy;
70    }
71
72    /**
73     * Creates a new <code>BodyPart</code> from the specified
74     * <code>Entity</code>. The <code>BodyPart</code> instance is initialized
75     * with copies of header and body of the specified <code>Entity</code>.
76     * The parent entity of the new body part is <code>null</code>.
77     *
78     * @param other
79     *            body part to copy.
80     * @throws UnsupportedOperationException
81     *             if <code>other</code> contains a {@link SingleBody} that
82     *             does not support the {@link SingleBody#copy() copy()}
83     *             operation.
84     * @throws IllegalArgumentException
85     *             if <code>other</code> contains a <code>Body</code> that
86     *             is neither a {@link Message}, {@link Multipart} or
87     *             {@link SingleBody}.
88     */
89    public BodyPart copy(Entity other) {
90        BodyPart copy = new BodyPart();
91        if (other.getHeader() != null) {
92            copy.setHeader(copy(other.getHeader()));
93        }
94        if (other.getBody() != null) {
95            copy.setBody(copy(other.getBody()));
96        }
97        return copy;
98    }
99
100    /**
101     * Creates a new <code>Multipart</code> from the specified
102     * <code>Multipart</code>. The <code>Multipart</code> instance is
103     * initialized with copies of preamble, epilogue, sub type and the list of
104     * body parts of the specified <code>Multipart</code>. The parent entity
105     * of the new multipart is <code>null</code>.
106     *
107     * @param other
108     *            multipart to copy.
109     * @throws UnsupportedOperationException
110     *             if <code>other</code> contains a {@link SingleBody} that
111     *             does not support the {@link SingleBody#copy() copy()}
112     *             operation.
113     * @throws IllegalArgumentException
114     *             if <code>other</code> contains a <code>Body</code> that
115     *             is neither a {@link Message}, {@link Multipart} or
116     *             {@link SingleBody}.
117     */
118    public Multipart copy(Multipart other) {
119        MultipartImpl copy = new MultipartImpl(other.getSubType());
120        for (Entity otherBodyPart : other.getBodyParts()) {
121            copy.addBodyPart(copy(otherBodyPart));
122        }
123        copy.setPreamble(other.getPreamble());
124        copy.setEpilogue(other.getEpilogue());
125        return copy;
126    }
127
128   
129    /**
130     * Returns a copy of the given {@link Body} that can be used (and modified)
131     * independently of the original. The copy should be
132     * {@link Disposable#dispose() disposed of} when it is no longer needed.
133     * <p>
134     * The {@link Body#getParent() parent} of the returned copy is
135     * <code>null</code>, that is, the copy is detached from the parent
136     * entity of the original.
137     *
138     * @param body
139     *            body to copy.
140     * @return a copy of the given body.
141     * @throws UnsupportedOperationException
142     *             if <code>body</code> is an instance of {@link SingleBody}
143     *             that does not support the {@link SingleBody#copy() copy()}
144     *             operation (or contains such a <code>SingleBody</code>).
145     * @throws IllegalArgumentException
146     *             if <code>body</code> is <code>null</code> or
147     *             <code>body</code> is a <code>Body</code> that is neither
148     *             a {@link MessageImpl}, {@link Multipart} or {@link SingleBody}
149     *             (or contains such a <code>Body</code>).
150     */
151    public Body copy(Body body) {
152        if (body == null)
153            throw new IllegalArgumentException("Body is null");
154
155        if (body instanceof Message)
156            return copy((Message) body);
157
158        if (body instanceof Multipart)
159            return copy((Multipart) body);
160
161        if (body instanceof SingleBody)
162            return ((SingleBody) body).copy();
163
164        throw new IllegalArgumentException("Unsupported body class");
165    }
166
167    /**
168     * Creates a new <code>Message</code> from the specified
169     * <code>Message</code>. The <code>Message</code> instance is
170     * initialized with copies of header and body of the specified
171     * <code>Message</code>. The parent entity of the new message is
172     * <code>null</code>.
173     *
174     * @param other
175     *            message to copy.
176     * @throws UnsupportedOperationException
177     *             if <code>other</code> contains a {@link SingleBody} that
178     *             does not support the {@link SingleBody#copy() copy()}
179     *             operation.
180     * @throws IllegalArgumentException
181     *             if <code>other</code> contains a <code>Body</code> that
182     *             is neither a {@link MessageImpl}, {@link Multipart} or
183     *             {@link SingleBody}.
184     */
185    public Message copy(Message other) {
186        MessageImpl copy = new MessageImpl();
187        if (other.getHeader() != null) {
188            copy.setHeader(copy(other.getHeader()));
189        }
190        if (other.getBody() != null) {
191            copy.setBody(copy(other.getBody()));
192        }
193        return copy;
194    }
195   
196    /**
197     * Creates a new <code>Header</code> from the specified stream.
198     *
199     * @param is the stream to read the header from.
200     *
201     * @throws IOException on I/O errors.
202     * @throws MimeIOException on MIME protocol violations.
203     */
204    public Header parse(
205            final InputStream is,
206            final DecodeMonitor monitor) throws IOException, MimeIOException {
207        final HeaderImpl header = new HeaderImpl();
208        final MimeStreamParser parser = new MimeStreamParser();
209        parser.setContentHandler(new AbstractContentHandler() {
210            @Override
211            public void endHeader() {
212                parser.stop();
213            }
214            @Override
215            public void field(RawField field) throws MimeException {
216                Field parsedField = DefaultFieldParser.parse(field.getRaw(), monitor);
217                header.addField(parsedField);
218            }
219        });
220        try {
221            parser.parse(is);
222        } catch (MimeException ex) {
223            throw new MimeIOException(ex);
224        }
225        return header;
226    }
227
228    /**
229     * Parses the specified MIME message stream into a <code>Message</code>
230     * instance using given {@link MimeEntityConfig} and {@link StorageProvider}.
231     *
232     * @param is
233     *            the stream to parse.
234     * @param config
235     *            {@link MimeEntityConfig} to use.
236     * @param bodyFactory
237     *            {@link BodyFactory} to use for storing text and binary
238     *            message bodies.
239     * @param bodyDescFactory
240     *            {@link MutableBodyDescriptorFactory} to use for creating body descriptors.
241     * @throws IOException
242     *             on I/O errors.
243     * @throws MimeIOException
244     *             on MIME protocol violations.
245     */
246    public Message parse(
247            final InputStream is,
248            final MimeEntityConfig config,
249            final BodyFactory bodyFactory,
250            final MutableBodyDescriptorFactory bodyDescFactory,
251            final boolean contentDecoding,
252            final boolean flatMode,
253            final DecodeMonitor monitor) throws IOException, MimeIOException {
254        try {
255            MessageImpl message = new MessageImpl();
256            DecodeMonitor mon = monitor != null ? monitor : DecodeMonitor.SILENT;
257            MimeStreamParser parser = new MimeStreamParser(config, bodyDescFactory, mon);
258            parser.setContentHandler(new EntityBuilder(message, bodyFactory, mon));
259            parser.setContentDecoding(contentDecoding);
260            if (flatMode) {
261                parser.setFlat();
262            } else {
263                parser.setRecurse();
264            }
265            parser.parse(is);
266            return message;
267        } catch (MimeException e) {
268            throw new MimeIOException(e);
269        }
270    }
271
272    /**
273     * Parses the specified MIME message stream into a <code>Message</code>
274     * instance using given {@link MimeEntityConfig} and {@link StorageProvider}.
275     *
276     * @param is
277     *            the stream to parse.
278     * @param config
279     *            {@link MimeEntityConfig} to use.
280     * @param storageProvider
281     *            {@link StorageProvider} to use for storing text and binary
282     *            message bodies.
283     * @param bodyDescFactory
284     *            {@link MutableBodyDescriptorFactory} to use for creating body descriptors.
285     * @throws IOException
286     *             on I/O errors.
287     * @throws MimeIOException
288     *             on MIME protocol violations.
289     */
290    public Message parse(
291            final InputStream is,
292            final MimeEntityConfig config,
293            final BodyFactory bodyFactory,
294            final MutableBodyDescriptorFactory bodyDescFactory,
295            final DecodeMonitor monitor) throws IOException, MimeIOException {
296        return parse(is, config, bodyFactory, bodyDescFactory, true, false, monitor);
297    }
298   
299    public Message parse(
300            final InputStream is,
301            final MimeEntityConfig config,
302            final BodyFactory bodyFactory,
303            final MutableBodyDescriptorFactory bodyDescFactory) throws IOException, MimeIOException {
304        return parse(is, config, bodyFactory, bodyDescFactory, null);
305    }
306
307    public Message parse(
308            final InputStream is,
309            final MimeEntityConfig config,
310            final BodyFactory bodyFactory) throws IOException, MimeIOException {
311        return parse(is, config, bodyFactory, null, null);
312    }
313
314    /**
315     * Parses the specified MIME message stream into a <code>Message</code>
316     * instance.
317     *
318     * @param is
319     *            the stream to parse.
320     * @throws IOException
321     *             on I/O errors.
322     * @throws MimeIOException
323     *             on MIME protocol violations.
324     */
325    public Message parse(InputStream is) throws IOException, MimeIOException {
326        return parse(is, null, null);
327    }
328
329    /**
330     * Parses the specified MIME message stream into a <code>Message</code>
331     * instance using given {@link MimeEntityConfig}.
332     *
333     * @param is
334     *            the stream to parse.
335     * @throws IOException
336     *             on I/O errors.
337     * @throws MimeIOException
338     *             on MIME protocol violations.
339     */
340    public Message parse(InputStream is, MimeEntityConfig config) throws IOException,
341            MimeIOException {
342        return parse(is, config, null);
343    }
344   
345}
Note: See TracBrowser for help on using the repository browser.